tx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/ieee802154_netdev.h>
  12. #include "6lowpan_i.h"
  13. /* don't save pan id, it's intra pan */
  14. struct lowpan_addr {
  15. u8 mode;
  16. union {
  17. /* IPv6 needs big endian here */
  18. __be64 extended_addr;
  19. __be16 short_addr;
  20. } u;
  21. };
  22. struct lowpan_addr_info {
  23. struct lowpan_addr daddr;
  24. struct lowpan_addr saddr;
  25. };
  26. static inline struct
  27. lowpan_addr_info *lowpan_skb_priv(const struct sk_buff *skb)
  28. {
  29. WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct lowpan_addr_info));
  30. return (struct lowpan_addr_info *)(skb->data -
  31. sizeof(struct lowpan_addr_info));
  32. }
  33. int lowpan_header_create(struct sk_buff *skb, struct net_device *dev,
  34. unsigned short type, const void *_daddr,
  35. const void *_saddr, unsigned int len)
  36. {
  37. const u8 *saddr = _saddr;
  38. const u8 *daddr = _daddr;
  39. struct lowpan_addr_info *info;
  40. /* TODO:
  41. * if this package isn't ipv6 one, where should it be routed?
  42. */
  43. if (type != ETH_P_IPV6)
  44. return 0;
  45. if (!saddr)
  46. saddr = dev->dev_addr;
  47. raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  48. raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  49. info = lowpan_skb_priv(skb);
  50. /* TODO: Currently we only support extended_addr */
  51. info->daddr.mode = IEEE802154_ADDR_LONG;
  52. memcpy(&info->daddr.u.extended_addr, daddr,
  53. sizeof(info->daddr.u.extended_addr));
  54. info->saddr.mode = IEEE802154_ADDR_LONG;
  55. memcpy(&info->saddr.u.extended_addr, saddr,
  56. sizeof(info->daddr.u.extended_addr));
  57. return 0;
  58. }
  59. static struct sk_buff*
  60. lowpan_alloc_frag(struct sk_buff *skb, int size,
  61. const struct ieee802154_hdr *master_hdr)
  62. {
  63. struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
  64. struct sk_buff *frag;
  65. int rc;
  66. frag = alloc_skb(real_dev->hard_header_len +
  67. real_dev->needed_tailroom + size,
  68. GFP_ATOMIC);
  69. if (likely(frag)) {
  70. frag->dev = real_dev;
  71. frag->priority = skb->priority;
  72. skb_reserve(frag, real_dev->hard_header_len);
  73. skb_reset_network_header(frag);
  74. *mac_cb(frag) = *mac_cb(skb);
  75. rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
  76. &master_hdr->source, size);
  77. if (rc < 0) {
  78. kfree_skb(frag);
  79. return ERR_PTR(rc);
  80. }
  81. } else {
  82. frag = ERR_PTR(-ENOMEM);
  83. }
  84. return frag;
  85. }
  86. static int
  87. lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
  88. u8 *frag_hdr, int frag_hdrlen,
  89. int offset, int len)
  90. {
  91. struct sk_buff *frag;
  92. raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
  93. frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
  94. if (IS_ERR(frag))
  95. return -PTR_ERR(frag);
  96. memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
  97. memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
  98. raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
  99. return dev_queue_xmit(frag);
  100. }
  101. static int
  102. lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
  103. const struct ieee802154_hdr *wpan_hdr)
  104. {
  105. u16 dgram_size, dgram_offset;
  106. __be16 frag_tag;
  107. u8 frag_hdr[5];
  108. int frag_cap, frag_len, payload_cap, rc;
  109. int skb_unprocessed, skb_offset;
  110. dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
  111. skb->mac_len;
  112. frag_tag = htons(lowpan_dev_info(dev)->fragment_tag);
  113. lowpan_dev_info(dev)->fragment_tag++;
  114. frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
  115. frag_hdr[1] = dgram_size & 0xff;
  116. memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
  117. payload_cap = ieee802154_max_payload(wpan_hdr);
  118. frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
  119. skb_network_header_len(skb), 8);
  120. skb_offset = skb_network_header_len(skb);
  121. skb_unprocessed = skb->len - skb->mac_len - skb_offset;
  122. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  123. LOWPAN_FRAG1_HEAD_SIZE, 0,
  124. frag_len + skb_network_header_len(skb));
  125. if (rc) {
  126. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  127. __func__, ntohs(frag_tag));
  128. goto err;
  129. }
  130. frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
  131. frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
  132. frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
  133. do {
  134. dgram_offset += frag_len;
  135. skb_offset += frag_len;
  136. skb_unprocessed -= frag_len;
  137. frag_len = min(frag_cap, skb_unprocessed);
  138. frag_hdr[4] = dgram_offset >> 3;
  139. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  140. LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
  141. frag_len);
  142. if (rc) {
  143. pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
  144. __func__, ntohs(frag_tag), skb_offset);
  145. goto err;
  146. }
  147. } while (skb_unprocessed > frag_cap);
  148. consume_skb(skb);
  149. return NET_XMIT_SUCCESS;
  150. err:
  151. kfree_skb(skb);
  152. return rc;
  153. }
  154. static int lowpan_header(struct sk_buff *skb, struct net_device *dev)
  155. {
  156. struct wpan_dev *wpan_dev = lowpan_dev_info(dev)->real_dev->ieee802154_ptr;
  157. struct ieee802154_addr sa, da;
  158. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  159. struct lowpan_addr_info info;
  160. void *daddr, *saddr;
  161. memcpy(&info, lowpan_skb_priv(skb), sizeof(info));
  162. /* TODO: Currently we only support extended_addr */
  163. daddr = &info.daddr.u.extended_addr;
  164. saddr = &info.saddr.u.extended_addr;
  165. lowpan_header_compress(skb, dev, ETH_P_IPV6, daddr, saddr, skb->len);
  166. cb->type = IEEE802154_FC_TYPE_DATA;
  167. /* prepare wpan address data */
  168. sa.mode = IEEE802154_ADDR_LONG;
  169. sa.pan_id = wpan_dev->pan_id;
  170. sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
  171. /* intra-PAN communications */
  172. da.pan_id = sa.pan_id;
  173. /* if the destination address is the broadcast address, use the
  174. * corresponding short address
  175. */
  176. if (lowpan_is_addr_broadcast((const u8 *)daddr)) {
  177. da.mode = IEEE802154_ADDR_SHORT;
  178. da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  179. cb->ackreq = false;
  180. } else {
  181. da.mode = IEEE802154_ADDR_LONG;
  182. da.extended_addr = ieee802154_devaddr_from_raw(daddr);
  183. cb->ackreq = wpan_dev->frame_retries >= 0;
  184. }
  185. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  186. ETH_P_IPV6, (void *)&da, (void *)&sa, 0);
  187. }
  188. netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  189. {
  190. struct ieee802154_hdr wpan_hdr;
  191. int max_single, ret;
  192. pr_debug("package xmit\n");
  193. /* We must take a copy of the skb before we modify/replace the ipv6
  194. * header as the header could be used elsewhere
  195. */
  196. skb = skb_unshare(skb, GFP_ATOMIC);
  197. if (!skb)
  198. return NET_XMIT_DROP;
  199. ret = lowpan_header(skb, dev);
  200. if (ret < 0) {
  201. kfree_skb(skb);
  202. return NET_XMIT_DROP;
  203. }
  204. if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
  205. kfree_skb(skb);
  206. return NET_XMIT_DROP;
  207. }
  208. max_single = ieee802154_max_payload(&wpan_hdr);
  209. if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
  210. skb->dev = lowpan_dev_info(dev)->real_dev;
  211. return dev_queue_xmit(skb);
  212. } else {
  213. netdev_tx_t rc;
  214. pr_debug("frame is too big, fragmentation is needed\n");
  215. rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
  216. return rc < 0 ? NET_XMIT_DROP : rc;
  217. }
  218. }