key.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. *
  30. * Revision History:
  31. *
  32. */
  33. #include "mac.h"
  34. #include "key.h"
  35. #include "usbpipe.h"
  36. int vnt_key_init_table(struct vnt_private *priv)
  37. {
  38. u8 i;
  39. u8 data[MAX_KEY_TABLE];
  40. for (i = 0; i < MAX_KEY_TABLE; i++)
  41. data[i] = i;
  42. return vnt_control_out(priv, MESSAGE_TYPE_CLRKEYENTRY,
  43. 0, 0, ARRAY_SIZE(data), data);
  44. }
  45. static int vnt_set_keymode(struct ieee80211_hw *hw, u8 *mac_addr,
  46. struct ieee80211_key_conf *key, u32 key_type, u32 mode,
  47. bool onfly_latch)
  48. {
  49. struct vnt_private *priv = hw->priv;
  50. u8 broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  51. u16 key_mode = 0;
  52. u32 entry = 0;
  53. u8 *bssid;
  54. u8 key_inx = key->keyidx;
  55. u8 i;
  56. if (mac_addr)
  57. bssid = mac_addr;
  58. else
  59. bssid = &broadcast[0];
  60. if (key_type != VNT_KEY_DEFAULTKEY) {
  61. for (i = 0; i < (MAX_KEY_TABLE - 1); i++) {
  62. if (!test_bit(i, &priv->key_entry_inuse)) {
  63. set_bit(i, &priv->key_entry_inuse);
  64. key->hw_key_idx = i;
  65. entry = key->hw_key_idx;
  66. break;
  67. }
  68. }
  69. }
  70. switch (key_type) {
  71. /* fallthrough */
  72. case VNT_KEY_DEFAULTKEY:
  73. /* default key last entry */
  74. entry = MAX_KEY_TABLE - 1;
  75. key->hw_key_idx = entry;
  76. case VNT_KEY_ALLGROUP:
  77. key_mode |= VNT_KEY_ALLGROUP;
  78. if (onfly_latch)
  79. key_mode |= VNT_KEY_ONFLY_ALL;
  80. case VNT_KEY_GROUP_ADDRESS:
  81. key_mode |= mode;
  82. case VNT_KEY_GROUP:
  83. key_mode |= (mode << 4);
  84. key_mode |= VNT_KEY_GROUP;
  85. break;
  86. case VNT_KEY_PAIRWISE:
  87. key_mode |= mode;
  88. key_inx = 4;
  89. /* Don't save entry for pairwise key for station mode */
  90. if (priv->op_mode == NL80211_IFTYPE_STATION)
  91. clear_bit(entry, &priv->key_entry_inuse);
  92. break;
  93. default:
  94. return -EINVAL;
  95. }
  96. if (onfly_latch)
  97. key_mode |= VNT_KEY_ONFLY;
  98. if (mode == KEY_CTL_WEP) {
  99. if (key->keylen == WLAN_KEY_LEN_WEP40)
  100. key->key[15] &= 0x7f;
  101. if (key->keylen == WLAN_KEY_LEN_WEP104)
  102. key->key[15] |= 0x80;
  103. }
  104. vnt_mac_set_keyentry(priv, key_mode, entry, key_inx, bssid, key->key);
  105. return 0;
  106. }
  107. int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
  108. struct ieee80211_vif *vif, struct ieee80211_key_conf *key)
  109. {
  110. struct ieee80211_bss_conf *conf = &vif->bss_conf;
  111. struct vnt_private *priv = hw->priv;
  112. u8 *mac_addr = NULL;
  113. u8 key_dec_mode = 0;
  114. int ret = 0, u;
  115. if (sta)
  116. mac_addr = &sta->addr[0];
  117. switch (key->cipher) {
  118. case 0:
  119. for (u = 0 ; u < MAX_KEY_TABLE; u++)
  120. vnt_mac_disable_keyentry(priv, u);
  121. return ret;
  122. case WLAN_CIPHER_SUITE_WEP40:
  123. case WLAN_CIPHER_SUITE_WEP104:
  124. for (u = 0; u < MAX_KEY_TABLE; u++)
  125. vnt_mac_disable_keyentry(priv, u);
  126. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
  127. KEY_CTL_WEP, true);
  128. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  129. return ret;
  130. case WLAN_CIPHER_SUITE_TKIP:
  131. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  132. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  133. key_dec_mode = KEY_CTL_TKIP;
  134. break;
  135. case WLAN_CIPHER_SUITE_CCMP:
  136. if (priv->local_id <= MAC_REVISION_A1)
  137. return -EINVAL;
  138. key_dec_mode = KEY_CTL_CCMP;
  139. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  140. }
  141. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  142. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE,
  143. key_dec_mode, true);
  144. } else {
  145. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
  146. key_dec_mode, true);
  147. vnt_set_keymode(hw, (u8 *)conf->bssid, key,
  148. VNT_KEY_GROUP_ADDRESS, key_dec_mode, true);
  149. }
  150. return 0;
  151. }