wpa.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 unsigned char abyOUI00[4] = { 0x00, 0x50, 0xf2, 0x00 };
  46. const unsigned char abyOUI01[4] = { 0x00, 0x50, 0xf2, 0x01 };
  47. const unsigned char abyOUI02[4] = { 0x00, 0x50, 0xf2, 0x02 };
  48. const unsigned char abyOUI03[4] = { 0x00, 0x50, 0xf2, 0x03 };
  49. const unsigned char abyOUI04[4] = { 0x00, 0x50, 0xf2, 0x04 };
  50. const unsigned char 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. unsigned char *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) && (j < sizeof(pBSSList->abyPKType)/sizeof(unsigned char)); i++) {
  138. if(pRSN->len >= 12+i*4+4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*i)
  139. if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI00, 4))
  140. pBSSList->abyPKType[j++] = WPA_NONE;
  141. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI02, 4))
  142. pBSSList->abyPKType[j++] = WPA_TKIP;
  143. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI03, 4))
  144. pBSSList->abyPKType[j++] = WPA_AESWRAP;
  145. else if ( !memcmp(pRSN->PKSList[i].abyOUI, abyOUI04, 4))
  146. pBSSList->abyPKType[j++] = WPA_AESCCMP;
  147. else
  148. // any vendor checks here
  149. ;
  150. }
  151. else
  152. break;
  153. //DBG_PRN_GRP14(("abyPKType[%d]: %X\n", j-1, pBSSList->abyPKType[j-1]));
  154. } //for
  155. pBSSList->wPKCount = (unsigned short)j;
  156. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wPKCount: %d\n", pBSSList->wPKCount);
  157. }
  158. m = pRSN->wPKCount;
  159. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"m: %d\n", m);
  160. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"14+m*4: %d\n", 14+m*4);
  161. if (pRSN->len >= 14+m*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)
  162. // overlay IE_RSN_Auth structure into correct place
  163. pIE_RSN_Auth = (PWLAN_IE_RSN_AUTH) pRSN->PKSList[m].abyOUI;
  164. j = 0;
  165. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wAuthCount: %d, sizeof(pBSSList->abyAuthType): %zu\n",
  166. pIE_RSN_Auth->wAuthCount, sizeof(pBSSList->abyAuthType));
  167. for(i = 0; (i < pIE_RSN_Auth->wAuthCount) && (j < sizeof(pBSSList->abyAuthType)/sizeof(unsigned char)); i++) {
  168. if(pRSN->len >= 14+4+(m+i)*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)+AKS(4*i)
  169. if ( !memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI01, 4))
  170. pBSSList->abyAuthType[j++] = WPA_AUTH_IEEE802_1X;
  171. else if ( !memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI02, 4))
  172. pBSSList->abyAuthType[j++] = WPA_AUTH_PSK;
  173. else
  174. // any vendor checks here
  175. ;
  176. }
  177. else
  178. break;
  179. //DBG_PRN_GRP14(("abyAuthType[%d]: %X\n", j-1, pBSSList->abyAuthType[j-1]));
  180. }
  181. if(j > 0)
  182. pBSSList->wAuthCount = (unsigned short)j;
  183. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"wAuthCount: %d\n", pBSSList->wAuthCount);
  184. }
  185. if (pIE_RSN_Auth != NULL) {
  186. n = pIE_RSN_Auth->wAuthCount;
  187. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"n: %d\n", n);
  188. DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"14+4+(m+n)*4: %d\n", 14+4+(m+n)*4);
  189. 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)
  190. pbyCaps = (unsigned char *)pIE_RSN_Auth->AuthKSList[n].abyOUI;
  191. pBSSList->byDefaultK_as_PK = (*pbyCaps) & WPA_GROUPFLAG;
  192. pBSSList->byReplayIdx = 2 << ((*pbyCaps >> WPA_REPLAYBITSSHIFT) & WPA_REPLAYBITS);
  193. pBSSList->sRSNCapObj.bRSNCapExist = true;
  194. pBSSList->sRSNCapObj.wRSNCap = *(unsigned short *)pbyCaps;
  195. //DBG_PRN_GRP14(("pbyCaps: %X\n", *pbyCaps));
  196. //DBG_PRN_GRP14(("byDefaultK_as_PK: %X\n", pBSSList->byDefaultK_as_PK));
  197. //DBG_PRN_GRP14(("byReplayIdx: %X\n", pBSSList->byReplayIdx));
  198. }
  199. }
  200. pBSSList->bWPAValid = true;
  201. }
  202. }
  203. /*+
  204. *
  205. * Description:
  206. * Search RSN information in BSSList.
  207. *
  208. * Parameters:
  209. * In:
  210. * byCmd - Search type
  211. * byEncrypt- Encrcypt Type
  212. * pBSSList - BSS list
  213. * Out:
  214. * none
  215. *
  216. * Return Value: none.
  217. *
  218. -*/
  219. bool
  220. WPA_SearchRSN (
  221. unsigned char byCmd,
  222. unsigned char byEncrypt,
  223. PKnownBSS pBSSList
  224. )
  225. {
  226. int ii;
  227. unsigned char byPKType = WPA_NONE;
  228. if (pBSSList->bWPAValid == false)
  229. return false;
  230. switch(byCmd) {
  231. case 0:
  232. if (byEncrypt != pBSSList->byGKType)
  233. return false;
  234. if (pBSSList->wPKCount > 0) {
  235. for (ii = 0; ii < pBSSList->wPKCount; ii ++) {
  236. if (pBSSList->abyPKType[ii] == WPA_AESCCMP)
  237. byPKType = WPA_AESCCMP;
  238. else if ((pBSSList->abyPKType[ii] == WPA_TKIP) && (byPKType != WPA_AESCCMP))
  239. byPKType = WPA_TKIP;
  240. else if ((pBSSList->abyPKType[ii] == WPA_WEP40) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  241. byPKType = WPA_WEP40;
  242. else if ((pBSSList->abyPKType[ii] == WPA_WEP104) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  243. byPKType = WPA_WEP104;
  244. }
  245. if (byEncrypt != byPKType)
  246. return false;
  247. }
  248. return true;
  249. // if (pBSSList->wAuthCount > 0)
  250. // for (ii=0; ii < pBSSList->wAuthCount; ii ++)
  251. // if (byAuth == pBSSList->abyAuthType[ii])
  252. // break;
  253. break;
  254. default:
  255. break;
  256. }
  257. return false;
  258. }
  259. /*+
  260. *
  261. * Description:
  262. * Check if RSN IE makes sense.
  263. *
  264. * Parameters:
  265. * In:
  266. * pRSN - Pointer to the RSN IE.
  267. * Out:
  268. * none
  269. *
  270. * Return Value: none.
  271. *
  272. -*/
  273. bool
  274. WPAb_Is_RSN (
  275. PWLAN_IE_RSN_EXT pRSN
  276. )
  277. {
  278. if (pRSN == NULL)
  279. return false;
  280. if ((pRSN->len >= 6) && // oui1(4)+ver(2)
  281. (pRSN->byElementID == WLAN_EID_RSN_WPA) && !memcmp(pRSN->abyOUI, abyOUI01, 4) &&
  282. (pRSN->wVersion == 1)) {
  283. return true;
  284. }
  285. else
  286. return false;
  287. }