key.c 3.1 KB

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