rx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Written by:
  14. * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
  15. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  16. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/crc-ccitt.h>
  23. #include <asm/unaligned.h>
  24. #include <net/mac802154.h>
  25. #include <net/ieee802154_netdev.h>
  26. #include <net/nl802154.h>
  27. #include "ieee802154_i.h"
  28. static int ieee802154_deliver_skb(struct sk_buff *skb)
  29. {
  30. skb->ip_summed = CHECKSUM_UNNECESSARY;
  31. skb->protocol = htons(ETH_P_IEEE802154);
  32. return netif_receive_skb(skb);
  33. }
  34. static int
  35. ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
  36. struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  37. {
  38. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  39. __le16 span, sshort;
  40. int rc;
  41. pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
  42. span = wpan_dev->pan_id;
  43. sshort = wpan_dev->short_addr;
  44. switch (mac_cb(skb)->dest.mode) {
  45. case IEEE802154_ADDR_NONE:
  46. if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
  47. /* FIXME: check if we are PAN coordinator */
  48. skb->pkt_type = PACKET_OTHERHOST;
  49. else
  50. /* ACK comes with both addresses empty */
  51. skb->pkt_type = PACKET_HOST;
  52. break;
  53. case IEEE802154_ADDR_LONG:
  54. if (mac_cb(skb)->dest.pan_id != span &&
  55. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  56. skb->pkt_type = PACKET_OTHERHOST;
  57. else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
  58. skb->pkt_type = PACKET_HOST;
  59. else
  60. skb->pkt_type = PACKET_OTHERHOST;
  61. break;
  62. case IEEE802154_ADDR_SHORT:
  63. if (mac_cb(skb)->dest.pan_id != span &&
  64. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  65. skb->pkt_type = PACKET_OTHERHOST;
  66. else if (mac_cb(skb)->dest.short_addr == sshort)
  67. skb->pkt_type = PACKET_HOST;
  68. else if (mac_cb(skb)->dest.short_addr ==
  69. cpu_to_le16(IEEE802154_ADDR_BROADCAST))
  70. skb->pkt_type = PACKET_BROADCAST;
  71. else
  72. skb->pkt_type = PACKET_OTHERHOST;
  73. break;
  74. default:
  75. pr_debug("invalid dest mode\n");
  76. goto fail;
  77. }
  78. skb->dev = sdata->dev;
  79. /* TODO this should be moved after netif_receive_skb call, otherwise
  80. * wireshark will show a mac header with security fields and the
  81. * payload is already decrypted.
  82. */
  83. rc = mac802154_llsec_decrypt(&sdata->sec, skb);
  84. if (rc) {
  85. pr_debug("decryption failed: %i\n", rc);
  86. goto fail;
  87. }
  88. sdata->dev->stats.rx_packets++;
  89. sdata->dev->stats.rx_bytes += skb->len;
  90. switch (mac_cb(skb)->type) {
  91. case IEEE802154_FC_TYPE_BEACON:
  92. case IEEE802154_FC_TYPE_ACK:
  93. case IEEE802154_FC_TYPE_MAC_CMD:
  94. goto fail;
  95. case IEEE802154_FC_TYPE_DATA:
  96. return ieee802154_deliver_skb(skb);
  97. default:
  98. pr_warn_ratelimited("ieee802154: bad frame received "
  99. "(type = %d)\n", mac_cb(skb)->type);
  100. goto fail;
  101. }
  102. fail:
  103. kfree_skb(skb);
  104. return NET_RX_DROP;
  105. }
  106. static void
  107. ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
  108. {
  109. if (addr->mode == IEEE802154_ADDR_NONE)
  110. pr_debug("%s not present\n", name);
  111. pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
  112. if (addr->mode == IEEE802154_ADDR_SHORT) {
  113. pr_debug("%s is short: %04x\n", name,
  114. le16_to_cpu(addr->short_addr));
  115. } else {
  116. u64 hw = swab64((__force u64)addr->extended_addr);
  117. pr_debug("%s is hardware: %8phC\n", name, &hw);
  118. }
  119. }
  120. static int
  121. ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
  122. {
  123. int hlen;
  124. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  125. skb_reset_mac_header(skb);
  126. hlen = ieee802154_hdr_pull(skb, hdr);
  127. if (hlen < 0)
  128. return -EINVAL;
  129. skb->mac_len = hlen;
  130. pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
  131. hdr->seq);
  132. cb->type = hdr->fc.type;
  133. cb->ackreq = hdr->fc.ack_request;
  134. cb->secen = hdr->fc.security_enabled;
  135. ieee802154_print_addr("destination", &hdr->dest);
  136. ieee802154_print_addr("source", &hdr->source);
  137. cb->source = hdr->source;
  138. cb->dest = hdr->dest;
  139. if (hdr->fc.security_enabled) {
  140. u64 key;
  141. pr_debug("seclevel %i\n", hdr->sec.level);
  142. switch (hdr->sec.key_id_mode) {
  143. case IEEE802154_SCF_KEY_IMPLICIT:
  144. pr_debug("implicit key\n");
  145. break;
  146. case IEEE802154_SCF_KEY_INDEX:
  147. pr_debug("key %02x\n", hdr->sec.key_id);
  148. break;
  149. case IEEE802154_SCF_KEY_SHORT_INDEX:
  150. pr_debug("key %04x:%04x %02x\n",
  151. le32_to_cpu(hdr->sec.short_src) >> 16,
  152. le32_to_cpu(hdr->sec.short_src) & 0xffff,
  153. hdr->sec.key_id);
  154. break;
  155. case IEEE802154_SCF_KEY_HW_INDEX:
  156. key = swab64((__force u64)hdr->sec.extended_src);
  157. pr_debug("key source %8phC %02x\n", &key,
  158. hdr->sec.key_id);
  159. break;
  160. }
  161. }
  162. return 0;
  163. }
  164. static void
  165. __ieee802154_rx_handle_packet(struct ieee802154_local *local,
  166. struct sk_buff *skb)
  167. {
  168. int ret;
  169. struct ieee802154_sub_if_data *sdata;
  170. struct ieee802154_hdr hdr;
  171. ret = ieee802154_parse_frame_start(skb, &hdr);
  172. if (ret) {
  173. pr_debug("got invalid frame\n");
  174. kfree_skb(skb);
  175. return;
  176. }
  177. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  178. if (sdata->wpan_dev.iftype != NL802154_IFTYPE_NODE)
  179. continue;
  180. if (!ieee802154_sdata_running(sdata))
  181. continue;
  182. ieee802154_subif_frame(sdata, skb, &hdr);
  183. skb = NULL;
  184. break;
  185. }
  186. kfree_skb(skb);
  187. }
  188. static void
  189. ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
  190. {
  191. struct sk_buff *skb2;
  192. struct ieee802154_sub_if_data *sdata;
  193. skb_reset_mac_header(skb);
  194. skb->ip_summed = CHECKSUM_UNNECESSARY;
  195. skb->pkt_type = PACKET_OTHERHOST;
  196. skb->protocol = htons(ETH_P_IEEE802154);
  197. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  198. if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
  199. continue;
  200. if (!ieee802154_sdata_running(sdata))
  201. continue;
  202. skb2 = skb_clone(skb, GFP_ATOMIC);
  203. if (skb2) {
  204. skb2->dev = sdata->dev;
  205. ieee802154_deliver_skb(skb2);
  206. sdata->dev->stats.rx_packets++;
  207. sdata->dev->stats.rx_bytes += skb->len;
  208. }
  209. }
  210. }
  211. void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
  212. {
  213. u16 crc;
  214. WARN_ON_ONCE(softirq_count() == 0);
  215. if (local->suspended)
  216. goto drop;
  217. /* TODO: When a transceiver omits the checksum here, we
  218. * add an own calculated one. This is currently an ugly
  219. * solution because the monitor needs a crc here.
  220. */
  221. if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
  222. crc = crc_ccitt(0, skb->data, skb->len);
  223. put_unaligned_le16(crc, skb_put(skb, 2));
  224. }
  225. rcu_read_lock();
  226. ieee802154_monitors_rx(local, skb);
  227. /* Check if transceiver doesn't validate the checksum.
  228. * If not we validate the checksum here.
  229. */
  230. if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) {
  231. crc = crc_ccitt(0, skb->data, skb->len);
  232. if (crc) {
  233. rcu_read_unlock();
  234. goto drop;
  235. }
  236. }
  237. /* remove crc */
  238. skb_trim(skb, skb->len - 2);
  239. __ieee802154_rx_handle_packet(local, skb);
  240. rcu_read_unlock();
  241. return;
  242. drop:
  243. kfree_skb(skb);
  244. }
  245. void
  246. ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
  247. {
  248. struct ieee802154_local *local = hw_to_local(hw);
  249. mac_cb(skb)->lqi = lqi;
  250. skb->pkt_type = IEEE802154_RX_MSG;
  251. skb_queue_tail(&local->skb_queue, skb);
  252. tasklet_schedule(&local->tasklet);
  253. }
  254. EXPORT_SYMBOL(ieee802154_rx_irqsafe);