core.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright 2011, Siemens AG
  2. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  3. */
  4. /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
  5. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  17. * Copyright (c) 2008, Swedish Institute of Computer Science.
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. Neither the name of the Institute nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include <linux/module.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/ieee802154.h>
  47. #include <net/ipv6.h>
  48. #include "6lowpan_i.h"
  49. LIST_HEAD(lowpan_devices);
  50. static int lowpan_open_count;
  51. static struct header_ops lowpan_header_ops = {
  52. .create = lowpan_header_create,
  53. };
  54. static struct lock_class_key lowpan_tx_busylock;
  55. static struct lock_class_key lowpan_netdev_xmit_lock_key;
  56. static void lowpan_set_lockdep_class_one(struct net_device *dev,
  57. struct netdev_queue *txq,
  58. void *_unused)
  59. {
  60. lockdep_set_class(&txq->_xmit_lock,
  61. &lowpan_netdev_xmit_lock_key);
  62. }
  63. static int lowpan_dev_init(struct net_device *dev)
  64. {
  65. netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
  66. dev->qdisc_tx_busylock = &lowpan_tx_busylock;
  67. return 0;
  68. }
  69. static const struct net_device_ops lowpan_netdev_ops = {
  70. .ndo_init = lowpan_dev_init,
  71. .ndo_start_xmit = lowpan_xmit,
  72. };
  73. static void lowpan_setup(struct net_device *dev)
  74. {
  75. dev->addr_len = IEEE802154_ADDR_LEN;
  76. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  77. dev->type = ARPHRD_6LOWPAN;
  78. /* Frame Control + Sequence Number + Address fields + Security Header */
  79. dev->hard_header_len = 2 + 1 + 20 + 14;
  80. dev->needed_tailroom = 2; /* FCS */
  81. dev->mtu = IPV6_MIN_MTU;
  82. dev->tx_queue_len = 0;
  83. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  84. dev->watchdog_timeo = 0;
  85. dev->netdev_ops = &lowpan_netdev_ops;
  86. dev->header_ops = &lowpan_header_ops;
  87. dev->destructor = free_netdev;
  88. dev->features |= NETIF_F_NETNS_LOCAL;
  89. }
  90. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  91. {
  92. if (tb[IFLA_ADDRESS]) {
  93. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  99. struct nlattr *tb[], struct nlattr *data[])
  100. {
  101. struct net_device *real_dev;
  102. struct lowpan_dev_record *entry;
  103. int ret;
  104. ASSERT_RTNL();
  105. pr_debug("adding new link\n");
  106. if (!tb[IFLA_LINK] ||
  107. !net_eq(dev_net(dev), &init_net))
  108. return -EINVAL;
  109. /* find and hold real wpan device */
  110. real_dev = dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
  111. if (!real_dev)
  112. return -ENODEV;
  113. if (real_dev->type != ARPHRD_IEEE802154) {
  114. dev_put(real_dev);
  115. return -EINVAL;
  116. }
  117. lowpan_dev_info(dev)->real_dev = real_dev;
  118. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  119. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  120. if (!entry) {
  121. dev_put(real_dev);
  122. lowpan_dev_info(dev)->real_dev = NULL;
  123. return -ENOMEM;
  124. }
  125. entry->ldev = dev;
  126. /* Set the lowpan hardware address to the wpan hardware address. */
  127. memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
  128. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  129. INIT_LIST_HEAD(&entry->list);
  130. list_add_tail(&entry->list, &lowpan_devices);
  131. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  132. ret = register_netdevice(dev);
  133. if (ret >= 0) {
  134. if (!lowpan_open_count)
  135. lowpan_rx_init();
  136. lowpan_open_count++;
  137. }
  138. return ret;
  139. }
  140. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  141. {
  142. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  143. struct net_device *real_dev = lowpan_dev->real_dev;
  144. struct lowpan_dev_record *entry, *tmp;
  145. ASSERT_RTNL();
  146. lowpan_open_count--;
  147. if (!lowpan_open_count)
  148. lowpan_rx_exit();
  149. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  150. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  151. if (entry->ldev == dev) {
  152. list_del(&entry->list);
  153. kfree(entry);
  154. }
  155. }
  156. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  157. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  158. unregister_netdevice_queue(dev, head);
  159. dev_put(real_dev);
  160. }
  161. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  162. .kind = "lowpan",
  163. .priv_size = sizeof(struct lowpan_dev_info),
  164. .setup = lowpan_setup,
  165. .newlink = lowpan_newlink,
  166. .dellink = lowpan_dellink,
  167. .validate = lowpan_validate,
  168. };
  169. static inline int __init lowpan_netlink_init(void)
  170. {
  171. return rtnl_link_register(&lowpan_link_ops);
  172. }
  173. static inline void lowpan_netlink_fini(void)
  174. {
  175. rtnl_link_unregister(&lowpan_link_ops);
  176. }
  177. static int lowpan_device_event(struct notifier_block *unused,
  178. unsigned long event, void *ptr)
  179. {
  180. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  181. LIST_HEAD(del_list);
  182. struct lowpan_dev_record *entry, *tmp;
  183. if (dev->type != ARPHRD_IEEE802154)
  184. goto out;
  185. if (event == NETDEV_UNREGISTER) {
  186. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  187. if (lowpan_dev_info(entry->ldev)->real_dev == dev)
  188. lowpan_dellink(entry->ldev, &del_list);
  189. }
  190. unregister_netdevice_many(&del_list);
  191. }
  192. out:
  193. return NOTIFY_DONE;
  194. }
  195. static struct notifier_block lowpan_dev_notifier = {
  196. .notifier_call = lowpan_device_event,
  197. };
  198. static int __init lowpan_init_module(void)
  199. {
  200. int err = 0;
  201. err = lowpan_net_frag_init();
  202. if (err < 0)
  203. goto out;
  204. err = lowpan_netlink_init();
  205. if (err < 0)
  206. goto out_frag;
  207. err = register_netdevice_notifier(&lowpan_dev_notifier);
  208. if (err < 0)
  209. goto out_pack;
  210. return 0;
  211. out_pack:
  212. lowpan_netlink_fini();
  213. out_frag:
  214. lowpan_net_frag_exit();
  215. out:
  216. return err;
  217. }
  218. static void __exit lowpan_cleanup_module(void)
  219. {
  220. lowpan_netlink_fini();
  221. lowpan_net_frag_exit();
  222. unregister_netdevice_notifier(&lowpan_dev_notifier);
  223. }
  224. module_init(lowpan_init_module);
  225. module_exit(lowpan_cleanup_module);
  226. MODULE_LICENSE("GPL");
  227. MODULE_ALIAS_RTNL_LINK("lowpan");