key.c 3.9 KB

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