rx.c 7.4 KB

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