ieee80211_tx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_copy_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. /* To encrypt, frame format is:
  137. * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
  138. atomic_inc(&crypt->refcnt);
  139. res = 0;
  140. if (crypt->ops->encrypt_mpdu)
  141. res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
  142. atomic_dec(&crypt->refcnt);
  143. if (res < 0) {
  144. printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
  145. ieee->dev->name, frag->len);
  146. ieee->ieee_stats.tx_discards++;
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. void ieee80211_txb_free(struct ieee80211_txb *txb)
  152. {
  153. int i;
  154. if (unlikely(!txb))
  155. return;
  156. for (i = 0; i < txb->nr_frags; i++)
  157. if (txb->fragments[i])
  158. dev_kfree_skb_any(txb->fragments[i]);
  159. kfree(txb);
  160. }
  161. static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
  162. int gfp_mask)
  163. {
  164. struct ieee80211_txb *txb;
  165. int i;
  166. txb = kmalloc(sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
  167. gfp_mask);
  168. if (!txb)
  169. return NULL;
  170. memset(txb, 0, sizeof(struct ieee80211_txb));
  171. txb->nr_frags = nr_frags;
  172. txb->frag_size = txb_size;
  173. for (i = 0; i < nr_frags; i++) {
  174. txb->fragments[i] = dev_alloc_skb(txb_size);
  175. if (unlikely(!txb->fragments[i])) {
  176. i--;
  177. break;
  178. }
  179. }
  180. if (unlikely(i != nr_frags)) {
  181. while (i >= 0)
  182. dev_kfree_skb_any(txb->fragments[i--]);
  183. kfree(txb);
  184. return NULL;
  185. }
  186. return txb;
  187. }
  188. /* Incoming skb is converted to a txb which consists of
  189. * a block of 802.11 fragment packets (stored as skbs) */
  190. int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
  191. {
  192. struct ieee80211_device *ieee = netdev_priv(dev);
  193. struct ieee80211_txb *txb = NULL;
  194. struct ieee80211_hdr_3addr *frag_hdr;
  195. int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size,
  196. rts_required;
  197. unsigned long flags;
  198. struct net_device_stats *stats = &ieee->stats;
  199. int ether_type, encrypt, host_encrypt, host_encrypt_msdu;
  200. int bytes, fc, hdr_len;
  201. struct sk_buff *skb_frag;
  202. struct ieee80211_hdr_3addr header = { /* Ensure zero initialized */
  203. .duration_id = 0,
  204. .seq_ctl = 0
  205. };
  206. u8 dest[ETH_ALEN], src[ETH_ALEN];
  207. struct ieee80211_crypt_data *crypt;
  208. int snapped = 0;
  209. spin_lock_irqsave(&ieee->lock, flags);
  210. /* If there is no driver handler to take the TXB, dont' bother
  211. * creating it... */
  212. if (!ieee->hard_start_xmit) {
  213. printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
  214. goto success;
  215. }
  216. if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
  217. printk(KERN_WARNING "%s: skb too small (%d).\n",
  218. ieee->dev->name, skb->len);
  219. goto success;
  220. }
  221. ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
  222. crypt = ieee->crypt[ieee->tx_keyidx];
  223. encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
  224. ieee->sec.encrypt;
  225. host_encrypt = ieee->host_encrypt && encrypt;
  226. host_encrypt_msdu = ieee->host_encrypt_msdu && encrypt;
  227. if (!encrypt && ieee->ieee802_1x &&
  228. ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
  229. stats->tx_dropped++;
  230. goto success;
  231. }
  232. /* Save source and destination addresses */
  233. memcpy(dest, skb->data, ETH_ALEN);
  234. memcpy(src, skb->data + ETH_ALEN, ETH_ALEN);
  235. /* Advance the SKB to the start of the payload */
  236. skb_pull(skb, sizeof(struct ethhdr));
  237. /* Determine total amount of storage required for TXB packets */
  238. bytes = skb->len + SNAP_SIZE + sizeof(u16);
  239. if (host_encrypt)
  240. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
  241. IEEE80211_FCTL_PROTECTED;
  242. else
  243. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  244. if (ieee->iw_mode == IW_MODE_INFRA) {
  245. fc |= IEEE80211_FCTL_TODS;
  246. /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
  247. memcpy(header.addr1, ieee->bssid, ETH_ALEN);
  248. memcpy(header.addr2, src, ETH_ALEN);
  249. memcpy(header.addr3, dest, ETH_ALEN);
  250. } else if (ieee->iw_mode == IW_MODE_ADHOC) {
  251. /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
  252. memcpy(header.addr1, dest, ETH_ALEN);
  253. memcpy(header.addr2, src, ETH_ALEN);
  254. memcpy(header.addr3, ieee->bssid, ETH_ALEN);
  255. }
  256. header.frame_ctl = cpu_to_le16(fc);
  257. hdr_len = IEEE80211_3ADDR_LEN;
  258. /* Encrypt msdu first on the whole data packet. */
  259. if ((host_encrypt || host_encrypt_msdu) &&
  260. crypt && crypt->ops && crypt->ops->encrypt_msdu) {
  261. int res = 0;
  262. int len = bytes + hdr_len + crypt->ops->extra_msdu_prefix_len +
  263. crypt->ops->extra_msdu_postfix_len;
  264. struct sk_buff *skb_new = dev_alloc_skb(len);
  265. if (unlikely(!skb_new))
  266. goto failed;
  267. skb_reserve(skb_new, crypt->ops->extra_msdu_prefix_len);
  268. memcpy(skb_put(skb_new, hdr_len), &header, hdr_len);
  269. snapped = 1;
  270. ieee80211_copy_snap(skb_put(skb_new, SNAP_SIZE + sizeof(u16)),
  271. ether_type);
  272. memcpy(skb_put(skb_new, skb->len), skb->data, skb->len);
  273. res = crypt->ops->encrypt_msdu(skb_new, hdr_len, crypt->priv);
  274. if (res < 0) {
  275. IEEE80211_ERROR("msdu encryption failed\n");
  276. dev_kfree_skb_any(skb_new);
  277. goto failed;
  278. }
  279. dev_kfree_skb_any(skb);
  280. skb = skb_new;
  281. bytes += crypt->ops->extra_msdu_prefix_len +
  282. crypt->ops->extra_msdu_postfix_len;
  283. skb_pull(skb, hdr_len);
  284. }
  285. if (host_encrypt || ieee->host_open_frag) {
  286. /* Determine fragmentation size based on destination (multicast
  287. * and broadcast are not fragmented) */
  288. if (is_multicast_ether_addr(dest))
  289. frag_size = MAX_FRAG_THRESHOLD;
  290. else
  291. frag_size = ieee->fts;
  292. /* Determine amount of payload per fragment. Regardless of if
  293. * this stack is providing the full 802.11 header, one will
  294. * eventually be affixed to this fragment -- so we must account
  295. * for it when determining the amount of payload space. */
  296. bytes_per_frag = frag_size - IEEE80211_3ADDR_LEN;
  297. if (ieee->config &
  298. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  299. bytes_per_frag -= IEEE80211_FCS_LEN;
  300. /* Each fragment may need to have room for encryptiong
  301. * pre/postfix */
  302. if (host_encrypt)
  303. bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
  304. crypt->ops->extra_mpdu_postfix_len;
  305. /* Number of fragments is the total
  306. * bytes_per_frag / payload_per_fragment */
  307. nr_frags = bytes / bytes_per_frag;
  308. bytes_last_frag = bytes % bytes_per_frag;
  309. if (bytes_last_frag)
  310. nr_frags++;
  311. else
  312. bytes_last_frag = bytes_per_frag;
  313. } else {
  314. nr_frags = 1;
  315. bytes_per_frag = bytes_last_frag = bytes;
  316. frag_size = bytes + IEEE80211_3ADDR_LEN;
  317. }
  318. rts_required = (frag_size > ieee->rts
  319. && ieee->config & CFG_IEEE80211_RTS);
  320. if (rts_required)
  321. nr_frags++;
  322. /* When we allocate the TXB we allocate enough space for the reserve
  323. * and full fragment bytes (bytes_per_frag doesn't include prefix,
  324. * postfix, header, FCS, etc.) */
  325. txb = ieee80211_alloc_txb(nr_frags, frag_size, GFP_ATOMIC);
  326. if (unlikely(!txb)) {
  327. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  328. ieee->dev->name);
  329. goto failed;
  330. }
  331. txb->encrypted = encrypt;
  332. if (host_encrypt)
  333. txb->payload_size = frag_size * (nr_frags - 1) +
  334. bytes_last_frag;
  335. else
  336. txb->payload_size = bytes;
  337. if (rts_required) {
  338. skb_frag = txb->fragments[0];
  339. frag_hdr =
  340. (struct ieee80211_hdr_3addr *)skb_put(skb_frag, hdr_len);
  341. /*
  342. * Set header frame_ctl to the RTS.
  343. */
  344. header.frame_ctl =
  345. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  346. memcpy(frag_hdr, &header, hdr_len);
  347. /*
  348. * Restore header frame_ctl to the original data setting.
  349. */
  350. header.frame_ctl = cpu_to_le16(fc);
  351. if (ieee->config &
  352. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  353. skb_put(skb_frag, 4);
  354. txb->rts_included = 1;
  355. i = 1;
  356. } else
  357. i = 0;
  358. for (; i < nr_frags; i++) {
  359. skb_frag = txb->fragments[i];
  360. if (host_encrypt)
  361. skb_reserve(skb_frag,
  362. crypt->ops->extra_mpdu_prefix_len);
  363. frag_hdr =
  364. (struct ieee80211_hdr_3addr *)skb_put(skb_frag, hdr_len);
  365. memcpy(frag_hdr, &header, hdr_len);
  366. /* If this is not the last fragment, then add the MOREFRAGS
  367. * bit to the frame control */
  368. if (i != nr_frags - 1) {
  369. frag_hdr->frame_ctl =
  370. cpu_to_le16(fc | IEEE80211_FCTL_MOREFRAGS);
  371. bytes = bytes_per_frag;
  372. } else {
  373. /* The last fragment takes the remaining length */
  374. bytes = bytes_last_frag;
  375. }
  376. if (i == 0 && !snapped) {
  377. ieee80211_copy_snap(skb_put
  378. (skb_frag, SNAP_SIZE + sizeof(u16)),
  379. ether_type);
  380. bytes -= SNAP_SIZE + sizeof(u16);
  381. }
  382. memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
  383. /* Advance the SKB... */
  384. skb_pull(skb, bytes);
  385. /* Encryption routine will move the header forward in order
  386. * to insert the IV between the header and the payload */
  387. if (host_encrypt)
  388. ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
  389. if (ieee->config &
  390. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  391. skb_put(skb_frag, 4);
  392. }
  393. success:
  394. spin_unlock_irqrestore(&ieee->lock, flags);
  395. dev_kfree_skb_any(skb);
  396. if (txb) {
  397. int ret = (*ieee->hard_start_xmit) (txb, dev);
  398. if (ret == 0) {
  399. stats->tx_packets++;
  400. stats->tx_bytes += txb->payload_size;
  401. return 0;
  402. }
  403. ieee80211_txb_free(txb);
  404. }
  405. return 0;
  406. failed:
  407. spin_unlock_irqrestore(&ieee->lock, flags);
  408. netif_stop_queue(dev);
  409. stats->tx_errors++;
  410. return 1;
  411. }
  412. /* Incoming 802.11 strucure is converted to a TXB
  413. * a block of 802.11 fragment packets (stored as skbs) */
  414. int ieee80211_tx_frame(struct ieee80211_device *ieee,
  415. struct ieee80211_hdr *frame, int len)
  416. {
  417. struct ieee80211_txb *txb = NULL;
  418. unsigned long flags;
  419. struct net_device_stats *stats = &ieee->stats;
  420. struct sk_buff *skb_frag;
  421. spin_lock_irqsave(&ieee->lock, flags);
  422. /* If there is no driver handler to take the TXB, dont' bother
  423. * creating it... */
  424. if (!ieee->hard_start_xmit) {
  425. printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
  426. goto success;
  427. }
  428. if (unlikely(len < 24)) {
  429. printk(KERN_WARNING "%s: skb too small (%d).\n",
  430. ieee->dev->name, len);
  431. goto success;
  432. }
  433. /* When we allocate the TXB we allocate enough space for the reserve
  434. * and full fragment bytes (bytes_per_frag doesn't include prefix,
  435. * postfix, header, FCS, etc.) */
  436. txb = ieee80211_alloc_txb(1, len, GFP_ATOMIC);
  437. if (unlikely(!txb)) {
  438. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  439. ieee->dev->name);
  440. goto failed;
  441. }
  442. txb->encrypted = 0;
  443. txb->payload_size = len;
  444. skb_frag = txb->fragments[0];
  445. memcpy(skb_put(skb_frag, len), frame, len);
  446. if (ieee->config &
  447. (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
  448. skb_put(skb_frag, 4);
  449. success:
  450. spin_unlock_irqrestore(&ieee->lock, flags);
  451. if (txb) {
  452. if ((*ieee->hard_start_xmit) (txb, ieee->dev) == 0) {
  453. stats->tx_packets++;
  454. stats->tx_bytes += txb->payload_size;
  455. return 0;
  456. }
  457. ieee80211_txb_free(txb);
  458. }
  459. return 0;
  460. failed:
  461. spin_unlock_irqrestore(&ieee->lock, flags);
  462. stats->tx_errors++;
  463. return 1;
  464. }
  465. EXPORT_SYMBOL(ieee80211_tx_frame);
  466. EXPORT_SYMBOL(ieee80211_txb_free);