tx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <net/6lowpan.h>
  11. #include <net/ndisc.h>
  12. #include <net/ieee802154_netdev.h>
  13. #include <net/mac802154.h>
  14. #include "6lowpan_i.h"
  15. #define LOWPAN_FRAG1_HEAD_SIZE 0x4
  16. #define LOWPAN_FRAGN_HEAD_SIZE 0x5
  17. struct lowpan_addr_info {
  18. struct ieee802154_addr daddr;
  19. struct ieee802154_addr saddr;
  20. };
  21. static inline struct
  22. lowpan_addr_info *lowpan_skb_priv(const struct sk_buff *skb)
  23. {
  24. WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct lowpan_addr_info));
  25. return (struct lowpan_addr_info *)(skb->data -
  26. sizeof(struct lowpan_addr_info));
  27. }
  28. /* This callback will be called from AF_PACKET and IPv6 stack, the AF_PACKET
  29. * sockets gives an 8 byte array for addresses only!
  30. *
  31. * TODO I think AF_PACKET DGRAM (sending/receiving) RAW (sending) makes no
  32. * sense here. We should disable it, the right use-case would be AF_INET6
  33. * RAW/DGRAM sockets.
  34. */
  35. int lowpan_header_create(struct sk_buff *skb, struct net_device *ldev,
  36. unsigned short type, const void *daddr,
  37. const void *saddr, unsigned int len)
  38. {
  39. struct wpan_dev *wpan_dev = lowpan_802154_dev(ldev)->wdev->ieee802154_ptr;
  40. struct lowpan_addr_info *info = lowpan_skb_priv(skb);
  41. struct lowpan_802154_neigh *llneigh = NULL;
  42. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  43. struct neighbour *n;
  44. /* TODO:
  45. * if this package isn't ipv6 one, where should it be routed?
  46. */
  47. if (type != ETH_P_IPV6)
  48. return 0;
  49. /* intra-pan communication */
  50. info->saddr.pan_id = wpan_dev->pan_id;
  51. info->daddr.pan_id = info->saddr.pan_id;
  52. if (!memcmp(daddr, ldev->broadcast, EUI64_ADDR_LEN)) {
  53. info->daddr.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  54. info->daddr.mode = IEEE802154_ADDR_SHORT;
  55. } else {
  56. __le16 short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
  57. n = neigh_lookup(&nd_tbl, &hdr->daddr, ldev);
  58. if (n) {
  59. llneigh = lowpan_802154_neigh(neighbour_priv(n));
  60. read_lock_bh(&n->lock);
  61. short_addr = llneigh->short_addr;
  62. read_unlock_bh(&n->lock);
  63. }
  64. if (llneigh &&
  65. lowpan_802154_is_valid_src_short_addr(short_addr)) {
  66. info->daddr.short_addr = short_addr;
  67. info->daddr.mode = IEEE802154_ADDR_SHORT;
  68. } else {
  69. info->daddr.mode = IEEE802154_ADDR_LONG;
  70. ieee802154_be64_to_le64(&info->daddr.extended_addr,
  71. daddr);
  72. }
  73. if (n)
  74. neigh_release(n);
  75. }
  76. if (!saddr) {
  77. if (lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) {
  78. info->saddr.mode = IEEE802154_ADDR_SHORT;
  79. info->saddr.short_addr = wpan_dev->short_addr;
  80. } else {
  81. info->saddr.mode = IEEE802154_ADDR_LONG;
  82. info->saddr.extended_addr = wpan_dev->extended_addr;
  83. }
  84. } else {
  85. info->saddr.mode = IEEE802154_ADDR_LONG;
  86. ieee802154_be64_to_le64(&info->saddr.extended_addr, saddr);
  87. }
  88. return 0;
  89. }
  90. static struct sk_buff*
  91. lowpan_alloc_frag(struct sk_buff *skb, int size,
  92. const struct ieee802154_hdr *master_hdr, bool frag1)
  93. {
  94. struct net_device *wdev = lowpan_802154_dev(skb->dev)->wdev;
  95. struct sk_buff *frag;
  96. int rc;
  97. frag = alloc_skb(wdev->needed_headroom + wdev->needed_tailroom + size,
  98. GFP_ATOMIC);
  99. if (likely(frag)) {
  100. frag->dev = wdev;
  101. frag->priority = skb->priority;
  102. skb_reserve(frag, wdev->needed_headroom);
  103. skb_reset_network_header(frag);
  104. *mac_cb(frag) = *mac_cb(skb);
  105. if (frag1) {
  106. memcpy(skb_put(frag, skb->mac_len),
  107. skb_mac_header(skb), skb->mac_len);
  108. } else {
  109. rc = wpan_dev_hard_header(frag, wdev,
  110. &master_hdr->dest,
  111. &master_hdr->source, size);
  112. if (rc < 0) {
  113. kfree_skb(frag);
  114. return ERR_PTR(rc);
  115. }
  116. }
  117. } else {
  118. frag = ERR_PTR(-ENOMEM);
  119. }
  120. return frag;
  121. }
  122. static int
  123. lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
  124. u8 *frag_hdr, int frag_hdrlen,
  125. int offset, int len, bool frag1)
  126. {
  127. struct sk_buff *frag;
  128. raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
  129. frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr, frag1);
  130. if (IS_ERR(frag))
  131. return PTR_ERR(frag);
  132. memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
  133. memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
  134. raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
  135. return dev_queue_xmit(frag);
  136. }
  137. static int
  138. lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *ldev,
  139. const struct ieee802154_hdr *wpan_hdr, u16 dgram_size,
  140. u16 dgram_offset)
  141. {
  142. __be16 frag_tag;
  143. u8 frag_hdr[5];
  144. int frag_cap, frag_len, payload_cap, rc;
  145. int skb_unprocessed, skb_offset;
  146. frag_tag = htons(lowpan_802154_dev(ldev)->fragment_tag);
  147. lowpan_802154_dev(ldev)->fragment_tag++;
  148. frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
  149. frag_hdr[1] = dgram_size & 0xff;
  150. memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
  151. payload_cap = ieee802154_max_payload(wpan_hdr);
  152. frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
  153. skb_network_header_len(skb), 8);
  154. skb_offset = skb_network_header_len(skb);
  155. skb_unprocessed = skb->len - skb->mac_len - skb_offset;
  156. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  157. LOWPAN_FRAG1_HEAD_SIZE, 0,
  158. frag_len + skb_network_header_len(skb),
  159. true);
  160. if (rc) {
  161. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  162. __func__, ntohs(frag_tag));
  163. goto err;
  164. }
  165. frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
  166. frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
  167. frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
  168. do {
  169. dgram_offset += frag_len;
  170. skb_offset += frag_len;
  171. skb_unprocessed -= frag_len;
  172. frag_len = min(frag_cap, skb_unprocessed);
  173. frag_hdr[4] = dgram_offset >> 3;
  174. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  175. LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
  176. frag_len, false);
  177. if (rc) {
  178. pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
  179. __func__, ntohs(frag_tag), skb_offset);
  180. goto err;
  181. }
  182. } while (skb_unprocessed > frag_cap);
  183. ldev->stats.tx_packets++;
  184. ldev->stats.tx_bytes += dgram_size;
  185. consume_skb(skb);
  186. return NET_XMIT_SUCCESS;
  187. err:
  188. kfree_skb(skb);
  189. return rc;
  190. }
  191. static int lowpan_header(struct sk_buff *skb, struct net_device *ldev,
  192. u16 *dgram_size, u16 *dgram_offset)
  193. {
  194. struct wpan_dev *wpan_dev = lowpan_802154_dev(ldev)->wdev->ieee802154_ptr;
  195. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  196. struct lowpan_addr_info info;
  197. memcpy(&info, lowpan_skb_priv(skb), sizeof(info));
  198. *dgram_size = skb->len;
  199. lowpan_header_compress(skb, ldev, &info.daddr, &info.saddr);
  200. /* dgram_offset = (saved bytes after compression) + lowpan header len */
  201. *dgram_offset = (*dgram_size - skb->len) + skb_network_header_len(skb);
  202. cb->type = IEEE802154_FC_TYPE_DATA;
  203. if (info.daddr.mode == IEEE802154_ADDR_SHORT &&
  204. ieee802154_is_broadcast_short_addr(info.daddr.short_addr))
  205. cb->ackreq = false;
  206. else
  207. cb->ackreq = wpan_dev->ackreq;
  208. return wpan_dev_hard_header(skb, lowpan_802154_dev(ldev)->wdev,
  209. &info.daddr, &info.saddr, 0);
  210. }
  211. netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *ldev)
  212. {
  213. struct ieee802154_hdr wpan_hdr;
  214. int max_single, ret;
  215. u16 dgram_size, dgram_offset;
  216. pr_debug("package xmit\n");
  217. WARN_ON_ONCE(skb->len > IPV6_MIN_MTU);
  218. /* We must take a copy of the skb before we modify/replace the ipv6
  219. * header as the header could be used elsewhere
  220. */
  221. skb = skb_unshare(skb, GFP_ATOMIC);
  222. if (!skb)
  223. return NET_XMIT_DROP;
  224. ret = lowpan_header(skb, ldev, &dgram_size, &dgram_offset);
  225. if (ret < 0) {
  226. kfree_skb(skb);
  227. return NET_XMIT_DROP;
  228. }
  229. if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
  230. kfree_skb(skb);
  231. return NET_XMIT_DROP;
  232. }
  233. max_single = ieee802154_max_payload(&wpan_hdr);
  234. if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
  235. skb->dev = lowpan_802154_dev(ldev)->wdev;
  236. ldev->stats.tx_packets++;
  237. ldev->stats.tx_bytes += dgram_size;
  238. return dev_queue_xmit(skb);
  239. } else {
  240. netdev_tx_t rc;
  241. pr_debug("frame is too big, fragmentation is needed\n");
  242. rc = lowpan_xmit_fragmented(skb, ldev, &wpan_hdr, dgram_size,
  243. dgram_offset);
  244. return rc < 0 ? NET_XMIT_DROP : rc;
  245. }
  246. }