rt2x00crypto.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  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, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Module: rt2x00lib
  17. Abstract: rt2x00 crypto specific routines.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include "rt2x00.h"
  22. #include "rt2x00lib.h"
  23. enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
  24. {
  25. switch (key->cipher) {
  26. case WLAN_CIPHER_SUITE_WEP40:
  27. return CIPHER_WEP64;
  28. case WLAN_CIPHER_SUITE_WEP104:
  29. return CIPHER_WEP128;
  30. case WLAN_CIPHER_SUITE_TKIP:
  31. return CIPHER_TKIP;
  32. case WLAN_CIPHER_SUITE_CCMP:
  33. return CIPHER_AES;
  34. default:
  35. return CIPHER_NONE;
  36. }
  37. }
  38. void rt2x00crypto_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
  39. struct sk_buff *skb,
  40. struct txentry_desc *txdesc)
  41. {
  42. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  43. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  44. if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || !hw_key)
  45. return;
  46. __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
  47. txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
  48. if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  49. __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
  50. txdesc->key_idx = hw_key->hw_key_idx;
  51. txdesc->iv_offset = txdesc->header_length;
  52. txdesc->iv_len = hw_key->iv_len;
  53. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  54. __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
  55. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
  56. __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
  57. }
  58. unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
  59. struct sk_buff *skb)
  60. {
  61. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  62. struct ieee80211_key_conf *key = tx_info->control.hw_key;
  63. unsigned int overhead = 0;
  64. if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || !key)
  65. return overhead;
  66. /*
  67. * Extend frame length to include IV/EIV/ICV/MMIC,
  68. * note that these lengths should only be added when
  69. * mac80211 does not generate it.
  70. */
  71. overhead += key->icv_len;
  72. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  73. overhead += key->iv_len;
  74. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  75. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  76. overhead += 8;
  77. }
  78. return overhead;
  79. }
  80. void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  81. {
  82. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  83. if (unlikely(!txdesc->iv_len))
  84. return;
  85. /* Copy IV/EIV data */
  86. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  87. }
  88. void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  89. {
  90. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  91. if (unlikely(!txdesc->iv_len))
  92. return;
  93. /* Copy IV/EIV data */
  94. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  95. /* Move ieee80211 header */
  96. memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
  97. /* Pull buffer to correct size */
  98. skb_pull(skb, txdesc->iv_len);
  99. txdesc->length -= txdesc->iv_len;
  100. /* IV/EIV data has officially been stripped */
  101. skbdesc->flags |= SKBDESC_IV_STRIPPED;
  102. }
  103. void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
  104. {
  105. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  106. const unsigned int iv_len =
  107. ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
  108. if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
  109. return;
  110. skb_push(skb, iv_len);
  111. /* Move ieee80211 header */
  112. memmove(skb->data, skb->data + iv_len, header_length);
  113. /* Copy IV/EIV data */
  114. memcpy(skb->data + header_length, skbdesc->iv, iv_len);
  115. /* IV/EIV data has returned into the frame */
  116. skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
  117. }
  118. void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
  119. unsigned int header_length,
  120. struct rxdone_entry_desc *rxdesc)
  121. {
  122. unsigned int payload_len = rxdesc->size - header_length;
  123. unsigned int align = ALIGN_SIZE(skb, header_length);
  124. unsigned int iv_len;
  125. unsigned int icv_len;
  126. unsigned int transfer = 0;
  127. /*
  128. * WEP64/WEP128: Provides IV & ICV
  129. * TKIP: Provides IV/EIV & ICV
  130. * AES: Provies IV/EIV & ICV
  131. */
  132. switch (rxdesc->cipher) {
  133. case CIPHER_WEP64:
  134. case CIPHER_WEP128:
  135. iv_len = 4;
  136. icv_len = 4;
  137. break;
  138. case CIPHER_TKIP:
  139. iv_len = 8;
  140. icv_len = 4;
  141. break;
  142. case CIPHER_AES:
  143. iv_len = 8;
  144. icv_len = 8;
  145. break;
  146. default:
  147. /* Unsupport type */
  148. return;
  149. }
  150. /*
  151. * Make room for new data. There are 2 possibilities
  152. * either the alignment is already present between
  153. * the 802.11 header and payload. In that case we
  154. * we have to move the header less then the iv_len
  155. * since we can use the already available l2pad bytes
  156. * for the iv data.
  157. * When the alignment must be added manually we must
  158. * move the header more then iv_len since we must
  159. * make room for the payload move as well.
  160. */
  161. if (rxdesc->dev_flags & RXDONE_L2PAD) {
  162. skb_push(skb, iv_len - align);
  163. skb_put(skb, icv_len);
  164. /* Move ieee80211 header */
  165. memmove(skb->data + transfer,
  166. skb->data + transfer + (iv_len - align),
  167. header_length);
  168. transfer += header_length;
  169. } else {
  170. skb_push(skb, iv_len + align);
  171. if (align < icv_len)
  172. skb_put(skb, icv_len - align);
  173. else if (align > icv_len)
  174. skb_trim(skb, rxdesc->size + iv_len + icv_len);
  175. /* Move ieee80211 header */
  176. memmove(skb->data + transfer,
  177. skb->data + transfer + iv_len + align,
  178. header_length);
  179. transfer += header_length;
  180. }
  181. /* Copy IV/EIV data */
  182. memcpy(skb->data + transfer, rxdesc->iv, iv_len);
  183. transfer += iv_len;
  184. /*
  185. * Move payload for alignment purposes. Note that
  186. * this is only needed when no l2 padding is present.
  187. */
  188. if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
  189. memmove(skb->data + transfer,
  190. skb->data + transfer + align,
  191. payload_len);
  192. }
  193. /*
  194. * NOTE: Always count the payload as transferred,
  195. * even when alignment was set to zero. This is required
  196. * for determining the correct offset for the ICV data.
  197. */
  198. transfer += payload_len;
  199. /*
  200. * Copy ICV data
  201. * AES appends 8 bytes, we can't fill the upper
  202. * 4 bytes, but mac80211 doesn't care about what
  203. * we provide here anyway and strips it immediately.
  204. */
  205. memcpy(skb->data + transfer, &rxdesc->icv, 4);
  206. transfer += icv_len;
  207. /* IV/EIV/ICV has been inserted into frame */
  208. rxdesc->size = transfer;
  209. rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
  210. }