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