]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ieee802154/6lowpan/core.c
ieee802154: 6lowpan: fix possible NULL deref in lowpan_device_event()
[mirror_ubuntu-bionic-kernel.git] / net / ieee802154 / 6lowpan / core.c
CommitLineData
d57fec84 1/* Copyright 2011, Siemens AG
44331fe2
AS
2 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
3 */
4
d57fec84 5/* Based on patches from Jon Smirl <jonsmirl@gmail.com>
44331fe2
AS
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.
44331fe2
AS
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
44331fe2 47#include <linux/module.h>
44331fe2 48#include <linux/netdevice.h>
4ca24aca 49#include <linux/ieee802154.h>
4dc315e2 50
44331fe2
AS
51#include <net/ipv6.h>
52
8691ee59 53#include "6lowpan_i.h"
44331fe2 54
07512728
AA
55static int open_count;
56
96a1c173 57static const struct header_ops lowpan_header_ops = {
44331fe2
AS
58 .create = lowpan_header_create,
59};
60
f4606583 61static int lowpan_dev_init(struct net_device *ldev)
20e7c4e8 62{
d3fff6c4 63 netdev_lockdep_set_classes(ldev);
f9eb8aea 64
20e7c4e8
ED
65 return 0;
66}
67
90997af7
AA
68static 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
503eebc2 84static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n)
8626a0c8
AA
85{
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
44331fe2 93static const struct net_device_ops lowpan_netdev_ops = {
20e7c4e8 94 .ndo_init = lowpan_dev_init,
44331fe2 95 .ndo_start_xmit = lowpan_xmit,
90997af7
AA
96 .ndo_open = lowpan_open,
97 .ndo_stop = lowpan_stop,
8626a0c8 98 .ndo_neigh_construct = lowpan_neigh_construct,
44331fe2
AS
99};
100
f4606583 101static void lowpan_setup(struct net_device *ldev)
44331fe2 102{
f4606583 103 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
87a93e4e
AA
104 /* We need an ipv6hdr as minimum len when calling xmit */
105 ldev->hard_header_len = sizeof(struct ipv6hdr);
f4606583 106 ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
f4606583
AA
107
108 ldev->netdev_ops = &lowpan_netdev_ops;
109 ldev->header_ops = &lowpan_header_ops;
cf124db5 110 ldev->needs_free_netdev = true;
f4606583 111 ldev->features |= NETIF_F_NETNS_LOCAL;
44331fe2
AS
112}
113
a8b8a889
MS
114static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[],
115 struct netlink_ext_ack *extack)
44331fe2 116{
44331fe2
AS
117 if (tb[IFLA_ADDRESS]) {
118 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
119 return -EINVAL;
120 }
121 return 0;
122}
123
f4606583 124static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
7a3f4a18
MS
125 struct nlattr *tb[], struct nlattr *data[],
126 struct netlink_ext_ack *extack)
44331fe2 127{
f4606583 128 struct net_device *wdev;
1ae2605e 129 int ret;
44331fe2 130
c37a8106
AA
131 ASSERT_RTNL();
132
e71094f9 133 pr_debug("adding new link\n");
44331fe2 134
1c5bf998 135 if (!tb[IFLA_LINK])
44331fe2 136 return -EINVAL;
f4606583
AA
137 /* find and hold wpan device */
138 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
139 if (!wdev)
44331fe2 140 return -ENODEV;
f4606583
AA
141 if (wdev->type != ARPHRD_IEEE802154) {
142 dev_put(wdev);
7adac1ec 143 return -EINVAL;
78032f9b 144 }
44331fe2 145
f4606583
AA
146 if (wdev->ieee802154_ptr->lowpan_dev) {
147 dev_put(wdev);
51e0e5d8 148 return -EBUSY;
dc00fd44 149 }
44331fe2 150
2e4d60cb 151 lowpan_802154_dev(ldev)->wdev = wdev;
69bb631e 152 /* Set the lowpan hardware address to the wpan hardware address. */
f4606583 153 memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
87a93e4e
AA
154 /* 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;
ab2d95df 163
8626a0c8
AA
164 ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh);
165
00f59314 166 ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154);
07512728 167 if (ret < 0) {
f4606583 168 dev_put(wdev);
07512728 169 return ret;
1ae2605e 170 }
44331fe2 171
f4606583 172 wdev->ieee802154_ptr->lowpan_dev = ldev;
07512728 173 return 0;
44331fe2
AS
174}
175
f4606583 176static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
44331fe2 177{
2e4d60cb 178 struct net_device *wdev = lowpan_802154_dev(ldev)->wdev;
44331fe2
AS
179
180 ASSERT_RTNL();
181
f4606583 182 wdev->ieee802154_ptr->lowpan_dev = NULL;
00f59314 183 lowpan_unregister_netdevice(ldev);
f4606583 184 dev_put(wdev);
44331fe2
AS
185}
186
187static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
188 .kind = "lowpan",
2e4d60cb 189 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)),
44331fe2
AS
190 .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
a07fdcec 201static inline void lowpan_netlink_fini(void)
44331fe2
AS
202{
203 rtnl_link_unregister(&lowpan_link_ops);
204}
205
a2dc375e 206static int lowpan_device_event(struct notifier_block *unused,
351638e7 207 unsigned long event, void *ptr)
a2dc375e 208{
fc9f8952
ED
209 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
210 struct wpan_dev *wpan_dev;
a2dc375e 211
fc9f8952
ED
212 if (ndev->type != ARPHRD_IEEE802154)
213 return NOTIFY_DONE;
214 wpan_dev = ndev->ieee802154_ptr;
215 if (!wpan_dev)
ebba380c 216 return NOTIFY_DONE;
a2dc375e 217
51e0e5d8
AA
218 switch (event) {
219 case NETDEV_UNREGISTER:
220 /* Check if wpan interface is unregistered that we
221 * also delete possible lowpan interfaces which belongs
222 * to the wpan interface.
223 */
fc9f8952
ED
224 if (wpan_dev->lowpan_dev)
225 lowpan_dellink(wpan_dev->lowpan_dev, NULL);
51e0e5d8
AA
226 break;
227 default:
ebba380c 228 return NOTIFY_DONE;
4c835019 229 }
a2dc375e 230
ebba380c 231 return NOTIFY_OK;
a2dc375e
AO
232}
233
234static struct notifier_block lowpan_dev_notifier = {
235 .notifier_call = lowpan_device_event,
236};
237
44331fe2
AS
238static int __init lowpan_init_module(void)
239{
240 int err = 0;
241
7240cdec 242 err = lowpan_net_frag_init();
44331fe2
AS
243 if (err < 0)
244 goto out;
245
7240cdec
AA
246 err = lowpan_netlink_init();
247 if (err < 0)
248 goto out_frag;
249
a2dc375e 250 err = register_netdevice_notifier(&lowpan_dev_notifier);
7240cdec
AA
251 if (err < 0)
252 goto out_pack;
253
254 return 0;
255
256out_pack:
7240cdec
AA
257 lowpan_netlink_fini();
258out_frag:
259 lowpan_net_frag_exit();
44331fe2
AS
260out:
261 return err;
262}
263
264static void __exit lowpan_cleanup_module(void)
265{
44331fe2
AS
266 lowpan_netlink_fini();
267
7240cdec 268 lowpan_net_frag_exit();
a2dc375e 269
7240cdec 270 unregister_netdevice_notifier(&lowpan_dev_notifier);
44331fe2
AS
271}
272
273module_init(lowpan_init_module);
274module_exit(lowpan_cleanup_module);
275MODULE_LICENSE("GPL");
276MODULE_ALIAS_RTNL_LINK("lowpan");