rx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/if_arp.h>
  3. #include <net/6lowpan.h>
  4. #include <net/mac802154.h>
  5. #include <net/ieee802154_netdev.h>
  6. #include "6lowpan_i.h"
  7. #define LOWPAN_DISPATCH_FIRST 0xc0
  8. #define LOWPAN_DISPATCH_FRAG_MASK 0xf8
  9. #define LOWPAN_DISPATCH_NALP 0x00
  10. #define LOWPAN_DISPATCH_ESC 0x40
  11. #define LOWPAN_DISPATCH_HC1 0x42
  12. #define LOWPAN_DISPATCH_DFF 0x43
  13. #define LOWPAN_DISPATCH_BC0 0x50
  14. #define LOWPAN_DISPATCH_MESH 0x80
  15. static int lowpan_give_skb_to_device(struct sk_buff *skb)
  16. {
  17. skb->protocol = htons(ETH_P_IPV6);
  18. skb->dev->stats.rx_packets++;
  19. skb->dev->stats.rx_bytes += skb->len;
  20. return netif_rx(skb);
  21. }
  22. static int lowpan_rx_handlers_result(struct sk_buff *skb, lowpan_rx_result res)
  23. {
  24. switch (res) {
  25. case RX_CONTINUE:
  26. /* nobody cared about this packet */
  27. net_warn_ratelimited("%s: received unknown dispatch\n",
  28. __func__);
  29. fallthrough;
  30. case RX_DROP_UNUSABLE:
  31. kfree_skb(skb);
  32. fallthrough;
  33. case RX_DROP:
  34. return NET_RX_DROP;
  35. case RX_QUEUED:
  36. return lowpan_give_skb_to_device(skb);
  37. default:
  38. break;
  39. }
  40. return NET_RX_DROP;
  41. }
  42. static inline bool lowpan_is_frag1(u8 dispatch)
  43. {
  44. return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAG1;
  45. }
  46. static inline bool lowpan_is_fragn(u8 dispatch)
  47. {
  48. return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAGN;
  49. }
  50. static lowpan_rx_result lowpan_rx_h_frag(struct sk_buff *skb)
  51. {
  52. int ret;
  53. if (!(lowpan_is_frag1(*skb_network_header(skb)) ||
  54. lowpan_is_fragn(*skb_network_header(skb))))
  55. return RX_CONTINUE;
  56. ret = lowpan_frag_rcv(skb, *skb_network_header(skb) &
  57. LOWPAN_DISPATCH_FRAG_MASK);
  58. if (ret == 1)
  59. return RX_QUEUED;
  60. /* Packet is freed by lowpan_frag_rcv on error or put into the frag
  61. * bucket.
  62. */
  63. return RX_DROP;
  64. }
  65. int lowpan_iphc_decompress(struct sk_buff *skb)
  66. {
  67. struct ieee802154_hdr hdr;
  68. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
  69. return -EINVAL;
  70. return lowpan_header_decompress(skb, skb->dev, &hdr.dest, &hdr.source);
  71. }
  72. static lowpan_rx_result lowpan_rx_h_iphc(struct sk_buff *skb)
  73. {
  74. int ret;
  75. if (!lowpan_is_iphc(*skb_network_header(skb)))
  76. return RX_CONTINUE;
  77. /* Setting datagram_offset to zero indicates non frag handling
  78. * while doing lowpan_header_decompress.
  79. */
  80. lowpan_802154_cb(skb)->d_size = 0;
  81. ret = lowpan_iphc_decompress(skb);
  82. if (ret < 0)
  83. return RX_DROP_UNUSABLE;
  84. return RX_QUEUED;
  85. }
  86. lowpan_rx_result lowpan_rx_h_ipv6(struct sk_buff *skb)
  87. {
  88. if (!lowpan_is_ipv6(*skb_network_header(skb)))
  89. return RX_CONTINUE;
  90. /* Pull off the 1-byte of 6lowpan header. */
  91. skb_pull(skb, 1);
  92. return RX_QUEUED;
  93. }
  94. static inline bool lowpan_is_esc(u8 dispatch)
  95. {
  96. return dispatch == LOWPAN_DISPATCH_ESC;
  97. }
  98. static lowpan_rx_result lowpan_rx_h_esc(struct sk_buff *skb)
  99. {
  100. if (!lowpan_is_esc(*skb_network_header(skb)))
  101. return RX_CONTINUE;
  102. net_warn_ratelimited("%s: %s\n", skb->dev->name,
  103. "6LoWPAN ESC not supported\n");
  104. return RX_DROP_UNUSABLE;
  105. }
  106. static inline bool lowpan_is_hc1(u8 dispatch)
  107. {
  108. return dispatch == LOWPAN_DISPATCH_HC1;
  109. }
  110. static lowpan_rx_result lowpan_rx_h_hc1(struct sk_buff *skb)
  111. {
  112. if (!lowpan_is_hc1(*skb_network_header(skb)))
  113. return RX_CONTINUE;
  114. net_warn_ratelimited("%s: %s\n", skb->dev->name,
  115. "6LoWPAN HC1 not supported\n");
  116. return RX_DROP_UNUSABLE;
  117. }
  118. static inline bool lowpan_is_dff(u8 dispatch)
  119. {
  120. return dispatch == LOWPAN_DISPATCH_DFF;
  121. }
  122. static lowpan_rx_result lowpan_rx_h_dff(struct sk_buff *skb)
  123. {
  124. if (!lowpan_is_dff(*skb_network_header(skb)))
  125. return RX_CONTINUE;
  126. net_warn_ratelimited("%s: %s\n", skb->dev->name,
  127. "6LoWPAN DFF not supported\n");
  128. return RX_DROP_UNUSABLE;
  129. }
  130. static inline bool lowpan_is_bc0(u8 dispatch)
  131. {
  132. return dispatch == LOWPAN_DISPATCH_BC0;
  133. }
  134. static lowpan_rx_result lowpan_rx_h_bc0(struct sk_buff *skb)
  135. {
  136. if (!lowpan_is_bc0(*skb_network_header(skb)))
  137. return RX_CONTINUE;
  138. net_warn_ratelimited("%s: %s\n", skb->dev->name,
  139. "6LoWPAN BC0 not supported\n");
  140. return RX_DROP_UNUSABLE;
  141. }
  142. static inline bool lowpan_is_mesh(u8 dispatch)
  143. {
  144. return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_MESH;
  145. }
  146. static lowpan_rx_result lowpan_rx_h_mesh(struct sk_buff *skb)
  147. {
  148. if (!lowpan_is_mesh(*skb_network_header(skb)))
  149. return RX_CONTINUE;
  150. net_warn_ratelimited("%s: %s\n", skb->dev->name,
  151. "6LoWPAN MESH not supported\n");
  152. return RX_DROP_UNUSABLE;
  153. }
  154. static int lowpan_invoke_rx_handlers(struct sk_buff *skb)
  155. {
  156. lowpan_rx_result res;
  157. #define CALL_RXH(rxh) \
  158. do { \
  159. res = rxh(skb); \
  160. if (res != RX_CONTINUE) \
  161. goto rxh_next; \
  162. } while (0)
  163. /* likely at first */
  164. CALL_RXH(lowpan_rx_h_iphc);
  165. CALL_RXH(lowpan_rx_h_frag);
  166. CALL_RXH(lowpan_rx_h_ipv6);
  167. CALL_RXH(lowpan_rx_h_esc);
  168. CALL_RXH(lowpan_rx_h_hc1);
  169. CALL_RXH(lowpan_rx_h_dff);
  170. CALL_RXH(lowpan_rx_h_bc0);
  171. CALL_RXH(lowpan_rx_h_mesh);
  172. rxh_next:
  173. return lowpan_rx_handlers_result(skb, res);
  174. #undef CALL_RXH
  175. }
  176. static inline bool lowpan_is_nalp(u8 dispatch)
  177. {
  178. return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_NALP;
  179. }
  180. /* Lookup for reserved dispatch values at:
  181. * https://www.iana.org/assignments/_6lowpan-parameters/_6lowpan-parameters.xhtml#_6lowpan-parameters-1
  182. *
  183. * Last Updated: 2015-01-22
  184. */
  185. static inline bool lowpan_is_reserved(u8 dispatch)
  186. {
  187. return ((dispatch >= 0x44 && dispatch <= 0x4F) ||
  188. (dispatch >= 0x51 && dispatch <= 0x5F) ||
  189. (dispatch >= 0xc8 && dispatch <= 0xdf) ||
  190. dispatch >= 0xe8);
  191. }
  192. /* lowpan_rx_h_check checks on generic 6LoWPAN requirements
  193. * in MAC and 6LoWPAN header.
  194. *
  195. * Don't manipulate the skb here, it could be shared buffer.
  196. */
  197. static inline bool lowpan_rx_h_check(struct sk_buff *skb)
  198. {
  199. __le16 fc = ieee802154_get_fc_from_skb(skb);
  200. /* check on ieee802154 conform 6LoWPAN header */
  201. if (!ieee802154_is_data(fc) ||
  202. !ieee802154_skb_is_intra_pan_addressing(fc, skb))
  203. return false;
  204. /* check if we can dereference the dispatch */
  205. if (unlikely(!skb->len))
  206. return false;
  207. if (lowpan_is_nalp(*skb_network_header(skb)) ||
  208. lowpan_is_reserved(*skb_network_header(skb)))
  209. return false;
  210. return true;
  211. }
  212. static int lowpan_rcv(struct sk_buff *skb, struct net_device *wdev,
  213. struct packet_type *pt, struct net_device *orig_wdev)
  214. {
  215. struct net_device *ldev;
  216. if (wdev->type != ARPHRD_IEEE802154 ||
  217. skb->pkt_type == PACKET_OTHERHOST ||
  218. !lowpan_rx_h_check(skb))
  219. goto drop;
  220. ldev = wdev->ieee802154_ptr->lowpan_dev;
  221. if (!ldev || !netif_running(ldev))
  222. goto drop;
  223. /* Replacing skb->dev and followed rx handlers will manipulate skb. */
  224. skb = skb_share_check(skb, GFP_ATOMIC);
  225. if (!skb)
  226. goto out;
  227. skb->dev = ldev;
  228. /* When receive frag1 it's likely that we manipulate the buffer.
  229. * When recevie iphc we manipulate the data buffer. So we need
  230. * to unshare the buffer.
  231. */
  232. if (lowpan_is_frag1(*skb_network_header(skb)) ||
  233. lowpan_is_iphc(*skb_network_header(skb))) {
  234. skb = skb_unshare(skb, GFP_ATOMIC);
  235. if (!skb)
  236. goto out;
  237. }
  238. return lowpan_invoke_rx_handlers(skb);
  239. drop:
  240. kfree_skb(skb);
  241. out:
  242. return NET_RX_DROP;
  243. }
  244. static struct packet_type lowpan_packet_type = {
  245. .type = htons(ETH_P_IEEE802154),
  246. .func = lowpan_rcv,
  247. };
  248. void lowpan_rx_init(void)
  249. {
  250. dev_add_pack(&lowpan_packet_type);
  251. }
  252. void lowpan_rx_exit(void)
  253. {
  254. dev_remove_pack(&lowpan_packet_type);
  255. }