]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - net/ieee802154/6lowpan/core.c
Merge tag 'wireless-drivers-next-for-davem-2018-03-24' of git://git.kernel.org/pub...
[mirror_ubuntu-eoan-kernel.git] / net / ieee802154 / 6lowpan / core.c
1 /* Copyright 2011, Siemens AG
2 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
3 */
4
5 /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
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.
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
47 #include <linux/module.h>
48 #include <linux/netdevice.h>
49 #include <linux/ieee802154.h>
50
51 #include <net/ipv6.h>
52
53 #include "6lowpan_i.h"
54
55 static int open_count;
56
57 static const struct header_ops lowpan_header_ops = {
58 .create = lowpan_header_create,
59 };
60
61 static int lowpan_dev_init(struct net_device *ldev)
62 {
63 netdev_lockdep_set_classes(ldev);
64
65 return 0;
66 }
67
68 static int lowpan_open(struct net_device *dev)
69 {
70 if (!open_count)
71 lowpan_rx_init();
72 open_count++;
73 return 0;
74 }
75
76 static int lowpan_stop(struct net_device *dev)
77 {
78 open_count--;
79 if (!open_count)
80 lowpan_rx_exit();
81 return 0;
82 }
83
84 static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n)
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
93 static const struct net_device_ops lowpan_netdev_ops = {
94 .ndo_init = lowpan_dev_init,
95 .ndo_start_xmit = lowpan_xmit,
96 .ndo_open = lowpan_open,
97 .ndo_stop = lowpan_stop,
98 .ndo_neigh_construct = lowpan_neigh_construct,
99 };
100
101 static void lowpan_setup(struct net_device *ldev)
102 {
103 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
104 /* We need an ipv6hdr as minimum len when calling xmit */
105 ldev->hard_header_len = sizeof(struct ipv6hdr);
106 ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
107 ldev->priv_flags |= IFF_NO_QUEUE;
108
109 ldev->netdev_ops = &lowpan_netdev_ops;
110 ldev->header_ops = &lowpan_header_ops;
111 ldev->needs_free_netdev = true;
112 ldev->features |= NETIF_F_NETNS_LOCAL;
113 }
114
115 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[],
116 struct netlink_ext_ack *extack)
117 {
118 if (tb[IFLA_ADDRESS]) {
119 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
120 return -EINVAL;
121 }
122 return 0;
123 }
124
125 static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
126 struct nlattr *tb[], struct nlattr *data[],
127 struct netlink_ext_ack *extack)
128 {
129 struct net_device *wdev;
130 int ret;
131
132 ASSERT_RTNL();
133
134 pr_debug("adding new link\n");
135
136 if (!tb[IFLA_LINK])
137 return -EINVAL;
138 /* find and hold wpan device */
139 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
140 if (!wdev)
141 return -ENODEV;
142 if (wdev->type != ARPHRD_IEEE802154) {
143 dev_put(wdev);
144 return -EINVAL;
145 }
146
147 if (wdev->ieee802154_ptr->lowpan_dev) {
148 dev_put(wdev);
149 return -EBUSY;
150 }
151
152 lowpan_802154_dev(ldev)->wdev = wdev;
153 /* Set the lowpan hardware address to the wpan hardware address. */
154 memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
155 /* We need headroom for possible wpan_dev_hard_header call and tailroom
156 * for encryption/fcs handling. The lowpan interface will replace
157 * the IPv6 header with 6LoWPAN header. At worst case the 6LoWPAN
158 * header has LOWPAN_IPHC_MAX_HEADER_LEN more bytes than the IPv6
159 * header.
160 */
161 ldev->needed_headroom = LOWPAN_IPHC_MAX_HEADER_LEN +
162 wdev->needed_headroom;
163 ldev->needed_tailroom = wdev->needed_tailroom;
164
165 ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh);
166
167 ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154);
168 if (ret < 0) {
169 dev_put(wdev);
170 return ret;
171 }
172
173 wdev->ieee802154_ptr->lowpan_dev = ldev;
174 return 0;
175 }
176
177 static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
178 {
179 struct net_device *wdev = lowpan_802154_dev(ldev)->wdev;
180
181 ASSERT_RTNL();
182
183 wdev->ieee802154_ptr->lowpan_dev = NULL;
184 lowpan_unregister_netdevice(ldev);
185 dev_put(wdev);
186 }
187
188 static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
189 .kind = "lowpan",
190 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)),
191 .setup = lowpan_setup,
192 .newlink = lowpan_newlink,
193 .dellink = lowpan_dellink,
194 .validate = lowpan_validate,
195 };
196
197 static inline int __init lowpan_netlink_init(void)
198 {
199 return rtnl_link_register(&lowpan_link_ops);
200 }
201
202 static inline void lowpan_netlink_fini(void)
203 {
204 rtnl_link_unregister(&lowpan_link_ops);
205 }
206
207 static int lowpan_device_event(struct notifier_block *unused,
208 unsigned long event, void *ptr)
209 {
210 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
211 struct wpan_dev *wpan_dev;
212
213 if (ndev->type != ARPHRD_IEEE802154)
214 return NOTIFY_DONE;
215 wpan_dev = ndev->ieee802154_ptr;
216 if (!wpan_dev)
217 return NOTIFY_DONE;
218
219 switch (event) {
220 case NETDEV_UNREGISTER:
221 /* Check if wpan interface is unregistered that we
222 * also delete possible lowpan interfaces which belongs
223 * to the wpan interface.
224 */
225 if (wpan_dev->lowpan_dev)
226 lowpan_dellink(wpan_dev->lowpan_dev, NULL);
227 break;
228 default:
229 return NOTIFY_DONE;
230 }
231
232 return NOTIFY_OK;
233 }
234
235 static struct notifier_block lowpan_dev_notifier = {
236 .notifier_call = lowpan_device_event,
237 };
238
239 static int __init lowpan_init_module(void)
240 {
241 int err = 0;
242
243 err = lowpan_net_frag_init();
244 if (err < 0)
245 goto out;
246
247 err = lowpan_netlink_init();
248 if (err < 0)
249 goto out_frag;
250
251 err = register_netdevice_notifier(&lowpan_dev_notifier);
252 if (err < 0)
253 goto out_pack;
254
255 return 0;
256
257 out_pack:
258 lowpan_netlink_fini();
259 out_frag:
260 lowpan_net_frag_exit();
261 out:
262 return err;
263 }
264
265 static void __exit lowpan_cleanup_module(void)
266 {
267 lowpan_netlink_fini();
268
269 lowpan_net_frag_exit();
270
271 unregister_netdevice_notifier(&lowpan_dev_notifier);
272 }
273
274 module_init(lowpan_init_module);
275 module_exit(lowpan_cleanup_module);
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS_RTNL_LINK("lowpan");