hostap_80211_tx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
  2. {
  3. struct ieee80211_hdr *hdr;
  4. u16 fc;
  5. hdr = (struct ieee80211_hdr *) skb->data;
  6. printk(KERN_DEBUG "%s: TX len=%d jiffies=%ld\n",
  7. name, skb->len, jiffies);
  8. if (skb->len < 2)
  9. return;
  10. fc = le16_to_cpu(hdr->frame_ctl);
  11. printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
  12. fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
  13. fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
  14. fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
  15. if (skb->len < IEEE80211_DATA_HDR3_LEN) {
  16. printk("\n");
  17. return;
  18. }
  19. printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
  20. le16_to_cpu(hdr->seq_ctl));
  21. printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
  22. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
  23. if (skb->len >= 30)
  24. printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
  25. printk("\n");
  26. }
  27. /* hard_start_xmit function for data interfaces (wlan#, wlan#wds#, wlan#sta)
  28. * Convert Ethernet header into a suitable IEEE 802.11 header depending on
  29. * device configuration. */
  30. int hostap_data_start_xmit(struct sk_buff *skb, struct net_device *dev)
  31. {
  32. struct hostap_interface *iface;
  33. local_info_t *local;
  34. int need_headroom, need_tailroom = 0;
  35. struct ieee80211_hdr hdr;
  36. u16 fc, ethertype = 0;
  37. enum {
  38. WDS_NO = 0, WDS_OWN_FRAME, WDS_COMPLIANT_FRAME
  39. } use_wds = WDS_NO;
  40. u8 *encaps_data;
  41. int hdr_len, encaps_len, skip_header_bytes;
  42. int to_assoc_ap = 0;
  43. struct hostap_skb_tx_data *meta;
  44. iface = netdev_priv(dev);
  45. local = iface->local;
  46. if (skb->len < ETH_HLEN) {
  47. printk(KERN_DEBUG "%s: hostap_data_start_xmit: short skb "
  48. "(len=%d)\n", dev->name, skb->len);
  49. kfree_skb(skb);
  50. return 0;
  51. }
  52. if (local->ddev != dev) {
  53. use_wds = (local->iw_mode == IW_MODE_MASTER &&
  54. !(local->wds_type & HOSTAP_WDS_STANDARD_FRAME)) ?
  55. WDS_OWN_FRAME : WDS_COMPLIANT_FRAME;
  56. if (dev == local->stadev) {
  57. to_assoc_ap = 1;
  58. use_wds = WDS_NO;
  59. } else if (dev == local->apdev) {
  60. printk(KERN_DEBUG "%s: prism2_tx: trying to use "
  61. "AP device with Ethernet net dev\n", dev->name);
  62. kfree_skb(skb);
  63. return 0;
  64. }
  65. } else {
  66. if (local->iw_mode == IW_MODE_REPEAT) {
  67. printk(KERN_DEBUG "%s: prism2_tx: trying to use "
  68. "non-WDS link in Repeater mode\n", dev->name);
  69. kfree_skb(skb);
  70. return 0;
  71. } else if (local->iw_mode == IW_MODE_INFRA &&
  72. (local->wds_type & HOSTAP_WDS_AP_CLIENT) &&
  73. memcmp(skb->data + ETH_ALEN, dev->dev_addr,
  74. ETH_ALEN) != 0) {
  75. /* AP client mode: send frames with foreign src addr
  76. * using 4-addr WDS frames */
  77. use_wds = WDS_COMPLIANT_FRAME;
  78. }
  79. }
  80. /* Incoming skb->data: dst_addr[6], src_addr[6], proto[2], payload
  81. * ==>
  82. * Prism2 TX frame with 802.11 header:
  83. * txdesc (address order depending on used mode; includes dst_addr and
  84. * src_addr), possible encapsulation (RFC1042/Bridge-Tunnel;
  85. * proto[2], payload {, possible addr4[6]} */
  86. ethertype = (skb->data[12] << 8) | skb->data[13];
  87. memset(&hdr, 0, sizeof(hdr));
  88. /* Length of data after IEEE 802.11 header */
  89. encaps_data = NULL;
  90. encaps_len = 0;
  91. skip_header_bytes = ETH_HLEN;
  92. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  93. encaps_data = bridge_tunnel_header;
  94. encaps_len = sizeof(bridge_tunnel_header);
  95. skip_header_bytes -= 2;
  96. } else if (ethertype >= 0x600) {
  97. encaps_data = rfc1042_header;
  98. encaps_len = sizeof(rfc1042_header);
  99. skip_header_bytes -= 2;
  100. }
  101. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  102. hdr_len = IEEE80211_DATA_HDR3_LEN;
  103. if (use_wds != WDS_NO) {
  104. /* Note! Prism2 station firmware has problems with sending real
  105. * 802.11 frames with four addresses; until these problems can
  106. * be fixed or worked around, 4-addr frames needed for WDS are
  107. * using incompatible format: FromDS flag is not set and the
  108. * fourth address is added after the frame payload; it is
  109. * assumed, that the receiving station knows how to handle this
  110. * frame format */
  111. if (use_wds == WDS_COMPLIANT_FRAME) {
  112. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  113. /* From&To DS: Addr1 = RA, Addr2 = TA, Addr3 = DA,
  114. * Addr4 = SA */
  115. memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  116. hdr_len += ETH_ALEN;
  117. } else {
  118. /* bogus 4-addr format to workaround Prism2 station
  119. * f/w bug */
  120. fc |= IEEE80211_FCTL_TODS;
  121. /* From DS: Addr1 = DA (used as RA),
  122. * Addr2 = BSSID (used as TA), Addr3 = SA (used as DA),
  123. */
  124. /* SA from skb->data + ETH_ALEN will be added after
  125. * frame payload; use hdr.addr4 as a temporary buffer
  126. */
  127. memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  128. need_tailroom += ETH_ALEN;
  129. }
  130. /* send broadcast and multicast frames to broadcast RA, if
  131. * configured; otherwise, use unicast RA of the WDS link */
  132. if ((local->wds_type & HOSTAP_WDS_BROADCAST_RA) &&
  133. skb->data[0] & 0x01)
  134. memset(&hdr.addr1, 0xff, ETH_ALEN);
  135. else if (iface->type == HOSTAP_INTERFACE_WDS)
  136. memcpy(&hdr.addr1, iface->u.wds.remote_addr,
  137. ETH_ALEN);
  138. else
  139. memcpy(&hdr.addr1, local->bssid, ETH_ALEN);
  140. memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
  141. memcpy(&hdr.addr3, skb->data, ETH_ALEN);
  142. } else if (local->iw_mode == IW_MODE_MASTER && !to_assoc_ap) {
  143. fc |= IEEE80211_FCTL_FROMDS;
  144. /* From DS: Addr1 = DA, Addr2 = BSSID, Addr3 = SA */
  145. memcpy(&hdr.addr1, skb->data, ETH_ALEN);
  146. memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
  147. memcpy(&hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  148. } else if (local->iw_mode == IW_MODE_INFRA || to_assoc_ap) {
  149. fc |= IEEE80211_FCTL_TODS;
  150. /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
  151. memcpy(&hdr.addr1, to_assoc_ap ?
  152. local->assoc_ap_addr : local->bssid, ETH_ALEN);
  153. memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  154. memcpy(&hdr.addr3, skb->data, ETH_ALEN);
  155. } else if (local->iw_mode == IW_MODE_ADHOC) {
  156. /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
  157. memcpy(&hdr.addr1, skb->data, ETH_ALEN);
  158. memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  159. memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
  160. }
  161. hdr.frame_ctl = cpu_to_le16(fc);
  162. skb_pull(skb, skip_header_bytes);
  163. need_headroom = local->func->need_tx_headroom + hdr_len + encaps_len;
  164. if (skb_tailroom(skb) < need_tailroom) {
  165. skb = skb_unshare(skb, GFP_ATOMIC);
  166. if (skb == NULL) {
  167. iface->stats.tx_dropped++;
  168. return 0;
  169. }
  170. if (pskb_expand_head(skb, need_headroom, need_tailroom,
  171. GFP_ATOMIC)) {
  172. kfree_skb(skb);
  173. iface->stats.tx_dropped++;
  174. return 0;
  175. }
  176. } else if (skb_headroom(skb) < need_headroom) {
  177. struct sk_buff *tmp = skb;
  178. skb = skb_realloc_headroom(skb, need_headroom);
  179. kfree_skb(tmp);
  180. if (skb == NULL) {
  181. iface->stats.tx_dropped++;
  182. return 0;
  183. }
  184. } else {
  185. skb = skb_unshare(skb, GFP_ATOMIC);
  186. if (skb == NULL) {
  187. iface->stats.tx_dropped++;
  188. return 0;
  189. }
  190. }
  191. if (encaps_data)
  192. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  193. memcpy(skb_push(skb, hdr_len), &hdr, hdr_len);
  194. if (use_wds == WDS_OWN_FRAME) {
  195. memcpy(skb_put(skb, ETH_ALEN), &hdr.addr4, ETH_ALEN);
  196. }
  197. iface->stats.tx_packets++;
  198. iface->stats.tx_bytes += skb->len;
  199. skb->mac.raw = skb->data;
  200. meta = (struct hostap_skb_tx_data *) skb->cb;
  201. memset(meta, 0, sizeof(*meta));
  202. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  203. if (use_wds)
  204. meta->flags |= HOSTAP_TX_FLAGS_WDS;
  205. meta->ethertype = ethertype;
  206. meta->iface = iface;
  207. /* Send IEEE 802.11 encapsulated frame using the master radio device */
  208. skb->dev = local->dev;
  209. dev_queue_xmit(skb);
  210. return 0;
  211. }
  212. /* hard_start_xmit function for hostapd wlan#ap interfaces */
  213. int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  214. {
  215. struct hostap_interface *iface;
  216. local_info_t *local;
  217. struct hostap_skb_tx_data *meta;
  218. struct ieee80211_hdr *hdr;
  219. u16 fc;
  220. iface = netdev_priv(dev);
  221. local = iface->local;
  222. if (skb->len < 10) {
  223. printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
  224. "(len=%d)\n", dev->name, skb->len);
  225. kfree_skb(skb);
  226. return 0;
  227. }
  228. iface->stats.tx_packets++;
  229. iface->stats.tx_bytes += skb->len;
  230. meta = (struct hostap_skb_tx_data *) skb->cb;
  231. memset(meta, 0, sizeof(*meta));
  232. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  233. meta->iface = iface;
  234. if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
  235. hdr = (struct ieee80211_hdr *) skb->data;
  236. fc = le16_to_cpu(hdr->frame_ctl);
  237. if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
  238. WLAN_FC_GET_STYPE(fc) == IEEE80211_STYPE_DATA) {
  239. u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
  240. sizeof(rfc1042_header)];
  241. meta->ethertype = (pos[0] << 8) | pos[1];
  242. }
  243. }
  244. /* Send IEEE 802.11 encapsulated frame using the master radio device */
  245. skb->dev = local->dev;
  246. dev_queue_xmit(skb);
  247. return 0;
  248. }
  249. /* Called only from software IRQ */
  250. struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
  251. struct ieee80211_crypt_data *crypt)
  252. {
  253. struct hostap_interface *iface;
  254. local_info_t *local;
  255. struct ieee80211_hdr *hdr;
  256. u16 fc;
  257. int hdr_len, res;
  258. iface = netdev_priv(skb->dev);
  259. local = iface->local;
  260. if (skb->len < IEEE80211_DATA_HDR3_LEN) {
  261. kfree_skb(skb);
  262. return NULL;
  263. }
  264. if (local->tkip_countermeasures &&
  265. crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
  266. hdr = (struct ieee80211_hdr *) skb->data;
  267. if (net_ratelimit()) {
  268. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  269. "TX packet to " MACSTR "\n",
  270. local->dev->name, MAC2STR(hdr->addr1));
  271. }
  272. kfree_skb(skb);
  273. return NULL;
  274. }
  275. skb = skb_unshare(skb, GFP_ATOMIC);
  276. if (skb == NULL)
  277. return NULL;
  278. if ((skb_headroom(skb) < crypt->ops->extra_prefix_len ||
  279. skb_tailroom(skb) < crypt->ops->extra_postfix_len) &&
  280. pskb_expand_head(skb, crypt->ops->extra_prefix_len,
  281. crypt->ops->extra_postfix_len, GFP_ATOMIC)) {
  282. kfree_skb(skb);
  283. return NULL;
  284. }
  285. hdr = (struct ieee80211_hdr *) skb->data;
  286. fc = le16_to_cpu(hdr->frame_ctl);
  287. hdr_len = hostap_80211_get_hdrlen(fc);
  288. /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
  289. * call both MSDU and MPDU encryption functions from here. */
  290. atomic_inc(&crypt->refcnt);
  291. res = 0;
  292. if (crypt->ops->encrypt_msdu)
  293. res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
  294. if (res == 0 && crypt->ops->encrypt_mpdu)
  295. res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
  296. atomic_dec(&crypt->refcnt);
  297. if (res < 0) {
  298. kfree_skb(skb);
  299. return NULL;
  300. }
  301. return skb;
  302. }
  303. /* hard_start_xmit function for master radio interface wifi#.
  304. * AP processing (TX rate control, power save buffering, etc.).
  305. * Use hardware TX function to send the frame. */
  306. int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
  307. {
  308. struct hostap_interface *iface;
  309. local_info_t *local;
  310. int ret = 1;
  311. u16 fc;
  312. struct hostap_tx_data tx;
  313. ap_tx_ret tx_ret;
  314. struct hostap_skb_tx_data *meta;
  315. int no_encrypt = 0;
  316. struct ieee80211_hdr *hdr;
  317. iface = netdev_priv(dev);
  318. local = iface->local;
  319. tx.skb = skb;
  320. tx.sta_ptr = NULL;
  321. meta = (struct hostap_skb_tx_data *) skb->cb;
  322. if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
  323. printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
  324. "expected 0x%08x)\n",
  325. dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
  326. ret = 0;
  327. iface->stats.tx_dropped++;
  328. goto fail;
  329. }
  330. if (local->host_encrypt) {
  331. /* Set crypt to default algorithm and key; will be replaced in
  332. * AP code if STA has own alg/key */
  333. tx.crypt = local->crypt[local->tx_keyidx];
  334. tx.host_encrypt = 1;
  335. } else {
  336. tx.crypt = NULL;
  337. tx.host_encrypt = 0;
  338. }
  339. if (skb->len < 24) {
  340. printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
  341. "(len=%d)\n", dev->name, skb->len);
  342. ret = 0;
  343. iface->stats.tx_dropped++;
  344. goto fail;
  345. }
  346. /* FIX (?):
  347. * Wi-Fi 802.11b test plan suggests that AP should ignore power save
  348. * bit in authentication and (re)association frames and assume tha
  349. * STA remains awake for the response. */
  350. tx_ret = hostap_handle_sta_tx(local, &tx);
  351. skb = tx.skb;
  352. meta = (struct hostap_skb_tx_data *) skb->cb;
  353. hdr = (struct ieee80211_hdr *) skb->data;
  354. fc = le16_to_cpu(hdr->frame_ctl);
  355. switch (tx_ret) {
  356. case AP_TX_CONTINUE:
  357. break;
  358. case AP_TX_CONTINUE_NOT_AUTHORIZED:
  359. if (local->ieee_802_1x &&
  360. WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
  361. meta->ethertype != ETH_P_PAE &&
  362. !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
  363. printk(KERN_DEBUG "%s: dropped frame to unauthorized "
  364. "port (IEEE 802.1X): ethertype=0x%04x\n",
  365. dev->name, meta->ethertype);
  366. hostap_dump_tx_80211(dev->name, skb);
  367. ret = 0; /* drop packet */
  368. iface->stats.tx_dropped++;
  369. goto fail;
  370. }
  371. break;
  372. case AP_TX_DROP:
  373. ret = 0; /* drop packet */
  374. iface->stats.tx_dropped++;
  375. goto fail;
  376. case AP_TX_RETRY:
  377. goto fail;
  378. case AP_TX_BUFFERED:
  379. /* do not free skb here, it will be freed when the
  380. * buffered frame is sent/timed out */
  381. ret = 0;
  382. goto tx_exit;
  383. }
  384. /* Request TX callback if protocol version is 2 in 802.11 header;
  385. * this version 2 is a special case used between hostapd and kernel
  386. * driver */
  387. if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
  388. local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
  389. meta->tx_cb_idx = local->ap->tx_callback_idx;
  390. /* remove special version from the frame header */
  391. fc &= ~IEEE80211_FCTL_VERS;
  392. hdr->frame_ctl = cpu_to_le16(fc);
  393. }
  394. if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_DATA) {
  395. no_encrypt = 1;
  396. tx.crypt = NULL;
  397. }
  398. if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
  399. !(fc & IEEE80211_FCTL_VERS)) {
  400. no_encrypt = 1;
  401. PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
  402. "unencrypted EAPOL frame\n", dev->name);
  403. tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
  404. }
  405. if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
  406. tx.crypt = NULL;
  407. else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
  408. /* Add ISWEP flag both for firmware and host based encryption
  409. */
  410. fc |= IEEE80211_FCTL_PROTECTED;
  411. hdr->frame_ctl = cpu_to_le16(fc);
  412. } else if (local->drop_unencrypted &&
  413. WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
  414. meta->ethertype != ETH_P_PAE) {
  415. if (net_ratelimit()) {
  416. printk(KERN_DEBUG "%s: dropped unencrypted TX data "
  417. "frame (drop_unencrypted=1)\n", dev->name);
  418. }
  419. iface->stats.tx_dropped++;
  420. ret = 0;
  421. goto fail;
  422. }
  423. if (tx.crypt) {
  424. skb = hostap_tx_encrypt(skb, tx.crypt);
  425. if (skb == NULL) {
  426. printk(KERN_DEBUG "%s: TX - encryption failed\n",
  427. dev->name);
  428. ret = 0;
  429. goto fail;
  430. }
  431. meta = (struct hostap_skb_tx_data *) skb->cb;
  432. if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
  433. printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
  434. "expected 0x%08x) after hostap_tx_encrypt\n",
  435. dev->name, meta->magic,
  436. HOSTAP_SKB_TX_DATA_MAGIC);
  437. ret = 0;
  438. iface->stats.tx_dropped++;
  439. goto fail;
  440. }
  441. }
  442. if (local->func->tx == NULL || local->func->tx(skb, dev)) {
  443. ret = 0;
  444. iface->stats.tx_dropped++;
  445. } else {
  446. ret = 0;
  447. iface->stats.tx_packets++;
  448. iface->stats.tx_bytes += skb->len;
  449. }
  450. fail:
  451. if (!ret && skb)
  452. dev_kfree_skb(skb);
  453. tx_exit:
  454. if (tx.sta_ptr)
  455. hostap_handle_sta_release(tx.sta_ptr);
  456. return ret;
  457. }
  458. EXPORT_SYMBOL(hostap_dump_tx_80211);
  459. EXPORT_SYMBOL(hostap_tx_encrypt);
  460. EXPORT_SYMBOL(hostap_master_start_xmit);