wpa.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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: wpa.c
  21. *
  22. * Purpose: Handles the Basic Service Set & Node Database functions
  23. *
  24. * Functions:
  25. * WPA_ParseRSN - Parse RSN IE.
  26. *
  27. * Revision History:
  28. *
  29. * Author: Kyle Hsu
  30. *
  31. * Date: July 14, 2003
  32. *
  33. */
  34. #include "ttype.h"
  35. #include "tmacro.h"
  36. #include "tether.h"
  37. #include "device.h"
  38. #include "80211hdr.h"
  39. #include "bssdb.h"
  40. #include "wmgr.h"
  41. #include "wpa.h"
  42. #include "80211mgr.h"
  43. /*--------------------- Static Variables --------------------------*/
  44. static int msglevel =MSG_LEVEL_INFO;
  45. const BYTE abyOUI00[4] = { 0x00, 0x50, 0xf2, 0x00 };
  46. const BYTE abyOUI01[4] = { 0x00, 0x50, 0xf2, 0x01 };
  47. const BYTE abyOUI02[4] = { 0x00, 0x50, 0xf2, 0x02 };
  48. const BYTE abyOUI03[4] = { 0x00, 0x50, 0xf2, 0x03 };
  49. const BYTE abyOUI04[4] = { 0x00, 0x50, 0xf2, 0x04 };
  50. const BYTE abyOUI05[4] = { 0x00, 0x50, 0xf2, 0x05 };
  51. /*+
  52. *
  53. * Description:
  54. * Clear RSN information in BSSList.
  55. *
  56. * Parameters:
  57. * In:
  58. * pBSSList - BSS list.
  59. * Out:
  60. * none
  61. *
  62. * Return Value: none.
  63. *
  64. -*/
  65. void
  66. WPA_ClearRSN(
  67. PKnownBSS pBSSList
  68. )
  69. {
  70. int ii;
  71. pBSSList->byGKType = WPA_TKIP;
  72. for (ii=0; ii < 4; ii ++)
  73. pBSSList->abyPKType[ii] = WPA_TKIP;
  74. pBSSList->wPKCount = 0;
  75. for (ii=0; ii < 4; ii ++)
  76. pBSSList->abyAuthType[ii] = WPA_AUTH_IEEE802_1X;
  77. pBSSList->wAuthCount = 0;
  78. pBSSList->byDefaultK_as_PK = 0;
  79. pBSSList->byReplayIdx = 0;
  80. pBSSList->sRSNCapObj.bRSNCapExist = FALSE;
  81. pBSSList->sRSNCapObj.wRSNCap = 0;
  82. pBSSList->bWPAValid = FALSE;
  83. }
  84. /*+
  85. *
  86. * Description:
  87. * Parse RSN IE.
  88. *
  89. * Parameters:
  90. * In:
  91. * pBSSList - BSS list.
  92. * pRSN - Pointer to the RSN IE.
  93. * Out:
  94. * none
  95. *
  96. * Return Value: none.
  97. *
  98. -*/
  99. void
  100. WPA_ParseRSN(
  101. PKnownBSS pBSSList,
  102. PWLAN_IE_RSN_EXT pRSN
  103. )
  104. {
  105. PWLAN_IE_RSN_AUTH pIE_RSN_Auth = NULL;
  106. int i, j, m, n = 0;
  107. PBYTE pbyCaps;
  108. WPA_ClearRSN(pBSSList);
  109. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"WPA_ParseRSN: [%d]\n", pRSN->len);
  110. // information element header makes sense
  111. if ((pRSN->len >= 6) // oui1(4)+ver(2)
  112. && (pRSN->byElementID == WLAN_EID_RSN_WPA) && !memcmp(pRSN->abyOUI, abyOUI01, 4)
  113. && (pRSN->wVersion == 1)) {
  114. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Legal RSN\n");
  115. // update each variable if pRSN is long enough to contain the variable
  116. if (pRSN->len >= 10) //oui1(4)+ver(2)+GKSuite(4)
  117. {
  118. if ( !memcmp(pRSN->abyMulticast, abyOUI01, 4))
  119. pBSSList->byGKType = WPA_WEP40;
  120. else if ( !memcmp(pRSN->abyMulticast, abyOUI02, 4))
  121. pBSSList->byGKType = WPA_TKIP;
  122. else if ( !memcmp(pRSN->abyMulticast, abyOUI03, 4))
  123. pBSSList->byGKType = WPA_AESWRAP;
  124. else if ( !memcmp(pRSN->abyMulticast, abyOUI04, 4))
  125. pBSSList->byGKType = WPA_AESCCMP;
  126. else if ( !memcmp(pRSN->abyMulticast, abyOUI05, 4))
  127. pBSSList->byGKType = WPA_WEP104;
  128. else
  129. // any vendor checks here
  130. pBSSList->byGKType = WPA_NONE;
  131. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"byGKType: %x\n", pBSSList->byGKType);
  132. }
  133. if (pRSN->len >= 12) //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)
  134. {
  135. j = 0;
  136. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wPKCount: %d, sizeof(pBSSList->abyPKType): %zu\n", pRSN->wPKCount, sizeof(pBSSList->abyPKType));
  137. for (i = 0; (i < pRSN->wPKCount) &&
  138. (j < sizeof(pBSSList->abyPKType)/sizeof(BYTE)); i++) {
  139. if(pRSN->len >= 12+i*4+4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*i)
  140. if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI00, 4))
  141. pBSSList->abyPKType[j++] = WPA_NONE;
  142. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI02, 4))
  143. pBSSList->abyPKType[j++] = WPA_TKIP;
  144. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI03, 4))
  145. pBSSList->abyPKType[j++] = WPA_AESWRAP;
  146. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI04, 4))
  147. pBSSList->abyPKType[j++] = WPA_AESCCMP;
  148. else
  149. // any vendor checks here
  150. ;
  151. }
  152. else
  153. break;
  154. //DBG_PRN_GRP14(("abyPKType[%d]: %X\n", j-1, pBSSList->abyPKType[j-1]));
  155. } //for
  156. pBSSList->wPKCount = (WORD)j;
  157. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wPKCount: %d\n", pBSSList->wPKCount);
  158. }
  159. m = pRSN->wPKCount;
  160. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"m: %d\n", m);
  161. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"14+m*4: %d\n", 14+m*4);
  162. if (pRSN->len >= 14+m*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)
  163. // overlay IE_RSN_Auth structure into correct place
  164. pIE_RSN_Auth = (PWLAN_IE_RSN_AUTH) pRSN->PKSList[m].abyOUI;
  165. j = 0;
  166. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wAuthCount: %d, sizeof(pBSSList->abyAuthType): %zu\n",
  167. pIE_RSN_Auth->wAuthCount, sizeof(pBSSList->abyAuthType));
  168. for (i = 0; (i < pIE_RSN_Auth->wAuthCount) &&
  169. (j < sizeof(pBSSList->abyAuthType)/sizeof(BYTE)); i++) {
  170. if(pRSN->len >= 14+4+(m+i)*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)+AKS(4*i)
  171. if ( !memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI01, 4))
  172. pBSSList->abyAuthType[j++] = WPA_AUTH_IEEE802_1X;
  173. else if ( !memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI02, 4))
  174. pBSSList->abyAuthType[j++] = WPA_AUTH_PSK;
  175. else
  176. // any vendor checks here
  177. ;
  178. }
  179. else
  180. break;
  181. //DBG_PRN_GRP14(("abyAuthType[%d]: %X\n", j-1, pBSSList->abyAuthType[j-1]));
  182. }
  183. if(j > 0)
  184. pBSSList->wAuthCount = (WORD)j;
  185. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wAuthCount: %d\n", pBSSList->wAuthCount);
  186. }
  187. if (pIE_RSN_Auth != NULL) {
  188. n = pIE_RSN_Auth->wAuthCount;
  189. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"n: %d\n", n);
  190. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"14+4+(m+n)*4: %d\n", 14+4+(m+n)*4);
  191. if(pRSN->len+2 >= 14+4+(m+n)*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)+AKS(4*n)+Cap(2)
  192. pbyCaps = (PBYTE)pIE_RSN_Auth->AuthKSList[n].abyOUI;
  193. pBSSList->byDefaultK_as_PK = (*pbyCaps) & WPA_GROUPFLAG;
  194. pBSSList->byReplayIdx = 2 << ((*pbyCaps >> WPA_REPLAYBITSSHIFT) & WPA_REPLAYBITS);
  195. pBSSList->sRSNCapObj.bRSNCapExist = TRUE;
  196. pBSSList->sRSNCapObj.wRSNCap = *(PWORD)pbyCaps;
  197. //DBG_PRN_GRP14(("pbyCaps: %X\n", *pbyCaps));
  198. //DBG_PRN_GRP14(("byDefaultK_as_PK: %X\n", pBSSList->byDefaultK_as_PK));
  199. //DBG_PRN_GRP14(("byReplayIdx: %X\n", pBSSList->byReplayIdx));
  200. }
  201. }
  202. pBSSList->bWPAValid = TRUE;
  203. }
  204. }
  205. /*+
  206. *
  207. * Description:
  208. * Search RSN information in BSSList.
  209. *
  210. * Parameters:
  211. * In:
  212. * byCmd - Search type
  213. * byEncrypt- Encrcypt Type
  214. * pBSSList - BSS list
  215. * Out:
  216. * none
  217. *
  218. * Return Value: none.
  219. *
  220. -*/
  221. BOOL
  222. WPA_SearchRSN(
  223. BYTE byCmd,
  224. BYTE byEncrypt,
  225. PKnownBSS pBSSList
  226. )
  227. {
  228. int ii;
  229. BYTE byPKType = WPA_NONE;
  230. if (pBSSList->bWPAValid == FALSE)
  231. return FALSE;
  232. switch(byCmd) {
  233. case 0:
  234. if (byEncrypt != pBSSList->byGKType)
  235. return FALSE;
  236. if (pBSSList->wPKCount > 0) {
  237. for (ii = 0; ii < pBSSList->wPKCount; ii ++) {
  238. if (pBSSList->abyPKType[ii] == WPA_AESCCMP)
  239. byPKType = WPA_AESCCMP;
  240. else if ((pBSSList->abyPKType[ii] == WPA_TKIP) && (byPKType != WPA_AESCCMP))
  241. byPKType = WPA_TKIP;
  242. else if ((pBSSList->abyPKType[ii] == WPA_WEP40) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  243. byPKType = WPA_WEP40;
  244. else if ((pBSSList->abyPKType[ii] == WPA_WEP104) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  245. byPKType = WPA_WEP104;
  246. }
  247. if (byEncrypt != byPKType)
  248. return FALSE;
  249. }
  250. return TRUE;
  251. // if (pBSSList->wAuthCount > 0)
  252. // for (ii=0; ii < pBSSList->wAuthCount; ii ++)
  253. // if (byAuth == pBSSList->abyAuthType[ii])
  254. // break;
  255. break;
  256. default:
  257. break;
  258. }
  259. return FALSE;
  260. }
  261. /*+
  262. *
  263. * Description:
  264. * Check if RSN IE makes sense.
  265. *
  266. * Parameters:
  267. * In:
  268. * pRSN - Pointer to the RSN IE.
  269. * Out:
  270. * none
  271. *
  272. * Return Value: none.
  273. *
  274. -*/
  275. BOOL
  276. WPAb_Is_RSN(
  277. PWLAN_IE_RSN_EXT pRSN
  278. )
  279. {
  280. if (pRSN == NULL)
  281. return FALSE;
  282. if ((pRSN->len >= 6) && // oui1(4)+ver(2)
  283. (pRSN->byElementID == WLAN_EID_RSN_WPA) && !memcmp(pRSN->abyOUI, abyOUI01, 4) &&
  284. (pRSN->wVersion == 1)) {
  285. return TRUE;
  286. }
  287. else
  288. return FALSE;
  289. }