libipw_tx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. Copyright(c) 2003 - 2005 Intel Corporation. All rights reserved.
  4. Contact Information:
  5. Intel Linux Wireless <ilw@linux.intel.com>
  6. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  7. ******************************************************************************/
  8. #include <linux/compiler.h>
  9. #include <linux/errno.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/in6.h>
  12. #include <linux/in.h>
  13. #include <linux/ip.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/slab.h>
  20. #include <linux/tcp.h>
  21. #include <linux/types.h>
  22. #include <linux/wireless.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/uaccess.h>
  25. #include "libipw.h"
  26. /*
  27. 802.11 Data Frame
  28. ,-------------------------------------------------------------------.
  29. Bytes | 2 | 2 | 6 | 6 | 6 | 2 | 0..2312 | 4 |
  30. |------|------|---------|---------|---------|------|---------|------|
  31. Desc. | ctrl | dura | DA/RA | TA | SA | Sequ | Frame | fcs |
  32. | | tion | (BSSID) | | | ence | data | |
  33. `--------------------------------------------------| |------'
  34. Total: 28 non-data bytes `----.----'
  35. |
  36. .- 'Frame data' expands, if WEP enabled, to <----------'
  37. |
  38. V
  39. ,-----------------------.
  40. Bytes | 4 | 0-2296 | 4 |
  41. |-----|-----------|-----|
  42. Desc. | IV | Encrypted | ICV |
  43. | | Packet | |
  44. `-----| |-----'
  45. `-----.-----'
  46. |
  47. .- 'Encrypted Packet' expands to
  48. |
  49. V
  50. ,---------------------------------------------------.
  51. Bytes | 1 | 1 | 1 | 3 | 2 | 0-2304 |
  52. |------|------|---------|----------|------|---------|
  53. Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP |
  54. | DSAP | SSAP | | | | Packet |
  55. | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8| | |
  56. `----------------------------------------------------
  57. Total: 8 non-data bytes
  58. 802.3 Ethernet Data Frame
  59. ,-----------------------------------------.
  60. Bytes | 6 | 6 | 2 | Variable | 4 |
  61. |-------|-------|------|-----------|------|
  62. Desc. | Dest. | Source| Type | IP Packet | fcs |
  63. | MAC | MAC | | | |
  64. `-----------------------------------------'
  65. Total: 18 non-data bytes
  66. In the event that fragmentation is required, the incoming payload is split into
  67. N parts of size ieee->fts. The first fragment contains the SNAP header and the
  68. remaining packets are just data.
  69. If encryption is enabled, each fragment payload size is reduced by enough space
  70. to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
  71. So if you have 1500 bytes of payload with ieee->fts set to 500 without
  72. encryption it will take 3 frames. With WEP it will take 4 frames as the
  73. payload of each frame is reduced to 492 bytes.
  74. * SKB visualization
  75. *
  76. * ,- skb->data
  77. * |
  78. * | ETHERNET HEADER ,-<-- PAYLOAD
  79. * | | 14 bytes from skb->data
  80. * | 2 bytes for Type --> ,T. | (sizeof ethhdr)
  81. * | | | |
  82. * |,-Dest.--. ,--Src.---. | | |
  83. * | 6 bytes| | 6 bytes | | | |
  84. * v | | | | | |
  85. * 0 | v 1 | v | v 2
  86. * 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
  87. * ^ | ^ | ^ |
  88. * | | | | | |
  89. * | | | | `T' <---- 2 bytes for Type
  90. * | | | |
  91. * | | '---SNAP--' <-------- 6 bytes for SNAP
  92. * | |
  93. * `-IV--' <-------------------- 4 bytes for IV (WEP)
  94. *
  95. * SNAP HEADER
  96. *
  97. */
  98. static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
  99. static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
  100. static int libipw_copy_snap(u8 * data, __be16 h_proto)
  101. {
  102. struct libipw_snap_hdr *snap;
  103. u8 *oui;
  104. snap = (struct libipw_snap_hdr *)data;
  105. snap->dsap = 0xaa;
  106. snap->ssap = 0xaa;
  107. snap->ctrl = 0x03;
  108. if (h_proto == htons(ETH_P_AARP) || h_proto == htons(ETH_P_IPX))
  109. oui = P802_1H_OUI;
  110. else
  111. oui = RFC1042_OUI;
  112. snap->oui[0] = oui[0];
  113. snap->oui[1] = oui[1];
  114. snap->oui[2] = oui[2];
  115. memcpy(data + SNAP_SIZE, &h_proto, sizeof(u16));
  116. return SNAP_SIZE + sizeof(u16);
  117. }
  118. static int libipw_encrypt_fragment(struct libipw_device *ieee,
  119. struct sk_buff *frag, int hdr_len)
  120. {
  121. struct lib80211_crypt_data *crypt =
  122. ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
  123. int res;
  124. if (crypt == NULL)
  125. return -1;
  126. /* To encrypt, frame format is:
  127. * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
  128. atomic_inc(&crypt->refcnt);
  129. res = 0;
  130. if (crypt->ops && crypt->ops->encrypt_mpdu)
  131. res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
  132. atomic_dec(&crypt->refcnt);
  133. if (res < 0) {
  134. printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
  135. ieee->dev->name, frag->len);
  136. ieee->ieee_stats.tx_discards++;
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. void libipw_txb_free(struct libipw_txb *txb)
  142. {
  143. int i;
  144. if (unlikely(!txb))
  145. return;
  146. for (i = 0; i < txb->nr_frags; i++)
  147. if (txb->fragments[i])
  148. dev_kfree_skb_any(txb->fragments[i]);
  149. kfree(txb);
  150. }
  151. static struct libipw_txb *libipw_alloc_txb(int nr_frags, int txb_size,
  152. int headroom, gfp_t gfp_mask)
  153. {
  154. struct libipw_txb *txb;
  155. int i;
  156. txb = kzalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
  157. if (!txb)
  158. return NULL;
  159. txb->nr_frags = nr_frags;
  160. txb->frag_size = txb_size;
  161. for (i = 0; i < nr_frags; i++) {
  162. txb->fragments[i] = __dev_alloc_skb(txb_size + headroom,
  163. gfp_mask);
  164. if (unlikely(!txb->fragments[i])) {
  165. i--;
  166. break;
  167. }
  168. skb_reserve(txb->fragments[i], headroom);
  169. }
  170. if (unlikely(i != nr_frags)) {
  171. while (i >= 0)
  172. dev_kfree_skb_any(txb->fragments[i--]);
  173. kfree(txb);
  174. return NULL;
  175. }
  176. return txb;
  177. }
  178. static int libipw_classify(struct sk_buff *skb)
  179. {
  180. struct ethhdr *eth;
  181. struct iphdr *ip;
  182. eth = (struct ethhdr *)skb->data;
  183. if (eth->h_proto != htons(ETH_P_IP))
  184. return 0;
  185. ip = ip_hdr(skb);
  186. switch (ip->tos & 0xfc) {
  187. case 0x20:
  188. return 2;
  189. case 0x40:
  190. return 1;
  191. case 0x60:
  192. return 3;
  193. case 0x80:
  194. return 4;
  195. case 0xa0:
  196. return 5;
  197. case 0xc0:
  198. return 6;
  199. case 0xe0:
  200. return 7;
  201. default:
  202. return 0;
  203. }
  204. }
  205. /* Incoming skb is converted to a txb which consists of
  206. * a block of 802.11 fragment packets (stored as skbs) */
  207. netdev_tx_t libipw_xmit(struct sk_buff *skb, struct net_device *dev)
  208. {
  209. struct libipw_device *ieee = netdev_priv(dev);
  210. struct libipw_txb *txb = NULL;
  211. struct libipw_hdr_3addrqos *frag_hdr;
  212. int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size,
  213. rts_required;
  214. unsigned long flags;
  215. int encrypt, host_encrypt, host_encrypt_msdu;
  216. __be16 ether_type;
  217. int bytes, fc, hdr_len;
  218. struct sk_buff *skb_frag;
  219. struct libipw_hdr_3addrqos header = {/* Ensure zero initialized */
  220. .duration_id = 0,
  221. .seq_ctl = 0,
  222. .qos_ctl = 0
  223. };
  224. u8 dest[ETH_ALEN], src[ETH_ALEN];
  225. struct lib80211_crypt_data *crypt;
  226. int priority = skb->priority;
  227. int snapped = 0;
  228. if (ieee->is_queue_full && (*ieee->is_queue_full) (dev, priority))
  229. return NETDEV_TX_BUSY;
  230. spin_lock_irqsave(&ieee->lock, flags);
  231. /* If there is no driver handler to take the TXB, dont' bother
  232. * creating it... */
  233. if (!ieee->hard_start_xmit) {
  234. printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
  235. goto success;
  236. }
  237. if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
  238. printk(KERN_WARNING "%s: skb too small (%d).\n",
  239. ieee->dev->name, skb->len);
  240. goto success;
  241. }
  242. ether_type = ((struct ethhdr *)skb->data)->h_proto;
  243. crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
  244. encrypt = !(ether_type == htons(ETH_P_PAE) && ieee->ieee802_1x) &&
  245. ieee->sec.encrypt;
  246. host_encrypt = ieee->host_encrypt && encrypt && crypt;
  247. host_encrypt_msdu = ieee->host_encrypt_msdu && encrypt && crypt;
  248. if (!encrypt && ieee->ieee802_1x &&
  249. ieee->drop_unencrypted && ether_type != htons(ETH_P_PAE)) {
  250. dev->stats.tx_dropped++;
  251. goto success;
  252. }
  253. /* Save source and destination addresses */
  254. skb_copy_from_linear_data(skb, dest, ETH_ALEN);
  255. skb_copy_from_linear_data_offset(skb, ETH_ALEN, src, ETH_ALEN);
  256. if (host_encrypt)
  257. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
  258. IEEE80211_FCTL_PROTECTED;
  259. else
  260. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  261. if (ieee->iw_mode == IW_MODE_INFRA) {
  262. fc |= IEEE80211_FCTL_TODS;
  263. /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
  264. memcpy(header.addr1, ieee->bssid, ETH_ALEN);
  265. memcpy(header.addr2, src, ETH_ALEN);
  266. memcpy(header.addr3, dest, ETH_ALEN);
  267. } else if (ieee->iw_mode == IW_MODE_ADHOC) {
  268. /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
  269. memcpy(header.addr1, dest, ETH_ALEN);
  270. memcpy(header.addr2, src, ETH_ALEN);
  271. memcpy(header.addr3, ieee->bssid, ETH_ALEN);
  272. }
  273. hdr_len = LIBIPW_3ADDR_LEN;
  274. if (ieee->is_qos_active && ieee->is_qos_active(dev, skb)) {
  275. fc |= IEEE80211_STYPE_QOS_DATA;
  276. hdr_len += 2;
  277. skb->priority = libipw_classify(skb);
  278. header.qos_ctl |= cpu_to_le16(skb->priority & LIBIPW_QCTL_TID);
  279. }
  280. header.frame_ctl = cpu_to_le16(fc);
  281. /* Advance the SKB to the start of the payload */
  282. skb_pull(skb, sizeof(struct ethhdr));
  283. /* Determine total amount of storage required for TXB packets */
  284. bytes = skb->len + SNAP_SIZE + sizeof(u16);
  285. /* Encrypt msdu first on the whole data packet. */
  286. if ((host_encrypt || host_encrypt_msdu) &&
  287. crypt && crypt->ops && crypt->ops->encrypt_msdu) {
  288. int res = 0;
  289. int len = bytes + hdr_len + crypt->ops->extra_msdu_prefix_len +
  290. crypt->ops->extra_msdu_postfix_len;
  291. struct sk_buff *skb_new = dev_alloc_skb(len);
  292. if (unlikely(!skb_new))
  293. goto failed;
  294. skb_reserve(skb_new, crypt->ops->extra_msdu_prefix_len);
  295. skb_put_data(skb_new, &header, hdr_len);
  296. snapped = 1;
  297. libipw_copy_snap(skb_put(skb_new, SNAP_SIZE + sizeof(u16)),
  298. ether_type);
  299. skb_copy_from_linear_data(skb, skb_put(skb_new, skb->len), skb->len);
  300. res = crypt->ops->encrypt_msdu(skb_new, hdr_len, crypt->priv);
  301. if (res < 0) {
  302. LIBIPW_ERROR("msdu encryption failed\n");
  303. dev_kfree_skb_any(skb_new);
  304. goto failed;
  305. }
  306. dev_kfree_skb_any(skb);
  307. skb = skb_new;
  308. bytes += crypt->ops->extra_msdu_prefix_len +
  309. crypt->ops->extra_msdu_postfix_len;
  310. skb_pull(skb, hdr_len);
  311. }
  312. if (host_encrypt || ieee->host_open_frag) {
  313. /* Determine fragmentation size based on destination (multicast
  314. * and broadcast are not fragmented) */
  315. if (is_multicast_ether_addr(dest) ||
  316. is_broadcast_ether_addr(dest))
  317. frag_size = MAX_FRAG_THRESHOLD;
  318. else
  319. frag_size = ieee->fts;
  320. /* Determine amount of payload per fragment. Regardless of if
  321. * this stack is providing the full 802.11 header, one will
  322. * eventually be affixed to this fragment -- so we must account
  323. * for it when determining the amount of payload space. */
  324. bytes_per_frag = frag_size - hdr_len;
  325. if (ieee->config &
  326. (CFG_LIBIPW_COMPUTE_FCS | CFG_LIBIPW_RESERVE_FCS))
  327. bytes_per_frag -= LIBIPW_FCS_LEN;
  328. /* Each fragment may need to have room for encryption
  329. * pre/postfix */
  330. if (host_encrypt && crypt && crypt->ops)
  331. bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
  332. crypt->ops->extra_mpdu_postfix_len;
  333. /* Number of fragments is the total
  334. * bytes_per_frag / payload_per_fragment */
  335. nr_frags = bytes / bytes_per_frag;
  336. bytes_last_frag = bytes % bytes_per_frag;
  337. if (bytes_last_frag)
  338. nr_frags++;
  339. else
  340. bytes_last_frag = bytes_per_frag;
  341. } else {
  342. nr_frags = 1;
  343. bytes_per_frag = bytes_last_frag = bytes;
  344. frag_size = bytes + hdr_len;
  345. }
  346. rts_required = (frag_size > ieee->rts
  347. && ieee->config & CFG_LIBIPW_RTS);
  348. if (rts_required)
  349. nr_frags++;
  350. /* When we allocate the TXB we allocate enough space for the reserve
  351. * and full fragment bytes (bytes_per_frag doesn't include prefix,
  352. * postfix, header, FCS, etc.) */
  353. txb = libipw_alloc_txb(nr_frags, frag_size,
  354. ieee->tx_headroom, GFP_ATOMIC);
  355. if (unlikely(!txb)) {
  356. printk(KERN_WARNING "%s: Could not allocate TXB\n",
  357. ieee->dev->name);
  358. goto failed;
  359. }
  360. txb->encrypted = encrypt;
  361. if (host_encrypt)
  362. txb->payload_size = frag_size * (nr_frags - 1) +
  363. bytes_last_frag;
  364. else
  365. txb->payload_size = bytes;
  366. if (rts_required) {
  367. skb_frag = txb->fragments[0];
  368. frag_hdr = skb_put(skb_frag, hdr_len);
  369. /*
  370. * Set header frame_ctl to the RTS.
  371. */
  372. header.frame_ctl =
  373. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  374. memcpy(frag_hdr, &header, hdr_len);
  375. /*
  376. * Restore header frame_ctl to the original data setting.
  377. */
  378. header.frame_ctl = cpu_to_le16(fc);
  379. if (ieee->config &
  380. (CFG_LIBIPW_COMPUTE_FCS | CFG_LIBIPW_RESERVE_FCS))
  381. skb_put(skb_frag, 4);
  382. txb->rts_included = 1;
  383. i = 1;
  384. } else
  385. i = 0;
  386. for (; i < nr_frags; i++) {
  387. skb_frag = txb->fragments[i];
  388. if (host_encrypt)
  389. skb_reserve(skb_frag,
  390. crypt->ops->extra_mpdu_prefix_len);
  391. frag_hdr = skb_put_data(skb_frag, &header, hdr_len);
  392. /* If this is not the last fragment, then add the MOREFRAGS
  393. * bit to the frame control */
  394. if (i != nr_frags - 1) {
  395. frag_hdr->frame_ctl =
  396. cpu_to_le16(fc | IEEE80211_FCTL_MOREFRAGS);
  397. bytes = bytes_per_frag;
  398. } else {
  399. /* The last fragment takes the remaining length */
  400. bytes = bytes_last_frag;
  401. }
  402. if (i == 0 && !snapped) {
  403. libipw_copy_snap(skb_put
  404. (skb_frag, SNAP_SIZE + sizeof(u16)),
  405. ether_type);
  406. bytes -= SNAP_SIZE + sizeof(u16);
  407. }
  408. skb_copy_from_linear_data(skb, skb_put(skb_frag, bytes), bytes);
  409. /* Advance the SKB... */
  410. skb_pull(skb, bytes);
  411. /* Encryption routine will move the header forward in order
  412. * to insert the IV between the header and the payload */
  413. if (host_encrypt)
  414. libipw_encrypt_fragment(ieee, skb_frag, hdr_len);
  415. if (ieee->config &
  416. (CFG_LIBIPW_COMPUTE_FCS | CFG_LIBIPW_RESERVE_FCS))
  417. skb_put(skb_frag, 4);
  418. }
  419. success:
  420. spin_unlock_irqrestore(&ieee->lock, flags);
  421. dev_kfree_skb_any(skb);
  422. if (txb) {
  423. netdev_tx_t ret = (*ieee->hard_start_xmit)(txb, dev, priority);
  424. if (ret == NETDEV_TX_OK) {
  425. dev->stats.tx_packets++;
  426. dev->stats.tx_bytes += txb->payload_size;
  427. return NETDEV_TX_OK;
  428. }
  429. libipw_txb_free(txb);
  430. }
  431. return NETDEV_TX_OK;
  432. failed:
  433. spin_unlock_irqrestore(&ieee->lock, flags);
  434. netif_stop_queue(dev);
  435. dev->stats.tx_errors++;
  436. return NETDEV_TX_BUSY;
  437. }
  438. EXPORT_SYMBOL(libipw_xmit);
  439. EXPORT_SYMBOL(libipw_txb_free);