PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ieee802154/6lowpan/core.c

https://gitlab.com/CadeLaRen/linux
C | 270 lines | 166 code | 47 blank | 57 comment | 18 complexity | 11b9284364f488e2ae30e4ac430c15f7 MD5 | raw file
  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. static int open_count;
  50. static struct header_ops lowpan_header_ops = {
  51. .create = lowpan_header_create,
  52. };
  53. static int lowpan_dev_init(struct net_device *ldev)
  54. {
  55. netdev_lockdep_set_classes(ldev);
  56. return 0;
  57. }
  58. static int lowpan_open(struct net_device *dev)
  59. {
  60. if (!open_count)
  61. lowpan_rx_init();
  62. open_count++;
  63. return 0;
  64. }
  65. static int lowpan_stop(struct net_device *dev)
  66. {
  67. open_count--;
  68. if (!open_count)
  69. lowpan_rx_exit();
  70. return 0;
  71. }
  72. static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n)
  73. {
  74. struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n));
  75. /* default no short_addr is available for a neighbour */
  76. neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
  77. return 0;
  78. }
  79. static const struct net_device_ops lowpan_netdev_ops = {
  80. .ndo_init = lowpan_dev_init,
  81. .ndo_start_xmit = lowpan_xmit,
  82. .ndo_open = lowpan_open,
  83. .ndo_stop = lowpan_stop,
  84. .ndo_neigh_construct = lowpan_neigh_construct,
  85. };
  86. static void lowpan_setup(struct net_device *ldev)
  87. {
  88. memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  89. /* We need an ipv6hdr as minimum len when calling xmit */
  90. ldev->hard_header_len = sizeof(struct ipv6hdr);
  91. ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
  92. ldev->netdev_ops = &lowpan_netdev_ops;
  93. ldev->header_ops = &lowpan_header_ops;
  94. ldev->destructor = free_netdev;
  95. ldev->features |= NETIF_F_NETNS_LOCAL;
  96. }
  97. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  98. {
  99. if (tb[IFLA_ADDRESS]) {
  100. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  101. return -EINVAL;
  102. }
  103. return 0;
  104. }
  105. static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
  106. struct nlattr *tb[], struct nlattr *data[])
  107. {
  108. struct net_device *wdev;
  109. int ret;
  110. ASSERT_RTNL();
  111. pr_debug("adding new link\n");
  112. if (!tb[IFLA_LINK])
  113. return -EINVAL;
  114. /* find and hold wpan device */
  115. wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
  116. if (!wdev)
  117. return -ENODEV;
  118. if (wdev->type != ARPHRD_IEEE802154) {
  119. dev_put(wdev);
  120. return -EINVAL;
  121. }
  122. if (wdev->ieee802154_ptr->lowpan_dev) {
  123. dev_put(wdev);
  124. return -EBUSY;
  125. }
  126. lowpan_802154_dev(ldev)->wdev = wdev;
  127. /* Set the lowpan hardware address to the wpan hardware address. */
  128. memcpy(ldev->dev_addr, wdev->dev_addr, IEEE802154_ADDR_LEN);
  129. /* We need headroom for possible wpan_dev_hard_header call and tailroom
  130. * for encryption/fcs handling. The lowpan interface will replace
  131. * the IPv6 header with 6LoWPAN header. At worst case the 6LoWPAN
  132. * header has LOWPAN_IPHC_MAX_HEADER_LEN more bytes than the IPv6
  133. * header.
  134. */
  135. ldev->needed_headroom = LOWPAN_IPHC_MAX_HEADER_LEN +
  136. wdev->needed_headroom;
  137. ldev->needed_tailroom = wdev->needed_tailroom;
  138. ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh);
  139. ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154);
  140. if (ret < 0) {
  141. dev_put(wdev);
  142. return ret;
  143. }
  144. wdev->ieee802154_ptr->lowpan_dev = ldev;
  145. return 0;
  146. }
  147. static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
  148. {
  149. struct net_device *wdev = lowpan_802154_dev(ldev)->wdev;
  150. ASSERT_RTNL();
  151. wdev->ieee802154_ptr->lowpan_dev = NULL;
  152. lowpan_unregister_netdevice(ldev);
  153. dev_put(wdev);
  154. }
  155. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  156. .kind = "lowpan",
  157. .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)),
  158. .setup = lowpan_setup,
  159. .newlink = lowpan_newlink,
  160. .dellink = lowpan_dellink,
  161. .validate = lowpan_validate,
  162. };
  163. static inline int __init lowpan_netlink_init(void)
  164. {
  165. return rtnl_link_register(&lowpan_link_ops);
  166. }
  167. static inline void lowpan_netlink_fini(void)
  168. {
  169. rtnl_link_unregister(&lowpan_link_ops);
  170. }
  171. static int lowpan_device_event(struct notifier_block *unused,
  172. unsigned long event, void *ptr)
  173. {
  174. struct net_device *wdev = netdev_notifier_info_to_dev(ptr);
  175. if (wdev->type != ARPHRD_IEEE802154)
  176. return NOTIFY_DONE;
  177. switch (event) {
  178. case NETDEV_UNREGISTER:
  179. /* Check if wpan interface is unregistered that we
  180. * also delete possible lowpan interfaces which belongs
  181. * to the wpan interface.
  182. */
  183. if (wdev->ieee802154_ptr->lowpan_dev)
  184. lowpan_dellink(wdev->ieee802154_ptr->lowpan_dev, NULL);
  185. break;
  186. default:
  187. return NOTIFY_DONE;
  188. }
  189. return NOTIFY_OK;
  190. }
  191. static struct notifier_block lowpan_dev_notifier = {
  192. .notifier_call = lowpan_device_event,
  193. };
  194. static int __init lowpan_init_module(void)
  195. {
  196. int err = 0;
  197. err = lowpan_net_frag_init();
  198. if (err < 0)
  199. goto out;
  200. err = lowpan_netlink_init();
  201. if (err < 0)
  202. goto out_frag;
  203. err = register_netdevice_notifier(&lowpan_dev_notifier);
  204. if (err < 0)
  205. goto out_pack;
  206. return 0;
  207. out_pack:
  208. lowpan_netlink_fini();
  209. out_frag:
  210. lowpan_net_frag_exit();
  211. out:
  212. return err;
  213. }
  214. static void __exit lowpan_cleanup_module(void)
  215. {
  216. lowpan_netlink_fini();
  217. lowpan_net_frag_exit();
  218. unregister_netdevice_notifier(&lowpan_dev_notifier);
  219. }
  220. module_init(lowpan_init_module);
  221. module_exit(lowpan_cleanup_module);
  222. MODULE_LICENSE("GPL");
  223. MODULE_ALIAS_RTNL_LINK("lowpan");