ieee80211_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /******************************************************************************
  2. Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of version 2 of the GNU General Public License as
  5. published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  9. more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc., 59
  12. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. The full GNU General Public License is included in this distribution in the
  14. file called LICENSE.
  15. Contact Information:
  16. James P. Ketrenos <ipw2100-admin@linux.intel.com>
  17. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  18. ******************************************************************************/
  19. #include <linux/compiler.h>
  20. #include <linux/config.h>
  21. #include <linux/errno.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/in6.h>
  24. #include <linux/in.h>
  25. #include <linux/ip.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/slab.h>
  32. #include <linux/tcp.h>
  33. #include <linux/types.h>
  34. #include <linux/version.h>
  35. #include <linux/wireless.h>
  36. #include <linux/etherdevice.h>
  37. #include <asm/uaccess.h>
  38. #include <net/ieee80211.h>
  39. /*
  40. 802.11 Data Frame
  41. ,-------------------------------------------------------------------.
  42. Bytes | 2 | 2 | 6 | 6 | 6 | 2 | 0..2312 | 4 |
  43. |------|------|---------|---------|---------|------|---------|------|
  44. Desc. | ctrl | dura | DA/RA | TA | SA | Sequ | Frame | fcs |
  45. | | tion | (BSSID) | | | ence | data | |
  46. `--------------------------------------------------| |------'
  47. Total: 28 non-data bytes `----.----'
  48. |
  49. .- 'Frame data' expands to <---------------------------'
  50. |
  51. V
  52. ,---------------------------------------------------.
  53. Bytes | 1 | 1 | 1 | 3 | 2 | 0-2304 |
  54. |------|------|---------|----------|------|---------|
  55. Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP |
  56. | DSAP | SSAP | | | | Packet |
  57. | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8| | |
  58. `-----------------------------------------| |
  59. Total: 8 non-data bytes `----.----'
  60. |
  61. .- 'IP Packet' expands, if WEP enabled, to <--'
  62. |
  63. V
  64. ,-----------------------.
  65. Bytes | 4 | 0-2296 | 4 |
  66. |-----|-----------|-----|
  67. Desc. | IV | Encrypted | ICV |
  68. | | IP Packet | |
  69. `-----------------------'
  70. Total: 8 non-data bytes
  71. 802.3 Ethernet Data Frame
  72. ,-----------------------------------------.
  73. Bytes | 6 | 6 | 2 | Variable | 4 |
  74. |-------|-------|------|-----------|------|
  75. Desc. | Dest. | Source| Type | IP Packet | fcs |
  76. | MAC | MAC | | | |
  77. `-----------------------------------------'
  78. Total: 18 non-data bytes
  79. In the event that fragmentation is required, the incoming payload is split into
  80. N parts of size ieee->fts. The first fragment contains the SNAP header and the
  81. remaining packets are just data.
  82. If encryption is enabled, each fragment payload size is reduced by enough space
  83. to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
  84. So if you have 1500 bytes of payload with ieee->fts set to 500 without
  85. encryption it will take 3 frames. With WEP it will take 4 frames as the
  86. payload of each frame is reduced to 492 bytes.
  87. * SKB visualization
  88. *
  89. * ,- skb->data
  90. * |
  91. * | ETHERNET HEADER ,-<-- PAYLOAD
  92. * | | 14 bytes from skb->data
  93. * | 2 bytes for Type --> ,T. | (sizeof ethhdr)
  94. * | | | |
  95. * |,-Dest.--. ,--Src.---. | | |
  96. * | 6 bytes| | 6 bytes | | | |
  97. * v | | | | | |
  98. * 0 | v 1 | v | v 2
  99. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  100. * ^ | ^ | ^ |
  101. * | | | | | |
  102. * | | | | `T' <---- 2 bytes for Type
  103. * | | | |
  104. * | | '---SNAP--' <-------- 6 bytes for SNAP
  105. * | |
  106. * `-IV--' <-------------------- 4 bytes for IV (WEP)
  107. *
  108. * SNAP HEADER
  109. *
  110. */
  111. static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
  112. static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
  113. static inline int ieee80211_put_snap(u8 * data, u16 h_proto)
  114. {
  115. struct ieee80211_snap_hdr *snap;
  116. u8 *oui;
  117. snap = (struct ieee80211_snap_hdr *)data;
  118. snap->dsap = 0xaa;
  119. snap->ssap = 0xaa;
  120. snap->ctrl = 0x03;
  121. if (h_proto == 0x8137 || h_proto == 0x80f3)
  122. oui = P802_1H_OUI;
  123. else
  124. oui = RFC1042_OUI;
  125. snap->oui[0] = oui[0];
  126. snap->oui[1] = oui[1];
  127. snap->oui[2] = oui[2];
  128. *(u16 *) (data + SNAP_SIZE) = htons(h_proto);
  129. return SNAP_SIZE + sizeof(u16);
  130. }
  131. static inline int ieee80211_encrypt_fragment(struct ieee80211_device *ieee,
  132. struct sk_buff *frag, int hdr_len)
  133. {
  134. struct ieee80211_crypt_data *crypt = ieee->crypt[ieee->tx_keyidx];
  135. int res;
  136. #ifdef CONFIG_IEEE80211_CRYPT_TKIP
  137. struct ieee80211_hdr *header;
  138. if (ieee->tkip_countermeasures &&
  139. crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
  140. header = (struct ieee80211_hdr *)frag->data;
  141. if (net_ratelimit()) {
  142. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  143. "TX packet to " MAC_FMT "\n",
  144. ieee->dev->name, MAC_ARG(header->addr1));
  145. }
  146. return -1;
  147. }
  148. #endif
  149. /* To encrypt, frame format is:
  150. * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
  151. // PR: FIXME: Copied from hostap. Check fragmentation/MSDU/MPDU encryption.
  152. /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
  153. * call both MSDU and MPDU encryption functions from here. */
  154. atomic_inc(&crypt->refcnt);
  155. res = 0;
  156. if (crypt->ops->encrypt_msdu)
  157. res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
  158. if (res == 0 && crypt->ops->encrypt_mpdu)
  159. res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
  160. atomic_dec(&crypt->refcnt);
  161. if (res < 0) {
  162. printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
  163. ieee->dev->name, frag->len);
  164. ieee->ieee_stats.tx_discards++;
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. void ieee80211_txb_free(struct ieee80211_txb *txb)
  170. {
  171. int i;
  172. if (unlikely(!txb))
  173. return;
  174. for (i = 0; i < txb->nr_frags; i++)
  175. if (txb->fragments[i])
  176. dev_kfree_skb_any(txb->fragments[i]);
  177. kfree(txb);
  178. }
  179. static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
  180. int gfp_mask)
  181. {
  182. struct ieee80211_txb *txb;
  183. int i;
  184. txb = kmalloc(sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
  185. gfp_mask);
  186. if (!txb)
  187. return NULL;
  188. memset(txb, 0, sizeof(struct ieee80211_txb));
  189. txb->nr_frags = nr_frags;
  190. txb->frag_size = txb_size;
  191. for (i = 0; i < nr_frags; i++) {
  192. txb->fragments[i] = dev_alloc_skb(txb_size);
  193. if (unlikely(!txb->fragments[i])) {
  194. i--;
  195. break;
  196. }
  197. }
  198. if (unlikely(i != nr_frags)) {
  199. while (i >= 0)
  200. dev_kfree_skb_any(txb->fragments[i--]);
  201. kfree(txb);
  202. return NULL;
  203. }
  204. return txb;
  205. }
  206. /* SKBs are added to the ieee->tx_queue. */
  207. int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
  208. {
  209. struct ieee80211_device *ieee = netdev_priv(dev);
  210. struct ieee80211_txb *txb = NULL;
  211. struct ieee80211_hdr *frag_hdr;
  212. int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
  213. unsigned long flags;
  214. struct net_device_stats *stats = &ieee->stats;
  215. int ether_type, encrypt;
  216. int bytes, fc, hdr_len;
  217. struct sk_buff *skb_frag;
  218. struct ieee80211_hdr header = { /* Ensure zero initialized */
  219. .duration_id = 0,
  220. .seq_ctl = 0
  221. };
  222. u8 dest[ETH_ALEN], src[ETH_ALEN];
  223. struct ieee80211_crypt_data *crypt;
  224. spin_lock_irqsave(&ieee->lock, flags);
  225. /* If there is no driver handler to take the TXB, dont' bother
  226. * creating it... */
  227. if (!ieee->hard_start_xmit) {
  228. printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
  229. goto success;
  230. }
  231. if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
  232. printk(KERN_WARNING "%s: skb too small (%d).\n",
  233. ieee->dev->name, skb->len);
  234. goto success;
  235. }
  236. ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
  237. crypt = ieee->crypt[ieee->tx_keyidx];
  238. encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
  239. ieee->host_encrypt && crypt && crypt->ops;
  240. if (!encrypt && ieee->ieee802_1x &&
  241. ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
  242. stats->tx_dropped++;
  243. goto success;
  244. }
  245. /* Save source and destination addresses */
  246. memcpy(&dest, skb->data, ETH_ALEN);
  247. memcpy(&src, skb->data + ETH_ALEN, ETH_ALEN);
  248. /* Advance the SKB to the start of the payload */
  249. skb_pull(skb, sizeof(struct ethhdr));
  250. /* Determine total amount of storage required for TXB packets */
  251. bytes = skb->len + SNAP_SIZE + sizeof(u16);
  252. if (encrypt)
  253. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
  254. IEEE80211_FCTL_PROTECTED;
  255. else
  256. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  257. if (ieee->iw_mode == IW_MODE_INFRA) {
  258. fc |= IEEE80211_FCTL_TODS;
  259. /* To DS: Addr1 = BSSID, Addr2 = SA,
  260. Addr3 = DA */
  261. memcpy(&header.addr1, ieee->bssid, ETH_ALEN);
  262. memcpy(&header.addr2, &src, ETH_ALEN);
  263. memcpy(&header.addr3, &dest, ETH_ALEN);
  264. } else if (ieee->iw_mode == IW_MODE_ADHOC) {
  265. /* not From/To DS: Addr1 = DA, Addr2 = SA,
  266. Addr3 = BSSID */
  267. memcpy(&header.addr1, dest, ETH_ALEN);
  268. memcpy(&header.addr2, src, ETH_ALEN);
  269. memcpy(&header.addr3, ieee->bssid, ETH_ALEN);
  270. }
  271. header.frame_ctl = cpu_to_le16(fc);
  272. hdr_len = IEEE80211_3ADDR_LEN;
  273. /* Determine fragmentation size based on destination (multicast
  274. * and broadcast are not fragmented) */
  275. if (is_multicast_ether_addr(dest) || is_broadcast_ether_addr(dest))
  276. frag_size = MAX_FRAG_THRESHOLD;
  277. else
  278. frag_size = ieee->fts;
  279. /* Determine amount of payload per fragment. Regardless of if
  280. * this stack is providing the full 802.11 header, one will
  281. * eventually be affixed to this fragment -- so we must account for
  282. * it when determining the amount of payload space. */
  283. bytes_per_frag = frag_size - IEEE80211_3ADDR_LEN;
  284. if (ieee->config &
  285. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  286. bytes_per_frag -= IEEE80211_FCS_LEN;
  287. /* Each fragment may need to have room for encryptiong pre/postfix */
  288. if (encrypt)
  289. bytes_per_frag -= crypt->ops->extra_prefix_len +
  290. crypt->ops->extra_postfix_len;
  291. /* Number of fragments is the total bytes_per_frag /
  292. * payload_per_fragment */
  293. nr_frags = bytes / bytes_per_frag;
  294. bytes_last_frag = bytes % bytes_per_frag;
  295. if (bytes_last_frag)
  296. nr_frags++;
  297. else
  298. bytes_last_frag = bytes_per_frag;
  299. /* When we allocate the TXB we allocate enough space for the reserve
  300. * and full fragment bytes (bytes_per_frag doesn't include prefix,
  301. * postfix, header, FCS, etc.) */
  302. txb = ieee80211_alloc_txb(nr_frags, frag_size, GFP_ATOMIC);
  303. if (unlikely(!txb)) {
  304. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  305. ieee->dev->name);
  306. goto failed;
  307. }
  308. txb->encrypted = encrypt;
  309. txb->payload_size = bytes;
  310. for (i = 0; i < nr_frags; i++) {
  311. skb_frag = txb->fragments[i];
  312. if (encrypt)
  313. skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
  314. frag_hdr = (struct ieee80211_hdr *)skb_put(skb_frag, hdr_len);
  315. memcpy(frag_hdr, &header, hdr_len);
  316. /* If this is not the last fragment, then add the MOREFRAGS
  317. * bit to the frame control */
  318. if (i != nr_frags - 1) {
  319. frag_hdr->frame_ctl =
  320. cpu_to_le16(fc | IEEE80211_FCTL_MOREFRAGS);
  321. bytes = bytes_per_frag;
  322. } else {
  323. /* The last fragment takes the remaining length */
  324. bytes = bytes_last_frag;
  325. }
  326. /* Put a SNAP header on the first fragment */
  327. if (i == 0) {
  328. ieee80211_put_snap(skb_put
  329. (skb_frag, SNAP_SIZE + sizeof(u16)),
  330. ether_type);
  331. bytes -= SNAP_SIZE + sizeof(u16);
  332. }
  333. memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
  334. /* Advance the SKB... */
  335. skb_pull(skb, bytes);
  336. /* Encryption routine will move the header forward in order
  337. * to insert the IV between the header and the payload */
  338. if (encrypt)
  339. ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
  340. if (ieee->config &
  341. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  342. skb_put(skb_frag, 4);
  343. }
  344. success:
  345. spin_unlock_irqrestore(&ieee->lock, flags);
  346. dev_kfree_skb_any(skb);
  347. if (txb) {
  348. if ((*ieee->hard_start_xmit) (txb, dev) == 0) {
  349. stats->tx_packets++;
  350. stats->tx_bytes += txb->payload_size;
  351. return 0;
  352. }
  353. ieee80211_txb_free(txb);
  354. }
  355. return 0;
  356. failed:
  357. spin_unlock_irqrestore(&ieee->lock, flags);
  358. netif_stop_queue(dev);
  359. stats->tx_errors++;
  360. return 1;
  361. }
  362. EXPORT_SYMBOL(ieee80211_txb_free);