rx.c 7.1 KB

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