]>
Commit | Line | Data |
---|---|---|
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 |
55 | static int open_count; |
56 | ||
44331fe2 AS |
57 | static struct header_ops lowpan_header_ops = { |
58 | .create = lowpan_header_create, | |
59 | }; | |
60 | ||
20e7c4e8 ED |
61 | static struct lock_class_key lowpan_tx_busylock; |
62 | static struct lock_class_key lowpan_netdev_xmit_lock_key; | |
63 | ||
f4606583 | 64 | static void lowpan_set_lockdep_class_one(struct net_device *ldev, |
20e7c4e8 ED |
65 | struct netdev_queue *txq, |
66 | void *_unused) | |
67 | { | |
68 | lockdep_set_class(&txq->_xmit_lock, | |
69 | &lowpan_netdev_xmit_lock_key); | |
70 | } | |
71 | ||
f4606583 | 72 | static int lowpan_dev_init(struct net_device *ldev) |
20e7c4e8 | 73 | { |
f4606583 AA |
74 | netdev_for_each_tx_queue(ldev, lowpan_set_lockdep_class_one, NULL); |
75 | ldev->qdisc_tx_busylock = &lowpan_tx_busylock; | |
20e7c4e8 ED |
76 | return 0; |
77 | } | |
78 | ||
90997af7 AA |
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 | ||
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 | ||
44331fe2 | 95 | static const struct net_device_ops lowpan_netdev_ops = { |
20e7c4e8 | 96 | .ndo_init = lowpan_dev_init, |
44331fe2 | 97 | .ndo_start_xmit = lowpan_xmit, |
90997af7 AA |
98 | .ndo_open = lowpan_open, |
99 | .ndo_stop = lowpan_stop, | |
44331fe2 AS |
100 | }; |
101 | ||
f4606583 | 102 | static void lowpan_setup(struct net_device *ldev) |
44331fe2 | 103 | { |
f4606583 AA |
104 | ldev->addr_len = IEEE802154_ADDR_LEN; |
105 | memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN); | |
106 | ldev->type = ARPHRD_6LOWPAN; | |
44331fe2 | 107 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
f4606583 AA |
108 | ldev->hard_header_len = 2 + 1 + 20 + 14; |
109 | ldev->needed_tailroom = 2; /* FCS */ | |
110 | ldev->mtu = IPV6_MIN_MTU; | |
111 | ldev->priv_flags |= IFF_NO_QUEUE; | |
112 | ldev->flags = IFF_BROADCAST | IFF_MULTICAST; | |
113 | ldev->watchdog_timeo = 0; | |
114 | ||
115 | ldev->netdev_ops = &lowpan_netdev_ops; | |
116 | ldev->header_ops = &lowpan_header_ops; | |
117 | ldev->destructor = free_netdev; | |
118 | ldev->features |= NETIF_F_NETNS_LOCAL; | |
44331fe2 AS |
119 | } |
120 | ||
121 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
122 | { | |
44331fe2 AS |
123 | if (tb[IFLA_ADDRESS]) { |
124 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) | |
125 | return -EINVAL; | |
126 | } | |
127 | return 0; | |
128 | } | |
129 | ||
f4606583 | 130 | static int lowpan_newlink(struct net *src_net, struct net_device *ldev, |
44331fe2 AS |
131 | struct nlattr *tb[], struct nlattr *data[]) |
132 | { | |
f4606583 | 133 | struct net_device *wdev; |
1ae2605e | 134 | int ret; |
44331fe2 | 135 | |
c37a8106 AA |
136 | ASSERT_RTNL(); |
137 | ||
e71094f9 | 138 | pr_debug("adding new link\n"); |
44331fe2 | 139 | |
f9d1ce8f | 140 | if (!tb[IFLA_LINK] || |
f4606583 | 141 | !net_eq(dev_net(ldev), &init_net)) |
44331fe2 | 142 | return -EINVAL; |
f4606583 AA |
143 | /* find and hold wpan device */ |
144 | wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); | |
145 | if (!wdev) | |
44331fe2 | 146 | return -ENODEV; |
f4606583 AA |
147 | if (wdev->type != ARPHRD_IEEE802154) { |
148 | dev_put(wdev); | |
7adac1ec | 149 | return -EINVAL; |
78032f9b | 150 | } |
44331fe2 | 151 | |
f4606583 AA |
152 | if (wdev->ieee802154_ptr->lowpan_dev) { |
153 | dev_put(wdev); | |
51e0e5d8 | 154 | return -EBUSY; |
dc00fd44 | 155 | } |
44331fe2 | 156 | |
f4606583 | 157 | lowpan_dev_info(ldev)->wdev = wdev; |
69bb631e | 158 | /* Set the lowpan hardware address to the wpan hardware address. */ |
f4606583 | 159 | memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN); |
ab2d95df | 160 | |
f4606583 | 161 | lowpan_netdev_setup(ldev, LOWPAN_LLTYPE_IEEE802154); |
b72f6f51 | 162 | |
f4606583 | 163 | ret = register_netdevice(ldev); |
07512728 | 164 | if (ret < 0) { |
f4606583 | 165 | dev_put(wdev); |
07512728 | 166 | return ret; |
1ae2605e | 167 | } |
44331fe2 | 168 | |
f4606583 | 169 | wdev->ieee802154_ptr->lowpan_dev = ldev; |
07512728 | 170 | return 0; |
44331fe2 AS |
171 | } |
172 | ||
f4606583 | 173 | static void lowpan_dellink(struct net_device *ldev, struct list_head *head) |
44331fe2 | 174 | { |
f4606583 | 175 | struct net_device *wdev = lowpan_dev_info(ldev)->wdev; |
44331fe2 AS |
176 | |
177 | ASSERT_RTNL(); | |
178 | ||
f4606583 AA |
179 | wdev->ieee802154_ptr->lowpan_dev = NULL; |
180 | unregister_netdevice(ldev); | |
181 | dev_put(wdev); | |
44331fe2 AS |
182 | } |
183 | ||
184 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { | |
185 | .kind = "lowpan", | |
b72f6f51 | 186 | .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev_info)), |
44331fe2 AS |
187 | .setup = lowpan_setup, |
188 | .newlink = lowpan_newlink, | |
189 | .dellink = lowpan_dellink, | |
190 | .validate = lowpan_validate, | |
191 | }; | |
192 | ||
193 | static inline int __init lowpan_netlink_init(void) | |
194 | { | |
195 | return rtnl_link_register(&lowpan_link_ops); | |
196 | } | |
197 | ||
a07fdcec | 198 | static inline void lowpan_netlink_fini(void) |
44331fe2 AS |
199 | { |
200 | rtnl_link_unregister(&lowpan_link_ops); | |
201 | } | |
202 | ||
a2dc375e | 203 | static int lowpan_device_event(struct notifier_block *unused, |
351638e7 | 204 | unsigned long event, void *ptr) |
a2dc375e | 205 | { |
f4606583 | 206 | struct net_device *wdev = netdev_notifier_info_to_dev(ptr); |
a2dc375e | 207 | |
f4606583 | 208 | if (wdev->type != ARPHRD_IEEE802154) |
a2dc375e AO |
209 | goto out; |
210 | ||
51e0e5d8 AA |
211 | switch (event) { |
212 | case NETDEV_UNREGISTER: | |
213 | /* Check if wpan interface is unregistered that we | |
214 | * also delete possible lowpan interfaces which belongs | |
215 | * to the wpan interface. | |
216 | */ | |
f4606583 AA |
217 | if (wdev->ieee802154_ptr && wdev->ieee802154_ptr->lowpan_dev) |
218 | lowpan_dellink(wdev->ieee802154_ptr->lowpan_dev, NULL); | |
51e0e5d8 AA |
219 | break; |
220 | default: | |
221 | break; | |
4c835019 | 222 | } |
a2dc375e AO |
223 | |
224 | out: | |
225 | return NOTIFY_DONE; | |
226 | } | |
227 | ||
228 | static struct notifier_block lowpan_dev_notifier = { | |
229 | .notifier_call = lowpan_device_event, | |
230 | }; | |
231 | ||
44331fe2 AS |
232 | static int __init lowpan_init_module(void) |
233 | { | |
234 | int err = 0; | |
235 | ||
7240cdec | 236 | err = lowpan_net_frag_init(); |
44331fe2 AS |
237 | if (err < 0) |
238 | goto out; | |
239 | ||
7240cdec AA |
240 | err = lowpan_netlink_init(); |
241 | if (err < 0) | |
242 | goto out_frag; | |
243 | ||
a2dc375e | 244 | err = register_netdevice_notifier(&lowpan_dev_notifier); |
7240cdec AA |
245 | if (err < 0) |
246 | goto out_pack; | |
247 | ||
248 | return 0; | |
249 | ||
250 | out_pack: | |
7240cdec AA |
251 | lowpan_netlink_fini(); |
252 | out_frag: | |
253 | lowpan_net_frag_exit(); | |
44331fe2 AS |
254 | out: |
255 | return err; | |
256 | } | |
257 | ||
258 | static void __exit lowpan_cleanup_module(void) | |
259 | { | |
44331fe2 AS |
260 | lowpan_netlink_fini(); |
261 | ||
7240cdec | 262 | lowpan_net_frag_exit(); |
a2dc375e | 263 | |
7240cdec | 264 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
44331fe2 AS |
265 | } |
266 | ||
267 | module_init(lowpan_init_module); | |
268 | module_exit(lowpan_cleanup_module); | |
269 | MODULE_LICENSE("GPL"); | |
270 | MODULE_ALIAS_RTNL_LINK("lowpan"); |