key.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  4. * All rights reserved.
  5. *
  6. * File: key.c
  7. *
  8. * Purpose: Implement functions for 802.11i Key management
  9. *
  10. * Author: Jerry Chen
  11. *
  12. * Date: May 29, 2003
  13. *
  14. * Functions:
  15. *
  16. * Revision History:
  17. *
  18. */
  19. #include "mac.h"
  20. #include "key.h"
  21. #include "usbpipe.h"
  22. int vnt_key_init_table(struct vnt_private *priv)
  23. {
  24. u8 i;
  25. u8 data[MAX_KEY_TABLE];
  26. for (i = 0; i < MAX_KEY_TABLE; i++)
  27. data[i] = i;
  28. return vnt_control_out(priv, MESSAGE_TYPE_CLRKEYENTRY,
  29. 0, 0, ARRAY_SIZE(data), data);
  30. }
  31. static int vnt_set_keymode(struct ieee80211_hw *hw, u8 *mac_addr,
  32. struct ieee80211_key_conf *key, u32 key_type,
  33. u32 mode, bool onfly_latch)
  34. {
  35. struct vnt_private *priv = hw->priv;
  36. u8 broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  37. u16 key_mode = 0;
  38. u32 entry = 0;
  39. u8 *bssid;
  40. u8 key_inx = key->keyidx;
  41. u8 i;
  42. if (mac_addr)
  43. bssid = mac_addr;
  44. else
  45. bssid = &broadcast[0];
  46. if (key_type != VNT_KEY_DEFAULTKEY) {
  47. for (i = 0; i < (MAX_KEY_TABLE - 1); i++) {
  48. if (!test_bit(i, &priv->key_entry_inuse)) {
  49. set_bit(i, &priv->key_entry_inuse);
  50. key->hw_key_idx = i;
  51. entry = key->hw_key_idx;
  52. break;
  53. }
  54. }
  55. }
  56. switch (key_type) {
  57. /* fallthrough */
  58. case VNT_KEY_DEFAULTKEY:
  59. /* default key last entry */
  60. entry = MAX_KEY_TABLE - 1;
  61. key->hw_key_idx = entry;
  62. case VNT_KEY_ALLGROUP:
  63. key_mode |= VNT_KEY_ALLGROUP;
  64. if (onfly_latch)
  65. key_mode |= VNT_KEY_ONFLY_ALL;
  66. case VNT_KEY_GROUP_ADDRESS:
  67. key_mode |= mode;
  68. case VNT_KEY_GROUP:
  69. key_mode |= (mode << 4);
  70. key_mode |= VNT_KEY_GROUP;
  71. break;
  72. case VNT_KEY_PAIRWISE:
  73. key_mode |= mode;
  74. key_inx = 4;
  75. /* Don't save entry for pairwise key for station mode */
  76. if (priv->op_mode == NL80211_IFTYPE_STATION)
  77. clear_bit(entry, &priv->key_entry_inuse);
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. if (onfly_latch)
  83. key_mode |= VNT_KEY_ONFLY;
  84. if (mode == KEY_CTL_WEP) {
  85. if (key->keylen == WLAN_KEY_LEN_WEP40)
  86. key->key[15] &= 0x7f;
  87. if (key->keylen == WLAN_KEY_LEN_WEP104)
  88. key->key[15] |= 0x80;
  89. }
  90. vnt_mac_set_keyentry(priv, key_mode, entry, key_inx, bssid, key->key);
  91. return 0;
  92. }
  93. int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
  94. struct ieee80211_vif *vif, struct ieee80211_key_conf *key)
  95. {
  96. struct ieee80211_bss_conf *conf = &vif->bss_conf;
  97. struct vnt_private *priv = hw->priv;
  98. u8 *mac_addr = NULL;
  99. u8 key_dec_mode = 0;
  100. int ret = 0, 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. vnt_mac_disable_keyentry(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. vnt_mac_disable_keyentry(priv, u);
  112. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
  113. 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. if (priv->local_id <= MAC_REVISION_A1)
  123. return -EINVAL;
  124. key_dec_mode = KEY_CTL_CCMP;
  125. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  126. }
  127. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  128. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE,
  129. key_dec_mode, true);
  130. } else {
  131. vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
  132. key_dec_mode, true);
  133. vnt_set_keymode(hw, (u8 *)conf->bssid, key,
  134. VNT_KEY_GROUP_ADDRESS, key_dec_mode, true);
  135. }
  136. return 0;
  137. }