rx.c 7.5 KB

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