key.c 3.7 KB

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