rx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/rtnetlink.h>
  27. #include <linux/nl802154.h>
  28. #include "ieee802154_i.h"
  29. static int ieee802154_deliver_skb(struct sk_buff *skb)
  30. {
  31. skb->ip_summed = CHECKSUM_UNNECESSARY;
  32. skb->protocol = htons(ETH_P_IEEE802154);
  33. return netif_receive_skb(skb);
  34. }
  35. static int
  36. ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
  37. struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  38. {
  39. __le16 span, sshort;
  40. int rc;
  41. pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
  42. spin_lock_bh(&sdata->mib_lock);
  43. span = sdata->pan_id;
  44. sshort = sdata->short_addr;
  45. switch (mac_cb(skb)->dest.mode) {
  46. case IEEE802154_ADDR_NONE:
  47. if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
  48. /* FIXME: check if we are PAN coordinator */
  49. skb->pkt_type = PACKET_OTHERHOST;
  50. else
  51. /* ACK comes with both addresses empty */
  52. skb->pkt_type = PACKET_HOST;
  53. break;
  54. case IEEE802154_ADDR_LONG:
  55. if (mac_cb(skb)->dest.pan_id != span &&
  56. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  57. skb->pkt_type = PACKET_OTHERHOST;
  58. else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
  59. skb->pkt_type = PACKET_HOST;
  60. else
  61. skb->pkt_type = PACKET_OTHERHOST;
  62. break;
  63. case IEEE802154_ADDR_SHORT:
  64. if (mac_cb(skb)->dest.pan_id != span &&
  65. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  66. skb->pkt_type = PACKET_OTHERHOST;
  67. else if (mac_cb(skb)->dest.short_addr == sshort)
  68. skb->pkt_type = PACKET_HOST;
  69. else if (mac_cb(skb)->dest.short_addr ==
  70. cpu_to_le16(IEEE802154_ADDR_BROADCAST))
  71. skb->pkt_type = PACKET_BROADCAST;
  72. else
  73. skb->pkt_type = PACKET_OTHERHOST;
  74. break;
  75. default:
  76. spin_unlock_bh(&sdata->mib_lock);
  77. pr_debug("invalid dest mode\n");
  78. kfree_skb(skb);
  79. return NET_RX_DROP;
  80. }
  81. spin_unlock_bh(&sdata->mib_lock);
  82. skb->dev = sdata->dev;
  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_DATA:
  92. return ieee802154_deliver_skb(skb);
  93. default:
  94. pr_warn("ieee802154: bad frame received (type = %d)\n",
  95. mac_cb(skb)->type);
  96. goto fail;
  97. }
  98. fail:
  99. kfree_skb(skb);
  100. return NET_RX_DROP;
  101. }
  102. static void
  103. ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
  104. {
  105. if (addr->mode == IEEE802154_ADDR_NONE)
  106. pr_debug("%s not present\n", name);
  107. pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
  108. if (addr->mode == IEEE802154_ADDR_SHORT) {
  109. pr_debug("%s is short: %04x\n", name,
  110. le16_to_cpu(addr->short_addr));
  111. } else {
  112. u64 hw = swab64((__force u64)addr->extended_addr);
  113. pr_debug("%s is hardware: %8phC\n", name, &hw);
  114. }
  115. }
  116. static int
  117. ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
  118. {
  119. int hlen;
  120. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  121. skb_reset_mac_header(skb);
  122. hlen = ieee802154_hdr_pull(skb, hdr);
  123. if (hlen < 0)
  124. return -EINVAL;
  125. skb->mac_len = hlen;
  126. pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
  127. hdr->seq);
  128. cb->type = hdr->fc.type;
  129. cb->ackreq = hdr->fc.ack_request;
  130. cb->secen = hdr->fc.security_enabled;
  131. ieee802154_print_addr("destination", &hdr->dest);
  132. ieee802154_print_addr("source", &hdr->source);
  133. cb->source = hdr->source;
  134. cb->dest = hdr->dest;
  135. if (hdr->fc.security_enabled) {
  136. u64 key;
  137. pr_debug("seclevel %i\n", hdr->sec.level);
  138. switch (hdr->sec.key_id_mode) {
  139. case IEEE802154_SCF_KEY_IMPLICIT:
  140. pr_debug("implicit key\n");
  141. break;
  142. case IEEE802154_SCF_KEY_INDEX:
  143. pr_debug("key %02x\n", hdr->sec.key_id);
  144. break;
  145. case IEEE802154_SCF_KEY_SHORT_INDEX:
  146. pr_debug("key %04x:%04x %02x\n",
  147. le32_to_cpu(hdr->sec.short_src) >> 16,
  148. le32_to_cpu(hdr->sec.short_src) & 0xffff,
  149. hdr->sec.key_id);
  150. break;
  151. case IEEE802154_SCF_KEY_HW_INDEX:
  152. key = swab64((__force u64)hdr->sec.extended_src);
  153. pr_debug("key source %8phC %02x\n", &key,
  154. hdr->sec.key_id);
  155. break;
  156. }
  157. }
  158. return 0;
  159. }
  160. static void
  161. __ieee802154_rx_handle_packet(struct ieee802154_local *local,
  162. struct sk_buff *skb)
  163. {
  164. int ret;
  165. struct ieee802154_sub_if_data *sdata;
  166. struct ieee802154_hdr hdr;
  167. ret = ieee802154_parse_frame_start(skb, &hdr);
  168. if (ret) {
  169. pr_debug("got invalid frame\n");
  170. kfree_skb(skb);
  171. return;
  172. }
  173. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  174. if (sdata->type != IEEE802154_DEV_WPAN ||
  175. !netif_running(sdata->dev))
  176. continue;
  177. ieee802154_subif_frame(sdata, skb, &hdr);
  178. skb = NULL;
  179. break;
  180. }
  181. if (skb)
  182. kfree_skb(skb);
  183. }
  184. static void
  185. ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
  186. {
  187. struct sk_buff *skb2;
  188. struct ieee802154_sub_if_data *sdata;
  189. skb_reset_mac_header(skb);
  190. skb->ip_summed = CHECKSUM_UNNECESSARY;
  191. skb->pkt_type = PACKET_OTHERHOST;
  192. skb->protocol = htons(ETH_P_IEEE802154);
  193. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  194. if (sdata->type != IEEE802154_DEV_MONITOR)
  195. continue;
  196. if (!ieee802154_sdata_running(sdata))
  197. continue;
  198. skb2 = skb_clone(skb, GFP_ATOMIC);
  199. if (skb2) {
  200. skb2->dev = sdata->dev;
  201. ieee802154_deliver_skb(skb2);
  202. sdata->dev->stats.rx_packets++;
  203. sdata->dev->stats.rx_bytes += skb->len;
  204. }
  205. }
  206. }
  207. void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb)
  208. {
  209. struct ieee802154_local *local = hw_to_local(hw);
  210. u16 crc;
  211. WARN_ON_ONCE(softirq_count() == 0);
  212. /* TODO: When a transceiver omits the checksum here, we
  213. * add an own calculated one. This is currently an ugly
  214. * solution because the monitor needs a crc here.
  215. */
  216. if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
  217. crc = crc_ccitt(0, skb->data, skb->len);
  218. put_unaligned_le16(crc, skb_put(skb, 2));
  219. }
  220. rcu_read_lock();
  221. ieee802154_monitors_rx(local, skb);
  222. /* Check if transceiver doesn't validate the checksum.
  223. * If not we validate the checksum here.
  224. */
  225. if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) {
  226. crc = crc_ccitt(0, skb->data, skb->len);
  227. if (crc) {
  228. rcu_read_unlock();
  229. kfree_skb(skb);
  230. return;
  231. }
  232. }
  233. /* remove crc */
  234. skb_trim(skb, skb->len - 2);
  235. __ieee802154_rx_handle_packet(local, skb);
  236. rcu_read_unlock();
  237. }
  238. EXPORT_SYMBOL(ieee802154_rx);
  239. void
  240. ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
  241. {
  242. struct ieee802154_local *local = hw_to_local(hw);
  243. mac_cb(skb)->lqi = lqi;
  244. skb->pkt_type = IEEE802154_RX_MSG;
  245. skb_queue_tail(&local->skb_queue, skb);
  246. tasklet_schedule(&local->tasklet);
  247. }
  248. EXPORT_SYMBOL(ieee802154_rx_irqsafe);