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 struct header_ops lowpan_header_ops = {
58 .create = lowpan_header_create,
59 };
60
61 static struct lock_class_key lowpan_tx_busylock;
62 static struct lock_class_key lowpan_netdev_xmit_lock_key;
63
lowpan_set_lockdep_class_one(struct net_device * ldev,struct netdev_queue * txq,void * _unused)64 static void lowpan_set_lockdep_class_one(struct net_device *ldev,
65 struct netdev_queue *txq,
66 void *_unused)
67 {
68 lockdep_set_class(&txq->_xmit_lock,
69 &lowpan_netdev_xmit_lock_key);
70 }
71
lowpan_dev_init(struct net_device * ldev)72 static int lowpan_dev_init(struct net_device *ldev)
73 {
74 netdev_for_each_tx_queue(ldev, lowpan_set_lockdep_class_one, NULL);
75 ldev->qdisc_tx_busylock = &lowpan_tx_busylock;
76 return 0;
77 }
78
lowpan_open(struct net_device * dev)79 static int lowpan_open(struct net_device *dev)
80 {
81 if (!open_count)
82 lowpan_rx_init();
83 open_count++;
84 return 0;
85 }
86
lowpan_stop(struct net_device * dev)87 static int lowpan_stop(struct net_device *dev)
88 {
89 open_count--;
90 if (!open_count)
91 lowpan_rx_exit();
92 return 0;
93 }
94
95 static const struct net_device_ops lowpan_netdev_ops = {
96 .ndo_init = lowpan_dev_init,
97 .ndo_start_xmit = lowpan_xmit,
98 .ndo_open = lowpan_open,
99 .ndo_stop = lowpan_stop,
100 };
101
lowpan_setup(struct net_device * ldev)102 static void lowpan_setup(struct net_device *ldev)
103 {
104 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
105 /* We need an ipv6hdr as minimum len when calling xmit */
106 ldev->hard_header_len = sizeof(struct ipv6hdr);
107 ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
108
109 ldev->netdev_ops = &lowpan_netdev_ops;
110 ldev->header_ops = &lowpan_header_ops;
111 ldev->destructor = free_netdev;
112 ldev->features |= NETIF_F_NETNS_LOCAL;
113 }
114
lowpan_validate(struct nlattr * tb[],struct nlattr * data[])115 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
116 {
117 if (tb[IFLA_ADDRESS]) {
118 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
119 return -EINVAL;
120 }
121 return 0;
122 }
123
lowpan_newlink(struct net * src_net,struct net_device * ldev,struct nlattr * tb[],struct nlattr * data[])124 static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
125 struct nlattr *tb[], struct nlattr *data[])
126 {
127 struct net_device *wdev;
128 int ret;
129
130 ASSERT_RTNL();
131
132 pr_debug("adding new link\n");
133
134 if (!tb[IFLA_LINK] ||
135 !net_eq(dev_net(ldev), &init_net))
136 return -EINVAL;
137 /* find and hold wpan device */
138 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
139 if (!wdev)
140 return -ENODEV;
141 if (wdev->type != ARPHRD_IEEE802154) {
142 dev_put(wdev);
143 return -EINVAL;
144 }
145
146 if (wdev->ieee802154_ptr->lowpan_dev) {
147 dev_put(wdev);
148 return -EBUSY;
149 }
150
151 lowpan_dev_info(ldev)->wdev = wdev;
152 /* Set the lowpan hardware address to the wpan hardware address. */
153 memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
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;
163
164 lowpan_netdev_setup(ldev, LOWPAN_LLTYPE_IEEE802154);
165
166 ret = register_netdevice(ldev);
167 if (ret < 0) {
168 dev_put(wdev);
169 return ret;
170 }
171
172 wdev->ieee802154_ptr->lowpan_dev = ldev;
173 return 0;
174 }
175
lowpan_dellink(struct net_device * ldev,struct list_head * head)176 static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
177 {
178 struct net_device *wdev = lowpan_dev_info(ldev)->wdev;
179
180 ASSERT_RTNL();
181
182 wdev->ieee802154_ptr->lowpan_dev = NULL;
183 unregister_netdevice(ldev);
184 dev_put(wdev);
185 }
186
187 static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
188 .kind = "lowpan",
189 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev_info)),
190 .setup = lowpan_setup,
191 .newlink = lowpan_newlink,
192 .dellink = lowpan_dellink,
193 .validate = lowpan_validate,
194 };
195
lowpan_netlink_init(void)196 static inline int __init lowpan_netlink_init(void)
197 {
198 return rtnl_link_register(&lowpan_link_ops);
199 }
200
lowpan_netlink_fini(void)201 static inline void lowpan_netlink_fini(void)
202 {
203 rtnl_link_unregister(&lowpan_link_ops);
204 }
205
lowpan_device_event(struct notifier_block * unused,unsigned long event,void * ptr)206 static int lowpan_device_event(struct notifier_block *unused,
207 unsigned long event, void *ptr)
208 {
209 struct net_device *wdev = netdev_notifier_info_to_dev(ptr);
210
211 if (wdev->type != ARPHRD_IEEE802154)
212 goto out;
213
214 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 */
220 if (wdev->ieee802154_ptr->lowpan_dev)
221 lowpan_dellink(wdev->ieee802154_ptr->lowpan_dev, NULL);
222 break;
223 default:
224 break;
225 }
226
227 out:
228 return NOTIFY_DONE;
229 }
230
231 static struct notifier_block lowpan_dev_notifier = {
232 .notifier_call = lowpan_device_event,
233 };
234
lowpan_init_module(void)235 static int __init lowpan_init_module(void)
236 {
237 int err = 0;
238
239 err = lowpan_net_frag_init();
240 if (err < 0)
241 goto out;
242
243 err = lowpan_netlink_init();
244 if (err < 0)
245 goto out_frag;
246
247 err = register_netdevice_notifier(&lowpan_dev_notifier);
248 if (err < 0)
249 goto out_pack;
250
251 return 0;
252
253 out_pack:
254 lowpan_netlink_fini();
255 out_frag:
256 lowpan_net_frag_exit();
257 out:
258 return err;
259 }
260
lowpan_cleanup_module(void)261 static void __exit lowpan_cleanup_module(void)
262 {
263 lowpan_netlink_fini();
264
265 lowpan_net_frag_exit();
266
267 unregister_netdevice_notifier(&lowpan_dev_notifier);
268 }
269
270 module_init(lowpan_init_module);
271 module_exit(lowpan_cleanup_module);
272 MODULE_LICENSE("GPL");
273 MODULE_ALIAS_RTNL_LINK("lowpan");
274