rt2x00crypto.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 crypto specific routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
  26. {
  27. switch (key->alg) {
  28. case ALG_WEP:
  29. if (key->keylen == WLAN_KEY_LEN_WEP40)
  30. return CIPHER_WEP64;
  31. else
  32. return CIPHER_WEP128;
  33. case ALG_TKIP:
  34. return CIPHER_TKIP;
  35. case ALG_CCMP:
  36. return CIPHER_AES;
  37. default:
  38. return CIPHER_NONE;
  39. }
  40. }
  41. void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
  42. struct txentry_desc *txdesc)
  43. {
  44. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  45. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  46. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  47. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
  48. !hw_key || entry->skb->do_not_encrypt)
  49. return;
  50. __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
  51. txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
  52. if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  53. __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
  54. txdesc->key_idx = hw_key->hw_key_idx;
  55. txdesc->iv_offset = txdesc->header_length;
  56. txdesc->iv_len = hw_key->iv_len;
  57. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  58. __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
  59. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
  60. __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
  61. }
  62. unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
  63. struct sk_buff *skb)
  64. {
  65. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  66. struct ieee80211_key_conf *key = tx_info->control.hw_key;
  67. unsigned int overhead = 0;
  68. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
  69. !key || skb->do_not_encrypt)
  70. return overhead;
  71. /*
  72. * Extend frame length to include IV/EIV/ICV/MMIC,
  73. * note that these lengths should only be added when
  74. * mac80211 does not generate it.
  75. */
  76. overhead += key->icv_len;
  77. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  78. overhead += key->iv_len;
  79. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  80. if (key->alg == ALG_TKIP)
  81. overhead += 8;
  82. }
  83. return overhead;
  84. }
  85. void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  86. {
  87. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  88. if (unlikely(!txdesc->iv_len))
  89. return;
  90. /* Copy IV/EIV data */
  91. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  92. }
  93. void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  94. {
  95. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  96. if (unlikely(!txdesc->iv_len))
  97. return;
  98. /* Copy IV/EIV data */
  99. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  100. /* Move ieee80211 header */
  101. memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
  102. /* Pull buffer to correct size */
  103. skb_pull(skb, txdesc->iv_len);
  104. /* IV/EIV data has officially be stripped */
  105. skbdesc->flags |= SKBDESC_IV_STRIPPED;
  106. }
  107. void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
  108. {
  109. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  110. const unsigned int iv_len =
  111. ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
  112. if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
  113. return;
  114. skb_push(skb, iv_len);
  115. /* Move ieee80211 header */
  116. memmove(skb->data, skb->data + iv_len, header_length);
  117. /* Copy IV/EIV data */
  118. memcpy(skb->data + header_length, skbdesc->iv, iv_len);
  119. /* IV/EIV data has returned into the frame */
  120. skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
  121. }
  122. void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, bool l2pad,
  123. unsigned int header_length,
  124. struct rxdone_entry_desc *rxdesc)
  125. {
  126. unsigned int payload_len = rxdesc->size - header_length;
  127. unsigned int align = ALIGN_SIZE(skb, header_length);
  128. unsigned int iv_len;
  129. unsigned int icv_len;
  130. unsigned int transfer = 0;
  131. /*
  132. * WEP64/WEP128: Provides IV & ICV
  133. * TKIP: Provides IV/EIV & ICV
  134. * AES: Provies IV/EIV & ICV
  135. */
  136. switch (rxdesc->cipher) {
  137. case CIPHER_WEP64:
  138. case CIPHER_WEP128:
  139. iv_len = 4;
  140. icv_len = 4;
  141. break;
  142. case CIPHER_TKIP:
  143. iv_len = 8;
  144. icv_len = 4;
  145. break;
  146. case CIPHER_AES:
  147. iv_len = 8;
  148. icv_len = 8;
  149. break;
  150. default:
  151. /* Unsupport type */
  152. return;
  153. }
  154. /*
  155. * Make room for new data. There are 2 possibilities
  156. * either the alignment is already present between
  157. * the 802.11 header and payload. In that case we
  158. * we have to move the header less then the iv_len
  159. * since we can use the already available l2pad bytes
  160. * for the iv data.
  161. * When the alignment must be added manually we must
  162. * move the header more then iv_len since we must
  163. * make room for the payload move as well.
  164. */
  165. if (l2pad) {
  166. skb_push(skb, iv_len - align);
  167. skb_put(skb, icv_len);
  168. /* Move ieee80211 header */
  169. memmove(skb->data + transfer,
  170. skb->data + transfer + (iv_len - align),
  171. header_length);
  172. transfer += header_length;
  173. } else {
  174. skb_push(skb, iv_len + align);
  175. if (align < icv_len)
  176. skb_put(skb, icv_len - align);
  177. else if (align > icv_len)
  178. skb_trim(skb, rxdesc->size + iv_len + icv_len);
  179. /* Move ieee80211 header */
  180. memmove(skb->data + transfer,
  181. skb->data + transfer + iv_len + align,
  182. header_length);
  183. transfer += header_length;
  184. }
  185. /* Copy IV/EIV data */
  186. memcpy(skb->data + transfer, rxdesc->iv, iv_len);
  187. transfer += iv_len;
  188. /*
  189. * Move payload for alignment purposes. Note that
  190. * this is only needed when no l2 padding is present.
  191. */
  192. if (!l2pad) {
  193. memmove(skb->data + transfer,
  194. skb->data + transfer + align,
  195. payload_len);
  196. }
  197. /*
  198. * NOTE: Always count the payload as transfered,
  199. * even when alignment was set to zero. This is required
  200. * for determining the correct offset for the ICV data.
  201. */
  202. transfer += payload_len;
  203. /*
  204. * Copy ICV data
  205. * AES appends 8 bytes, we can't fill the upper
  206. * 4 bytes, but mac80211 doesn't care about what
  207. * we provide here anyway and strips it immediately.
  208. */
  209. memcpy(skb->data + transfer, &rxdesc->icv, 4);
  210. transfer += icv_len;
  211. /* IV/EIV/ICV has been inserted into frame */
  212. rxdesc->size = transfer;
  213. rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
  214. }