rx.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This file contains the handling of RX in wlan driver.
  3. */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/etherdevice.h>
  6. #include <linux/hardirq.h>
  7. #include <linux/slab.h>
  8. #include <linux/types.h>
  9. #include <linux/export.h>
  10. #include <net/cfg80211.h>
  11. #include "defs.h"
  12. #include "host.h"
  13. #include "radiotap.h"
  14. #include "decl.h"
  15. #include "dev.h"
  16. #include "mesh.h"
  17. struct eth803hdr {
  18. u8 dest_addr[6];
  19. u8 src_addr[6];
  20. u16 h803_len;
  21. } __packed;
  22. struct rfc1042hdr {
  23. u8 llc_dsap;
  24. u8 llc_ssap;
  25. u8 llc_ctrl;
  26. u8 snap_oui[3];
  27. u16 snap_type;
  28. } __packed;
  29. struct rxpackethdr {
  30. struct eth803hdr eth803_hdr;
  31. struct rfc1042hdr rfc1042_hdr;
  32. } __packed;
  33. struct rx80211packethdr {
  34. struct rxpd rx_pd;
  35. void *eth80211_hdr;
  36. } __packed;
  37. static int process_rxed_802_11_packet(struct lbs_private *priv,
  38. struct sk_buff *skb);
  39. /**
  40. * lbs_process_rxed_packet - processes received packet and forwards it
  41. * to kernel/upper layer
  42. *
  43. * @priv: A pointer to &struct lbs_private
  44. * @skb: A pointer to skb which includes the received packet
  45. * returns: 0 or -1
  46. */
  47. int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
  48. {
  49. int ret = 0;
  50. struct net_device *dev = priv->dev;
  51. struct rxpackethdr *p_rx_pkt;
  52. struct rxpd *p_rx_pd;
  53. int hdrchop;
  54. struct ethhdr *p_ethhdr;
  55. static const u8 rfc1042_eth_hdr[] = {
  56. 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
  57. };
  58. BUG_ON(!skb);
  59. skb->ip_summed = CHECKSUM_NONE;
  60. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
  61. ret = process_rxed_802_11_packet(priv, skb);
  62. goto done;
  63. }
  64. p_rx_pd = (struct rxpd *) skb->data;
  65. p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
  66. le32_to_cpu(p_rx_pd->pkt_ptr));
  67. dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
  68. lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
  69. min_t(unsigned int, skb->len, 100));
  70. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  71. lbs_deb_rx("rx err: frame received with bad length\n");
  72. dev->stats.rx_length_errors++;
  73. ret = -EINVAL;
  74. dev_kfree_skb(skb);
  75. goto done;
  76. }
  77. lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
  78. skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
  79. skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
  80. lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
  81. sizeof(p_rx_pkt->eth803_hdr.dest_addr));
  82. lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
  83. sizeof(p_rx_pkt->eth803_hdr.src_addr));
  84. if (memcmp(&p_rx_pkt->rfc1042_hdr,
  85. rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
  86. /*
  87. * Replace the 803 header and rfc1042 header (llc/snap) with an
  88. * EthernetII header, keep the src/dst and snap_type (ethertype)
  89. *
  90. * The firmware only passes up SNAP frames converting
  91. * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
  92. *
  93. * To create the Ethernet II, just move the src, dst address right
  94. * before the snap_type.
  95. */
  96. p_ethhdr = (struct ethhdr *)
  97. ((u8 *) &p_rx_pkt->eth803_hdr
  98. + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
  99. - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
  100. - sizeof(p_rx_pkt->eth803_hdr.src_addr)
  101. - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
  102. memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
  103. sizeof(p_ethhdr->h_source));
  104. memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
  105. sizeof(p_ethhdr->h_dest));
  106. /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
  107. * that was removed
  108. */
  109. hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
  110. } else {
  111. lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
  112. (u8 *) &p_rx_pkt->rfc1042_hdr,
  113. sizeof(p_rx_pkt->rfc1042_hdr));
  114. /* Chop off the rxpd */
  115. hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
  116. }
  117. /* Chop off the leading header bytes so the skb points to the start of
  118. * either the reconstructed EthII frame or the 802.2/llc/snap frame
  119. */
  120. skb_pull(skb, hdrchop);
  121. priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
  122. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  123. dev->stats.rx_bytes += skb->len;
  124. dev->stats.rx_packets++;
  125. skb->protocol = eth_type_trans(skb, dev);
  126. if (in_interrupt())
  127. netif_rx(skb);
  128. else
  129. netif_rx_ni(skb);
  130. ret = 0;
  131. done:
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
  135. /**
  136. * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
  137. * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
  138. *
  139. * @rate: Input rate
  140. * returns: Output Rate (0 if invalid)
  141. */
  142. static u8 convert_mv_rate_to_radiotap(u8 rate)
  143. {
  144. switch (rate) {
  145. case 0: /* 1 Mbps */
  146. return 2;
  147. case 1: /* 2 Mbps */
  148. return 4;
  149. case 2: /* 5.5 Mbps */
  150. return 11;
  151. case 3: /* 11 Mbps */
  152. return 22;
  153. /* case 4: reserved */
  154. case 5: /* 6 Mbps */
  155. return 12;
  156. case 6: /* 9 Mbps */
  157. return 18;
  158. case 7: /* 12 Mbps */
  159. return 24;
  160. case 8: /* 18 Mbps */
  161. return 36;
  162. case 9: /* 24 Mbps */
  163. return 48;
  164. case 10: /* 36 Mbps */
  165. return 72;
  166. case 11: /* 48 Mbps */
  167. return 96;
  168. case 12: /* 54 Mbps */
  169. return 108;
  170. }
  171. pr_alert("Invalid Marvell WLAN rate %i\n", rate);
  172. return 0;
  173. }
  174. /**
  175. * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
  176. * it to kernel/upper layer
  177. *
  178. * @priv: A pointer to &struct lbs_private
  179. * @skb: A pointer to skb which includes the received packet
  180. * returns: 0 or -1
  181. */
  182. static int process_rxed_802_11_packet(struct lbs_private *priv,
  183. struct sk_buff *skb)
  184. {
  185. int ret = 0;
  186. struct net_device *dev = priv->dev;
  187. struct rx80211packethdr *p_rx_pkt;
  188. struct rxpd *prxpd;
  189. struct rx_radiotap_hdr radiotap_hdr;
  190. struct rx_radiotap_hdr *pradiotap_hdr;
  191. p_rx_pkt = (struct rx80211packethdr *) skb->data;
  192. prxpd = &p_rx_pkt->rx_pd;
  193. /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
  194. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  195. lbs_deb_rx("rx err: frame received with bad length\n");
  196. dev->stats.rx_length_errors++;
  197. ret = -EINVAL;
  198. kfree_skb(skb);
  199. goto done;
  200. }
  201. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  202. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  203. /* create the exported radio header */
  204. /* radiotap header */
  205. memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
  206. /* XXX must check radiotap_hdr.hdr.it_pad for pad */
  207. radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
  208. radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
  209. radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
  210. /* XXX must check no carryout */
  211. radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
  212. /* chop the rxpd */
  213. skb_pull(skb, sizeof(struct rxpd));
  214. /* add space for the new radio header */
  215. if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
  216. pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
  217. netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
  218. ret = -ENOMEM;
  219. kfree_skb(skb);
  220. goto done;
  221. }
  222. pradiotap_hdr = skb_push(skb, sizeof(struct rx_radiotap_hdr));
  223. memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
  224. priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
  225. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  226. dev->stats.rx_bytes += skb->len;
  227. dev->stats.rx_packets++;
  228. skb->protocol = eth_type_trans(skb, priv->dev);
  229. if (in_interrupt())
  230. netif_rx(skb);
  231. else
  232. netif_rx_ni(skb);
  233. ret = 0;
  234. done:
  235. return ret;
  236. }