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