Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 1 | /* Copyright 2011, Siemens AG |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 2 | * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 3 | */ |
| 4 | |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 5 | /* Based on patches from Jon Smirl <jonsmirl@gmail.com> |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 6 | * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* Jon's code is based on 6lowpan implementation for Contiki which is: |
| 19 | * Copyright (c) 2008, Swedish Institute of Computer Science. |
| 20 | * All rights reserved. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. Neither the name of the Institute nor the names of its contributors |
| 31 | * may be used to endorse or promote products derived from this software |
| 32 | * without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 35 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 36 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 37 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 38 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 39 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 40 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 41 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 42 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 43 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 44 | * SUCH DAMAGE. |
| 45 | */ |
| 46 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 47 | #include <linux/bitops.h> |
| 48 | #include <linux/if_arp.h> |
| 49 | #include <linux/module.h> |
| 50 | #include <linux/moduleparam.h> |
| 51 | #include <linux/netdevice.h> |
| 52 | #include <net/af_ieee802154.h> |
| 53 | #include <net/ieee802154.h> |
| 54 | #include <net/ieee802154_netdev.h> |
| 55 | #include <net/ipv6.h> |
| 56 | |
| 57 | #include "6lowpan.h" |
| 58 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 59 | static LIST_HEAD(lowpan_devices); |
| 60 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 61 | /* private device info */ |
| 62 | struct lowpan_dev_info { |
| 63 | struct net_device *real_dev; /* real WPAN device ptr */ |
| 64 | struct mutex dev_list_mtx; /* mutex for list ops */ |
Alexander Aring | 02600d0 | 2014-02-28 07:32:46 +0100 | [diff] [blame] | 65 | __be16 fragment_tag; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct lowpan_dev_record { |
| 69 | struct net_device *ldev; |
| 70 | struct list_head list; |
| 71 | }; |
| 72 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 73 | struct lowpan_fragment { |
| 74 | struct sk_buff *skb; /* skb to be assembled */ |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 75 | u16 length; /* length to be assemled */ |
| 76 | u32 bytes_rcv; /* bytes received */ |
| 77 | u16 tag; /* current fragment tag */ |
| 78 | struct timer_list timer; /* assembling timer */ |
| 79 | struct list_head list; /* fragments list */ |
| 80 | }; |
| 81 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 82 | static LIST_HEAD(lowpan_fragments); |
alex.bluesman.smirnov@gmail.com | 4d27de1 | 2012-07-10 21:22:42 +0000 | [diff] [blame] | 83 | static DEFINE_SPINLOCK(flist_lock); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 84 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 85 | static inline struct |
| 86 | lowpan_dev_info *lowpan_dev_info(const struct net_device *dev) |
| 87 | { |
| 88 | return netdev_priv(dev); |
| 89 | } |
| 90 | |
| 91 | static inline void lowpan_address_flip(u8 *src, u8 *dest) |
| 92 | { |
| 93 | int i; |
| 94 | for (i = 0; i < IEEE802154_ADDR_LEN; i++) |
| 95 | (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i]; |
| 96 | } |
| 97 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 98 | static int lowpan_header_create(struct sk_buff *skb, |
| 99 | struct net_device *dev, |
| 100 | unsigned short type, const void *_daddr, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 101 | const void *_saddr, unsigned int len) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 102 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 103 | const u8 *saddr = _saddr; |
| 104 | const u8 *daddr = _daddr; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 105 | struct ieee802154_addr sa, da; |
| 106 | |
Alexander Aring | fc4e98d | 2013-02-05 10:23:43 +0000 | [diff] [blame] | 107 | /* TODO: |
| 108 | * if this package isn't ipv6 one, where should it be routed? |
| 109 | */ |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 110 | if (type != ETH_P_IPV6) |
| 111 | return 0; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 112 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 113 | if (!saddr) |
| 114 | saddr = dev->dev_addr; |
| 115 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 116 | raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8); |
| 117 | raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 118 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 119 | lowpan_header_compress(skb, dev, type, daddr, saddr, len); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 120 | |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 121 | /* NOTE1: I'm still unsure about the fact that compression and WPAN |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 122 | * header are created here and not later in the xmit. So wait for |
| 123 | * an opinion of net maintainers. |
| 124 | */ |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 125 | /* NOTE2: to be absolutely correct, we must derive PANid information |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 126 | * from MAC subif of the 'dev' and 'real_dev' network devices, but |
| 127 | * this isn't implemented in mainline yet, so currently we assign 0xff |
| 128 | */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 129 | mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA; |
| 130 | mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
Tony Cheneau | 58ef67c | 2013-03-25 17:59:25 +0000 | [diff] [blame] | 131 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 132 | /* prepare wpan address data */ |
| 133 | sa.addr_type = IEEE802154_ADDR_LONG; |
| 134 | sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 135 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 136 | memcpy(&(sa.hwaddr), saddr, 8); |
| 137 | /* intra-PAN communications */ |
| 138 | da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
Tony Cheneau | 43de7aa | 2013-03-25 17:59:31 +0000 | [diff] [blame] | 139 | |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 140 | /* if the destination address is the broadcast address, use the |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 141 | * corresponding short address |
| 142 | */ |
| 143 | if (lowpan_is_addr_broadcast(daddr)) { |
| 144 | da.addr_type = IEEE802154_ADDR_SHORT; |
| 145 | da.short_addr = IEEE802154_ADDR_BROADCAST; |
| 146 | } else { |
| 147 | da.addr_type = IEEE802154_ADDR_LONG; |
| 148 | memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 149 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 150 | /* request acknowledgment */ |
| 151 | mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 152 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 153 | |
| 154 | return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev, |
| 155 | type, (void *)&da, (void *)&sa, skb->len); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 158 | static int lowpan_give_skb_to_devices(struct sk_buff *skb, |
| 159 | struct net_device *dev) |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 160 | { |
| 161 | struct lowpan_dev_record *entry; |
| 162 | struct sk_buff *skb_cp; |
| 163 | int stat = NET_RX_SUCCESS; |
| 164 | |
| 165 | rcu_read_lock(); |
| 166 | list_for_each_entry_rcu(entry, &lowpan_devices, list) |
| 167 | if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) { |
| 168 | skb_cp = skb_copy(skb, GFP_ATOMIC); |
| 169 | if (!skb_cp) { |
| 170 | stat = -ENOMEM; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | skb_cp->dev = entry->ldev; |
| 175 | stat = netif_rx(skb_cp); |
| 176 | } |
| 177 | rcu_read_unlock(); |
| 178 | |
| 179 | return stat; |
| 180 | } |
| 181 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 182 | static void lowpan_fragment_timer_expired(unsigned long entry_addr) |
| 183 | { |
| 184 | struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr; |
| 185 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 186 | pr_debug("timer expired for frame with tag %d\n", entry->tag); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 187 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 188 | list_del(&entry->list); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 189 | dev_kfree_skb(entry->skb); |
| 190 | kfree(entry); |
| 191 | } |
| 192 | |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 193 | static struct lowpan_fragment * |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 194 | lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag) |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 195 | { |
| 196 | struct lowpan_fragment *frame; |
| 197 | |
| 198 | frame = kzalloc(sizeof(struct lowpan_fragment), |
| 199 | GFP_ATOMIC); |
| 200 | if (!frame) |
| 201 | goto frame_err; |
| 202 | |
| 203 | INIT_LIST_HEAD(&frame->list); |
| 204 | |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 205 | frame->length = len; |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 206 | frame->tag = tag; |
| 207 | |
| 208 | /* allocate buffer for frame assembling */ |
alex.bluesman.smirnov@gmail.com | 79ff1db | 2012-07-10 21:22:45 +0000 | [diff] [blame] | 209 | frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length + |
| 210 | sizeof(struct ipv6hdr)); |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 211 | |
| 212 | if (!frame->skb) |
| 213 | goto skb_err; |
| 214 | |
| 215 | frame->skb->priority = skb->priority; |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 216 | |
| 217 | /* reserve headroom for uncompressed ipv6 header */ |
| 218 | skb_reserve(frame->skb, sizeof(struct ipv6hdr)); |
| 219 | skb_put(frame->skb, frame->length); |
| 220 | |
David Hauweele | 31afe1f | 2013-08-16 21:59:55 +0200 | [diff] [blame] | 221 | /* copy the first control block to keep a |
| 222 | * trace of the link-layer addresses in case |
| 223 | * of a link-local compressed address |
| 224 | */ |
| 225 | memcpy(frame->skb->cb, skb->cb, sizeof(skb->cb)); |
| 226 | |
alex.bluesman.smirnov@gmail.com | c2e94d7 | 2012-04-25 23:35:50 +0000 | [diff] [blame] | 227 | init_timer(&frame->timer); |
| 228 | /* time out is the same as for ipv6 - 60 sec */ |
| 229 | frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT; |
| 230 | frame->timer.data = (unsigned long)frame; |
| 231 | frame->timer.function = lowpan_fragment_timer_expired; |
| 232 | |
| 233 | add_timer(&frame->timer); |
| 234 | |
| 235 | list_add_tail(&frame->list, &lowpan_fragments); |
| 236 | |
| 237 | return frame; |
| 238 | |
| 239 | skb_err: |
| 240 | kfree(frame); |
| 241 | frame_err: |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 245 | static int process_data(struct sk_buff *skb) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 246 | { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 247 | u8 iphc0, iphc1; |
Alexander Aring | ce2463b | 2013-08-16 21:59:58 +0200 | [diff] [blame] | 248 | const struct ieee802154_addr *_saddr, *_daddr; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 249 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 250 | raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 251 | /* at least two bytes will be used for the encoding */ |
| 252 | if (skb->len < 2) |
| 253 | goto drop; |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 254 | |
| 255 | if (lowpan_fetch_skb_u8(skb, &iphc0)) |
| 256 | goto drop; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 257 | |
| 258 | /* fragments assembling */ |
| 259 | switch (iphc0 & LOWPAN_DISPATCH_MASK) { |
| 260 | case LOWPAN_DISPATCH_FRAG1: |
| 261 | case LOWPAN_DISPATCH_FRAGN: |
| 262 | { |
| 263 | struct lowpan_fragment *frame; |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 264 | /* slen stores the rightmost 8 bits of the 11 bits length */ |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 265 | u8 slen, offset = 0; |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 266 | u16 len, tag; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 267 | bool found = false; |
| 268 | |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 269 | if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */ |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 270 | lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */ |
| 271 | goto drop; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 272 | |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 273 | /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */ |
| 274 | len = ((iphc0 & 7) << 8) | slen; |
| 275 | |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 276 | if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1) { |
| 277 | pr_debug("%s received a FRAG1 packet (tag: %d, " |
| 278 | "size of the entire IP packet: %d)", |
| 279 | __func__, tag, len); |
| 280 | } else { /* FRAGN */ |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 281 | if (lowpan_fetch_skb_u8(skb, &offset)) |
| 282 | goto unlock_and_drop; |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 283 | pr_debug("%s received a FRAGN packet (tag: %d, " |
| 284 | "size of the entire IP packet: %d, " |
| 285 | "offset: %d)", __func__, tag, len, offset * 8); |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 286 | } |
| 287 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 288 | /* |
| 289 | * check if frame assembling with the same tag is |
| 290 | * already in progress |
| 291 | */ |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 292 | spin_lock_bh(&flist_lock); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 293 | |
| 294 | list_for_each_entry(frame, &lowpan_fragments, list) |
| 295 | if (frame->tag == tag) { |
| 296 | found = true; |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | /* alloc new frame structure */ |
| 301 | if (!found) { |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 302 | pr_debug("%s first fragment received for tag %d, " |
| 303 | "begin packet reassembly", __func__, tag); |
Tony Cheneau | 5e96855 | 2012-07-11 06:51:16 +0000 | [diff] [blame] | 304 | frame = lowpan_alloc_new_frame(skb, len, tag); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 305 | if (!frame) |
| 306 | goto unlock_and_drop; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 307 | } |
| 308 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 309 | /* if payload fits buffer, copy it */ |
| 310 | if (likely((offset * 8 + skb->len) <= frame->length)) |
| 311 | skb_copy_to_linear_data_offset(frame->skb, offset * 8, |
| 312 | skb->data, skb->len); |
| 313 | else |
| 314 | goto unlock_and_drop; |
| 315 | |
| 316 | frame->bytes_rcv += skb->len; |
| 317 | |
| 318 | /* frame assembling complete */ |
| 319 | if ((frame->bytes_rcv == frame->length) && |
| 320 | frame->timer.expires > jiffies) { |
| 321 | /* if timer haven't expired - first of all delete it */ |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 322 | del_timer_sync(&frame->timer); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 323 | list_del(&frame->list); |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 324 | spin_unlock_bh(&flist_lock); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 325 | |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 326 | pr_debug("%s successfully reassembled fragment " |
| 327 | "(tag %d)", __func__, tag); |
| 328 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 329 | dev_kfree_skb(skb); |
| 330 | skb = frame->skb; |
| 331 | kfree(frame); |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 332 | |
| 333 | if (lowpan_fetch_skb_u8(skb, &iphc0)) |
Dan Carpenter | 747cf6e | 2012-06-26 20:53:09 +0000 | [diff] [blame] | 334 | goto drop; |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 335 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 338 | spin_unlock_bh(&flist_lock); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 339 | |
| 340 | return kfree_skb(skb), 0; |
| 341 | } |
| 342 | default: |
| 343 | break; |
| 344 | } |
| 345 | |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 346 | if (lowpan_fetch_skb_u8(skb, &iphc1)) |
| 347 | goto drop; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 348 | |
Alexander Aring | ce2463b | 2013-08-16 21:59:58 +0200 | [diff] [blame] | 349 | _saddr = &mac_cb(skb)->sa; |
| 350 | _daddr = &mac_cb(skb)->da; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 351 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 352 | return lowpan_process_data(skb, skb->dev, (u8 *)_saddr->hwaddr, |
| 353 | _saddr->addr_type, IEEE802154_ADDR_LEN, |
| 354 | (u8 *)_daddr->hwaddr, _daddr->addr_type, |
| 355 | IEEE802154_ADDR_LEN, iphc0, iphc1, |
| 356 | lowpan_give_skb_to_devices); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 357 | |
| 358 | unlock_and_drop: |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 359 | spin_unlock_bh(&flist_lock); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 360 | drop: |
Dan Carpenter | 90d0963 | 2011-08-30 03:45:52 +0000 | [diff] [blame] | 361 | kfree_skb(skb); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 362 | return -EINVAL; |
| 363 | } |
| 364 | |
alex.bluesman.smirnov@gmail.com | 42c3629 | 2012-07-01 19:58:46 +0000 | [diff] [blame] | 365 | static int lowpan_set_address(struct net_device *dev, void *p) |
| 366 | { |
| 367 | struct sockaddr *sa = p; |
| 368 | |
| 369 | if (netif_running(dev)) |
| 370 | return -EBUSY; |
| 371 | |
| 372 | /* TODO: validate addr */ |
| 373 | memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 378 | static int |
| 379 | lowpan_fragment_xmit(struct sk_buff *skb, u8 *head, |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 380 | int mlen, int plen, int offset, int type) |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 381 | { |
| 382 | struct sk_buff *frag; |
Alexander Aring | 545f361 | 2013-10-28 10:24:16 +0100 | [diff] [blame] | 383 | int hlen; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 384 | |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 385 | hlen = (type == LOWPAN_DISPATCH_FRAG1) ? |
| 386 | LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 387 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 388 | raw_dump_inline(__func__, "6lowpan fragment header", head, hlen); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 389 | |
Alexander Aring | b614442 | 2013-10-28 10:24:18 +0100 | [diff] [blame] | 390 | frag = netdev_alloc_skb(skb->dev, |
| 391 | hlen + mlen + plen + IEEE802154_MFR_SIZE); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 392 | if (!frag) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | frag->priority = skb->priority; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 396 | |
| 397 | /* copy header, MFR and payload */ |
Alexander Aring | 3582b90 | 2013-10-30 09:18:24 +0100 | [diff] [blame] | 398 | skb_put(frag, mlen); |
| 399 | skb_copy_to_linear_data(frag, skb_mac_header(skb), mlen); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 400 | |
Alexander Aring | 3582b90 | 2013-10-30 09:18:24 +0100 | [diff] [blame] | 401 | skb_put(frag, hlen); |
| 402 | skb_copy_to_linear_data_offset(frag, mlen, head, hlen); |
| 403 | |
| 404 | skb_put(frag, plen); |
| 405 | skb_copy_to_linear_data_offset(frag, mlen + hlen, |
| 406 | skb_network_header(skb) + offset, plen); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 407 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 408 | raw_dump_table(__func__, " raw fragment dump", frag->data, frag->len); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 409 | |
Alexander Aring | 545f361 | 2013-10-28 10:24:16 +0100 | [diff] [blame] | 410 | return dev_queue_xmit(frag); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int |
Tony Cheneau | d4ac323 | 2013-03-25 17:59:28 +0000 | [diff] [blame] | 414 | lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 415 | { |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 416 | int err; |
| 417 | u16 dgram_offset, dgram_size, payload_length, header_length, |
Alexander Aring | 02600d0 | 2014-02-28 07:32:46 +0100 | [diff] [blame] | 418 | lowpan_size, frag_plen, offset; |
| 419 | __be16 tag; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 420 | u8 head[5]; |
| 421 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 422 | header_length = skb->mac_len; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 423 | payload_length = skb->len - header_length; |
Tony Cheneau | d4ac323 | 2013-03-25 17:59:28 +0000 | [diff] [blame] | 424 | tag = lowpan_dev_info(dev)->fragment_tag++; |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 425 | lowpan_size = skb_network_header_len(skb); |
| 426 | dgram_size = lowpan_uncompress_size(skb, &dgram_offset) - |
| 427 | header_length; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 428 | |
| 429 | /* first fragment header */ |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 430 | head[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x7); |
| 431 | head[1] = dgram_size & 0xff; |
Tony Cheneau | 4576039 | 2012-07-11 06:51:15 +0000 | [diff] [blame] | 432 | head[2] = tag >> 8; |
| 433 | head[3] = tag & 0xff; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 434 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 435 | /* calc the nearest payload length(divided to 8) for first fragment |
| 436 | * which fits into a IEEE802154_MTU |
| 437 | */ |
| 438 | frag_plen = round_down(IEEE802154_MTU - header_length - |
| 439 | LOWPAN_FRAG1_HEAD_SIZE - lowpan_size - |
| 440 | IEEE802154_MFR_SIZE, 8); |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 441 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 442 | err = lowpan_fragment_xmit(skb, head, header_length, |
| 443 | frag_plen + lowpan_size, 0, |
| 444 | LOWPAN_DISPATCH_FRAG1); |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 445 | if (err) { |
| 446 | pr_debug("%s unable to send FRAG1 packet (tag: %d)", |
| 447 | __func__, tag); |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 448 | goto exit; |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 449 | } |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 450 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 451 | offset = lowpan_size + frag_plen; |
| 452 | dgram_offset += frag_plen; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 453 | |
| 454 | /* next fragment header */ |
| 455 | head[0] &= ~LOWPAN_DISPATCH_FRAG1; |
| 456 | head[0] |= LOWPAN_DISPATCH_FRAGN; |
| 457 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 458 | frag_plen = round_down(IEEE802154_MTU - header_length - |
| 459 | LOWPAN_FRAGN_HEAD_SIZE - IEEE802154_MFR_SIZE, 8); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 460 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 461 | while (payload_length - offset > 0) { |
| 462 | int len = frag_plen; |
| 463 | |
| 464 | head[4] = dgram_offset >> 3; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 465 | |
| 466 | if (payload_length - offset < len) |
| 467 | len = payload_length - offset; |
| 468 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 469 | err = lowpan_fragment_xmit(skb, head, header_length, len, |
| 470 | offset, LOWPAN_DISPATCH_FRAGN); |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 471 | if (err) { |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 472 | pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n", |
| 473 | __func__, tag, offset); |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 474 | goto exit; |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 475 | } |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 476 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 477 | offset += len; |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 478 | dgram_offset += len; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 481 | exit: |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 482 | return err; |
| 483 | } |
| 484 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 485 | static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 486 | { |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 487 | int err = -1; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 488 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 489 | pr_debug("package xmit\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 490 | |
| 491 | skb->dev = lowpan_dev_info(dev)->real_dev; |
| 492 | if (skb->dev == NULL) { |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 493 | pr_debug("ERROR: no real wpan device found\n"); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 494 | goto error; |
| 495 | } |
| 496 | |
Alan Ott | b333b7e | 2012-11-29 15:55:44 +0000 | [diff] [blame] | 497 | /* Send directly if less than the MTU minus the 2 checksum bytes. */ |
| 498 | if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 499 | err = dev_queue_xmit(skb); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 500 | goto out; |
| 501 | } |
| 502 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 503 | pr_debug("frame is too big, fragmentation is needed\n"); |
Tony Cheneau | d4ac323 | 2013-03-25 17:59:28 +0000 | [diff] [blame] | 504 | err = lowpan_skb_fragmentation(skb, dev); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 505 | error: |
| 506 | dev_kfree_skb(skb); |
| 507 | out: |
Alan Ott | fc52eea | 2013-04-03 04:00:58 +0000 | [diff] [blame] | 508 | if (err) |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 509 | pr_debug("ERROR: xmit failed\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 510 | |
Alan Ott | fc52eea | 2013-04-03 04:00:58 +0000 | [diff] [blame] | 511 | return (err < 0) ? NET_XMIT_DROP : err; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 512 | } |
| 513 | |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 514 | static struct wpan_phy *lowpan_get_phy(const struct net_device *dev) |
| 515 | { |
| 516 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
| 517 | return ieee802154_mlme_ops(real_dev)->get_phy(real_dev); |
| 518 | } |
| 519 | |
| 520 | static u16 lowpan_get_pan_id(const struct net_device *dev) |
| 521 | { |
| 522 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
| 523 | return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev); |
| 524 | } |
| 525 | |
| 526 | static u16 lowpan_get_short_addr(const struct net_device *dev) |
| 527 | { |
| 528 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
| 529 | return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev); |
| 530 | } |
| 531 | |
Tony Cheneau | c7d0ab2 | 2013-03-25 17:59:30 +0000 | [diff] [blame] | 532 | static u8 lowpan_get_dsn(const struct net_device *dev) |
| 533 | { |
| 534 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
| 535 | return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev); |
| 536 | } |
| 537 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 538 | static struct header_ops lowpan_header_ops = { |
| 539 | .create = lowpan_header_create, |
| 540 | }; |
| 541 | |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 542 | static struct lock_class_key lowpan_tx_busylock; |
| 543 | static struct lock_class_key lowpan_netdev_xmit_lock_key; |
| 544 | |
| 545 | static void lowpan_set_lockdep_class_one(struct net_device *dev, |
| 546 | struct netdev_queue *txq, |
| 547 | void *_unused) |
| 548 | { |
| 549 | lockdep_set_class(&txq->_xmit_lock, |
| 550 | &lowpan_netdev_xmit_lock_key); |
| 551 | } |
| 552 | |
| 553 | |
| 554 | static int lowpan_dev_init(struct net_device *dev) |
| 555 | { |
| 556 | netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL); |
| 557 | dev->qdisc_tx_busylock = &lowpan_tx_busylock; |
| 558 | return 0; |
| 559 | } |
| 560 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 561 | static const struct net_device_ops lowpan_netdev_ops = { |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 562 | .ndo_init = lowpan_dev_init, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 563 | .ndo_start_xmit = lowpan_xmit, |
alex.bluesman.smirnov@gmail.com | 42c3629 | 2012-07-01 19:58:46 +0000 | [diff] [blame] | 564 | .ndo_set_mac_address = lowpan_set_address, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 565 | }; |
| 566 | |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 567 | static struct ieee802154_mlme_ops lowpan_mlme = { |
| 568 | .get_pan_id = lowpan_get_pan_id, |
| 569 | .get_phy = lowpan_get_phy, |
| 570 | .get_short_addr = lowpan_get_short_addr, |
Tony Cheneau | c7d0ab2 | 2013-03-25 17:59:30 +0000 | [diff] [blame] | 571 | .get_dsn = lowpan_get_dsn, |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 574 | static void lowpan_setup(struct net_device *dev) |
| 575 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 576 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 577 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
| 578 | dev->type = ARPHRD_IEEE802154; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 579 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
| 580 | dev->hard_header_len = 2 + 1 + 20 + 14; |
| 581 | dev->needed_tailroom = 2; /* FCS */ |
| 582 | dev->mtu = 1281; |
| 583 | dev->tx_queue_len = 0; |
alex.bluesman.smirnov@gmail.com | 4d039f6 | 2011-11-10 07:39:37 +0000 | [diff] [blame] | 584 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 585 | dev->watchdog_timeo = 0; |
| 586 | |
| 587 | dev->netdev_ops = &lowpan_netdev_ops; |
| 588 | dev->header_ops = &lowpan_header_ops; |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 589 | dev->ml_priv = &lowpan_mlme; |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 590 | dev->destructor = free_netdev; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 594 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 595 | if (tb[IFLA_ADDRESS]) { |
| 596 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev, |
| 603 | struct packet_type *pt, struct net_device *orig_dev) |
| 604 | { |
Alan Ott | a437d27 | 2012-09-01 05:57:06 +0000 | [diff] [blame] | 605 | struct sk_buff *local_skb; |
| 606 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 607 | if (!netif_running(dev)) |
| 608 | goto drop; |
| 609 | |
| 610 | if (dev->type != ARPHRD_IEEE802154) |
| 611 | goto drop; |
| 612 | |
| 613 | /* check that it's our buffer */ |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 614 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
| 615 | /* Copy the packet so that the IPv6 header is |
| 616 | * properly aligned. |
| 617 | */ |
| 618 | local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1, |
| 619 | skb_tailroom(skb), GFP_ATOMIC); |
Alan Ott | a437d27 | 2012-09-01 05:57:06 +0000 | [diff] [blame] | 620 | if (!local_skb) |
| 621 | goto drop; |
Alan Ott | a437d27 | 2012-09-01 05:57:06 +0000 | [diff] [blame] | 622 | |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 623 | local_skb->protocol = htons(ETH_P_IPV6); |
| 624 | local_skb->pkt_type = PACKET_HOST; |
| 625 | |
| 626 | /* Pull off the 1-byte of 6lowpan header. */ |
| 627 | skb_pull(local_skb, 1); |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 628 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 629 | lowpan_give_skb_to_devices(local_skb, NULL); |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 630 | |
| 631 | kfree_skb(local_skb); |
Alan Ott | a437d27 | 2012-09-01 05:57:06 +0000 | [diff] [blame] | 632 | kfree_skb(skb); |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 633 | } else { |
| 634 | switch (skb->data[0] & 0xe0) { |
| 635 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ |
| 636 | case LOWPAN_DISPATCH_FRAG1: /* first fragment header */ |
| 637 | case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */ |
| 638 | local_skb = skb_clone(skb, GFP_ATOMIC); |
| 639 | if (!local_skb) |
| 640 | goto drop; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 641 | process_data(local_skb); |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 642 | |
| 643 | kfree_skb(skb); |
| 644 | break; |
| 645 | default: |
| 646 | break; |
| 647 | } |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 648 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 649 | |
| 650 | return NET_RX_SUCCESS; |
| 651 | |
| 652 | drop: |
| 653 | kfree_skb(skb); |
| 654 | return NET_RX_DROP; |
| 655 | } |
| 656 | |
| 657 | static int lowpan_newlink(struct net *src_net, struct net_device *dev, |
| 658 | struct nlattr *tb[], struct nlattr *data[]) |
| 659 | { |
| 660 | struct net_device *real_dev; |
| 661 | struct lowpan_dev_record *entry; |
| 662 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 663 | pr_debug("adding new link\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 664 | |
| 665 | if (!tb[IFLA_LINK]) |
| 666 | return -EINVAL; |
| 667 | /* find and hold real wpan device */ |
| 668 | real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
| 669 | if (!real_dev) |
| 670 | return -ENODEV; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 671 | if (real_dev->type != ARPHRD_IEEE802154) { |
| 672 | dev_put(real_dev); |
Alan Ott | 7adac1e | 2013-10-05 23:15:18 -0400 | [diff] [blame] | 673 | return -EINVAL; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 674 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 675 | |
| 676 | lowpan_dev_info(dev)->real_dev = real_dev; |
Tony Cheneau | d4ac323 | 2013-03-25 17:59:28 +0000 | [diff] [blame] | 677 | lowpan_dev_info(dev)->fragment_tag = 0; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 678 | mutex_init(&lowpan_dev_info(dev)->dev_list_mtx); |
| 679 | |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame^] | 680 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 681 | if (!entry) { |
| 682 | dev_put(real_dev); |
| 683 | lowpan_dev_info(dev)->real_dev = NULL; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 684 | return -ENOMEM; |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 685 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 686 | |
| 687 | entry->ldev = dev; |
| 688 | |
Alan Ott | ab2d95d | 2013-10-05 23:15:19 -0400 | [diff] [blame] | 689 | /* Set the lowpan harware address to the wpan hardware address. */ |
| 690 | memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN); |
| 691 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 692 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 693 | INIT_LIST_HEAD(&entry->list); |
| 694 | list_add_tail(&entry->list, &lowpan_devices); |
| 695 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 696 | |
| 697 | register_netdevice(dev); |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static void lowpan_dellink(struct net_device *dev, struct list_head *head) |
| 703 | { |
| 704 | struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev); |
| 705 | struct net_device *real_dev = lowpan_dev->real_dev; |
alex.bluesman.smirnov@gmail.com | 8deff4a | 2012-04-25 23:24:57 +0000 | [diff] [blame] | 706 | struct lowpan_dev_record *entry, *tmp; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 707 | |
| 708 | ASSERT_RTNL(); |
| 709 | |
| 710 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
Dan Carpenter | aec9db3 | 2011-08-30 03:46:40 +0000 | [diff] [blame] | 711 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 712 | if (entry->ldev == dev) { |
| 713 | list_del(&entry->list); |
| 714 | kfree(entry); |
| 715 | } |
Dan Carpenter | aec9db3 | 2011-08-30 03:46:40 +0000 | [diff] [blame] | 716 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 717 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 718 | |
| 719 | mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx); |
| 720 | |
| 721 | unregister_netdevice_queue(dev, head); |
| 722 | |
| 723 | dev_put(real_dev); |
| 724 | } |
| 725 | |
| 726 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { |
| 727 | .kind = "lowpan", |
| 728 | .priv_size = sizeof(struct lowpan_dev_info), |
| 729 | .setup = lowpan_setup, |
| 730 | .newlink = lowpan_newlink, |
| 731 | .dellink = lowpan_dellink, |
| 732 | .validate = lowpan_validate, |
| 733 | }; |
| 734 | |
| 735 | static inline int __init lowpan_netlink_init(void) |
| 736 | { |
| 737 | return rtnl_link_register(&lowpan_link_ops); |
| 738 | } |
| 739 | |
David S. Miller | a07fdce | 2013-02-06 15:54:38 -0500 | [diff] [blame] | 740 | static inline void lowpan_netlink_fini(void) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 741 | { |
| 742 | rtnl_link_unregister(&lowpan_link_ops); |
| 743 | } |
| 744 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 745 | static int lowpan_device_event(struct notifier_block *unused, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 746 | unsigned long event, void *ptr) |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 747 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 748 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 749 | LIST_HEAD(del_list); |
| 750 | struct lowpan_dev_record *entry, *tmp; |
| 751 | |
| 752 | if (dev->type != ARPHRD_IEEE802154) |
| 753 | goto out; |
| 754 | |
| 755 | if (event == NETDEV_UNREGISTER) { |
| 756 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
| 757 | if (lowpan_dev_info(entry->ldev)->real_dev == dev) |
| 758 | lowpan_dellink(entry->ldev, &del_list); |
| 759 | } |
| 760 | |
| 761 | unregister_netdevice_many(&del_list); |
Peter Senna Tschudin | 4c83501 | 2012-09-18 07:10:43 +0000 | [diff] [blame] | 762 | } |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 763 | |
| 764 | out: |
| 765 | return NOTIFY_DONE; |
| 766 | } |
| 767 | |
| 768 | static struct notifier_block lowpan_dev_notifier = { |
| 769 | .notifier_call = lowpan_device_event, |
| 770 | }; |
| 771 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 772 | static struct packet_type lowpan_packet_type = { |
| 773 | .type = __constant_htons(ETH_P_IEEE802154), |
| 774 | .func = lowpan_rcv, |
| 775 | }; |
| 776 | |
| 777 | static int __init lowpan_init_module(void) |
| 778 | { |
| 779 | int err = 0; |
| 780 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 781 | err = lowpan_netlink_init(); |
| 782 | if (err < 0) |
| 783 | goto out; |
| 784 | |
| 785 | dev_add_pack(&lowpan_packet_type); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 786 | |
| 787 | err = register_netdevice_notifier(&lowpan_dev_notifier); |
| 788 | if (err < 0) { |
| 789 | dev_remove_pack(&lowpan_packet_type); |
| 790 | lowpan_netlink_fini(); |
| 791 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 792 | out: |
| 793 | return err; |
| 794 | } |
| 795 | |
| 796 | static void __exit lowpan_cleanup_module(void) |
| 797 | { |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 798 | struct lowpan_fragment *frame, *tframe; |
| 799 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 800 | lowpan_netlink_fini(); |
| 801 | |
| 802 | dev_remove_pack(&lowpan_packet_type); |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 803 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 804 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
| 805 | |
alex.bluesman.smirnov@gmail.com | 33c34c5 | 2012-07-10 21:22:48 +0000 | [diff] [blame] | 806 | /* Now 6lowpan packet_type is removed, so no new fragments are |
| 807 | * expected on RX, therefore that's the time to clean incomplete |
| 808 | * fragments. |
| 809 | */ |
| 810 | spin_lock_bh(&flist_lock); |
| 811 | list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) { |
| 812 | del_timer_sync(&frame->timer); |
| 813 | list_del(&frame->list); |
| 814 | dev_kfree_skb(frame->skb); |
| 815 | kfree(frame); |
| 816 | } |
| 817 | spin_unlock_bh(&flist_lock); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | module_init(lowpan_init_module); |
| 821 | module_exit(lowpan_cleanup_module); |
| 822 | MODULE_LICENSE("GPL"); |
| 823 | MODULE_ALIAS_RTNL_LINK("lowpan"); |