core.c 7.8 KB

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