blob: 180e9f5f86c37239b58ada88015b63186dff314a [file] [log] [blame]
Alexander Aringd57fec82014-02-28 07:32:48 +01001/* Copyright 2011, Siemens AG
Alexander Smirnov44331fe2011-08-24 19:34:42 -07002 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
3 */
4
Alexander Aringd57fec82014-02-28 07:32:48 +01005/* Based on patches from Jon Smirl <jonsmirl@gmail.com>
Alexander Smirnov44331fe2011-08-24 19:34:42 -07006 * 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 Smirnov44331fe2011-08-24 19:34:42 -070016 */
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 Smirnov44331fe2011-08-24 19:34:42 -070047#include <linux/module.h>
Alexander Smirnov44331fe2011-08-24 19:34:42 -070048#include <linux/netdevice.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020049#include <linux/ieee802154.h>
Alexander Aring4dc315e2015-01-04 17:10:56 +010050
Alexander Smirnov44331fe2011-08-24 19:34:42 -070051#include <net/ipv6.h>
52
Alexander Aring8691ee52015-01-04 17:10:54 +010053#include "6lowpan_i.h"
Alexander Smirnov44331fe2011-08-24 19:34:42 -070054
Alexander Smirnov44331fe2011-08-24 19:34:42 -070055static struct header_ops lowpan_header_ops = {
56 .create = lowpan_header_create,
57};
58
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080059static struct lock_class_key lowpan_tx_busylock;
60static struct lock_class_key lowpan_netdev_xmit_lock_key;
61
62static void lowpan_set_lockdep_class_one(struct net_device *dev,
63 struct netdev_queue *txq,
64 void *_unused)
65{
66 lockdep_set_class(&txq->_xmit_lock,
67 &lowpan_netdev_xmit_lock_key);
68}
69
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080070static int lowpan_dev_init(struct net_device *dev)
71{
72 netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
73 dev->qdisc_tx_busylock = &lowpan_tx_busylock;
74 return 0;
75}
76
Alexander Smirnov44331fe2011-08-24 19:34:42 -070077static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080078 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070079 .ndo_start_xmit = lowpan_xmit,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070080};
81
82static void lowpan_setup(struct net_device *dev)
83{
Alexander Smirnov44331fe2011-08-24 19:34:42 -070084 dev->addr_len = IEEE802154_ADDR_LEN;
85 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
Alexander Aring965e6132015-03-02 15:10:03 +010086 dev->type = ARPHRD_6LOWPAN;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070087 /* Frame Control + Sequence Number + Address fields + Security Header */
88 dev->hard_header_len = 2 + 1 + 20 + 14;
89 dev->needed_tailroom = 2; /* FCS */
Alexander Aring685d6322014-08-19 19:03:31 +020090 dev->mtu = IPV6_MIN_MTU;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070091 dev->tx_queue_len = 0;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +000092 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070093 dev->watchdog_timeo = 0;
94
95 dev->netdev_ops = &lowpan_netdev_ops;
96 dev->header_ops = &lowpan_header_ops;
Alan Otta2dc375e2012-09-01 05:57:07 +000097 dev->destructor = free_netdev;
Nicolas Dichtelf9d1ce82015-02-05 18:21:30 +010098 dev->features |= NETIF_F_NETNS_LOCAL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070099}
100
101static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
102{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700103 if (tb[IFLA_ADDRESS]) {
104 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
105 return -EINVAL;
106 }
107 return 0;
108}
109
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700110static int lowpan_newlink(struct net *src_net, struct net_device *dev,
111 struct nlattr *tb[], struct nlattr *data[])
112{
113 struct net_device *real_dev;
Alexander Aring1ae26052014-10-13 10:33:06 +0200114 int ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700115
Alexander Aringc37a8102014-10-13 10:33:07 +0200116 ASSERT_RTNL();
117
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000118 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700119
Nicolas Dichtelf9d1ce82015-02-05 18:21:30 +0100120 if (!tb[IFLA_LINK] ||
121 !net_eq(dev_net(dev), &init_net))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700122 return -EINVAL;
123 /* find and hold real wpan device */
Nicolas Dichtelf9d1ce82015-02-05 18:21:30 +0100124 real_dev = dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700125 if (!real_dev)
126 return -ENODEV;
Dan Carpenter78032f92013-11-07 10:44:45 +0300127 if (real_dev->type != ARPHRD_IEEE802154) {
128 dev_put(real_dev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400129 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300130 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700131
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200132 if (real_dev->ieee802154_ptr->lowpan_dev) {
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000133 dev_put(real_dev);
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200134 return -EBUSY;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000135 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700136
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200137 lowpan_dev_info(dev)->real_dev = real_dev;
Varka Bhadram69bb6312014-11-25 10:04:57 +0530138 /* Set the lowpan hardware address to the wpan hardware address. */
Alan Ottab2d95d2013-10-05 23:15:19 -0400139 memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
140
Alexander Aringb72f6f512015-08-11 21:44:08 +0200141 lowpan_netdev_setup(dev, LOWPAN_LLTYPE_IEEE802154);
142
Alexander Aring1ae26052014-10-13 10:33:06 +0200143 ret = register_netdevice(dev);
144 if (ret >= 0) {
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200145 real_dev->ieee802154_ptr->lowpan_dev = dev;
146 lowpan_rx_init();
Alexander Aring1ae26052014-10-13 10:33:06 +0200147 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700148
Alexander Aring1ae26052014-10-13 10:33:06 +0200149 return ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700150}
151
152static void lowpan_dellink(struct net_device *dev, struct list_head *head)
153{
154 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
155 struct net_device *real_dev = lowpan_dev->real_dev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700156
157 ASSERT_RTNL();
158
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200159 lowpan_rx_exit();
160 real_dev->ieee802154_ptr->lowpan_dev = NULL;
161 unregister_netdevice(dev);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700162 dev_put(real_dev);
163}
164
165static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
166 .kind = "lowpan",
Alexander Aringb72f6f512015-08-11 21:44:08 +0200167 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev_info)),
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700168 .setup = lowpan_setup,
169 .newlink = lowpan_newlink,
170 .dellink = lowpan_dellink,
171 .validate = lowpan_validate,
172};
173
174static inline int __init lowpan_netlink_init(void)
175{
176 return rtnl_link_register(&lowpan_link_ops);
177}
178
David S. Millera07fdce2013-02-06 15:54:38 -0500179static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700180{
181 rtnl_link_unregister(&lowpan_link_ops);
182}
183
Alan Otta2dc375e2012-09-01 05:57:07 +0000184static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000185 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000186{
Jiri Pirko351638e2013-05-28 01:30:21 +0000187 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000188
189 if (dev->type != ARPHRD_IEEE802154)
190 goto out;
191
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200192 switch (event) {
193 case NETDEV_UNREGISTER:
194 /* Check if wpan interface is unregistered that we
195 * also delete possible lowpan interfaces which belongs
196 * to the wpan interface.
197 */
198 if (dev->ieee802154_ptr && dev->ieee802154_ptr->lowpan_dev)
199 lowpan_dellink(dev->ieee802154_ptr->lowpan_dev, NULL);
200 break;
201 default:
202 break;
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000203 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000204
205out:
206 return NOTIFY_DONE;
207}
208
209static struct notifier_block lowpan_dev_notifier = {
210 .notifier_call = lowpan_device_event,
211};
212
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700213static int __init lowpan_init_module(void)
214{
215 int err = 0;
216
Alexander Aring7240cde2014-02-28 07:32:50 +0100217 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700218 if (err < 0)
219 goto out;
220
Alexander Aring7240cde2014-02-28 07:32:50 +0100221 err = lowpan_netlink_init();
222 if (err < 0)
223 goto out_frag;
224
Alan Otta2dc375e2012-09-01 05:57:07 +0000225 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100226 if (err < 0)
227 goto out_pack;
228
229 return 0;
230
231out_pack:
Alexander Aring7240cde2014-02-28 07:32:50 +0100232 lowpan_netlink_fini();
233out_frag:
234 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700235out:
236 return err;
237}
238
239static void __exit lowpan_cleanup_module(void)
240{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700241 lowpan_netlink_fini();
242
Alexander Aring7240cde2014-02-28 07:32:50 +0100243 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000244
Alexander Aring7240cde2014-02-28 07:32:50 +0100245 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700246}
247
248module_init(lowpan_init_module);
249module_exit(lowpan_cleanup_module);
250MODULE_LICENSE("GPL");
251MODULE_ALIAS_RTNL_LINK("lowpan");