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> |
Alexander Aring | 4ca24ac | 2014-10-25 09:41:04 +0200 | [diff] [blame] | 52 | #include <linux/ieee802154.h> |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 53 | #include <net/af_ieee802154.h> |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 54 | #include <net/ieee802154_netdev.h> |
Alexander Aring | cefc8c8 | 2014-03-05 14:29:05 +0100 | [diff] [blame] | 55 | #include <net/6lowpan.h> |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 56 | #include <net/ipv6.h> |
| 57 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 58 | #include "reassembly.h" |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 59 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 60 | static LIST_HEAD(lowpan_devices); |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 61 | static int lowpan_open_count; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 62 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 63 | /* private device info */ |
| 64 | struct lowpan_dev_info { |
| 65 | struct net_device *real_dev; /* real WPAN device ptr */ |
| 66 | struct mutex dev_list_mtx; /* mutex for list ops */ |
Alexander Aring | cd97a71 | 2014-10-06 11:00:50 +0200 | [diff] [blame] | 67 | u16 fragment_tag; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct lowpan_dev_record { |
| 71 | struct net_device *ldev; |
| 72 | struct list_head list; |
| 73 | }; |
| 74 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 75 | /* don't save pan id, it's intra pan */ |
| 76 | struct lowpan_addr { |
| 77 | u8 mode; |
| 78 | union { |
| 79 | /* IPv6 needs big endian here */ |
| 80 | __be64 extended_addr; |
| 81 | __be16 short_addr; |
| 82 | } u; |
| 83 | }; |
| 84 | |
| 85 | struct lowpan_addr_info { |
| 86 | struct lowpan_addr daddr; |
| 87 | struct lowpan_addr saddr; |
| 88 | }; |
| 89 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 90 | static inline struct |
| 91 | lowpan_dev_info *lowpan_dev_info(const struct net_device *dev) |
| 92 | { |
| 93 | return netdev_priv(dev); |
| 94 | } |
| 95 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 96 | static inline struct |
| 97 | lowpan_addr_info *lowpan_skb_priv(const struct sk_buff *skb) |
| 98 | { |
| 99 | WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct lowpan_addr_info)); |
| 100 | return (struct lowpan_addr_info *)(skb->data - |
| 101 | sizeof(struct lowpan_addr_info)); |
| 102 | } |
| 103 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 104 | static int lowpan_header_create(struct sk_buff *skb, struct net_device *dev, |
| 105 | unsigned short type, const void *_daddr, |
| 106 | const void *_saddr, unsigned int len) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 107 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 108 | const u8 *saddr = _saddr; |
| 109 | const u8 *daddr = _daddr; |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 110 | struct lowpan_addr_info *info; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 111 | |
Alexander Aring | fc4e98d | 2013-02-05 10:23:43 +0000 | [diff] [blame] | 112 | /* TODO: |
| 113 | * if this package isn't ipv6 one, where should it be routed? |
| 114 | */ |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 115 | if (type != ETH_P_IPV6) |
| 116 | return 0; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 117 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 118 | if (!saddr) |
| 119 | saddr = dev->dev_addr; |
| 120 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 121 | raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8); |
| 122 | raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 123 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 124 | info = lowpan_skb_priv(skb); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 125 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 126 | /* TODO: Currently we only support extended_addr */ |
| 127 | info->daddr.mode = IEEE802154_ADDR_LONG; |
| 128 | memcpy(&info->daddr.u.extended_addr, daddr, |
| 129 | sizeof(info->daddr.u.extended_addr)); |
| 130 | info->saddr.mode = IEEE802154_ADDR_LONG; |
| 131 | memcpy(&info->saddr.u.extended_addr, saddr, |
| 132 | sizeof(info->daddr.u.extended_addr)); |
Tony Cheneau | 58ef67c | 2013-03-25 17:59:25 +0000 | [diff] [blame] | 133 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 134 | return 0; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 137 | static int lowpan_give_skb_to_devices(struct sk_buff *skb, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 138 | struct net_device *dev) |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 139 | { |
| 140 | struct lowpan_dev_record *entry; |
| 141 | struct sk_buff *skb_cp; |
| 142 | int stat = NET_RX_SUCCESS; |
| 143 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 144 | skb->protocol = htons(ETH_P_IPV6); |
| 145 | skb->pkt_type = PACKET_HOST; |
| 146 | |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 147 | rcu_read_lock(); |
| 148 | list_for_each_entry_rcu(entry, &lowpan_devices, list) |
| 149 | if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) { |
| 150 | skb_cp = skb_copy(skb, GFP_ATOMIC); |
| 151 | if (!skb_cp) { |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 152 | kfree_skb(skb); |
| 153 | rcu_read_unlock(); |
| 154 | return NET_RX_DROP; |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | skb_cp->dev = entry->ldev; |
| 158 | stat = netif_rx(skb_cp); |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 159 | if (stat == NET_RX_DROP) |
| 160 | break; |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 161 | } |
| 162 | rcu_read_unlock(); |
| 163 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 164 | consume_skb(skb); |
| 165 | |
Alan Ott | 0c44621 | 2013-01-16 19:09:47 +0000 | [diff] [blame] | 166 | return stat; |
| 167 | } |
| 168 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 169 | static int |
| 170 | iphc_decompress(struct sk_buff *skb, const struct ieee802154_hdr *hdr) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 171 | { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 172 | u8 iphc0, iphc1; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 173 | struct ieee802154_addr_sa sa, da; |
| 174 | void *sap, *dap; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 175 | |
Alexander Aring | 841a5ec | 2013-12-12 20:15:25 +0100 | [diff] [blame] | 176 | raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 177 | /* at least two bytes will be used for the encoding */ |
| 178 | if (skb->len < 2) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 179 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 180 | |
| 181 | if (lowpan_fetch_skb_u8(skb, &iphc0)) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 182 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 183 | |
alex.bluesman.smirnov@gmail.com | c5d3687 | 2012-06-25 03:49:01 +0000 | [diff] [blame] | 184 | if (lowpan_fetch_skb_u8(skb, &iphc1)) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 185 | return -EINVAL; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 186 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 187 | ieee802154_addr_to_sa(&sa, &hdr->source); |
| 188 | ieee802154_addr_to_sa(&da, &hdr->dest); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 189 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 190 | if (sa.addr_type == IEEE802154_ADDR_SHORT) |
| 191 | sap = &sa.short_addr; |
| 192 | else |
| 193 | sap = &sa.hwaddr; |
| 194 | |
| 195 | if (da.addr_type == IEEE802154_ADDR_SHORT) |
| 196 | dap = &da.short_addr; |
| 197 | else |
| 198 | dap = &da.hwaddr; |
| 199 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 200 | return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type, |
| 201 | IEEE802154_ADDR_LEN, dap, da.addr_type, |
| 202 | IEEE802154_ADDR_LEN, iphc0, iphc1); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 205 | static struct sk_buff* |
| 206 | lowpan_alloc_frag(struct sk_buff *skb, int size, |
| 207 | const struct ieee802154_hdr *master_hdr) |
| 208 | { |
| 209 | struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev; |
| 210 | struct sk_buff *frag; |
| 211 | int rc; |
| 212 | |
| 213 | frag = alloc_skb(real_dev->hard_header_len + |
| 214 | real_dev->needed_tailroom + size, |
| 215 | GFP_ATOMIC); |
| 216 | |
| 217 | if (likely(frag)) { |
| 218 | frag->dev = real_dev; |
| 219 | frag->priority = skb->priority; |
| 220 | skb_reserve(frag, real_dev->hard_header_len); |
| 221 | skb_reset_network_header(frag); |
| 222 | *mac_cb(frag) = *mac_cb(skb); |
| 223 | |
| 224 | rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest, |
| 225 | &master_hdr->source, size); |
| 226 | if (rc < 0) { |
| 227 | kfree_skb(frag); |
Alexander Aring | c0bffc7 | 2014-10-06 11:00:52 +0200 | [diff] [blame] | 228 | return ERR_PTR(rc); |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 229 | } |
| 230 | } else { |
Alexander Aring | c4cb901 | 2014-08-19 19:03:29 +0200 | [diff] [blame] | 231 | frag = ERR_PTR(-ENOMEM); |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return frag; |
| 235 | } |
| 236 | |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 237 | static int |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 238 | lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr, |
| 239 | u8 *frag_hdr, int frag_hdrlen, |
| 240 | int offset, int len) |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 241 | { |
| 242 | struct sk_buff *frag; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 243 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 244 | raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 245 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 246 | frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr); |
| 247 | if (IS_ERR(frag)) |
| 248 | return -PTR_ERR(frag); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 249 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 250 | memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen); |
| 251 | memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 252 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 253 | raw_dump_table(__func__, " fragment dump", frag->data, frag->len); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 254 | |
Alexander Aring | 545f361 | 2013-10-28 10:24:16 +0100 | [diff] [blame] | 255 | return dev_queue_xmit(frag); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 259 | lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev, |
| 260 | const struct ieee802154_hdr *wpan_hdr) |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 261 | { |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 262 | u16 dgram_size, dgram_offset; |
| 263 | __be16 frag_tag; |
| 264 | u8 frag_hdr[5]; |
| 265 | int frag_cap, frag_len, payload_cap, rc; |
| 266 | int skb_unprocessed, skb_offset; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 267 | |
Alexander Aring | 96cb3eb | 2014-02-28 07:32:45 +0100 | [diff] [blame] | 268 | dgram_size = lowpan_uncompress_size(skb, &dgram_offset) - |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 269 | skb->mac_len; |
Alexander Aring | cd97a71 | 2014-10-06 11:00:50 +0200 | [diff] [blame] | 270 | frag_tag = htons(lowpan_dev_info(dev)->fragment_tag); |
| 271 | lowpan_dev_info(dev)->fragment_tag++; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 272 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 273 | frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07); |
| 274 | frag_hdr[1] = dgram_size & 0xff; |
| 275 | memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag)); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 276 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 277 | payload_cap = ieee802154_max_payload(wpan_hdr); |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 278 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 279 | frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE - |
| 280 | skb_network_header_len(skb), 8); |
| 281 | |
| 282 | skb_offset = skb_network_header_len(skb); |
| 283 | skb_unprocessed = skb->len - skb->mac_len - skb_offset; |
| 284 | |
| 285 | rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr, |
| 286 | LOWPAN_FRAG1_HEAD_SIZE, 0, |
| 287 | frag_len + skb_network_header_len(skb)); |
| 288 | if (rc) { |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 289 | pr_debug("%s unable to send FRAG1 packet (tag: %d)", |
Alexander Aring | cd97a71 | 2014-10-06 11:00:50 +0200 | [diff] [blame] | 290 | __func__, ntohs(frag_tag)); |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 291 | goto err; |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 292 | } |
Tony Cheneau | d991b98 | 2013-03-25 17:59:26 +0000 | [diff] [blame] | 293 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 294 | frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1; |
| 295 | frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN; |
| 296 | frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 297 | |
Alexander Aring | 51263ff | 2014-06-02 13:21:57 +0200 | [diff] [blame] | 298 | do { |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 299 | dgram_offset += frag_len; |
| 300 | skb_offset += frag_len; |
| 301 | skb_unprocessed -= frag_len; |
| 302 | frag_len = min(frag_cap, skb_unprocessed); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 303 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 304 | frag_hdr[4] = dgram_offset >> 3; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 305 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 306 | rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr, |
| 307 | LOWPAN_FRAGN_HEAD_SIZE, skb_offset, |
| 308 | frag_len); |
| 309 | if (rc) { |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame] | 310 | pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n", |
Alexander Aring | cd97a71 | 2014-10-06 11:00:50 +0200 | [diff] [blame] | 311 | __func__, ntohs(frag_tag), skb_offset); |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 312 | goto err; |
Tony Cheneau | 9da2924 | 2013-03-25 17:59:27 +0000 | [diff] [blame] | 313 | } |
Alexander Aring | eb06481 | 2014-06-02 13:21:58 +0200 | [diff] [blame] | 314 | } while (skb_unprocessed > frag_cap); |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 315 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 316 | consume_skb(skb); |
| 317 | return NET_XMIT_SUCCESS; |
| 318 | |
| 319 | err: |
| 320 | kfree_skb(skb); |
| 321 | return rc; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 324 | static int lowpan_header(struct sk_buff *skb, struct net_device *dev) |
| 325 | { |
| 326 | struct ieee802154_addr sa, da; |
| 327 | struct ieee802154_mac_cb *cb = mac_cb_init(skb); |
| 328 | struct lowpan_addr_info info; |
| 329 | void *daddr, *saddr; |
| 330 | |
| 331 | memcpy(&info, lowpan_skb_priv(skb), sizeof(info)); |
| 332 | |
| 333 | /* TODO: Currently we only support extended_addr */ |
| 334 | daddr = &info.daddr.u.extended_addr; |
| 335 | saddr = &info.saddr.u.extended_addr; |
| 336 | |
| 337 | lowpan_header_compress(skb, dev, ETH_P_IPV6, daddr, saddr, skb->len); |
| 338 | |
| 339 | cb->type = IEEE802154_FC_TYPE_DATA; |
| 340 | |
| 341 | /* prepare wpan address data */ |
| 342 | sa.mode = IEEE802154_ADDR_LONG; |
| 343 | sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
| 344 | sa.extended_addr = ieee802154_devaddr_from_raw(saddr); |
| 345 | |
| 346 | /* intra-PAN communications */ |
| 347 | da.pan_id = sa.pan_id; |
| 348 | |
| 349 | /* if the destination address is the broadcast address, use the |
| 350 | * corresponding short address |
| 351 | */ |
| 352 | if (lowpan_is_addr_broadcast((const u8 *)daddr)) { |
| 353 | da.mode = IEEE802154_ADDR_SHORT; |
| 354 | da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
| 355 | cb->ackreq = false; |
| 356 | } else { |
| 357 | da.mode = IEEE802154_ADDR_LONG; |
| 358 | da.extended_addr = ieee802154_devaddr_from_raw(daddr); |
| 359 | cb->ackreq = true; |
| 360 | } |
| 361 | |
| 362 | return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev, |
| 363 | ETH_P_IPV6, (void *)&da, (void *)&sa, 0); |
| 364 | } |
| 365 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 366 | static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 367 | { |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 368 | struct ieee802154_hdr wpan_hdr; |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 369 | int max_single, ret; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 370 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 371 | pr_debug("package xmit\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 372 | |
Simon Vincent | f19f4f9 | 2014-09-24 12:21:33 +0200 | [diff] [blame] | 373 | /* We must take a copy of the skb before we modify/replace the ipv6 |
| 374 | * header as the header could be used elsewhere |
| 375 | */ |
| 376 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 377 | if (!skb) |
| 378 | return NET_XMIT_DROP; |
| 379 | |
| 380 | ret = lowpan_header(skb, dev); |
| 381 | if (ret < 0) { |
| 382 | kfree_skb(skb); |
| 383 | return NET_XMIT_DROP; |
| 384 | } |
| 385 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 386 | if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) { |
| 387 | kfree_skb(skb); |
| 388 | return NET_XMIT_DROP; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Phoebe Buckheister | d4b2816 | 2014-05-14 17:43:09 +0200 | [diff] [blame] | 391 | max_single = ieee802154_max_payload(&wpan_hdr); |
| 392 | |
| 393 | if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) { |
| 394 | skb->dev = lowpan_dev_info(dev)->real_dev; |
| 395 | return dev_queue_xmit(skb); |
| 396 | } else { |
| 397 | netdev_tx_t rc; |
| 398 | |
| 399 | pr_debug("frame is too big, fragmentation is needed\n"); |
| 400 | rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr); |
| 401 | |
| 402 | return rc < 0 ? NET_XMIT_DROP : rc; |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 403 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 406 | static __le16 lowpan_get_pan_id(const struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 407 | { |
| 408 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 409 | |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 410 | return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev); |
| 411 | } |
| 412 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 413 | static __le16 lowpan_get_short_addr(const struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 414 | { |
| 415 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 416 | |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 417 | return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev); |
| 418 | } |
| 419 | |
Tony Cheneau | c7d0ab2 | 2013-03-25 17:59:30 +0000 | [diff] [blame] | 420 | static u8 lowpan_get_dsn(const struct net_device *dev) |
| 421 | { |
| 422 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 423 | |
Tony Cheneau | c7d0ab2 | 2013-03-25 17:59:30 +0000 | [diff] [blame] | 424 | return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev); |
| 425 | } |
| 426 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 427 | static struct header_ops lowpan_header_ops = { |
| 428 | .create = lowpan_header_create, |
| 429 | }; |
| 430 | |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 431 | static struct lock_class_key lowpan_tx_busylock; |
| 432 | static struct lock_class_key lowpan_netdev_xmit_lock_key; |
| 433 | |
| 434 | static void lowpan_set_lockdep_class_one(struct net_device *dev, |
| 435 | struct netdev_queue *txq, |
| 436 | void *_unused) |
| 437 | { |
| 438 | lockdep_set_class(&txq->_xmit_lock, |
| 439 | &lowpan_netdev_xmit_lock_key); |
| 440 | } |
| 441 | |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 442 | static int lowpan_dev_init(struct net_device *dev) |
| 443 | { |
| 444 | netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL); |
| 445 | dev->qdisc_tx_busylock = &lowpan_tx_busylock; |
| 446 | return 0; |
| 447 | } |
| 448 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 449 | static const struct net_device_ops lowpan_netdev_ops = { |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 450 | .ndo_init = lowpan_dev_init, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 451 | .ndo_start_xmit = lowpan_xmit, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 452 | }; |
| 453 | |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 454 | static struct ieee802154_mlme_ops lowpan_mlme = { |
| 455 | .get_pan_id = lowpan_get_pan_id, |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 456 | .get_short_addr = lowpan_get_short_addr, |
Tony Cheneau | c7d0ab2 | 2013-03-25 17:59:30 +0000 | [diff] [blame] | 457 | .get_dsn = lowpan_get_dsn, |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 460 | static void lowpan_setup(struct net_device *dev) |
| 461 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 462 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 463 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
| 464 | dev->type = ARPHRD_IEEE802154; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 465 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
| 466 | dev->hard_header_len = 2 + 1 + 20 + 14; |
| 467 | dev->needed_tailroom = 2; /* FCS */ |
Alexander Aring | 685d632 | 2014-08-19 19:03:31 +0200 | [diff] [blame] | 468 | dev->mtu = IPV6_MIN_MTU; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 469 | dev->tx_queue_len = 0; |
alex.bluesman.smirnov@gmail.com | 4d039f6 | 2011-11-10 07:39:37 +0000 | [diff] [blame] | 470 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 471 | dev->watchdog_timeo = 0; |
| 472 | |
| 473 | dev->netdev_ops = &lowpan_netdev_ops; |
| 474 | dev->header_ops = &lowpan_header_ops; |
alex.bluesman.smirnov@gmail.com | 0848e40 | 2012-04-25 23:24:56 +0000 | [diff] [blame] | 475 | dev->ml_priv = &lowpan_mlme; |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 476 | dev->destructor = free_netdev; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 480 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 481 | if (tb[IFLA_ADDRESS]) { |
| 482 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 489 | struct packet_type *pt, struct net_device *orig_dev) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 490 | { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 491 | struct ieee802154_hdr hdr; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 492 | int ret; |
Alan Ott | a437d27 | 2012-09-01 05:57:06 +0000 | [diff] [blame] | 493 | |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 494 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 495 | if (!skb) |
| 496 | goto drop; |
| 497 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 498 | if (!netif_running(dev)) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 499 | goto drop_skb; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 500 | |
Simon Vincent | 39f6eb1 | 2014-10-05 20:23:12 +0200 | [diff] [blame] | 501 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 502 | goto drop_skb; |
| 503 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 504 | if (dev->type != ARPHRD_IEEE802154) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 505 | goto drop_skb; |
| 506 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 507 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) |
| 508 | goto drop_skb; |
| 509 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 510 | /* check that it's our buffer */ |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 511 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 512 | /* Pull off the 1-byte of 6lowpan header. */ |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 513 | skb_pull(skb, 1); |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 514 | return lowpan_give_skb_to_devices(skb, NULL); |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 515 | } else { |
| 516 | switch (skb->data[0] & 0xe0) { |
| 517 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 518 | ret = iphc_decompress(skb, &hdr); |
Martin Townsend | 04dfd73 | 2014-10-23 15:40:54 +0100 | [diff] [blame] | 519 | if (ret < 0) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 520 | goto drop_skb; |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 521 | |
| 522 | return lowpan_give_skb_to_devices(skb, NULL); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 523 | case LOWPAN_DISPATCH_FRAG1: /* first fragment header */ |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 524 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 525 | if (ret == 1) { |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 526 | ret = iphc_decompress(skb, &hdr); |
Martin Townsend | 04dfd73 | 2014-10-23 15:40:54 +0100 | [diff] [blame] | 527 | if (ret < 0) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 528 | goto drop_skb; |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 529 | |
| 530 | return lowpan_give_skb_to_devices(skb, NULL); |
| 531 | } else if (ret == -1) { |
| 532 | return NET_RX_DROP; |
| 533 | } else { |
| 534 | return NET_RX_SUCCESS; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 535 | } |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 536 | case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */ |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 537 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 538 | if (ret == 1) { |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 539 | ret = iphc_decompress(skb, &hdr); |
Martin Townsend | 04dfd73 | 2014-10-23 15:40:54 +0100 | [diff] [blame] | 540 | if (ret < 0) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 541 | goto drop_skb; |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 542 | |
| 543 | return lowpan_give_skb_to_devices(skb, NULL); |
| 544 | } else if (ret == -1) { |
| 545 | return NET_RX_DROP; |
| 546 | } else { |
| 547 | return NET_RX_SUCCESS; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 548 | } |
Alan Ott | ee21c7e | 2013-01-16 19:09:48 +0000 | [diff] [blame] | 549 | default: |
| 550 | break; |
| 551 | } |
alex.bluesman.smirnov@gmail.com | 719269a | 2011-11-10 07:38:38 +0000 | [diff] [blame] | 552 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 553 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 554 | drop_skb: |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 555 | kfree_skb(skb); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 556 | drop: |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 557 | return NET_RX_DROP; |
| 558 | } |
| 559 | |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 560 | static struct packet_type lowpan_packet_type = { |
| 561 | .type = htons(ETH_P_IEEE802154), |
| 562 | .func = lowpan_rcv, |
| 563 | }; |
| 564 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 565 | static int lowpan_newlink(struct net *src_net, struct net_device *dev, |
| 566 | struct nlattr *tb[], struct nlattr *data[]) |
| 567 | { |
| 568 | struct net_device *real_dev; |
| 569 | struct lowpan_dev_record *entry; |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 570 | int ret; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 571 | |
Alexander Aring | c37a810 | 2014-10-13 10:33:07 +0200 | [diff] [blame] | 572 | ASSERT_RTNL(); |
| 573 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 574 | pr_debug("adding new link\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 575 | |
| 576 | if (!tb[IFLA_LINK]) |
| 577 | return -EINVAL; |
| 578 | /* find and hold real wpan device */ |
| 579 | real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
| 580 | if (!real_dev) |
| 581 | return -ENODEV; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 582 | if (real_dev->type != ARPHRD_IEEE802154) { |
| 583 | dev_put(real_dev); |
Alan Ott | 7adac1e | 2013-10-05 23:15:18 -0400 | [diff] [blame] | 584 | return -EINVAL; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 585 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 586 | |
| 587 | lowpan_dev_info(dev)->real_dev = real_dev; |
| 588 | mutex_init(&lowpan_dev_info(dev)->dev_list_mtx); |
| 589 | |
Alexander Aring | d57fec8 | 2014-02-28 07:32:48 +0100 | [diff] [blame] | 590 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 591 | if (!entry) { |
| 592 | dev_put(real_dev); |
| 593 | lowpan_dev_info(dev)->real_dev = NULL; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 594 | return -ENOMEM; |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 595 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 596 | |
| 597 | entry->ldev = dev; |
| 598 | |
Varka Bhadram | 69bb631 | 2014-11-25 10:04:57 +0530 | [diff] [blame] | 599 | /* Set the lowpan hardware address to the wpan hardware address. */ |
Alan Ott | ab2d95d | 2013-10-05 23:15:19 -0400 | [diff] [blame] | 600 | memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN); |
| 601 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 602 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 603 | INIT_LIST_HEAD(&entry->list); |
| 604 | list_add_tail(&entry->list, &lowpan_devices); |
| 605 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 606 | |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 607 | ret = register_netdevice(dev); |
| 608 | if (ret >= 0) { |
| 609 | if (!lowpan_open_count) |
| 610 | dev_add_pack(&lowpan_packet_type); |
| 611 | lowpan_open_count++; |
| 612 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 613 | |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 614 | return ret; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void lowpan_dellink(struct net_device *dev, struct list_head *head) |
| 618 | { |
| 619 | struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev); |
| 620 | struct net_device *real_dev = lowpan_dev->real_dev; |
alex.bluesman.smirnov@gmail.com | 8deff4a | 2012-04-25 23:24:57 +0000 | [diff] [blame] | 621 | struct lowpan_dev_record *entry, *tmp; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 622 | |
| 623 | ASSERT_RTNL(); |
| 624 | |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 625 | lowpan_open_count--; |
| 626 | if (!lowpan_open_count) |
| 627 | dev_remove_pack(&lowpan_packet_type); |
| 628 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 629 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
Dan Carpenter | aec9db3 | 2011-08-30 03:46:40 +0000 | [diff] [blame] | 630 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 631 | if (entry->ldev == dev) { |
| 632 | list_del(&entry->list); |
| 633 | kfree(entry); |
| 634 | } |
Dan Carpenter | aec9db3 | 2011-08-30 03:46:40 +0000 | [diff] [blame] | 635 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 636 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
| 637 | |
| 638 | mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx); |
| 639 | |
| 640 | unregister_netdevice_queue(dev, head); |
| 641 | |
| 642 | dev_put(real_dev); |
| 643 | } |
| 644 | |
| 645 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { |
| 646 | .kind = "lowpan", |
| 647 | .priv_size = sizeof(struct lowpan_dev_info), |
| 648 | .setup = lowpan_setup, |
| 649 | .newlink = lowpan_newlink, |
| 650 | .dellink = lowpan_dellink, |
| 651 | .validate = lowpan_validate, |
| 652 | }; |
| 653 | |
| 654 | static inline int __init lowpan_netlink_init(void) |
| 655 | { |
| 656 | return rtnl_link_register(&lowpan_link_ops); |
| 657 | } |
| 658 | |
David S. Miller | a07fdce | 2013-02-06 15:54:38 -0500 | [diff] [blame] | 659 | static inline void lowpan_netlink_fini(void) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 660 | { |
| 661 | rtnl_link_unregister(&lowpan_link_ops); |
| 662 | } |
| 663 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 664 | static int lowpan_device_event(struct notifier_block *unused, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 665 | unsigned long event, void *ptr) |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 666 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 667 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 668 | LIST_HEAD(del_list); |
| 669 | struct lowpan_dev_record *entry, *tmp; |
| 670 | |
| 671 | if (dev->type != ARPHRD_IEEE802154) |
| 672 | goto out; |
| 673 | |
| 674 | if (event == NETDEV_UNREGISTER) { |
| 675 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
| 676 | if (lowpan_dev_info(entry->ldev)->real_dev == dev) |
| 677 | lowpan_dellink(entry->ldev, &del_list); |
| 678 | } |
| 679 | |
| 680 | unregister_netdevice_many(&del_list); |
Peter Senna Tschudin | 4c83501 | 2012-09-18 07:10:43 +0000 | [diff] [blame] | 681 | } |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 682 | |
| 683 | out: |
| 684 | return NOTIFY_DONE; |
| 685 | } |
| 686 | |
| 687 | static struct notifier_block lowpan_dev_notifier = { |
| 688 | .notifier_call = lowpan_device_event, |
| 689 | }; |
| 690 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 691 | static int __init lowpan_init_module(void) |
| 692 | { |
| 693 | int err = 0; |
| 694 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 695 | err = lowpan_net_frag_init(); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 696 | if (err < 0) |
| 697 | goto out; |
| 698 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 699 | err = lowpan_netlink_init(); |
| 700 | if (err < 0) |
| 701 | goto out_frag; |
| 702 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 703 | err = register_netdevice_notifier(&lowpan_dev_notifier); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 704 | if (err < 0) |
| 705 | goto out_pack; |
| 706 | |
| 707 | return 0; |
| 708 | |
| 709 | out_pack: |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 710 | lowpan_netlink_fini(); |
| 711 | out_frag: |
| 712 | lowpan_net_frag_exit(); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 713 | out: |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | static void __exit lowpan_cleanup_module(void) |
| 718 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 719 | lowpan_netlink_fini(); |
| 720 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 721 | lowpan_net_frag_exit(); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 722 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 723 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | module_init(lowpan_init_module); |
| 727 | module_exit(lowpan_cleanup_module); |
| 728 | MODULE_LICENSE("GPL"); |
| 729 | MODULE_ALIAS_RTNL_LINK("lowpan"); |