wpa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 const unsigned char abyOUI00[4] = { 0x00, 0x50, 0xf2, 0x00 };
  45. static const unsigned char abyOUI01[4] = { 0x00, 0x50, 0xf2, 0x01 };
  46. static const unsigned char abyOUI02[4] = { 0x00, 0x50, 0xf2, 0x02 };
  47. static const unsigned char abyOUI03[4] = { 0x00, 0x50, 0xf2, 0x03 };
  48. static const unsigned char abyOUI04[4] = { 0x00, 0x50, 0xf2, 0x04 };
  49. static const unsigned char abyOUI05[4] = { 0x00, 0x50, 0xf2, 0x05 };
  50. /*+
  51. *
  52. * Description:
  53. * Clear RSN information in BSSList.
  54. *
  55. * Parameters:
  56. * In:
  57. * pBSSList - BSS list.
  58. * Out:
  59. * none
  60. *
  61. * Return Value: none.
  62. *
  63. -*/
  64. void
  65. WPA_ClearRSN(
  66. PKnownBSS pBSSList
  67. )
  68. {
  69. int ii;
  70. pBSSList->byGKType = WPA_TKIP;
  71. for (ii = 0; ii < 4; ii++)
  72. pBSSList->abyPKType[ii] = WPA_TKIP;
  73. pBSSList->wPKCount = 0;
  74. for (ii = 0; ii < 4; ii++)
  75. pBSSList->abyAuthType[ii] = WPA_AUTH_IEEE802_1X;
  76. pBSSList->wAuthCount = 0;
  77. pBSSList->byDefaultK_as_PK = 0;
  78. pBSSList->byReplayIdx = 0;
  79. pBSSList->sRSNCapObj.bRSNCapExist = false;
  80. pBSSList->sRSNCapObj.wRSNCap = 0;
  81. pBSSList->bWPAValid = false;
  82. }
  83. /*+
  84. *
  85. * Description:
  86. * Parse RSN IE.
  87. *
  88. * Parameters:
  89. * In:
  90. * pBSSList - BSS list.
  91. * pRSN - Pointer to the RSN IE.
  92. * Out:
  93. * none
  94. *
  95. * Return Value: none.
  96. *
  97. -*/
  98. void
  99. WPA_ParseRSN(
  100. PKnownBSS pBSSList,
  101. PWLAN_IE_RSN_EXT pRSN
  102. )
  103. {
  104. PWLAN_IE_RSN_AUTH pIE_RSN_Auth = NULL;
  105. int i, j, m, n = 0;
  106. unsigned char *pbyCaps;
  107. WPA_ClearRSN(pBSSList);
  108. pr_debug("WPA_ParseRSN: [%d]\n", pRSN->len);
  109. // information element header makes sense
  110. if ((pRSN->len >= 6) // oui1(4)+ver(2)
  111. && (pRSN->byElementID == WLAN_EID_RSN_WPA) && !memcmp(pRSN->abyOUI, abyOUI01, 4)
  112. && (pRSN->wVersion == 1)) {
  113. pr_debug("Legal RSN\n");
  114. // update each variable if pRSN is long enough to contain the variable
  115. if (pRSN->len >= 10) {
  116. //OUI1(4)+ver(2)+GKSuite(4)
  117. if (!memcmp(pRSN->abyMulticast, abyOUI01, 4))
  118. pBSSList->byGKType = WPA_WEP40;
  119. else if (!memcmp(pRSN->abyMulticast, abyOUI02, 4))
  120. pBSSList->byGKType = WPA_TKIP;
  121. else if (!memcmp(pRSN->abyMulticast, abyOUI03, 4))
  122. pBSSList->byGKType = WPA_AESWRAP;
  123. else if (!memcmp(pRSN->abyMulticast, abyOUI04, 4))
  124. pBSSList->byGKType = WPA_AESCCMP;
  125. else if (!memcmp(pRSN->abyMulticast, abyOUI05, 4))
  126. pBSSList->byGKType = WPA_WEP104;
  127. else
  128. // any vendor checks here
  129. pBSSList->byGKType = WPA_NONE;
  130. pr_debug("byGKType: %x\n", pBSSList->byGKType);
  131. }
  132. if (pRSN->len >= 12) {
  133. //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)
  134. j = 0;
  135. pr_debug("wPKCount: %d, sizeof(pBSSList->abyPKType): %zu\n",
  136. pRSN->wPKCount, sizeof(pBSSList->abyPKType));
  137. for (i = 0; (i < pRSN->wPKCount) && (j < ARRAY_SIZE(pBSSList->abyPKType)); 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. } else
  151. break;
  152. }
  153. pBSSList->wPKCount = (unsigned short)j;
  154. pr_debug("wPKCount: %d\n", pBSSList->wPKCount);
  155. }
  156. m = pRSN->wPKCount;
  157. pr_debug("m: %d\n", m);
  158. pr_debug("14+m*4: %d\n", 14+m*4);
  159. if (pRSN->len >= 14+m*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)
  160. // overlay IE_RSN_Auth structure into correct place
  161. pIE_RSN_Auth = (PWLAN_IE_RSN_AUTH) pRSN->PKSList[m].abyOUI;
  162. j = 0;
  163. pr_debug("wAuthCount: %d, sizeof(pBSSList->abyAuthType): %zu\n",
  164. pIE_RSN_Auth->wAuthCount,
  165. sizeof(pBSSList->abyAuthType));
  166. for (i = 0; (i < pIE_RSN_Auth->wAuthCount) && (j < ARRAY_SIZE(pBSSList->abyAuthType)); i++) {
  167. if (pRSN->len >= 14+4+(m+i)*4) { //oui1(4)+ver(2)+GKS(4)+PKSCnt(2)+PKS(4*m)+AKC(2)+AKS(4*i)
  168. if (!memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI01, 4))
  169. pBSSList->abyAuthType[j++] = WPA_AUTH_IEEE802_1X;
  170. else if (!memcmp(pIE_RSN_Auth->AuthKSList[i].abyOUI, abyOUI02, 4))
  171. pBSSList->abyAuthType[j++] = WPA_AUTH_PSK;
  172. else
  173. // any vendor checks here
  174. ;
  175. } else
  176. break;
  177. }
  178. if (j > 0)
  179. pBSSList->wAuthCount = (unsigned short)j;
  180. pr_debug("wAuthCount: %d\n", pBSSList->wAuthCount);
  181. }
  182. if (pIE_RSN_Auth != NULL) {
  183. n = pIE_RSN_Auth->wAuthCount;
  184. pr_debug("n: %d\n", n);
  185. pr_debug("14+4+(m+n)*4: %d\n", 14+4+(m+n)*4);
  186. 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)
  187. pbyCaps = (unsigned char *)pIE_RSN_Auth->AuthKSList[n].abyOUI;
  188. pBSSList->byDefaultK_as_PK = (*pbyCaps) & WPA_GROUPFLAG;
  189. pBSSList->byReplayIdx = 2 << ((*pbyCaps >> WPA_REPLAYBITSSHIFT) & WPA_REPLAYBITS);
  190. pBSSList->sRSNCapObj.bRSNCapExist = true;
  191. pBSSList->sRSNCapObj.wRSNCap = *(unsigned short *)pbyCaps;
  192. }
  193. }
  194. pBSSList->bWPAValid = true;
  195. }
  196. }
  197. /*+
  198. *
  199. * Description:
  200. * Search RSN information in BSSList.
  201. *
  202. * Parameters:
  203. * In:
  204. * byCmd - Search type
  205. * byEncrypt- Encrypt Type
  206. * pBSSList - BSS list
  207. * Out:
  208. * none
  209. *
  210. * Return Value: none.
  211. *
  212. -*/
  213. bool
  214. WPA_SearchRSN(
  215. unsigned char byCmd,
  216. unsigned char byEncrypt,
  217. PKnownBSS pBSSList
  218. )
  219. {
  220. int ii;
  221. unsigned char byPKType = WPA_NONE;
  222. if (!pBSSList->bWPAValid)
  223. return false;
  224. switch (byCmd) {
  225. case 0:
  226. if (byEncrypt != pBSSList->byGKType)
  227. return false;
  228. if (pBSSList->wPKCount > 0) {
  229. for (ii = 0; ii < pBSSList->wPKCount; ii++) {
  230. if (pBSSList->abyPKType[ii] == WPA_AESCCMP)
  231. byPKType = WPA_AESCCMP;
  232. else if ((pBSSList->abyPKType[ii] == WPA_TKIP) && (byPKType != WPA_AESCCMP))
  233. byPKType = WPA_TKIP;
  234. else if ((pBSSList->abyPKType[ii] == WPA_WEP40) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  235. byPKType = WPA_WEP40;
  236. else if ((pBSSList->abyPKType[ii] == WPA_WEP104) && (byPKType != WPA_AESCCMP) && (byPKType != WPA_TKIP))
  237. byPKType = WPA_WEP104;
  238. }
  239. if (byEncrypt != byPKType)
  240. return false;
  241. }
  242. return true;
  243. default:
  244. break;
  245. }
  246. return false;
  247. }
  248. /*+
  249. *
  250. * Description:
  251. * Check if RSN IE makes sense.
  252. *
  253. * Parameters:
  254. * In:
  255. * pRSN - Pointer to the RSN IE.
  256. * Out:
  257. * none
  258. *
  259. * Return Value: none.
  260. *
  261. -*/
  262. bool
  263. WPAb_Is_RSN(
  264. PWLAN_IE_RSN_EXT pRSN
  265. )
  266. {
  267. if (pRSN == NULL)
  268. return false;
  269. if ((pRSN->len >= 6) && // oui1(4)+ver(2)
  270. (pRSN->byElementID == WLAN_EID_RSN_WPA) && !memcmp(pRSN->abyOUI, abyOUI01, 4) &&
  271. (pRSN->wVersion == 1)) {
  272. return true;
  273. } else
  274. return false;
  275. }