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/module.h> |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 48 | #include <linux/netdevice.h> |
Alexander Aring | 4ca24ac | 2014-10-25 09:41:04 +0200 | [diff] [blame] | 49 | #include <linux/ieee802154.h> |
Alexander Aring | 4dc315e | 2015-01-04 17:10:56 +0100 | [diff] [blame] | 50 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 51 | #include <net/ipv6.h> |
| 52 | |
Alexander Aring | 8691ee5 | 2015-01-04 17:10:54 +0100 | [diff] [blame] | 53 | #include "6lowpan_i.h" |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 54 | |
Alexander Aring | 0751272 | 2015-08-15 11:00:32 +0200 | [diff] [blame^] | 55 | static int open_count; |
| 56 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 57 | static struct header_ops lowpan_header_ops = { |
| 58 | .create = lowpan_header_create, |
| 59 | }; |
| 60 | |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 61 | static struct lock_class_key lowpan_tx_busylock; |
| 62 | static struct lock_class_key lowpan_netdev_xmit_lock_key; |
| 63 | |
| 64 | static void lowpan_set_lockdep_class_one(struct net_device *dev, |
| 65 | struct netdev_queue *txq, |
| 66 | void *_unused) |
| 67 | { |
| 68 | lockdep_set_class(&txq->_xmit_lock, |
| 69 | &lowpan_netdev_xmit_lock_key); |
| 70 | } |
| 71 | |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 72 | static int lowpan_dev_init(struct net_device *dev) |
| 73 | { |
| 74 | netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL); |
| 75 | dev->qdisc_tx_busylock = &lowpan_tx_busylock; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 79 | static const struct net_device_ops lowpan_netdev_ops = { |
Eric Dumazet | 20e7c4e8 | 2014-02-10 11:42:35 -0800 | [diff] [blame] | 80 | .ndo_init = lowpan_dev_init, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 81 | .ndo_start_xmit = lowpan_xmit, |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static void lowpan_setup(struct net_device *dev) |
| 85 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 86 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 87 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
Alexander Aring | 965e613 | 2015-03-02 15:10:03 +0100 | [diff] [blame] | 88 | dev->type = ARPHRD_6LOWPAN; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 89 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
| 90 | dev->hard_header_len = 2 + 1 + 20 + 14; |
| 91 | dev->needed_tailroom = 2; /* FCS */ |
Alexander Aring | 685d632 | 2014-08-19 19:03:31 +0200 | [diff] [blame] | 92 | dev->mtu = IPV6_MIN_MTU; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 93 | dev->tx_queue_len = 0; |
alex.bluesman.smirnov@gmail.com | 4d039f6 | 2011-11-10 07:39:37 +0000 | [diff] [blame] | 94 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 95 | dev->watchdog_timeo = 0; |
| 96 | |
| 97 | dev->netdev_ops = &lowpan_netdev_ops; |
| 98 | dev->header_ops = &lowpan_header_ops; |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 99 | dev->destructor = free_netdev; |
Nicolas Dichtel | f9d1ce8 | 2015-02-05 18:21:30 +0100 | [diff] [blame] | 100 | dev->features |= NETIF_F_NETNS_LOCAL; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 104 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 105 | if (tb[IFLA_ADDRESS]) { |
| 106 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 112 | static int lowpan_newlink(struct net *src_net, struct net_device *dev, |
| 113 | struct nlattr *tb[], struct nlattr *data[]) |
| 114 | { |
| 115 | struct net_device *real_dev; |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 116 | int ret; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 117 | |
Alexander Aring | c37a810 | 2014-10-13 10:33:07 +0200 | [diff] [blame] | 118 | ASSERT_RTNL(); |
| 119 | |
alex.bluesman.smirnov@gmail.com | e71094f | 2012-06-25 03:49:03 +0000 | [diff] [blame] | 120 | pr_debug("adding new link\n"); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 121 | |
Nicolas Dichtel | f9d1ce8 | 2015-02-05 18:21:30 +0100 | [diff] [blame] | 122 | if (!tb[IFLA_LINK] || |
| 123 | !net_eq(dev_net(dev), &init_net)) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | /* find and hold real wpan device */ |
Nicolas Dichtel | f9d1ce8 | 2015-02-05 18:21:30 +0100 | [diff] [blame] | 126 | real_dev = dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 127 | if (!real_dev) |
| 128 | return -ENODEV; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 129 | if (real_dev->type != ARPHRD_IEEE802154) { |
| 130 | dev_put(real_dev); |
Alan Ott | 7adac1e | 2013-10-05 23:15:18 -0400 | [diff] [blame] | 131 | return -EINVAL; |
Dan Carpenter | 78032f9 | 2013-11-07 10:44:45 +0300 | [diff] [blame] | 132 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 133 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 134 | if (real_dev->ieee802154_ptr->lowpan_dev) { |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 135 | dev_put(real_dev); |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 136 | return -EBUSY; |
Dan Carpenter | dc00fd4 | 2011-08-30 03:51:09 +0000 | [diff] [blame] | 137 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 138 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 139 | lowpan_dev_info(dev)->real_dev = real_dev; |
Varka Bhadram | 69bb631 | 2014-11-25 10:04:57 +0530 | [diff] [blame] | 140 | /* Set the lowpan hardware address to the wpan hardware address. */ |
Alan Ott | ab2d95d | 2013-10-05 23:15:19 -0400 | [diff] [blame] | 141 | memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN); |
| 142 | |
Alexander Aring | b72f6f51 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 143 | lowpan_netdev_setup(dev, LOWPAN_LLTYPE_IEEE802154); |
| 144 | |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 145 | ret = register_netdevice(dev); |
Alexander Aring | 0751272 | 2015-08-15 11:00:32 +0200 | [diff] [blame^] | 146 | if (ret < 0) { |
| 147 | dev_put(real_dev); |
| 148 | return ret; |
Alexander Aring | 1ae2605 | 2014-10-13 10:33:06 +0200 | [diff] [blame] | 149 | } |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 150 | |
Alexander Aring | 0751272 | 2015-08-15 11:00:32 +0200 | [diff] [blame^] | 151 | real_dev->ieee802154_ptr->lowpan_dev = dev; |
| 152 | if (!open_count) |
| 153 | lowpan_rx_init(); |
| 154 | |
| 155 | open_count++; |
| 156 | |
| 157 | return 0; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void lowpan_dellink(struct net_device *dev, struct list_head *head) |
| 161 | { |
| 162 | struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev); |
| 163 | struct net_device *real_dev = lowpan_dev->real_dev; |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 164 | |
| 165 | ASSERT_RTNL(); |
| 166 | |
Alexander Aring | 0751272 | 2015-08-15 11:00:32 +0200 | [diff] [blame^] | 167 | open_count--; |
| 168 | |
| 169 | if (!open_count) |
| 170 | lowpan_rx_exit(); |
| 171 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 172 | real_dev->ieee802154_ptr->lowpan_dev = NULL; |
| 173 | unregister_netdevice(dev); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 174 | dev_put(real_dev); |
| 175 | } |
| 176 | |
| 177 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { |
| 178 | .kind = "lowpan", |
Alexander Aring | b72f6f51 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 179 | .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev_info)), |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 180 | .setup = lowpan_setup, |
| 181 | .newlink = lowpan_newlink, |
| 182 | .dellink = lowpan_dellink, |
| 183 | .validate = lowpan_validate, |
| 184 | }; |
| 185 | |
| 186 | static inline int __init lowpan_netlink_init(void) |
| 187 | { |
| 188 | return rtnl_link_register(&lowpan_link_ops); |
| 189 | } |
| 190 | |
David S. Miller | a07fdce | 2013-02-06 15:54:38 -0500 | [diff] [blame] | 191 | static inline void lowpan_netlink_fini(void) |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 192 | { |
| 193 | rtnl_link_unregister(&lowpan_link_ops); |
| 194 | } |
| 195 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 196 | static int lowpan_device_event(struct notifier_block *unused, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 197 | unsigned long event, void *ptr) |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 198 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 199 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 200 | |
| 201 | if (dev->type != ARPHRD_IEEE802154) |
| 202 | goto out; |
| 203 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 204 | switch (event) { |
| 205 | case NETDEV_UNREGISTER: |
| 206 | /* Check if wpan interface is unregistered that we |
| 207 | * also delete possible lowpan interfaces which belongs |
| 208 | * to the wpan interface. |
| 209 | */ |
| 210 | if (dev->ieee802154_ptr && dev->ieee802154_ptr->lowpan_dev) |
| 211 | lowpan_dellink(dev->ieee802154_ptr->lowpan_dev, NULL); |
| 212 | break; |
| 213 | default: |
| 214 | break; |
Peter Senna Tschudin | 4c83501 | 2012-09-18 07:10:43 +0000 | [diff] [blame] | 215 | } |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 216 | |
| 217 | out: |
| 218 | return NOTIFY_DONE; |
| 219 | } |
| 220 | |
| 221 | static struct notifier_block lowpan_dev_notifier = { |
| 222 | .notifier_call = lowpan_device_event, |
| 223 | }; |
| 224 | |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 225 | static int __init lowpan_init_module(void) |
| 226 | { |
| 227 | int err = 0; |
| 228 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 229 | err = lowpan_net_frag_init(); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 230 | if (err < 0) |
| 231 | goto out; |
| 232 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 233 | err = lowpan_netlink_init(); |
| 234 | if (err < 0) |
| 235 | goto out_frag; |
| 236 | |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 237 | err = register_netdevice_notifier(&lowpan_dev_notifier); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 238 | if (err < 0) |
| 239 | goto out_pack; |
| 240 | |
| 241 | return 0; |
| 242 | |
| 243 | out_pack: |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 244 | lowpan_netlink_fini(); |
| 245 | out_frag: |
| 246 | lowpan_net_frag_exit(); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 247 | out: |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static void __exit lowpan_cleanup_module(void) |
| 252 | { |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 253 | lowpan_netlink_fini(); |
| 254 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 255 | lowpan_net_frag_exit(); |
Alan Ott | a2dc375e | 2012-09-01 05:57:07 +0000 | [diff] [blame] | 256 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 257 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
Alexander Smirnov | 44331fe | 2011-08-24 19:34:42 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | module_init(lowpan_init_module); |
| 261 | module_exit(lowpan_cleanup_module); |
| 262 | MODULE_LICENSE("GPL"); |
| 263 | MODULE_ALIAS_RTNL_LINK("lowpan"); |