|
root / base / usr / src / uts / common / io / net80211 / net80211_node.c
net80211_node.c C 1638 lines 45.3 KB
   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
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2001 Atsushi Onoe
 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Node management routines
 */

#include "net80211_impl.h"

static ieee80211_node_t *ieee80211_node_alloc(ieee80211com_t *);
static void ieee80211_node_cleanup(ieee80211_node_t *);
static void ieee80211_node_free(ieee80211_node_t *);
static uint8_t ieee80211_node_getrssi(const ieee80211_node_t *);
static void ieee80211_setup_node(ieee80211com_t *, ieee80211_node_table_t *,
    ieee80211_node_t *, const uint8_t *);
static void ieee80211_node_reclaim(ieee80211_node_table_t *,
    ieee80211_node_t *);
static void ieee80211_free_node_locked(ieee80211_node_t *);
static void ieee80211_free_allnodes(ieee80211_node_table_t *);
static void ieee80211_node_leave(ieee80211com_t *, ieee80211_node_t *);
static void ieee80211_timeout_scan_candidates(ieee80211_node_table_t *);
static void ieee80211_timeout_stations(ieee80211_node_table_t *);
static void ieee80211_node_table_init(ieee80211com_t *,
    ieee80211_node_table_t *, const char *, int, int,
    void (*timeout)(ieee80211_node_table_t *));
static void ieee80211_node_table_cleanup(ieee80211_node_table_t *);

/*
 * association failures before ignored
 * The failure may be caused by the response frame is lost for
 * environmental reason. So Try associate more than once before
 * ignore the node
 */
#define	IEEE80211_STA_FAILS_MAX	2

/*
 * Initialize node database management callbacks for the interface.
 * This function is called by ieee80211_attach(). These callback
 * functions may be overridden in special circumstances, as long as
 * as this is done after calling ieee80211_attach() and prior to any
 * other call which may allocate a node
 */
void
ieee80211_node_attach(ieee80211com_t *ic)
{
	struct ieee80211_impl *im = ic->ic_private;

	ic->ic_node_alloc = ieee80211_node_alloc;
	ic->ic_node_free = ieee80211_node_free;
	ic->ic_node_cleanup = ieee80211_node_cleanup;
	ic->ic_node_getrssi = ieee80211_node_getrssi;

	/* default station inactivity timer setings */
	im->im_inact_init = IEEE80211_INACT_INIT;
	im->im_inact_assoc = IEEE80211_INACT_ASSOC;
	im->im_inact_run = IEEE80211_INACT_RUN;
	im->im_inact_probe = IEEE80211_INACT_PROBE;
}

/*
 * Initialize node databases and the ic_bss node element.
 */
void
ieee80211_node_lateattach(ieee80211com_t *ic)
{
	/*
	 * Calculate ic_tim_bitmap size in bytes
	 * IEEE80211_AID_MAX defines maximum bits in ic_tim_bitmap
	 */
	ic->ic_tim_len = howmany(IEEE80211_AID_MAX, 8) * sizeof (uint8_t);

	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
	    IEEE80211_INACT_INIT, IEEE80211_WEP_NKID,
	    ieee80211_timeout_stations);
	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
	    IEEE80211_INACT_SCAN, 0, ieee80211_timeout_scan_candidates);

	ieee80211_reset_bss(ic);
}

/*
 * Destroy all node databases and is usually called during device detach
 */
void
ieee80211_node_detach(ieee80211com_t *ic)
{
	/* Node Detach */
	if (ic->ic_bss != NULL) {
		ieee80211_free_node(ic->ic_bss);
		ic->ic_bss = NULL;
	}
	ieee80211_node_table_cleanup(&ic->ic_scan);
	ieee80211_node_table_cleanup(&ic->ic_sta);
}

/*
 * Increase a node's reference count
 *
 * Return pointer to the node
 */
ieee80211_node_t *
ieee80211_ref_node(ieee80211_node_t *in)
{
	ieee80211_node_incref(in);
	return (in);
}

/*
 * Dexrease a node's reference count
 */
void
ieee80211_unref_node(ieee80211_node_t **in)
{
	ieee80211_node_decref(*in);
	*in = NULL;			/* guard against use */
}

/*
 * Mark ports authorized for data traffic. This function is usually
 * used by 802.1x authenticator.
 */
void
ieee80211_node_authorize(ieee80211_node_t *in)
{
	ieee80211_impl_t *im = in->in_ic->ic_private;

	in->in_flags |= IEEE80211_NODE_AUTH;
	in->in_inact_reload = im->im_inact_run;
	in->in_inact = in->in_inact_reload;
}

/*
 * Mark ports unauthorized for data traffic. This function is usually
 * used by 802.1x authenticator.
 */
void
ieee80211_node_unauthorize(ieee80211_node_t *in)
{
	in->in_flags &= ~IEEE80211_NODE_AUTH;
}

/*
 * Set/change the channel.  The rate set is also updated as
 * to insure a consistent view by drivers.
 */
static void
ieee80211_node_setchan(ieee80211com_t *ic, ieee80211_node_t *in,
    struct ieee80211_channel *chan)
{
	if (chan == IEEE80211_CHAN_ANYC)
		chan = ic->ic_curchan;
	in->in_chan = chan;
	if (IEEE80211_IS_CHAN_HT(chan)) {
		/*
		 * Gotta be careful here; the rate set returned by
		 * ieee80211_get_suprates is actually any HT rate
		 * set so blindly copying it will be bad.  We must
		 * install the legacy rate est in ni_rates and the
		 * HT rate set in ni_htrates.
		 */
		in->in_htrates = *ieee80211_get_suphtrates(ic, chan);
	}
	in->in_rates = *ieee80211_get_suprates(ic, chan);
	/* in->in_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; */
}

/*
 * Initialize the channel set to scan based on the available channels
 * and the current PHY mode.
 */
static void
ieee80211_reset_scan(ieee80211com_t *ic)
{
	ieee80211_impl_t	*im = ic->ic_private;

	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
		(void) memset(im->im_chan_scan, 0, sizeof (im->im_chan_scan));
		ieee80211_setbit(im->im_chan_scan,
		    ieee80211_chan2ieee(ic, ic->ic_des_chan));
	} else {
		bcopy(ic->ic_chan_active, im->im_chan_scan,
		    sizeof (ic->ic_chan_active));
	}
	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_reset_scan(): "
	    "start chan %u\n", ieee80211_chan2ieee(ic, ic->ic_curchan));
}

/*
 * Begin an active scan. Initialize the node cache. The scan
 * begins on the next radio channel by calling ieee80211_next_scan().
 * The actual scanning is not automated. The driver itself
 * only handles setting the radio frequency and stepping through
 * the channels.
 */
void
ieee80211_begin_scan(ieee80211com_t *ic, boolean_t reset)
{
	IEEE80211_LOCK(ic);

	if (ic->ic_opmode != IEEE80211_M_HOSTAP)
		ic->ic_flags |= IEEE80211_F_ASCAN;
	ieee80211_dbg(IEEE80211_MSG_SCAN,
	    "begin %s scan in %s mode on channel %u\n",
	    (ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
	    ieee80211_phymode_name[ic->ic_curmode],
	    ieee80211_chan2ieee(ic, ic->ic_curchan));

	/*
	 * Clear scan state and flush any previously seen AP's.
	 */
	ieee80211_reset_scan(ic);
	if (reset)
		ieee80211_free_allnodes(&ic->ic_scan);

	ic->ic_flags |= IEEE80211_F_SCAN;
	IEEE80211_UNLOCK(ic);

	/* Scan the next channel. */
	ieee80211_next_scan(ic);
}

/*
 * Switch to the next channel marked for scanning.
 * A driver is expected to first call ieee80211_begin_scan(),
 * to initialize the node cache, then set the radio channel
 * on the device. And then after a certain time has elapsed,
 * call ieee80211_next_scan() to move to the next channel.
 * Typically, a timeout routine is used to automate this process.
 */
void
ieee80211_next_scan(ieee80211com_t *ic)
{
	ieee80211_impl_t *im = ic->ic_private;
	struct ieee80211_channel *chan;

	IEEE80211_LOCK(ic);
	/*
	 * Insure any previous mgt frame timeouts don't fire.
	 * This assumes the driver does the right thing in
	 * flushing anything queued in the driver and below.
	 */
	im->im_mgt_timer = 0;

	chan = ic->ic_curchan;
	do {
		if (++chan > &ic->ic_sup_channels[IEEE80211_CHAN_MAX])
			chan = &ic->ic_sup_channels[0];
		if (ieee80211_isset(im->im_chan_scan,
		    ieee80211_chan2ieee(ic, chan))) {
			ieee80211_clrbit(im->im_chan_scan,
			    ieee80211_chan2ieee(ic, chan));
			ieee80211_dbg(IEEE80211_MSG_SCAN,
			    "ieee80211_next_scan: chan %d->%d\n",
			    ieee80211_chan2ieee(ic, ic->ic_curchan),
			    ieee80211_chan2ieee(ic, chan));
			ic->ic_curchan = chan;
			/*
			 * drivers should do this as needed,
			 * for now maintain compatibility
			 */
			ic->ic_bss->in_rates =
			    ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
			IEEE80211_UNLOCK(ic);
			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
			return;
		}
	} while (chan != ic->ic_curchan);
	IEEE80211_UNLOCK(ic);
	ieee80211_end_scan(ic);
}

/*
 * Copy useful state from node obss into nbss.
 */
static void
ieee80211_copy_bss(ieee80211_node_t *nbss, const ieee80211_node_t *obss)
{
	/* propagate useful state */
	nbss->in_authmode = obss->in_authmode;
	nbss->in_txpower = obss->in_txpower;
	nbss->in_vlan = obss->in_vlan;
}

/*
 * Setup the net80211 specific portion of an interface's softc, ic,
 * for use in IBSS mode
 */
void
ieee80211_create_ibss(ieee80211com_t *ic, struct ieee80211_channel *chan)
{
	ieee80211_impl_t *im = ic->ic_private;
	ieee80211_node_table_t *nt;
	ieee80211_node_t *in;

	IEEE80211_LOCK_ASSERT(ic);
	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_create_ibss: "
	    "creating ibss\n");

	/*
	 * Create the station/neighbor table.  Note that for adhoc
	 * mode we make the initial inactivity timer longer since
	 * we create nodes only through discovery and they typically
	 * are long-lived associations.
	 */
	nt = &ic->ic_sta;
	IEEE80211_NODE_LOCK(nt);
	nt->nt_name = "neighbor";
	nt->nt_inact_init = im->im_inact_run;
	IEEE80211_NODE_UNLOCK(nt);

	in = ieee80211_alloc_node(ic, &ic->ic_sta, ic->ic_macaddr);
	if (in == NULL) {
		ieee80211_err("ieee80211_create_ibss(): alloc node failed\n");
		return;
	}
	IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_macaddr);
	in->in_esslen = ic->ic_des_esslen;
	(void) memcpy(in->in_essid, ic->ic_des_essid, in->in_esslen);
	ieee80211_copy_bss(in, ic->ic_bss);
	in->in_intval = ic->ic_bintval;
	if (ic->ic_flags & IEEE80211_F_PRIVACY)
		in->in_capinfo |= IEEE80211_CAPINFO_PRIVACY;
	if (ic->ic_phytype == IEEE80211_T_FH) {
		in->in_fhdwell = 200;
		in->in_fhindex = 1;
	}
	switch (ic->ic_opmode) {
	case IEEE80211_M_IBSS:
		ic->ic_flags |= IEEE80211_F_SIBSS;
		in->in_capinfo |= IEEE80211_CAPINFO_IBSS;
		if (ic->ic_flags & IEEE80211_F_DESBSSID)
			IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid);
		else
			in->in_bssid[0] |= 0x02;	/* local bit for IBSS */
		break;
	case IEEE80211_M_AHDEMO:
		if (ic->ic_flags & IEEE80211_F_DESBSSID)
			IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid);
		else
			(void) memset(in->in_bssid, 0, IEEE80211_ADDR_LEN);
		break;
	default:
		ieee80211_err("ieee80211_create_ibss(): "
		    "wrong opmode %u to creat IBSS, abort\n",
		    ic->ic_opmode);
		ieee80211_free_node(in);
		return;
	}

	/*
	 * Fix the channel and related attributes.
	 */
	ieee80211_node_setchan(ic, in, chan);
	ic->ic_curchan = chan;
	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
	/*
	 * Do mode-specific rate setup.
	 */
	ieee80211_setbasicrates(&in->in_rates, ic->ic_curmode);
	IEEE80211_UNLOCK(ic);
	ieee80211_sta_join(ic, ieee80211_ref_node(in));
	IEEE80211_LOCK(ic);
}

void
ieee80211_reset_bss(ieee80211com_t *ic)
{
	ieee80211_node_t *in;
	ieee80211_node_t *obss;

	ieee80211_node_table_reset(&ic->ic_sta);
	ieee80211_reset_erp(ic);

	in = ieee80211_alloc_node(ic, &ic->ic_scan, ic->ic_macaddr);
	ASSERT(in != NULL);
	obss = ic->ic_bss;
	ic->ic_bss = ieee80211_ref_node(in);
	if (obss != NULL) {
		ieee80211_copy_bss(in, obss);
		in->in_intval = ic->ic_bintval;
		ieee80211_free_node(obss);
	}
}

static int
ieee80211_match_bss(ieee80211com_t *ic, ieee80211_node_t *in)
{
	uint8_t rate;
	int fail;

	fail = 0;
	if (ieee80211_isclr(ic->ic_chan_active,
	    ieee80211_chan2ieee(ic, in->in_chan))) {
		fail |= IEEE80211_BADCHAN;
	}
	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
	    in->in_chan != ic->ic_des_chan) {
		fail |= IEEE80211_BADCHAN;
	}
	if (ic->ic_opmode == IEEE80211_M_IBSS) {
		if (!(in->in_capinfo & IEEE80211_CAPINFO_IBSS))
			fail |= IEEE80211_BADOPMODE;
	} else {
		if (!(in->in_capinfo & IEEE80211_CAPINFO_ESS))
			fail |= IEEE80211_BADOPMODE;
	}
	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
		if (!(in->in_capinfo & IEEE80211_CAPINFO_PRIVACY))
			fail |= IEEE80211_BADPRIVACY;
	} else {
		if (in->in_capinfo & IEEE80211_CAPINFO_PRIVACY)
			fail |= IEEE80211_BADPRIVACY;
	}
	rate = ieee80211_fix_rate(in, &in->in_rates,
	    IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
	if (rate & IEEE80211_RATE_BASIC)
		fail |= IEEE80211_BADRATE;
	if (ic->ic_des_esslen != 0 &&
	    (in->in_esslen != ic->ic_des_esslen ||
	    memcmp(in->in_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) {
		fail |= IEEE80211_BADESSID;
	}
	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, in->in_bssid)) {
		fail |= IEEE80211_BADBSSID;
	}
	if (in->in_fails >= IEEE80211_STA_FAILS_MAX)
		fail |= IEEE80211_NODEFAIL;

	return (fail);
}

#define	IEEE80211_MAXRATE(_rs) \
	((_rs).ir_rates[(_rs).ir_nrates - 1] & IEEE80211_RATE_VAL)

/*
 * Compare the capabilities of node a with node b and decide which is
 * more desirable (return b if b is considered better than a).  Note
 * that we assume compatibility/usability has already been checked
 * so we don't need to (e.g. validate whether privacy is supported).
 * Used to select the best scan candidate for association in a BSS.
 *
 * Return desired node
 */
static ieee80211_node_t *
ieee80211_node_compare(ieee80211com_t *ic, ieee80211_node_t *a,
    ieee80211_node_t *b)
{
	uint8_t maxa;
	uint8_t maxb;
	uint8_t rssia;
	uint8_t rssib;

	/* privacy support preferred */
	if ((a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
	    !(b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) {
		return (a);
	}
	if (!(a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
	    (b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) {
		return (b);
	}

	/* compare count of previous failures */
	if (b->in_fails != a->in_fails)
		return ((a->in_fails > b->in_fails) ? b : a);

	rssia = ic->ic_node_getrssi(a);
	rssib = ic->ic_node_getrssi(b);
	if (ABS(rssib - rssia) < IEEE80211_RSSI_CMP_THRESHOLD) {
		/* best/max rate preferred if signal level close enough */
		maxa = IEEE80211_MAXRATE(a->in_rates);
		maxb = IEEE80211_MAXRATE(b->in_rates);
		if (maxa != maxb)
			return ((maxb > maxa) ? b : a);
		/* for now just prefer 5Ghz band to all other bands */
		if (IEEE80211_IS_CHAN_5GHZ(a->in_chan) &&
		    !IEEE80211_IS_CHAN_5GHZ(b->in_chan)) {
			return (a);
		}
		if (!IEEE80211_IS_CHAN_5GHZ(a->in_chan) &&
		    IEEE80211_IS_CHAN_5GHZ(b->in_chan)) {
			return (b);
		}
	}
	/* all things being equal, compare signal level */
	return ((rssib > rssia) ? b : a);
}

/*
 * Mark an ongoing scan stopped.
 */
void
ieee80211_cancel_scan(ieee80211com_t *ic)
{
	IEEE80211_LOCK(ic);
	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_cancel_scan()"
	    "end %s scan\n",
	    (ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
	cv_broadcast(&((ieee80211_impl_t *)ic->ic_private)->im_scan_cv);
	IEEE80211_UNLOCK(ic);
}

/*
 * Complete a scan of potential channels. It is called by
 * ieee80211_next_scan() when the state machine has performed
 * a full cycle of scaning on all available radio channels.
 * ieee80211_end_scan() will inspect the node cache for suitable
 * APs found during scaning, and associate with one, should
 * the parameters of the node match those of the configuration
 * requested from userland.
 */
void
ieee80211_end_scan(ieee80211com_t *ic)
{
	ieee80211_node_table_t *nt = &ic->ic_scan;
	ieee80211_node_t *in;
	ieee80211_node_t *selbs;

	ieee80211_cancel_scan(ic);
	/* notify SCAN done */
	ieee80211_notify(ic, EVENT_SCAN_RESULTS);
	IEEE80211_LOCK(ic);

	/*
	 * Automatic sequencing; look for a candidate and
	 * if found join the network.
	 */
	/* NB: unlocked read should be ok */
	in = list_head(&nt->nt_node);
	if (in == NULL && (ic->ic_flags & IEEE80211_F_WPA) == 0) {
		ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_end_scan: "
		    "no scan candidate\n");
	notfound:
		if (ic->ic_opmode == IEEE80211_M_IBSS &&
		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
		    ic->ic_des_esslen != 0) {
			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
			IEEE80211_UNLOCK(ic);
			return;
		}

		/*
		 * Reset the list of channels to scan and start again.
		 */
		ieee80211_reset_scan(ic);
		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
		IEEE80211_UNLOCK(ic);

		ieee80211_next_scan(ic);
		return;
	}

	if (ic->ic_flags & IEEE80211_F_SCANONLY ||
	    ic->ic_flags & IEEE80211_F_WPA) {	/* scan only */
		ic->ic_flags &= ~IEEE80211_F_SCANONLY;
		IEEE80211_UNLOCK(ic);
		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
		return;
	}

	selbs = NULL;
	IEEE80211_NODE_LOCK(nt);
	while (in != NULL) {
		if (in->in_fails >= IEEE80211_STA_FAILS_MAX) {
			ieee80211_node_t *tmpin = in;

			/*
			 * The configuration of the access points may change
			 * during my scan.  So delete the entry for the AP
			 * and retry to associate if there is another beacon.
			 */
			in = list_next(&nt->nt_node, tmpin);
			ieee80211_node_reclaim(nt, tmpin);
			continue;
		}
		/*
		 * It's possible at some special moments, the in_chan will
		 * be none. Need to skip the null node.
		 */
		if (in->in_chan == IEEE80211_CHAN_ANYC) {
			in = list_next(&nt->nt_node, in);
			continue;
		}
		if (ieee80211_match_bss(ic, in) == 0) {
			if (selbs == NULL)
				selbs = in;
			else
				selbs = ieee80211_node_compare(ic, selbs, in);
		}
		in = list_next(&nt->nt_node, in);
	}
	if (selbs != NULL)	/* grab ref while dropping lock */
		(void) ieee80211_ref_node(selbs);
	IEEE80211_NODE_UNLOCK(nt);
	if (selbs == NULL)
		goto notfound;
	IEEE80211_UNLOCK(ic);
	ieee80211_sta_join(ic, selbs);
}


/*
 * Handle 802.11 ad hoc network merge.  The convention, set by the
 * Wireless Ethernet Compatibility Alliance (WECA), is that an 802.11
 * station will change its BSSID to match the "oldest" 802.11 ad hoc
 * network, on the same channel, that has the station's desired SSID.
 * The "oldest" 802.11 network sends beacons with the greatest TSF
 * timestamp.
 * The caller is assumed to validate TSF's before attempting a merge.
 *
 * Return B_TRUE if the BSSID changed, B_FALSE otherwise.
 */
boolean_t
ieee80211_ibss_merge(ieee80211_node_t *in)
{
	ieee80211com_t *ic = in->in_ic;

	if (in == ic->ic_bss ||
	    IEEE80211_ADDR_EQ(in->in_bssid, ic->ic_bss->in_bssid)) {
		/* unchanged, nothing to do */
		return (B_FALSE);
	}
	if (ieee80211_match_bss(ic, in) != 0) {	/* capabilities mismatch */
		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: "
		    " merge failed, capabilities mismatch\n");
		return (B_FALSE);
	}
	ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: "
	    "new bssid %s: %s preamble, %s slot time%s\n",
	    ieee80211_macaddr_sprintf(in->in_bssid),
	    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long",
	    (ic->ic_flags & IEEE80211_F_SHSLOT) ? "short" : "long",
	    (ic->ic_flags&IEEE80211_F_USEPROT) ? ", protection" : "");
	ieee80211_sta_join(ic, ieee80211_ref_node(in));
	return (B_TRUE);
}

/*
 * Change the bss channel.
 */
void
ieee80211_setcurchan(ieee80211com_t *ic, struct ieee80211_channel *c)
{
	ic->ic_curchan = c;
	ic->ic_curmode = ieee80211_chan2mode(ic, ic->ic_curchan);
	if (ic->ic_set_channel != NULL)
		ic->ic_set_channel(ic);
}

/*
 * Join the specified IBSS/BSS network.  The node is assumed to
 * be passed in with a held reference.
 */
void
ieee80211_sta_join(ieee80211com_t *ic, ieee80211_node_t *selbs)
{
	ieee80211_impl_t *im = ic->ic_private;
	ieee80211_node_t *obss;

	IEEE80211_LOCK(ic);
	if (ic->ic_opmode == IEEE80211_M_IBSS) {
		ieee80211_node_table_t *nt;

		/*
		 * Delete unusable rates; we've already checked
		 * that the negotiated rate set is acceptable.
		 */
		(void) ieee80211_fix_rate(selbs, &selbs->in_rates,
		    IEEE80211_F_DODEL);
		/*
		 * Fillin the neighbor table
		 */
		nt = &ic->ic_sta;
		IEEE80211_NODE_LOCK(nt);
		nt->nt_name = "neighbor";
		nt->nt_inact_init = im->im_inact_run;
		IEEE80211_NODE_UNLOCK(nt);
	}

	/*
	 * Committed to selbs, setup state.
	 */
	obss = ic->ic_bss;
	ic->ic_bss = selbs;	/* caller assumed to bump refcnt */
	if (obss != NULL) {
		ieee80211_copy_bss(selbs, obss);
		ieee80211_free_node(obss);
	}
	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->in_chan);
	ic->ic_curchan = selbs->in_chan;
	ic->ic_phytype = selbs->in_phytype;
	/*
	 * Set the erp state (mostly the slot time) to deal with
	 * the auto-select case; this should be redundant if the
	 * mode is locked.
	 */
	ieee80211_reset_erp(ic);
	ieee80211_wme_initparams(ic);

	IEEE80211_UNLOCK(ic);
	if (ic->ic_opmode == IEEE80211_M_STA)
		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
	else
		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
}

/*
 * Leave the specified IBSS/BSS network.  The node is assumed to
 * be passed in with a held reference.
 */
void
ieee80211_sta_leave(ieee80211com_t *ic, ieee80211_node_t *in)
{
	IEEE80211_LOCK(ic);
	ic->ic_node_cleanup(in);
	ieee80211_notify_node_leave(ic, in);
	IEEE80211_UNLOCK(ic);
}

/*
 * Allocate a node. This is the default callback function for
 * ic_node_alloc. This function may be overridden by the driver
 * to allocate device specific node structure.
 */
/* ARGSUSED */
static ieee80211_node_t *
ieee80211_node_alloc(ieee80211com_t *ic)
{
	return (kmem_zalloc(sizeof (ieee80211_node_t), KM_SLEEP));
}

/*
 * Cleanup a node, free any memory associated with the node.
 * This is the default callback function for ic_node_cleanup
 * and may be overridden by the driver.
 */
static void
ieee80211_node_cleanup(ieee80211_node_t *in)
{
	in->in_associd = 0;
	in->in_rssi = 0;
	in->in_rstamp = 0;
	if (in->in_challenge != NULL) {
		kmem_free(in->in_challenge, IEEE80211_CHALLENGE_LEN);
		in->in_challenge = NULL;
	}
	if (in->in_rxfrag != NULL) {
		freemsg(in->in_rxfrag);
		in->in_rxfrag = NULL;
	}
}

/*
 * Free a node. This is the default callback function for ic_node_free
 * and may be overridden by the driver to free memory used by device
 * specific node structure
 */
static void
ieee80211_node_free(ieee80211_node_t *in)
{
	ieee80211com_t *ic = in->in_ic;

	ic->ic_node_cleanup(in);
	if (in->in_wpa_ie != NULL)
		ieee80211_free(in->in_wpa_ie);
	if (in->in_wme_ie != NULL)
		ieee80211_free(in->in_wme_ie);
	if (in->in_htcap_ie != NULL)
		ieee80211_free(in->in_htcap_ie);
	kmem_free(in, sizeof (ieee80211_node_t));
}

/*
 * Get a node current RSSI value. This is the default callback function
 * for ic_node_getrssi and may be overridden by the driver to provide
 * device specific RSSI calculation algorithm.
 */
static uint8_t
ieee80211_node_getrssi(const ieee80211_node_t *in)
{
	return (in->in_rssi);
}

/* Free fragment if not needed anymore */
static void
node_cleanfrag(ieee80211_node_t *in)
{
	clock_t ticks;

	ticks = ddi_get_lbolt();
	if (in->in_rxfrag != NULL && ticks > (in->in_rxfragstamp + hz)) {
		freemsg(in->in_rxfrag);
		in->in_rxfrag = NULL;
	}
}

/*
 * Setup a node. Initialize the node with specified macaddr. Associate
 * with the interface softc, ic, and add it to the specified node
 * database.
 */
static void
ieee80211_setup_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
    ieee80211_node_t *in, const uint8_t *macaddr)
{
	int32_t hash;

	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_setup_node(): "
	    "%p<%s> in %s table\n", in,
	    ieee80211_macaddr_sprintf(macaddr),
	    (nt != NULL) ? nt->nt_name : "NULL");

	in->in_ic = ic;
	IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
	hash = ieee80211_node_hash(macaddr);
	ieee80211_node_initref(in);		/* mark referenced */
	in->in_authmode = IEEE80211_AUTH_OPEN;
	in->in_txpower = ic->ic_txpowlimit;	/* max power */
	in->in_chan = IEEE80211_CHAN_ANYC;
	in->in_inact_reload = IEEE80211_INACT_INIT;
	in->in_inact = in->in_inact_reload;
	ieee80211_crypto_resetkey(ic, &in->in_ucastkey, IEEE80211_KEYIX_NONE);

	if (nt != NULL) {
		IEEE80211_NODE_LOCK(nt);
		list_insert_tail(&nt->nt_node, in);
		list_insert_tail(&nt->nt_hash[hash], in);
		in->in_table = nt;
		in->in_inact_reload = nt->nt_inact_init;
		IEEE80211_NODE_UNLOCK(nt);
	}
}

/*
 * Allocates and initialize a node with specified MAC address.
 * Associate the node with the interface ic. If the allocation
 * is successful, the node structure is initialized by
 * ieee80211_setup_node(); otherwise, NULL is returned
 */
ieee80211_node_t *
ieee80211_alloc_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
    const uint8_t *macaddr)
{
	ieee80211_node_t *in;

	in = ic->ic_node_alloc(ic);
	if (in != NULL)
		ieee80211_setup_node(ic, nt, in, macaddr);
	return (in);
}

/*
 * Craft a temporary node suitable for sending a management frame
 * to the specified station.  We craft only as much state as we
 * need to do the work since the node will be immediately reclaimed
 * once the send completes.
 */
ieee80211_node_t *
ieee80211_tmp_node(ieee80211com_t *ic, const uint8_t *macaddr)
{
	ieee80211_node_t *in;

	in = ic->ic_node_alloc(ic);
	if (in != NULL) {
		ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_tmp_node: "
		    "%p<%s>\n", in, ieee80211_macaddr_sprintf(macaddr));

		IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
		ieee80211_node_initref(in);		/* mark referenced */
		in->in_txpower = ic->ic_bss->in_txpower;
		/* NB: required by ieee80211_fix_rate */
		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
		ieee80211_crypto_resetkey(ic, &in->in_ucastkey,
		    IEEE80211_KEYIX_NONE);

		in->in_table = NULL;		/* NB: pedantic */
		in->in_ic = ic;
	}

	return (in);
}

/*
 * ieee80211_dup_bss() is similar to ieee80211_alloc_node(),
 * but is instead used to create a node database entry for
 * the specified BSSID. If the allocation is successful, the
 * node is initialized,  otherwise, NULL is returned.
 */
ieee80211_node_t *
ieee80211_dup_bss(ieee80211_node_table_t *nt, const uint8_t *macaddr)
{
	ieee80211com_t *ic = nt->nt_ic;
	ieee80211_node_t *in;

	in = ieee80211_alloc_node(ic, nt, macaddr);
	if (in != NULL) {
		/*
		 * Inherit from ic_bss.
		 */
		ieee80211_copy_bss(in, ic->ic_bss);
		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
	}

	return (in);
}

/*
 * Iterate through the node table, searching for a node entry which
 * matches macaddr. If the entry is found, its reference count is
 * incremented, and a pointer to the node is returned; otherwise,
 * NULL will be returned.
 * The node table lock is acquired by the caller.
 */
static ieee80211_node_t *
ieee80211_find_node_locked(ieee80211_node_table_t *nt, const uint8_t *macaddr)
{
	ieee80211_node_t *in;
	int hash;

	ASSERT(IEEE80211_NODE_IS_LOCKED(nt));

	hash = ieee80211_node_hash(macaddr);
	in = list_head(&nt->nt_hash[hash]);
	while (in != NULL) {
		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr))
			return (ieee80211_ref_node(in)); /* mark referenced */
		in = list_next(&nt->nt_hash[hash], in);
	}
	return (NULL);
}

/*
 * Iterate through the node table, searching for a node entry
 * which match specified mac address.
 * Return NULL if no matching node found.
 */
ieee80211_node_t *
ieee80211_find_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
{
	ieee80211_node_t *in;

	IEEE80211_NODE_LOCK(nt);
	in = ieee80211_find_node_locked(nt, macaddr);
	IEEE80211_NODE_UNLOCK(nt);
	return (in);
}

/*
 * Like find but search based on the ssid too.
 */
ieee80211_node_t *
ieee80211_find_node_with_ssid(ieee80211_node_table_t *nt,
	const uint8_t *macaddr, uint32_t ssidlen, const uint8_t *ssid)
{
	ieee80211_node_t *in;
	int hash;

	IEEE80211_NODE_LOCK(nt);

	hash = ieee80211_node_hash(macaddr);
	in = list_head(&nt->nt_hash[hash]);
	while (in != NULL) {
		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr) &&
		    in->in_esslen == ssidlen &&
		    memcmp(in->in_essid, ssid, ssidlen) == 0)
			break;
		in = list_next(&nt->nt_hash[hash], in);
	}
	if (in != NULL) {
		(void) ieee80211_ref_node(in); /* mark referenced */
	}
	IEEE80211_NODE_UNLOCK(nt);

	return (in);
}

/*
 * Fake up a node; this handles node discovery in adhoc mode.
 * Note that for the driver's benefit we treat this like an
 * association so the driver has an opportunity to setup it's
 * private state.
 */
ieee80211_node_t *
ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
{
	ieee80211com_t *ic = nt->nt_ic;
	ieee80211_node_t *in;

	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_fakeup_adhoc_node: "
	    "mac<%s>\n", ieee80211_macaddr_sprintf(macaddr));
	in = ieee80211_dup_bss(nt, macaddr);
	if (in != NULL) {
		/* no rate negotiation; just dup */
		in->in_rates = ic->ic_bss->in_rates;
		if (ic->ic_node_newassoc != NULL)
			ic->ic_node_newassoc(in, 1);
		ieee80211_node_authorize(in);
	}
	return (in);
}

static void
ieee80211_saveie(uint8_t **iep, const uint8_t *ie)
{
	uint_t ielen = ie[1]+2;
	/*
	 * Record information element for later use.
	 */
	if (*iep == NULL || (*iep)[1] != ie[1]) {
		if (*iep != NULL)
			ieee80211_free(*iep);
		*iep = ieee80211_malloc(ielen);
	}
	if (*iep != NULL)
		(void) memcpy(*iep, ie, ielen);
}

static void
saveie(uint8_t **iep, const uint8_t *ie)
{
	if (ie == NULL) {
		if (*iep != NULL)
			ieee80211_free(*iep);
		*iep = NULL;
	}
	else
		ieee80211_saveie(iep, ie);
}

/*
 * Process a beacon or probe response frame.
 */
void
ieee80211_add_scan(ieee80211com_t *ic, const struct ieee80211_scanparams *sp,
    const struct ieee80211_frame *wh, int subtype, int rssi, int rstamp)
{
	ieee80211_node_table_t *nt = &ic->ic_scan;
	ieee80211_node_t *in;
	boolean_t newnode = B_FALSE;

	in = ieee80211_find_node(nt, wh->i_addr3);
	if (in == NULL) {
		/*
		 * Create a new entry.
		 */
		in = ieee80211_alloc_node(ic, nt, wh->i_addr3);
		if (in == NULL) {
			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_add_scan: "
			    "alloc node failed\n");
			return;
		}
		/*
		 * inherit from ic_bss.
		 */
		ieee80211_copy_bss(in, ic->ic_bss);
		ieee80211_node_setchan(ic, in, ic->ic_curchan);
		newnode = B_TRUE;
	}

	/* ap beaconing multiple ssid w/ same bssid */

	/*
	 * sp->ssid[0] - element ID
	 * sp->ssid[1] - length
	 * sp->ssid[2]... - ssid
	 */
	if (sp->ssid[1] != 0 &&
	    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP ||
	    in->in_esslen == 0) {
		in->in_esslen = sp->ssid[1];
		bzero(in->in_essid, sizeof (in->in_essid));
		bcopy(sp->ssid + 2, in->in_essid, sp->ssid[1]);
	}
	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
	in->in_rssi = (uint8_t)rssi;
	in->in_rstamp = rstamp;
	bcopy(sp->tstamp, in->in_tstamp.data, sizeof (in->in_tstamp));
	in->in_intval = sp->bintval;
	in->in_capinfo = sp->capinfo;
	in->in_chan = &ic->ic_sup_channels[sp->chan];
	in->in_phytype = sp->phytype;
	in->in_fhdwell = sp->fhdwell;
	in->in_fhindex = sp->fhindex;
	in->in_erp = sp->erp;
	if (sp->tim != NULL) {
		struct ieee80211_tim_ie *ie;

		ie = (struct ieee80211_tim_ie *)sp->tim;
		in->in_dtim_count = ie->tim_count;
		in->in_dtim_period = ie->tim_period;
	}
	/*
	 * Record the byte offset from the mac header to
	 * the start of the TIM information element for
	 * use by hardware and/or to speedup software
	 * processing of beacon frames.
	 */
	in->in_tim_off = sp->timoff;
	/*
	 * Record optional information elements that might be
	 * used by applications or drivers.
	 */
	saveie(&in->in_wme_ie, sp->wme);
	saveie(&in->in_wpa_ie, sp->wpa);
	saveie(&in->in_htcap_ie, sp->htcap);
	/* parsed in ieee80211_sta_join() */
	if (sp->htcap != NULL)
		ieee80211_parse_htcap(in, in->in_htcap_ie);

	/* NB: must be after in_chan is setup */
	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
	    IEEE80211_F_DOSORT);

	if (!newnode)
		ieee80211_free_node(in);
}

/*
 * Initialize/update an ad-hoc node with contents from a received
 * beacon frame.
 */
void
ieee80211_init_neighbor(ieee80211_node_t *in, const struct ieee80211_frame *wh,
    const struct ieee80211_scanparams *sp)
{
	in->in_esslen = sp->ssid[1];
	(void) memcpy(in->in_essid, sp->ssid + 2, sp->ssid[1]);
	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
	(void) memcpy(in->in_tstamp.data, sp->tstamp, sizeof (in->in_tstamp));
	in->in_intval = sp->bintval;
	in->in_capinfo = sp->capinfo;
	in->in_chan = in->in_ic->ic_curchan;
	in->in_fhdwell = sp->fhdwell;
	in->in_fhindex = sp->fhindex;
	in->in_erp = sp->erp;
	in->in_tim_off = sp->timoff;
	if (sp->wme != NULL)
		ieee80211_saveie(&in->in_wme_ie, sp->wme);

	/* NB: must be after in_chan is setup */
	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
	    IEEE80211_F_DOSORT);
}

/*
 * Do node discovery in adhoc mode on receipt of a beacon
 * or probe response frame.  Note that for the driver's
 * benefit we we treat this like an association so the
 * driver has an opportuinty to setup it's private state.
 */
ieee80211_node_t *
ieee80211_add_neighbor(ieee80211com_t *ic, const struct ieee80211_frame *wh,
    const struct ieee80211_scanparams *sp)
{
	ieee80211_node_t *in;

	in = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
	if (in != NULL) {
		ieee80211_init_neighbor(in, wh, sp);
		if (ic->ic_node_newassoc != NULL)
			ic->ic_node_newassoc(in, 1);
	}
	return (in);
}

#define	IEEE80211_IS_CTL(wh)	\
	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)

#define	IEEE80211_IS_PSPOLL(wh)	\
	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==	\
	    IEEE80211_FC0_SUBTYPE_PS_POLL)

#define	IEEE80211_IS_BAR(wh)	\
	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==	\
	    IEEE80211_FC0_SUBTYPE_BAR)

/*
 * Locate the node for sender, track state, and then pass the
 * (referenced) node up to the 802.11 layer for its use.  We
 * are required to pass some node so we fall back to ic_bss
 * when this frame is from an unknown sender.  The 802.11 layer
 * knows this means the sender wasn't in the node table and
 * acts accordingly.
 */
ieee80211_node_t *
ieee80211_find_rxnode(ieee80211com_t *ic, const struct ieee80211_frame *wh)
{
	ieee80211_node_table_t *nt;
	ieee80211_node_t *in;

	/* may want scanned nodes in the neighbor table for adhoc */
	if (ic->ic_opmode == IEEE80211_M_STA ||
	    (ic->ic_flags & IEEE80211_F_SCAN)) {
		nt = &ic->ic_scan;
	} else {
		nt = &ic->ic_sta;
	}

	IEEE80211_NODE_LOCK(nt);
	if (IEEE80211_IS_CTL(wh) &&
	    !IEEE80211_IS_PSPOLL(wh) && !IEEE80211_IS_BAR(wh))
		in = ieee80211_find_node_locked(nt, wh->i_addr1);
	else
		in = ieee80211_find_node_locked(nt, wh->i_addr2);
	IEEE80211_NODE_UNLOCK(nt);

	if (in == NULL)
		in = ieee80211_ref_node(ic->ic_bss);

	return (in);
}

#undef IEEE80211_IS_BAR
#undef IEEE80211_IS_PSPOLL
#undef IEEE80211_IS_CTL

/*
 * Return a reference to the appropriate node for sending
 * a data frame.  This handles node discovery in adhoc networks.
 */
ieee80211_node_t *
ieee80211_find_txnode(ieee80211com_t *ic, const uint8_t *daddr)
{
	ieee80211_node_table_t *nt = &ic->ic_sta;
	ieee80211_node_t *in;

	/*
	 * The destination address should be in the node table
	 * unless this is a multicast/broadcast frame.  We can
	 * also optimize station mode operation, all frames go
	 * to the bss node.
	 */
	IEEE80211_NODE_LOCK(nt);
	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(daddr))
		in = ieee80211_ref_node(ic->ic_bss);
	else
		in = ieee80211_find_node_locked(nt, daddr);
	IEEE80211_NODE_UNLOCK(nt);

	if (in == NULL) {
		if (ic->ic_opmode == IEEE80211_M_IBSS) {
			/*
			 * In adhoc mode cons up a node for the destination.
			 * Note that we need an additional reference for the
			 * caller to be consistent with
			 * ieee80211_find_node_locked
			 * can't hold lock across ieee80211_dup_bss 'cuz of
			 * recursive locking
			 */
			in = ieee80211_fakeup_adhoc_node(nt, daddr);
			if (in != NULL)
				(void) ieee80211_ref_node(in);
		} else {
			ieee80211_dbg(IEEE80211_MSG_OUTPUT,
			    "ieee80211_find_txnode: "
			    "[%s] no node, discard frame\n",
			    ieee80211_macaddr_sprintf(daddr));
		}
	}
	return (in);
}

/*
 * Remove a node from the node database entries and free memory
 * associated with the node. The node table lock is acquired by
 * the caller.
 */
static void
ieee80211_free_node_locked(ieee80211_node_t *in)
{
	ieee80211com_t *ic = in->in_ic;
	ieee80211_node_table_t *nt = in->in_table;
	int32_t hash;

	if (nt != NULL) {
		hash = ieee80211_node_hash(in->in_macaddr);
		list_remove(&nt->nt_hash[hash], in);
		list_remove(&nt->nt_node, in);
	}
	ic->ic_node_free(in);
}

/*
 * Remove a node from the node database entries and free any
 * memory associated with the node.
 * This method can be overridden in ieee80211_attach()
 */
void
ieee80211_free_node(ieee80211_node_t *in)
{
	ieee80211_node_table_t *nt = in->in_table;

	if (nt != NULL)
		IEEE80211_NODE_LOCK(nt);
	if (ieee80211_node_decref_nv(in) == 0)
		ieee80211_free_node_locked(in);
	if (nt != NULL)
		IEEE80211_NODE_UNLOCK(nt);
}

/*
 * Reclaim a node.  If this is the last reference count then
 * do the normal free work.  Otherwise remove it from the node
 * table and mark it gone by clearing the back-reference.
 */
static void
ieee80211_node_reclaim(ieee80211_node_table_t *nt, ieee80211_node_t *in)
{
	int32_t hash;

	IEEE80211_NODE_LOCK_ASSERT(nt);
	ieee80211_dbg(IEEE80211_MSG_NODE, "node_reclaim: "
	    " remove %p<%s> from %s table, refcnt %d\n",
	    in, ieee80211_macaddr_sprintf(in->in_macaddr), nt->nt_name,
	    ieee80211_node_refcnt(in));

	if (ieee80211_node_decref_nv(in) != 0) {
		/*
		 * Clear any entry in the unicast key mapping table.
		 * We need to do it here so rx lookups don't find it
		 * in the mapping table even if it's not in the hash
		 * table.  We cannot depend on the mapping table entry
		 * being cleared because the node may not be free'd.
		 */
		hash = ieee80211_node_hash(in->in_macaddr);
		list_remove(&nt->nt_hash[hash], in);
		list_remove(&nt->nt_node, in);
		in->in_table = NULL;
	} else {
		ieee80211_free_node_locked(in);
	}
}

/*
 * Iterate through the node list and reclaim all node in the node table.
 * The node table lock is acquired by the caller
 */
static void
ieee80211_free_allnodes_locked(ieee80211_node_table_t *nt)
{
	ieee80211_node_t *in;

	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_free_allnodes_locked(): "
	    "free all nodes in %s table\n", nt->nt_name);

	in = list_head(&nt->nt_node);
	while (in != NULL) {
		ieee80211_node_reclaim(nt, in);
		in = list_head(&nt->nt_node);
	}
	ieee80211_reset_erp(nt->nt_ic);
}

/*
 * Iterate through the node list, calling ieee80211_node_reclaim() for
 * all nodes associated with the interface.
 */
static void
ieee80211_free_allnodes(ieee80211_node_table_t *nt)
{
	IEEE80211_NODE_LOCK(nt);
	ieee80211_free_allnodes_locked(nt);
	IEEE80211_NODE_UNLOCK(nt);
}

/*
 * Timeout entries in the scan cache. This is the timeout callback
 * function of node table ic_scan which is called when the inactivity
 * timer expires.
 */
static void
ieee80211_timeout_scan_candidates(ieee80211_node_table_t *nt)
{
	ieee80211com_t *ic = nt->nt_ic;
	ieee80211_node_t *in;

	IEEE80211_NODE_LOCK(nt);
	in = ic->ic_bss;
	node_cleanfrag(in);	/* Free fragment if not needed */
	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
	IEEE80211_NODE_UNLOCK(nt);
}

/*
 * Timeout inactive stations and do related housekeeping.
 * Note that we cannot hold the node lock while sending a
 * frame as this would lead to a LOR.  Instead we use a
 * generation number to mark nodes that we've scanned and
 * drop the lock and restart a scan if we have to time out
 * a node.  Since we are single-threaded by virtue of
 * controlling the inactivity timer we can be sure this will
 * process each node only once.
 */
static void
ieee80211_timeout_stations(ieee80211_node_table_t *nt)
{
	ieee80211com_t *ic = nt->nt_ic;
	ieee80211_impl_t *im = ic->ic_private;
	ieee80211_node_t *in = NULL;
	uint32_t gen;
	boolean_t isadhoc;

	IEEE80211_LOCK_ASSERT(ic);
	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
	    ic->ic_opmode == IEEE80211_M_AHDEMO);
	IEEE80211_SCAN_LOCK(nt);
	gen = ++nt->nt_scangen;
restart:
	IEEE80211_NODE_LOCK(nt);
	for (in = list_head(&nt->nt_node); in != NULL;
	    in = list_next(&nt->nt_node, in)) {
		if (in->in_scangen == gen)	/* previously handled */
			continue;
		in->in_scangen = gen;
		node_cleanfrag(in);	/* free fragment if not needed */

		/*
		 * Special case ourself; we may be idle for extended periods
		 * of time and regardless reclaiming our state is wrong.
		 */
		if (in == ic->ic_bss)
			continue;
		in->in_inact--;
		if (in->in_associd != 0 || isadhoc) {
			/*
			 * Probe the station before time it out.  We
			 * send a null data frame which may not be
			 * uinversally supported by drivers (need it
			 * for ps-poll support so it should be...).
			 */
			if (0 < in->in_inact &&
			    in->in_inact <= im->im_inact_probe) {
				ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
				    "probe station due to inactivity\n");
				IEEE80211_NODE_UNLOCK(nt);
				IEEE80211_UNLOCK(ic);
				(void) ieee80211_send_nulldata(in);
				IEEE80211_LOCK(ic);
				goto restart;
			}
		}
		if (in->in_inact <= 0) {
			ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
			    "station timed out due to inact (refcnt %u)\n",
			    ieee80211_node_refcnt(in));
			/*
			 * Send a deauthenticate frame and drop the station.
			 * This is somewhat complicated due to reference counts
			 * and locking.  At this point a station will typically
			 * have a reference count of 1.  ieee80211_node_leave
			 * will do a "free" of the node which will drop the
			 * reference count.  But in the meantime a reference
			 * wil be held by the deauth frame.  The actual reclaim
			 * of the node will happen either after the tx is
			 * completed or by ieee80211_node_leave.
			 *
			 * Separately we must drop the node lock before sending
			 * in case the driver takes a lock, as this will result
			 * in  LOR between the node lock and the driver lock.
			 */
			IEEE80211_NODE_UNLOCK(nt);
			if (in->in_associd != 0) {
				IEEE80211_UNLOCK(ic);
				IEEE80211_SEND_MGMT(ic, in,
				    IEEE80211_FC0_SUBTYPE_DEAUTH,
				    IEEE80211_REASON_AUTH_EXPIRE);
				IEEE80211_LOCK(ic);
			}
			ieee80211_node_leave(ic, in);
			goto restart;
		}
	}
	IEEE80211_NODE_UNLOCK(nt);

	IEEE80211_SCAN_UNLOCK(nt);

	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
}

/*
 * Call the user-defined call back function for all nodes in
 * the node cache. The callback is invoked with the user-supplied
 * value and a pointer to the current node.
 */
void
ieee80211_iterate_nodes(ieee80211_node_table_t *nt, ieee80211_iter_func *f,
    void *arg)
{
	ieee80211_node_t *in;

	IEEE80211_NODE_LOCK(nt);
	in = list_head(&nt->nt_node);
	while (in != NULL) {
		if (in->in_chan == IEEE80211_CHAN_ANYC) {
			in = list_next(&nt->nt_node, in);
			continue;
		}
		(void) ieee80211_ref_node(in);
		IEEE80211_NODE_UNLOCK(nt);
		(*f)(arg, in);
		ieee80211_free_node(in);
		IEEE80211_NODE_LOCK(nt);
		in = list_next(&nt->nt_node, in);
	}
	IEEE80211_NODE_UNLOCK(nt);
}

/*
 * Handle bookkeeping for station deauthentication/disassociation
 * when operating as an ap.
 */
static void
ieee80211_node_leave(ieee80211com_t *ic, ieee80211_node_t *in)
{
	ieee80211_node_table_t *nt = in->in_table;

	ASSERT(ic->ic_opmode == IEEE80211_M_IBSS);

	/*
	 * Remove the node from any table it's recorded in and
	 * drop the caller's reference.  Removal from the table
	 * is important to insure the node is not reprocessed
	 * for inactivity.
	 */
	if (nt != NULL) {
		IEEE80211_NODE_LOCK(nt);
		ieee80211_node_reclaim(nt, in);
		IEEE80211_NODE_UNLOCK(nt);
	} else {
		ieee80211_free_node(in);
	}
}

/*
 * Initialize a node table with specified name, inactivity timer value
 * and callback inactivity timeout function. Associate the node table
 * with interface softc, ic.
 */
static void
ieee80211_node_table_init(ieee80211com_t *ic, ieee80211_node_table_t *nt,
    const char *name, int inact, int keyixmax,
    void (*timeout)(ieee80211_node_table_t *))
{
	int i;

	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_init():"
	    "%s table, inact %d\n", name, inact);

	nt->nt_ic = ic;
	nt->nt_name = name;
	nt->nt_inact_timer = 0;
	nt->nt_inact_init = inact;
	nt->nt_timeout = timeout;
	nt->nt_keyixmax = keyixmax;
	nt->nt_scangen = 1;
	mutex_init(&nt->nt_scanlock, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&nt->nt_nodelock, NULL, MUTEX_DRIVER, NULL);

	list_create(&nt->nt_node, sizeof (ieee80211_node_t),
	    offsetof(ieee80211_node_t, in_node));
	for (i = 0; i < IEEE80211_NODE_HASHSIZE; i++) {
		list_create(&nt->nt_hash[i], sizeof (ieee80211_node_t),
		    offsetof(ieee80211_node_t, in_hash));
	}
}

/*
 * Reset a node table. Clean its inactivity timer and call
 * ieee80211_free_allnodes_locked() to free all nodes in the
 * node table.
 */
void
ieee80211_node_table_reset(ieee80211_node_table_t *nt)
{
	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_reset(): "
	    "%s table\n", nt->nt_name);

	IEEE80211_NODE_LOCK(nt);
	nt->nt_inact_timer = 0;
	ieee80211_free_allnodes_locked(nt);
	IEEE80211_NODE_UNLOCK(nt);
}

/*
 * Destroy a node table. Free all nodes in the node table.
 * This function is usually called by node detach function.
 */
static void
ieee80211_node_table_cleanup(ieee80211_node_table_t *nt)
{
	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_cleanup(): "
	    "%s table\n", nt->nt_name);

	IEEE80211_NODE_LOCK(nt);
	ieee80211_free_allnodes_locked(nt);
	IEEE80211_NODE_UNLOCK(nt);
	mutex_destroy(&nt->nt_nodelock);
	mutex_destroy(&nt->nt_scanlock);
}