key.c 4.0 KB

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