rx.c 7.3 KB

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