hostap_80211_tx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
  2. {
  3. struct hostap_ieee80211_hdr *hdr;
  4. u16 fc;
  5. hdr = (struct hostap_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_control);
  11. printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
  12. fc, WLAN_FC_GET_TYPE(fc), WLAN_FC_GET_STYPE(fc),
  13. fc & WLAN_FC_TODS ? " [ToDS]" : "",
  14. fc & WLAN_FC_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_ctrl));
  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 hostap_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 = (WLAN_FC_TYPE_DATA << 2) | (WLAN_FC_STYPE_DATA << 4);
  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 |= WLAN_FC_FROMDS | WLAN_FC_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 |= WLAN_FC_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 |= WLAN_FC_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 |= WLAN_FC_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_control = 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. meta->wds = use_wds;
  204. meta->ethertype = ethertype;
  205. meta->iface = iface;
  206. /* Send IEEE 802.11 encapsulated frame using the master radio device */
  207. skb->dev = local->dev;
  208. dev_queue_xmit(skb);
  209. return 0;
  210. }
  211. /* hard_start_xmit function for hostapd wlan#ap interfaces */
  212. int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  213. {
  214. struct hostap_interface *iface;
  215. local_info_t *local;
  216. struct hostap_skb_tx_data *meta;
  217. struct hostap_ieee80211_hdr *hdr;
  218. u16 fc;
  219. iface = netdev_priv(dev);
  220. local = iface->local;
  221. if (skb->len < 10) {
  222. printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
  223. "(len=%d)\n", dev->name, skb->len);
  224. kfree_skb(skb);
  225. return 0;
  226. }
  227. iface->stats.tx_packets++;
  228. iface->stats.tx_bytes += skb->len;
  229. meta = (struct hostap_skb_tx_data *) skb->cb;
  230. memset(meta, 0, sizeof(*meta));
  231. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  232. meta->iface = iface;
  233. if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
  234. hdr = (struct hostap_ieee80211_hdr *) skb->data;
  235. fc = le16_to_cpu(hdr->frame_control);
  236. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  237. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_DATA) {
  238. u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
  239. sizeof(rfc1042_header)];
  240. meta->ethertype = (pos[0] << 8) | pos[1];
  241. }
  242. }
  243. /* Send IEEE 802.11 encapsulated frame using the master radio device */
  244. skb->dev = local->dev;
  245. dev_queue_xmit(skb);
  246. return 0;
  247. }
  248. /* Called only from software IRQ */
  249. struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
  250. struct prism2_crypt_data *crypt)
  251. {
  252. struct hostap_interface *iface;
  253. local_info_t *local;
  254. struct hostap_ieee80211_hdr *hdr;
  255. u16 fc;
  256. int hdr_len, res;
  257. iface = netdev_priv(skb->dev);
  258. local = iface->local;
  259. if (skb->len < IEEE80211_DATA_HDR3_LEN) {
  260. kfree_skb(skb);
  261. return NULL;
  262. }
  263. if (local->tkip_countermeasures &&
  264. crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
  265. hdr = (struct hostap_ieee80211_hdr *) skb->data;
  266. if (net_ratelimit()) {
  267. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  268. "TX packet to " MACSTR "\n",
  269. local->dev->name, MAC2STR(hdr->addr1));
  270. }
  271. kfree_skb(skb);
  272. return NULL;
  273. }
  274. skb = skb_unshare(skb, GFP_ATOMIC);
  275. if (skb == NULL)
  276. return NULL;
  277. if ((skb_headroom(skb) < crypt->ops->extra_prefix_len ||
  278. skb_tailroom(skb) < crypt->ops->extra_postfix_len) &&
  279. pskb_expand_head(skb, crypt->ops->extra_prefix_len,
  280. crypt->ops->extra_postfix_len, GFP_ATOMIC)) {
  281. kfree_skb(skb);
  282. return NULL;
  283. }
  284. hdr = (struct hostap_ieee80211_hdr *) skb->data;
  285. fc = le16_to_cpu(hdr->frame_control);
  286. hdr_len = hostap_80211_get_hdrlen(fc);
  287. /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
  288. * call both MSDU and MPDU encryption functions from here. */
  289. atomic_inc(&crypt->refcnt);
  290. res = 0;
  291. if (crypt->ops->encrypt_msdu)
  292. res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
  293. if (res == 0 && crypt->ops->encrypt_mpdu)
  294. res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
  295. atomic_dec(&crypt->refcnt);
  296. if (res < 0) {
  297. kfree_skb(skb);
  298. return NULL;
  299. }
  300. return skb;
  301. }
  302. /* hard_start_xmit function for master radio interface wifi#.
  303. * AP processing (TX rate control, power save buffering, etc.).
  304. * Use hardware TX function to send the frame. */
  305. int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
  306. {
  307. struct hostap_interface *iface;
  308. local_info_t *local;
  309. int ret = 1;
  310. u16 fc;
  311. struct hostap_tx_data tx;
  312. ap_tx_ret tx_ret;
  313. struct hostap_skb_tx_data *meta;
  314. int no_encrypt = 0;
  315. struct hostap_ieee80211_hdr *hdr;
  316. iface = netdev_priv(dev);
  317. local = iface->local;
  318. tx.skb = skb;
  319. tx.sta_ptr = NULL;
  320. meta = (struct hostap_skb_tx_data *) skb->cb;
  321. if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
  322. printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
  323. "expected 0x%08x)\n",
  324. dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
  325. ret = 0;
  326. iface->stats.tx_dropped++;
  327. goto fail;
  328. }
  329. if (local->host_encrypt) {
  330. /* Set crypt to default algorithm and key; will be replaced in
  331. * AP code if STA has own alg/key */
  332. tx.crypt = local->crypt[local->tx_keyidx];
  333. tx.host_encrypt = 1;
  334. } else {
  335. tx.crypt = NULL;
  336. tx.host_encrypt = 0;
  337. }
  338. if (skb->len < 24) {
  339. printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
  340. "(len=%d)\n", dev->name, skb->len);
  341. ret = 0;
  342. iface->stats.tx_dropped++;
  343. goto fail;
  344. }
  345. /* FIX (?):
  346. * Wi-Fi 802.11b test plan suggests that AP should ignore power save
  347. * bit in authentication and (re)association frames and assume tha
  348. * STA remains awake for the response. */
  349. tx_ret = hostap_handle_sta_tx(local, &tx);
  350. skb = tx.skb;
  351. meta = (struct hostap_skb_tx_data *) skb->cb;
  352. hdr = (struct hostap_ieee80211_hdr *) skb->data;
  353. fc = le16_to_cpu(hdr->frame_control);
  354. switch (tx_ret) {
  355. case AP_TX_CONTINUE:
  356. break;
  357. case AP_TX_CONTINUE_NOT_AUTHORIZED:
  358. if (local->ieee_802_1x &&
  359. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  360. meta->ethertype != ETH_P_PAE && !meta->wds) {
  361. printk(KERN_DEBUG "%s: dropped frame to unauthorized "
  362. "port (IEEE 802.1X): ethertype=0x%04x\n",
  363. dev->name, meta->ethertype);
  364. hostap_dump_tx_80211(dev->name, skb);
  365. ret = 0; /* drop packet */
  366. iface->stats.tx_dropped++;
  367. goto fail;
  368. }
  369. break;
  370. case AP_TX_DROP:
  371. ret = 0; /* drop packet */
  372. iface->stats.tx_dropped++;
  373. goto fail;
  374. case AP_TX_RETRY:
  375. goto fail;
  376. case AP_TX_BUFFERED:
  377. /* do not free skb here, it will be freed when the
  378. * buffered frame is sent/timed out */
  379. ret = 0;
  380. goto tx_exit;
  381. }
  382. /* Request TX callback if protocol version is 2 in 802.11 header;
  383. * this version 2 is a special case used between hostapd and kernel
  384. * driver */
  385. if (((fc & WLAN_FC_PVER) == BIT(1)) &&
  386. local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
  387. meta->tx_cb_idx = local->ap->tx_callback_idx;
  388. /* remove special version from the frame header */
  389. fc &= ~WLAN_FC_PVER;
  390. hdr->frame_control = cpu_to_le16(fc);
  391. }
  392. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_DATA) {
  393. no_encrypt = 1;
  394. tx.crypt = NULL;
  395. }
  396. if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
  397. !(fc & WLAN_FC_ISWEP)) {
  398. no_encrypt = 1;
  399. PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
  400. "unencrypted EAPOL frame\n", dev->name);
  401. tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
  402. }
  403. if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
  404. tx.crypt = NULL;
  405. else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
  406. /* Add ISWEP flag both for firmware and host based encryption
  407. */
  408. fc |= WLAN_FC_ISWEP;
  409. hdr->frame_control = cpu_to_le16(fc);
  410. } else if (local->drop_unencrypted &&
  411. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  412. meta->ethertype != ETH_P_PAE) {
  413. if (net_ratelimit()) {
  414. printk(KERN_DEBUG "%s: dropped unencrypted TX data "
  415. "frame (drop_unencrypted=1)\n", dev->name);
  416. }
  417. iface->stats.tx_dropped++;
  418. ret = 0;
  419. goto fail;
  420. }
  421. if (tx.crypt) {
  422. skb = hostap_tx_encrypt(skb, tx.crypt);
  423. if (skb == NULL) {
  424. printk(KERN_DEBUG "%s: TX - encryption failed\n",
  425. dev->name);
  426. ret = 0;
  427. goto fail;
  428. }
  429. meta = (struct hostap_skb_tx_data *) skb->cb;
  430. if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
  431. printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
  432. "expected 0x%08x) after hostap_tx_encrypt\n",
  433. dev->name, meta->magic,
  434. HOSTAP_SKB_TX_DATA_MAGIC);
  435. ret = 0;
  436. iface->stats.tx_dropped++;
  437. goto fail;
  438. }
  439. }
  440. if (local->func->tx == NULL || local->func->tx(skb, dev)) {
  441. ret = 0;
  442. iface->stats.tx_dropped++;
  443. } else {
  444. ret = 0;
  445. iface->stats.tx_packets++;
  446. iface->stats.tx_bytes += skb->len;
  447. }
  448. fail:
  449. if (!ret && skb)
  450. dev_kfree_skb(skb);
  451. tx_exit:
  452. if (tx.sta_ptr)
  453. hostap_handle_sta_release(tx.sta_ptr);
  454. return ret;
  455. }
  456. EXPORT_SYMBOL(hostap_dump_tx_80211);
  457. EXPORT_SYMBOL(hostap_tx_encrypt);
  458. EXPORT_SYMBOL(hostap_master_start_xmit);