blob: 974765b7d92a75546fe5ae5dbc332078a54f627a [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 Aring4dc315e22015-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
Bhumika Goyal96a1c172017-08-25 19:51:43 +053057static const struct header_ops lowpan_header_ops = {
Alexander Smirnov44331fe2011-08-24 19:34:42 -070058 .create = lowpan_header_create,
59};
60
Alexander Aringf4606582015-09-02 14:21:16 +020061static int lowpan_dev_init(struct net_device *ldev)
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080062{
Eric Dumazetd3fff6c2016-06-09 07:45:12 -070063 netdev_lockdep_set_classes(ldev);
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070064
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080065 return 0;
66}
67
Alexander Aring90997af2015-09-02 14:21:17 +020068static int lowpan_open(struct net_device *dev)
69{
70 if (!open_count)
71 lowpan_rx_init();
72 open_count++;
73 return 0;
74}
75
76static int lowpan_stop(struct net_device *dev)
77{
78 open_count--;
79 if (!open_count)
80 lowpan_rx_exit();
81 return 0;
82}
83
Jiri Pirko503eebc2016-07-05 11:27:37 +020084static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n)
Alexander Aring8626a0c2016-06-15 21:20:16 +020085{
86 struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n));
87
88 /* default no short_addr is available for a neighbour */
89 neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
90 return 0;
91}
92
Alexander Smirnov44331fe2011-08-24 19:34:42 -070093static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -080094 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070095 .ndo_start_xmit = lowpan_xmit,
Alexander Aring90997af2015-09-02 14:21:17 +020096 .ndo_open = lowpan_open,
97 .ndo_stop = lowpan_stop,
Alexander Aring8626a0c2016-06-15 21:20:16 +020098 .ndo_neigh_construct = lowpan_neigh_construct,
Alexander Smirnov44331fe2011-08-24 19:34:42 -070099};
100
Alexander Aringf4606582015-09-02 14:21:16 +0200101static void lowpan_setup(struct net_device *ldev)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700102{
Alexander Aringf4606582015-09-02 14:21:16 +0200103 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
Alexander Aring87a93e42015-09-18 11:30:43 +0200104 /* We need an ipv6hdr as minimum len when calling xmit */
105 ldev->hard_header_len = sizeof(struct ipv6hdr);
Alexander Aringf4606582015-09-02 14:21:16 +0200106 ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700107
Alexander Aringf4606582015-09-02 14:21:16 +0200108 ldev->netdev_ops = &lowpan_netdev_ops;
109 ldev->header_ops = &lowpan_header_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400110 ldev->needs_free_netdev = true;
Alexander Aringf4606582015-09-02 14:21:16 +0200111 ldev->features |= NETIF_F_NETNS_LOCAL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700112}
113
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200114static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[],
115 struct netlink_ext_ack *extack)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700116{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700117 if (tb[IFLA_ADDRESS]) {
118 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
119 return -EINVAL;
120 }
121 return 0;
122}
123
Alexander Aringf4606582015-09-02 14:21:16 +0200124static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +0200125 struct nlattr *tb[], struct nlattr *data[],
126 struct netlink_ext_ack *extack)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700127{
Alexander Aringf4606582015-09-02 14:21:16 +0200128 struct net_device *wdev;
Alexander Aring1ae26052014-10-13 10:33:06 +0200129 int ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700130
Alexander Aringc37a8102014-10-13 10:33:07 +0200131 ASSERT_RTNL();
132
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000133 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700134
Alexander Aring1c5bf998b2016-06-18 10:45:35 +0200135 if (!tb[IFLA_LINK])
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700136 return -EINVAL;
Alexander Aringf4606582015-09-02 14:21:16 +0200137 /* find and hold wpan device */
138 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
139 if (!wdev)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700140 return -ENODEV;
Alexander Aringf4606582015-09-02 14:21:16 +0200141 if (wdev->type != ARPHRD_IEEE802154) {
142 dev_put(wdev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400143 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300144 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700145
Alexander Aringf4606582015-09-02 14:21:16 +0200146 if (wdev->ieee802154_ptr->lowpan_dev) {
147 dev_put(wdev);
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200148 return -EBUSY;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000149 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700150
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200151 lowpan_802154_dev(ldev)->wdev = wdev;
Varka Bhadram69bb6312014-11-25 10:04:57 +0530152 /* Set the lowpan hardware address to the wpan hardware address. */
Alexander Aringf4606582015-09-02 14:21:16 +0200153 memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
Alexander Aring87a93e42015-09-18 11:30:43 +0200154 /* We need headroom for possible wpan_dev_hard_header call and tailroom
155 * for encryption/fcs handling. The lowpan interface will replace
156 * the IPv6 header with 6LoWPAN header. At worst case the 6LoWPAN
157 * header has LOWPAN_IPHC_MAX_HEADER_LEN more bytes than the IPv6
158 * header.
159 */
160 ldev->needed_headroom = LOWPAN_IPHC_MAX_HEADER_LEN +
161 wdev->needed_headroom;
162 ldev->needed_tailroom = wdev->needed_tailroom;
Alan Ottab2d95d2013-10-05 23:15:19 -0400163
Alexander Aring8626a0c2016-06-15 21:20:16 +0200164 ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh);
165
Alexander Aring00f59312015-12-09 22:46:29 +0100166 ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154);
Alexander Aring07512722015-08-15 11:00:32 +0200167 if (ret < 0) {
Alexander Aringf4606582015-09-02 14:21:16 +0200168 dev_put(wdev);
Alexander Aring07512722015-08-15 11:00:32 +0200169 return ret;
Alexander Aring1ae26052014-10-13 10:33:06 +0200170 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700171
Alexander Aringf4606582015-09-02 14:21:16 +0200172 wdev->ieee802154_ptr->lowpan_dev = ldev;
Alexander Aring07512722015-08-15 11:00:32 +0200173 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700174}
175
Alexander Aringf4606582015-09-02 14:21:16 +0200176static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700177{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200178 struct net_device *wdev = lowpan_802154_dev(ldev)->wdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700179
180 ASSERT_RTNL();
181
Alexander Aringf4606582015-09-02 14:21:16 +0200182 wdev->ieee802154_ptr->lowpan_dev = NULL;
Alexander Aring00f59312015-12-09 22:46:29 +0100183 lowpan_unregister_netdevice(ldev);
Alexander Aringf4606582015-09-02 14:21:16 +0200184 dev_put(wdev);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700185}
186
187static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
188 .kind = "lowpan",
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200189 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)),
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700190 .setup = lowpan_setup,
191 .newlink = lowpan_newlink,
192 .dellink = lowpan_dellink,
193 .validate = lowpan_validate,
194};
195
196static inline int __init lowpan_netlink_init(void)
197{
198 return rtnl_link_register(&lowpan_link_ops);
199}
200
David S. Millera07fdce2013-02-06 15:54:38 -0500201static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700202{
203 rtnl_link_unregister(&lowpan_link_ops);
204}
205
Alan Otta2dc375e2012-09-01 05:57:07 +0000206static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000207 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000208{
Alexander Aringf4606582015-09-02 14:21:16 +0200209 struct net_device *wdev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000210
Alexander Aringf4606582015-09-02 14:21:16 +0200211 if (wdev->type != ARPHRD_IEEE802154)
Alexander Aringebba3802016-02-22 09:13:55 +0100212 return NOTIFY_DONE;
Alan Otta2dc375e2012-09-01 05:57:07 +0000213
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200214 switch (event) {
215 case NETDEV_UNREGISTER:
216 /* Check if wpan interface is unregistered that we
217 * also delete possible lowpan interfaces which belongs
218 * to the wpan interface.
219 */
Alexander Aring2c88b522015-09-02 14:21:18 +0200220 if (wdev->ieee802154_ptr->lowpan_dev)
Alexander Aringf4606582015-09-02 14:21:16 +0200221 lowpan_dellink(wdev->ieee802154_ptr->lowpan_dev, NULL);
Alexander Aring51e0e5d2015-08-10 21:15:53 +0200222 break;
223 default:
Alexander Aringebba3802016-02-22 09:13:55 +0100224 return NOTIFY_DONE;
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000225 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000226
Alexander Aringebba3802016-02-22 09:13:55 +0100227 return NOTIFY_OK;
Alan Otta2dc375e2012-09-01 05:57:07 +0000228}
229
230static struct notifier_block lowpan_dev_notifier = {
231 .notifier_call = lowpan_device_event,
232};
233
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700234static int __init lowpan_init_module(void)
235{
236 int err = 0;
237
Alexander Aring7240cde2014-02-28 07:32:50 +0100238 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700239 if (err < 0)
240 goto out;
241
Alexander Aring7240cde2014-02-28 07:32:50 +0100242 err = lowpan_netlink_init();
243 if (err < 0)
244 goto out_frag;
245
Alan Otta2dc375e2012-09-01 05:57:07 +0000246 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100247 if (err < 0)
248 goto out_pack;
249
250 return 0;
251
252out_pack:
Alexander Aring7240cde2014-02-28 07:32:50 +0100253 lowpan_netlink_fini();
254out_frag:
255 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700256out:
257 return err;
258}
259
260static void __exit lowpan_cleanup_module(void)
261{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700262 lowpan_netlink_fini();
263
Alexander Aring7240cde2014-02-28 07:32:50 +0100264 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000265
Alexander Aring7240cde2014-02-28 07:32:50 +0100266 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700267}
268
269module_init(lowpan_init_module);
270module_exit(lowpan_cleanup_module);
271MODULE_LICENSE("GPL");
272MODULE_ALIAS_RTNL_LINK("lowpan");