blob: b0f04601b0f8caddaccf4e17f326c050fe70ff13 [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 Aring07512722015-08-15 11:00:32 +020055static int open_count;
56
Alexander Smirnov44331fe2011-08-24 19:34:42 -070057static struct header_ops lowpan_header_ops = {
58 .create = lowpan_header_create,
59};
60
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080061static struct lock_class_key lowpan_tx_busylock;
62static struct lock_class_key lowpan_netdev_xmit_lock_key;
63
Alexander Aringf4606582015-09-02 14:21:16 +020064static void lowpan_set_lockdep_class_one(struct net_device *ldev,
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080065 struct netdev_queue *txq,
66 void *_unused)
67{
68 lockdep_set_class(&txq->_xmit_lock,
69 &lowpan_netdev_xmit_lock_key);
70}
71
Alexander Aringf4606582015-09-02 14:21:16 +020072static int lowpan_dev_init(struct net_device *ldev)
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080073{
Alexander Aringf4606582015-09-02 14:21:16 +020074 netdev_for_each_tx_queue(ldev, lowpan_set_lockdep_class_one, NULL);
75 ldev->qdisc_tx_busylock = &lowpan_tx_busylock;
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080076 return 0;
77}
78
Alexander Smirnov44331fe2011-08-24 19:34:42 -070079static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080080 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070081 .ndo_start_xmit = lowpan_xmit,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070082};
83
Alexander Aringf4606582015-09-02 14:21:16 +020084static void lowpan_setup(struct net_device *ldev)
Alexander Smirnov44331fe2011-08-24 19:34:42 -070085{
Alexander Aringf4606582015-09-02 14:21:16 +020086 ldev->addr_len = IEEE802154_ADDR_LEN;
87 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
88 ldev->type = ARPHRD_6LOWPAN;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070089 /* Frame Control + Sequence Number + Address fields + Security Header */
Alexander Aringf4606582015-09-02 14:21:16 +020090 ldev->hard_header_len = 2 + 1 + 20 + 14;
91 ldev->needed_tailroom = 2; /* FCS */
92 ldev->mtu = IPV6_MIN_MTU;
93 ldev->priv_flags |= IFF_NO_QUEUE;
94 ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
95 ldev->watchdog_timeo = 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070096
Alexander Aringf4606582015-09-02 14:21:16 +020097 ldev->netdev_ops = &lowpan_netdev_ops;
98 ldev->header_ops = &lowpan_header_ops;
99 ldev->destructor = free_netdev;
100 ldev->features |= NETIF_F_NETNS_LOCAL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700101}
102
103static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
104{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700105 if (tb[IFLA_ADDRESS]) {
106 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
107 return -EINVAL;
108 }
109 return 0;
110}
111
Alexander Aringf4606582015-09-02 14:21:16 +0200112static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700113 struct nlattr *tb[], struct nlattr *data[])
114{
Alexander Aringf4606582015-09-02 14:21:16 +0200115 struct net_device *wdev;
Alexander Aring1ae26052014-10-13 10:33:06 +0200116 int ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700117
Alexander Aringc37a8102014-10-13 10:33:07 +0200118 ASSERT_RTNL();
119
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000120 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700121
Nicolas Dichtelf9d1ce82015-02-05 18:21:30 +0100122 if (!tb[IFLA_LINK] ||
Alexander Aringf4606582015-09-02 14:21:16 +0200123 !net_eq(dev_net(ldev), &init_net))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700124 return -EINVAL;
Alexander Aringf4606582015-09-02 14:21:16 +0200125 /* find and hold wpan device */
126 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
127 if (!wdev)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700128 return -ENODEV;
Alexander Aringf4606582015-09-02 14:21:16 +0200129 if (wdev->type != ARPHRD_IEEE802154) {
130 dev_put(wdev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400131 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300132 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700133
Alexander Aringf4606582015-09-02 14:21:16 +0200134 if (wdev->ieee802154_ptr->lowpan_dev) {
135 dev_put(wdev);
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200136 return -EBUSY;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000137 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700138
Alexander Aringf4606582015-09-02 14:21:16 +0200139 lowpan_dev_info(ldev)->wdev = wdev;
Varka Bhadram69bb6312014-11-25 10:04:57 +0530140 /* Set the lowpan hardware address to the wpan hardware address. */
Alexander Aringf4606582015-09-02 14:21:16 +0200141 memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
Alan Ottab2d95d2013-10-05 23:15:19 -0400142
Alexander Aringf4606582015-09-02 14:21:16 +0200143 lowpan_netdev_setup(ldev, LOWPAN_LLTYPE_IEEE802154);
Alexander Aringb72f6f512015-08-11 21:44:08 +0200144
Alexander Aringf4606582015-09-02 14:21:16 +0200145 ret = register_netdevice(ldev);
Alexander Aring07512722015-08-15 11:00:32 +0200146 if (ret < 0) {
Alexander Aringf4606582015-09-02 14:21:16 +0200147 dev_put(wdev);
Alexander Aring07512722015-08-15 11:00:32 +0200148 return ret;
Alexander Aring1ae26052014-10-13 10:33:06 +0200149 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700150
Alexander Aringf4606582015-09-02 14:21:16 +0200151 wdev->ieee802154_ptr->lowpan_dev = ldev;
Alexander Aring07512722015-08-15 11:00:32 +0200152 if (!open_count)
153 lowpan_rx_init();
154
155 open_count++;
156
157 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700158}
159
Alexander Aringf4606582015-09-02 14:21:16 +0200160static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700161{
Alexander Aringf4606582015-09-02 14:21:16 +0200162 struct net_device *wdev = lowpan_dev_info(ldev)->wdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700163
164 ASSERT_RTNL();
165
Alexander Aring07512722015-08-15 11:00:32 +0200166 open_count--;
167
168 if (!open_count)
169 lowpan_rx_exit();
170
Alexander Aringf4606582015-09-02 14:21:16 +0200171 wdev->ieee802154_ptr->lowpan_dev = NULL;
172 unregister_netdevice(ldev);
173 dev_put(wdev);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700174}
175
176static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
177 .kind = "lowpan",
Alexander Aringb72f6f512015-08-11 21:44:08 +0200178 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev_info)),
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700179 .setup = lowpan_setup,
180 .newlink = lowpan_newlink,
181 .dellink = lowpan_dellink,
182 .validate = lowpan_validate,
183};
184
185static inline int __init lowpan_netlink_init(void)
186{
187 return rtnl_link_register(&lowpan_link_ops);
188}
189
David S. Millera07fdce2013-02-06 15:54:38 -0500190static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700191{
192 rtnl_link_unregister(&lowpan_link_ops);
193}
194
Alan Otta2dc375e2012-09-01 05:57:07 +0000195static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000196 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000197{
Alexander Aringf4606582015-09-02 14:21:16 +0200198 struct net_device *wdev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000199
Alexander Aringf4606582015-09-02 14:21:16 +0200200 if (wdev->type != ARPHRD_IEEE802154)
Alan Otta2dc375e2012-09-01 05:57:07 +0000201 goto out;
202
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200203 switch (event) {
204 case NETDEV_UNREGISTER:
205 /* Check if wpan interface is unregistered that we
206 * also delete possible lowpan interfaces which belongs
207 * to the wpan interface.
208 */
Alexander Aringf4606582015-09-02 14:21:16 +0200209 if (wdev->ieee802154_ptr && wdev->ieee802154_ptr->lowpan_dev)
210 lowpan_dellink(wdev->ieee802154_ptr->lowpan_dev, NULL);
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200211 break;
212 default:
213 break;
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000214 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000215
216out:
217 return NOTIFY_DONE;
218}
219
220static struct notifier_block lowpan_dev_notifier = {
221 .notifier_call = lowpan_device_event,
222};
223
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700224static int __init lowpan_init_module(void)
225{
226 int err = 0;
227
Alexander Aring7240cde2014-02-28 07:32:50 +0100228 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700229 if (err < 0)
230 goto out;
231
Alexander Aring7240cde2014-02-28 07:32:50 +0100232 err = lowpan_netlink_init();
233 if (err < 0)
234 goto out_frag;
235
Alan Otta2dc375e2012-09-01 05:57:07 +0000236 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100237 if (err < 0)
238 goto out_pack;
239
240 return 0;
241
242out_pack:
Alexander Aring7240cde2014-02-28 07:32:50 +0100243 lowpan_netlink_fini();
244out_frag:
245 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700246out:
247 return err;
248}
249
250static void __exit lowpan_cleanup_module(void)
251{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700252 lowpan_netlink_fini();
253
Alexander Aring7240cde2014-02-28 07:32:50 +0100254 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000255
Alexander Aring7240cde2014-02-28 07:32:50 +0100256 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700257}
258
259module_init(lowpan_init_module);
260module_exit(lowpan_cleanup_module);
261MODULE_LICENSE("GPL");
262MODULE_ALIAS_RTNL_LINK("lowpan");