key.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  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. * File: key.c
  21. *
  22. * Purpose: Implement functions for 802.11i Key management
  23. *
  24. * Author: Jerry Chen
  25. *
  26. * Date: May 29, 2003
  27. *
  28. * Functions:
  29. * KeyvInitTable - Init Key management table
  30. * KeybGetKey - Get Key from table
  31. * KeybSetKey - Set Key to table
  32. * KeybRemoveKey - Remove Key from table
  33. * KeybGetTransmitKey - Get Transmit Key from table
  34. *
  35. * Revision History:
  36. *
  37. */
  38. #include "tmacro.h"
  39. #include "key.h"
  40. #include "mac.h"
  41. /*--------------------- Static Definitions -------------------------*/
  42. /*--------------------- Static Classes ----------------------------*/
  43. /*--------------------- Static Variables --------------------------*/
  44. static int msglevel =MSG_LEVEL_INFO;
  45. //static int msglevel =MSG_LEVEL_DEBUG;
  46. /*--------------------- Static Functions --------------------------*/
  47. /*--------------------- Export Variables --------------------------*/
  48. /*--------------------- Static Definitions -------------------------*/
  49. /*--------------------- Static Classes ----------------------------*/
  50. /*--------------------- Static Variables --------------------------*/
  51. /*--------------------- Static Functions --------------------------*/
  52. static void
  53. s_vCheckKeyTableValid (PSKeyManagement pTable, unsigned long dwIoBase)
  54. {
  55. int i;
  56. for (i=0;i<MAX_KEY_TABLE;i++) {
  57. if ((pTable->KeyTable[i].bInUse == true) &&
  58. (pTable->KeyTable[i].PairwiseKey.bKeyValid == false) &&
  59. (pTable->KeyTable[i].GroupKey[0].bKeyValid == false) &&
  60. (pTable->KeyTable[i].GroupKey[1].bKeyValid == false) &&
  61. (pTable->KeyTable[i].GroupKey[2].bKeyValid == false) &&
  62. (pTable->KeyTable[i].GroupKey[3].bKeyValid == false)
  63. ) {
  64. pTable->KeyTable[i].bInUse = false;
  65. pTable->KeyTable[i].wKeyCtl = 0;
  66. pTable->KeyTable[i].bSoftWEP = false;
  67. MACvDisableKeyEntry(dwIoBase, i);
  68. }
  69. }
  70. }
  71. /*--------------------- Export Functions --------------------------*/
  72. /*
  73. * Description: Init Key management table
  74. *
  75. * Parameters:
  76. * In:
  77. * pTable - Pointer to Key table
  78. * Out:
  79. * none
  80. *
  81. * Return Value: none
  82. *
  83. */
  84. void KeyvInitTable (PSKeyManagement pTable, unsigned long dwIoBase)
  85. {
  86. int i;
  87. int jj;
  88. for (i=0;i<MAX_KEY_TABLE;i++) {
  89. pTable->KeyTable[i].bInUse = false;
  90. pTable->KeyTable[i].PairwiseKey.bKeyValid = false;
  91. pTable->KeyTable[i].PairwiseKey.pvKeyTable = (void *)&pTable->KeyTable[i];
  92. for (jj=0; jj < MAX_GROUP_KEY; jj++) {
  93. pTable->KeyTable[i].GroupKey[jj].bKeyValid = false;
  94. pTable->KeyTable[i].GroupKey[jj].pvKeyTable = (void *)&pTable->KeyTable[i];
  95. }
  96. pTable->KeyTable[i].wKeyCtl = 0;
  97. pTable->KeyTable[i].dwGTKeyIndex = 0;
  98. pTable->KeyTable[i].bSoftWEP = false;
  99. MACvDisableKeyEntry(dwIoBase, i);
  100. }
  101. }
  102. /*
  103. * Description: Get Key from table
  104. *
  105. * Parameters:
  106. * In:
  107. * pTable - Pointer to Key table
  108. * pbyBSSID - BSSID of Key
  109. * dwKeyIndex - Key Index (0xFFFFFFFF means pairwise key)
  110. * Out:
  111. * pKey - Key return
  112. *
  113. * Return Value: true if found otherwise false
  114. *
  115. */
  116. bool KeybGetKey (
  117. PSKeyManagement pTable,
  118. unsigned char *pbyBSSID,
  119. unsigned long dwKeyIndex,
  120. PSKeyItem *pKey
  121. )
  122. {
  123. int i;
  124. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetKey() \n");
  125. *pKey = NULL;
  126. for (i=0;i<MAX_KEY_TABLE;i++) {
  127. if ((pTable->KeyTable[i].bInUse == true) &&
  128. !compare_ether_addr(pTable->KeyTable[i].abyBSSID, pbyBSSID)) {
  129. if (dwKeyIndex == 0xFFFFFFFF) {
  130. if (pTable->KeyTable[i].PairwiseKey.bKeyValid == true) {
  131. *pKey = &(pTable->KeyTable[i].PairwiseKey);
  132. return (true);
  133. }
  134. else {
  135. return (false);
  136. }
  137. } else if (dwKeyIndex < MAX_GROUP_KEY) {
  138. if (pTable->KeyTable[i].GroupKey[dwKeyIndex].bKeyValid == true) {
  139. *pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex]);
  140. return (true);
  141. }
  142. else {
  143. return (false);
  144. }
  145. }
  146. else {
  147. return (false);
  148. }
  149. }
  150. }
  151. return (false);
  152. }
  153. /*
  154. * Description: Set Key to table
  155. *
  156. * Parameters:
  157. * In:
  158. * pTable - Pointer to Key table
  159. * pbyBSSID - BSSID of Key
  160. * dwKeyIndex - Key index (reference to NDIS DDK)
  161. * uKeyLength - Key length
  162. * KeyRSC - Key RSC
  163. * pbyKey - Pointer to key
  164. * Out:
  165. * none
  166. *
  167. * Return Value: true if success otherwise false
  168. *
  169. */
  170. bool KeybSetKey (
  171. PSKeyManagement pTable,
  172. unsigned char *pbyBSSID,
  173. unsigned long dwKeyIndex,
  174. unsigned long uKeyLength,
  175. PQWORD pKeyRSC,
  176. unsigned char *pbyKey,
  177. unsigned char byKeyDecMode,
  178. unsigned long dwIoBase,
  179. unsigned char byLocalID
  180. )
  181. {
  182. int i,j;
  183. unsigned int ii;
  184. PSKeyItem pKey;
  185. unsigned int uKeyIdx;
  186. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetKey: %lX\n", dwKeyIndex);
  187. j = (MAX_KEY_TABLE-1);
  188. for (i=0;i<(MAX_KEY_TABLE-1);i++) {
  189. if ((pTable->KeyTable[i].bInUse == false) &&
  190. (j == (MAX_KEY_TABLE-1))) {
  191. // found empty table
  192. j = i;
  193. }
  194. if ((pTable->KeyTable[i].bInUse == true) &&
  195. !compare_ether_addr(pTable->KeyTable[i].abyBSSID, pbyBSSID)) {
  196. // found table already exist
  197. if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
  198. // Pairwise key
  199. pKey = &(pTable->KeyTable[i].PairwiseKey);
  200. pTable->KeyTable[i].wKeyCtl &= 0xFFF0; // clear pairwise key control filed
  201. pTable->KeyTable[i].wKeyCtl |= byKeyDecMode;
  202. uKeyIdx = 4; // use HW key entry 4 for pairwise key
  203. } else {
  204. // Group key
  205. if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY)
  206. return (false);
  207. pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF]);
  208. if ((dwKeyIndex & TRANSMIT_KEY) != 0) {
  209. // Group transmit key
  210. pTable->KeyTable[i].dwGTKeyIndex = dwKeyIndex;
  211. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[i].dwGTKeyIndex, i);
  212. }
  213. pTable->KeyTable[i].wKeyCtl &= 0xFF0F; // clear group key control filed
  214. pTable->KeyTable[i].wKeyCtl |= (byKeyDecMode << 4);
  215. pTable->KeyTable[i].wKeyCtl |= 0x0040; // use group key for group address
  216. uKeyIdx = (dwKeyIndex & 0x000000FF);
  217. }
  218. pTable->KeyTable[i].wKeyCtl |= 0x8000; // enable on-fly
  219. pKey->bKeyValid = true;
  220. pKey->uKeyLength = uKeyLength;
  221. pKey->dwKeyIndex = dwKeyIndex;
  222. pKey->byCipherSuite = byKeyDecMode;
  223. memcpy(pKey->abyKey, pbyKey, uKeyLength);
  224. if (byKeyDecMode == KEY_CTL_WEP) {
  225. if (uKeyLength == WLAN_WEP40_KEYLEN)
  226. pKey->abyKey[15] &= 0x7F;
  227. if (uKeyLength == WLAN_WEP104_KEYLEN)
  228. pKey->abyKey[15] |= 0x80;
  229. }
  230. MACvSetKeyEntry(dwIoBase, pTable->KeyTable[i].wKeyCtl, i, uKeyIdx, pbyBSSID, (unsigned long *)pKey->abyKey, byLocalID);
  231. if ((dwKeyIndex & USE_KEYRSC) == 0) {
  232. // RSC set by NIC
  233. memset(&(pKey->KeyRSC), 0, sizeof(QWORD));
  234. }
  235. else {
  236. memcpy(&(pKey->KeyRSC), pKeyRSC, sizeof(QWORD));
  237. }
  238. pKey->dwTSC47_16 = 0;
  239. pKey->wTSC15_0 = 0;
  240. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
  241. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
  242. //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", pKey->uKeyLength);
  243. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
  244. for (ii = 0; ii < pKey->uKeyLength; ii++) {
  245. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02x ", pKey->abyKey[ii]);
  246. }
  247. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  248. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n ", pKey->dwTSC47_16);
  249. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n ", pKey->wTSC15_0);
  250. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n ", pKey->dwKeyIndex);
  251. return (true);
  252. }
  253. }
  254. if (j < (MAX_KEY_TABLE-1)) {
  255. memcpy(pTable->KeyTable[j].abyBSSID,pbyBSSID,ETH_ALEN);
  256. pTable->KeyTable[j].bInUse = true;
  257. if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
  258. // Pairwise key
  259. pKey = &(pTable->KeyTable[j].PairwiseKey);
  260. pTable->KeyTable[j].wKeyCtl &= 0xFFF0; // clear pairwise key control filed
  261. pTable->KeyTable[j].wKeyCtl |= byKeyDecMode;
  262. uKeyIdx = 4; // use HW key entry 4 for pairwise key
  263. } else {
  264. // Group key
  265. if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY)
  266. return (false);
  267. pKey = &(pTable->KeyTable[j].GroupKey[dwKeyIndex & 0x000000FF]);
  268. if ((dwKeyIndex & TRANSMIT_KEY) != 0) {
  269. // Group transmit key
  270. pTable->KeyTable[j].dwGTKeyIndex = dwKeyIndex;
  271. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(N)[%lX]: %d\n", pTable->KeyTable[j].dwGTKeyIndex, j);
  272. }
  273. pTable->KeyTable[j].wKeyCtl &= 0xFF0F; // clear group key control filed
  274. pTable->KeyTable[j].wKeyCtl |= (byKeyDecMode << 4);
  275. pTable->KeyTable[j].wKeyCtl |= 0x0040; // use group key for group address
  276. uKeyIdx = (dwKeyIndex & 0x000000FF);
  277. }
  278. pTable->KeyTable[j].wKeyCtl |= 0x8000; // enable on-fly
  279. pKey->bKeyValid = true;
  280. pKey->uKeyLength = uKeyLength;
  281. pKey->dwKeyIndex = dwKeyIndex;
  282. pKey->byCipherSuite = byKeyDecMode;
  283. memcpy(pKey->abyKey, pbyKey, uKeyLength);
  284. if (byKeyDecMode == KEY_CTL_WEP) {
  285. if (uKeyLength == WLAN_WEP40_KEYLEN)
  286. pKey->abyKey[15] &= 0x7F;
  287. if (uKeyLength == WLAN_WEP104_KEYLEN)
  288. pKey->abyKey[15] |= 0x80;
  289. }
  290. MACvSetKeyEntry(dwIoBase, pTable->KeyTable[j].wKeyCtl, j, uKeyIdx, pbyBSSID, (unsigned long *)pKey->abyKey, byLocalID);
  291. if ((dwKeyIndex & USE_KEYRSC) == 0) {
  292. // RSC set by NIC
  293. memset(&(pKey->KeyRSC), 0, sizeof(QWORD));
  294. }
  295. else {
  296. memcpy(&(pKey->KeyRSC), pKeyRSC, sizeof(QWORD));
  297. }
  298. pKey->dwTSC47_16 = 0;
  299. pKey->wTSC15_0 = 0;
  300. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(N): \n");
  301. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
  302. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", (int)pKey->uKeyLength);
  303. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
  304. for (ii = 0; ii < pKey->uKeyLength; ii++) {
  305. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02x ", pKey->abyKey[ii]);
  306. }
  307. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  308. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n ", pKey->dwTSC47_16);
  309. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n ", pKey->wTSC15_0);
  310. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n ", pKey->dwKeyIndex);
  311. return (true);
  312. }
  313. return (false);
  314. }
  315. /*
  316. * Description: Remove Key from table
  317. *
  318. * Parameters:
  319. * In:
  320. * pTable - Pointer to Key table
  321. * pbyBSSID - BSSID of Key
  322. * dwKeyIndex - Key Index (reference to NDIS DDK)
  323. * Out:
  324. * none
  325. *
  326. * Return Value: true if success otherwise false
  327. *
  328. */
  329. bool KeybRemoveKey (
  330. PSKeyManagement pTable,
  331. unsigned char *pbyBSSID,
  332. unsigned long dwKeyIndex,
  333. unsigned long dwIoBase
  334. )
  335. {
  336. int i;
  337. if (is_broadcast_ether_addr(pbyBSSID)) {
  338. // dealte all key
  339. if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
  340. for (i=0;i<MAX_KEY_TABLE;i++) {
  341. pTable->KeyTable[i].PairwiseKey.bKeyValid = false;
  342. }
  343. s_vCheckKeyTableValid(pTable, dwIoBase);
  344. return true;
  345. }
  346. else if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
  347. for (i=0;i<MAX_KEY_TABLE;i++) {
  348. pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = false;
  349. if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[i].dwGTKeyIndex & 0x7FFFFFFF)) {
  350. // remove Group transmit key
  351. pTable->KeyTable[i].dwGTKeyIndex = 0;
  352. }
  353. }
  354. s_vCheckKeyTableValid(pTable, dwIoBase);
  355. return true;
  356. }
  357. else {
  358. return false;
  359. }
  360. }
  361. for (i=0;i<MAX_KEY_TABLE;i++) {
  362. if ((pTable->KeyTable[i].bInUse == true) &&
  363. !compare_ether_addr(pTable->KeyTable[i].abyBSSID, pbyBSSID)) {
  364. if ((dwKeyIndex & PAIRWISE_KEY) != 0) {
  365. pTable->KeyTable[i].PairwiseKey.bKeyValid = false;
  366. s_vCheckKeyTableValid(pTable, dwIoBase);
  367. return (true);
  368. }
  369. else if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
  370. pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = false;
  371. if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[i].dwGTKeyIndex & 0x7FFFFFFF)) {
  372. // remove Group transmit key
  373. pTable->KeyTable[i].dwGTKeyIndex = 0;
  374. }
  375. s_vCheckKeyTableValid(pTable, dwIoBase);
  376. return (true);
  377. }
  378. else {
  379. return (false);
  380. }
  381. }
  382. }
  383. return (false);
  384. }
  385. /*
  386. * Description: Remove Key from table
  387. *
  388. * Parameters:
  389. * In:
  390. * pTable - Pointer to Key table
  391. * pbyBSSID - BSSID of Key
  392. * Out:
  393. * none
  394. *
  395. * Return Value: true if success otherwise false
  396. *
  397. */
  398. bool KeybRemoveAllKey (
  399. PSKeyManagement pTable,
  400. unsigned char *pbyBSSID,
  401. unsigned long dwIoBase
  402. )
  403. {
  404. int i,u;
  405. for (i=0;i<MAX_KEY_TABLE;i++) {
  406. if ((pTable->KeyTable[i].bInUse == true) &&
  407. !compare_ether_addr(pTable->KeyTable[i].abyBSSID, pbyBSSID)) {
  408. pTable->KeyTable[i].PairwiseKey.bKeyValid = false;
  409. for(u=0;u<MAX_GROUP_KEY;u++) {
  410. pTable->KeyTable[i].GroupKey[u].bKeyValid = false;
  411. }
  412. pTable->KeyTable[i].dwGTKeyIndex = 0;
  413. s_vCheckKeyTableValid(pTable, dwIoBase);
  414. return (true);
  415. }
  416. }
  417. return (false);
  418. }
  419. /*
  420. * Description: Remove WEP Key from table
  421. *
  422. * Parameters:
  423. * In:
  424. * pTable - Pointer to Key table
  425. * Out:
  426. * none
  427. *
  428. * Return Value: true if success otherwise false
  429. *
  430. */
  431. void KeyvRemoveWEPKey (
  432. PSKeyManagement pTable,
  433. unsigned long dwKeyIndex,
  434. unsigned long dwIoBase
  435. )
  436. {
  437. if ((dwKeyIndex & 0x000000FF) < MAX_GROUP_KEY) {
  438. if (pTable->KeyTable[MAX_KEY_TABLE-1].bInUse == true) {
  439. if (pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF].byCipherSuite == KEY_CTL_WEP) {
  440. pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF].bKeyValid = false;
  441. if ((dwKeyIndex & 0x7FFFFFFF) == (pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex & 0x7FFFFFFF)) {
  442. // remove Group transmit key
  443. pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex = 0;
  444. }
  445. }
  446. }
  447. s_vCheckKeyTableValid(pTable, dwIoBase);
  448. }
  449. return;
  450. }
  451. void KeyvRemoveAllWEPKey (
  452. PSKeyManagement pTable,
  453. unsigned long dwIoBase
  454. )
  455. {
  456. int i;
  457. for(i=0;i<MAX_GROUP_KEY;i++) {
  458. KeyvRemoveWEPKey(pTable, i, dwIoBase);
  459. }
  460. }
  461. /*
  462. * Description: Get Transmit Key from table
  463. *
  464. * Parameters:
  465. * In:
  466. * pTable - Pointer to Key table
  467. * pbyBSSID - BSSID of Key
  468. * Out:
  469. * pKey - Key return
  470. *
  471. * Return Value: true if found otherwise false
  472. *
  473. */
  474. bool KeybGetTransmitKey (
  475. PSKeyManagement pTable,
  476. unsigned char *pbyBSSID,
  477. unsigned long dwKeyType,
  478. PSKeyItem *pKey
  479. )
  480. {
  481. int i, ii;
  482. *pKey = NULL;
  483. for (i=0;i<MAX_KEY_TABLE;i++) {
  484. if ((pTable->KeyTable[i].bInUse == true) &&
  485. !compare_ether_addr(pTable->KeyTable[i].abyBSSID, pbyBSSID)) {
  486. if (dwKeyType == PAIRWISE_KEY) {
  487. if (pTable->KeyTable[i].PairwiseKey.bKeyValid == true) {
  488. *pKey = &(pTable->KeyTable[i].PairwiseKey);
  489. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetTransmitKey:");
  490. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PAIRWISE_KEY: KeyTable.abyBSSID: ");
  491. for (ii = 0; ii < 6; ii++) {
  492. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x ", pTable->KeyTable[i].abyBSSID[ii]);
  493. }
  494. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  495. return (true);
  496. }
  497. else {
  498. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PairwiseKey.bKeyValid == false\n");
  499. return (false);
  500. }
  501. } // End of Type == PAIRWISE
  502. else {
  503. if (pTable->KeyTable[i].dwGTKeyIndex == 0) {
  504. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ERROR: dwGTKeyIndex == 0 !!!\n");
  505. return false;
  506. }
  507. if (pTable->KeyTable[i].GroupKey[(pTable->KeyTable[i].dwGTKeyIndex&0x000000FF)].bKeyValid == true) {
  508. *pKey = &(pTable->KeyTable[i].GroupKey[(pTable->KeyTable[i].dwGTKeyIndex&0x000000FF)]);
  509. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybGetTransmitKey:");
  510. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GROUP_KEY: KeyTable.abyBSSID\n");
  511. for (ii = 0; ii < 6; ii++) {
  512. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x ", pTable->KeyTable[i].abyBSSID[ii]);
  513. }
  514. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  515. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"dwGTKeyIndex: %lX\n", pTable->KeyTable[i].dwGTKeyIndex);
  516. return (true);
  517. }
  518. else {
  519. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GroupKey.bKeyValid == false\n");
  520. return (false);
  521. }
  522. } // End of Type = GROUP
  523. } // BSSID match
  524. }
  525. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ERROR: NO Match BSSID !!! ");
  526. for (ii = 0; ii < 6; ii++) {
  527. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *(pbyBSSID+ii));
  528. }
  529. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  530. return (false);
  531. }
  532. /*
  533. * Description: Check Pairewise Key
  534. *
  535. * Parameters:
  536. * In:
  537. * pTable - Pointer to Key table
  538. * Out:
  539. * none
  540. *
  541. * Return Value: true if found otherwise false
  542. *
  543. */
  544. bool KeybCheckPairewiseKey (
  545. PSKeyManagement pTable,
  546. PSKeyItem *pKey
  547. )
  548. {
  549. int i;
  550. *pKey = NULL;
  551. for (i=0;i<MAX_KEY_TABLE;i++) {
  552. if ((pTable->KeyTable[i].bInUse == true) &&
  553. (pTable->KeyTable[i].PairwiseKey.bKeyValid == true)) {
  554. *pKey = &(pTable->KeyTable[i].PairwiseKey);
  555. return (true);
  556. }
  557. }
  558. return (false);
  559. }
  560. /*
  561. * Description: Set Key to table
  562. *
  563. * Parameters:
  564. * In:
  565. * pTable - Pointer to Key table
  566. * dwKeyIndex - Key index (reference to NDIS DDK)
  567. * uKeyLength - Key length
  568. * KeyRSC - Key RSC
  569. * pbyKey - Pointer to key
  570. * Out:
  571. * none
  572. *
  573. * Return Value: true if success otherwise false
  574. *
  575. */
  576. bool KeybSetDefaultKey (
  577. PSKeyManagement pTable,
  578. unsigned long dwKeyIndex,
  579. unsigned long uKeyLength,
  580. PQWORD pKeyRSC,
  581. unsigned char *pbyKey,
  582. unsigned char byKeyDecMode,
  583. unsigned long dwIoBase,
  584. unsigned char byLocalID
  585. )
  586. {
  587. unsigned int ii;
  588. PSKeyItem pKey;
  589. unsigned int uKeyIdx;
  590. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetDefaultKey: %1x, %d \n", (int)dwKeyIndex, (int)uKeyLength);
  591. if ((dwKeyIndex & PAIRWISE_KEY) != 0) { // Pairwise key
  592. return (false);
  593. } else if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY) {
  594. return (false);
  595. }
  596. pTable->KeyTable[MAX_KEY_TABLE-1].bInUse = true;
  597. for(ii=0;ii<ETH_ALEN;ii++)
  598. pTable->KeyTable[MAX_KEY_TABLE-1].abyBSSID[ii] = 0xFF;
  599. // Group key
  600. pKey = &(pTable->KeyTable[MAX_KEY_TABLE-1].GroupKey[dwKeyIndex & 0x000000FF]);
  601. if ((dwKeyIndex & TRANSMIT_KEY) != 0) {
  602. // Group transmit key
  603. pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex = dwKeyIndex;
  604. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[MAX_KEY_TABLE-1].dwGTKeyIndex, MAX_KEY_TABLE-1);
  605. }
  606. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl &= 0x7F00; // clear all key control filed
  607. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= (byKeyDecMode << 4);
  608. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= (byKeyDecMode);
  609. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0x0044; // use group key for all address
  610. uKeyIdx = (dwKeyIndex & 0x000000FF);
  611. if ((uKeyLength == WLAN_WEP232_KEYLEN) &&
  612. (byKeyDecMode == KEY_CTL_WEP)) {
  613. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0x4000; // disable on-fly disable address match
  614. pTable->KeyTable[MAX_KEY_TABLE-1].bSoftWEP = true;
  615. } else {
  616. if (pTable->KeyTable[MAX_KEY_TABLE-1].bSoftWEP == false)
  617. pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl |= 0xC000; // enable on-fly disable address match
  618. }
  619. pKey->bKeyValid = true;
  620. pKey->uKeyLength = uKeyLength;
  621. pKey->dwKeyIndex = dwKeyIndex;
  622. pKey->byCipherSuite = byKeyDecMode;
  623. memcpy(pKey->abyKey, pbyKey, uKeyLength);
  624. if (byKeyDecMode == KEY_CTL_WEP) {
  625. if (uKeyLength == WLAN_WEP40_KEYLEN)
  626. pKey->abyKey[15] &= 0x7F;
  627. if (uKeyLength == WLAN_WEP104_KEYLEN)
  628. pKey->abyKey[15] |= 0x80;
  629. }
  630. MACvSetKeyEntry(dwIoBase, pTable->KeyTable[MAX_KEY_TABLE-1].wKeyCtl, MAX_KEY_TABLE-1, uKeyIdx, pTable->KeyTable[MAX_KEY_TABLE-1].abyBSSID, (unsigned long *)pKey->abyKey, byLocalID);
  631. if ((dwKeyIndex & USE_KEYRSC) == 0) {
  632. // RSC set by NIC
  633. memset(&(pKey->KeyRSC), 0, sizeof(QWORD));
  634. } else {
  635. memcpy(&(pKey->KeyRSC), pKeyRSC, sizeof(QWORD));
  636. }
  637. pKey->dwTSC47_16 = 0;
  638. pKey->wTSC15_0 = 0;
  639. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
  640. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n", pKey->bKeyValid);
  641. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n", (int)pKey->uKeyLength);
  642. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: \n");
  643. for (ii = 0; ii < pKey->uKeyLength; ii++) {
  644. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%x", pKey->abyKey[ii]);
  645. }
  646. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  647. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwTSC47_16: %lx\n", pKey->dwTSC47_16);
  648. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->wTSC15_0: %x\n", pKey->wTSC15_0);
  649. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->dwKeyIndex: %lx\n", pKey->dwKeyIndex);
  650. return (true);
  651. }
  652. /*
  653. * Description: Set Key to table
  654. *
  655. * Parameters:
  656. * In:
  657. * pTable - Pointer to Key table
  658. * dwKeyIndex - Key index (reference to NDIS DDK)
  659. * uKeyLength - Key length
  660. * KeyRSC - Key RSC
  661. * pbyKey - Pointer to key
  662. * Out:
  663. * none
  664. *
  665. * Return Value: true if success otherwise false
  666. *
  667. */
  668. bool KeybSetAllGroupKey (
  669. PSKeyManagement pTable,
  670. unsigned long dwKeyIndex,
  671. unsigned long uKeyLength,
  672. PQWORD pKeyRSC,
  673. unsigned char *pbyKey,
  674. unsigned char byKeyDecMode,
  675. unsigned long dwIoBase,
  676. unsigned char byLocalID
  677. )
  678. {
  679. int i;
  680. unsigned int ii;
  681. PSKeyItem pKey;
  682. unsigned int uKeyIdx;
  683. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Enter KeybSetAllGroupKey: %lX\n", dwKeyIndex);
  684. if ((dwKeyIndex & PAIRWISE_KEY) != 0) { // Pairwise key
  685. return (false);
  686. } else if ((dwKeyIndex & 0x000000FF) >= MAX_GROUP_KEY) {
  687. return (false);
  688. }
  689. for (i=0; i < MAX_KEY_TABLE-1; i++) {
  690. if (pTable->KeyTable[i].bInUse == true) {
  691. // found table already exist
  692. // Group key
  693. pKey = &(pTable->KeyTable[i].GroupKey[dwKeyIndex & 0x000000FF]);
  694. if ((dwKeyIndex & TRANSMIT_KEY) != 0) {
  695. // Group transmit key
  696. pTable->KeyTable[i].dwGTKeyIndex = dwKeyIndex;
  697. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Group transmit key(R)[%lX]: %d\n", pTable->KeyTable[i].dwGTKeyIndex, i);
  698. }
  699. pTable->KeyTable[i].wKeyCtl &= 0xFF0F; // clear group key control filed
  700. pTable->KeyTable[i].wKeyCtl |= (byKeyDecMode << 4);
  701. pTable->KeyTable[i].wKeyCtl |= 0x0040; // use group key for group address
  702. uKeyIdx = (dwKeyIndex & 0x000000FF);
  703. pTable->KeyTable[i].wKeyCtl |= 0x8000; // enable on-fly
  704. pKey->bKeyValid = true;
  705. pKey->uKeyLength = uKeyLength;
  706. pKey->dwKeyIndex = dwKeyIndex;
  707. pKey->byCipherSuite = byKeyDecMode;
  708. memcpy(pKey->abyKey, pbyKey, uKeyLength);
  709. if (byKeyDecMode == KEY_CTL_WEP) {
  710. if (uKeyLength == WLAN_WEP40_KEYLEN)
  711. pKey->abyKey[15] &= 0x7F;
  712. if (uKeyLength == WLAN_WEP104_KEYLEN)
  713. pKey->abyKey[15] |= 0x80;
  714. }
  715. MACvSetKeyEntry(dwIoBase, pTable->KeyTable[i].wKeyCtl, i, uKeyIdx, pTable->KeyTable[i].abyBSSID, (unsigned long *)pKey->abyKey, byLocalID);
  716. if ((dwKeyIndex & USE_KEYRSC) == 0) {
  717. // RSC set by NIC
  718. memset(&(pKey->KeyRSC), 0, sizeof(QWORD));
  719. }
  720. else {
  721. memcpy(&(pKey->KeyRSC), pKeyRSC, sizeof(QWORD));
  722. }
  723. pKey->dwTSC47_16 = 0;
  724. pKey->wTSC15_0 = 0;
  725. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KeybSetKey(R): \n");
  726. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->bKeyValid: %d\n ", pKey->bKeyValid);
  727. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->uKeyLength: %d\n ", (int)pKey->uKeyLength);
  728. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey->abyKey: ");
  729. for (ii = 0; ii < pKey->uKeyLength; ii++) {
  730. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", pKey->abyKey[ii]);
  731. }
  732. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
  733. //DBG_PRN_GRP12(("pKey->dwTSC47_16: %lX\n ", pKey->dwTSC47_16));
  734. //DBG_PRN_GRP12(("pKey->wTSC15_0: %X\n ", pKey->wTSC15_0));
  735. //DBG_PRN_GRP12(("pKey->dwKeyIndex: %lX\n ", pKey->dwKeyIndex));
  736. } // (pTable->KeyTable[i].bInUse == true)
  737. }
  738. return (true);
  739. }