core.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Authors:
  11. * (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
  12. */
  13. #include <linux/module.h>
  14. #include <net/6lowpan.h>
  15. #include <net/addrconf.h>
  16. #include "6lowpan_i.h"
  17. int lowpan_register_netdevice(struct net_device *dev,
  18. enum lowpan_lltypes lltype)
  19. {
  20. int i, ret;
  21. dev->addr_len = EUI64_ADDR_LEN;
  22. dev->type = ARPHRD_6LOWPAN;
  23. dev->mtu = IPV6_MIN_MTU;
  24. dev->priv_flags |= IFF_NO_QUEUE;
  25. lowpan_dev(dev)->lltype = lltype;
  26. spin_lock_init(&lowpan_dev(dev)->ctx.lock);
  27. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  28. lowpan_dev(dev)->ctx.table[i].id = i;
  29. dev->ndisc_ops = &lowpan_ndisc_ops;
  30. ret = register_netdevice(dev);
  31. if (ret < 0)
  32. return ret;
  33. ret = lowpan_dev_debugfs_init(dev);
  34. if (ret < 0)
  35. unregister_netdevice(dev);
  36. return ret;
  37. }
  38. EXPORT_SYMBOL(lowpan_register_netdevice);
  39. int lowpan_register_netdev(struct net_device *dev,
  40. enum lowpan_lltypes lltype)
  41. {
  42. int ret;
  43. rtnl_lock();
  44. ret = lowpan_register_netdevice(dev, lltype);
  45. rtnl_unlock();
  46. return ret;
  47. }
  48. EXPORT_SYMBOL(lowpan_register_netdev);
  49. void lowpan_unregister_netdevice(struct net_device *dev)
  50. {
  51. unregister_netdevice(dev);
  52. lowpan_dev_debugfs_exit(dev);
  53. }
  54. EXPORT_SYMBOL(lowpan_unregister_netdevice);
  55. void lowpan_unregister_netdev(struct net_device *dev)
  56. {
  57. rtnl_lock();
  58. lowpan_unregister_netdevice(dev);
  59. rtnl_unlock();
  60. }
  61. EXPORT_SYMBOL(lowpan_unregister_netdev);
  62. int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev)
  63. {
  64. struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
  65. /* Set short_addr autoconfiguration if short_addr is present only */
  66. if (!lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr))
  67. return -1;
  68. /* For either address format, all zero addresses MUST NOT be used */
  69. if (wpan_dev->pan_id == cpu_to_le16(0x0000) &&
  70. wpan_dev->short_addr == cpu_to_le16(0x0000))
  71. return -1;
  72. /* Alternatively, if no PAN ID is known, 16 zero bits may be used */
  73. if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  74. memset(eui, 0, 2);
  75. else
  76. ieee802154_le16_to_be16(eui, &wpan_dev->pan_id);
  77. /* The "Universal/Local" (U/L) bit shall be set to zero */
  78. eui[0] &= ~2;
  79. eui[2] = 0;
  80. eui[3] = 0xFF;
  81. eui[4] = 0xFE;
  82. eui[5] = 0;
  83. ieee802154_le16_to_be16(&eui[6], &wpan_dev->short_addr);
  84. return 0;
  85. }
  86. static int lowpan_event(struct notifier_block *unused,
  87. unsigned long event, void *ptr)
  88. {
  89. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  90. struct inet6_dev *idev;
  91. struct in6_addr addr;
  92. int i;
  93. if (dev->type != ARPHRD_6LOWPAN)
  94. return NOTIFY_DONE;
  95. idev = __in6_dev_get(dev);
  96. if (!idev)
  97. return NOTIFY_DONE;
  98. switch (event) {
  99. case NETDEV_UP:
  100. case NETDEV_CHANGE:
  101. /* (802.15.4 6LoWPAN short address slaac handling */
  102. if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) &&
  103. addrconf_ifid_802154_6lowpan(addr.s6_addr + 8, dev) == 0) {
  104. __ipv6_addr_set_half(&addr.s6_addr32[0],
  105. htonl(0xFE800000), 0);
  106. addrconf_add_linklocal(idev, &addr, 0);
  107. }
  108. break;
  109. case NETDEV_DOWN:
  110. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  111. clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE,
  112. &lowpan_dev(dev)->ctx.table[i].flags);
  113. break;
  114. default:
  115. return NOTIFY_DONE;
  116. }
  117. return NOTIFY_OK;
  118. }
  119. static struct notifier_block lowpan_notifier = {
  120. .notifier_call = lowpan_event,
  121. };
  122. static int __init lowpan_module_init(void)
  123. {
  124. int ret;
  125. ret = lowpan_debugfs_init();
  126. if (ret < 0)
  127. return ret;
  128. ret = register_netdevice_notifier(&lowpan_notifier);
  129. if (ret < 0) {
  130. lowpan_debugfs_exit();
  131. return ret;
  132. }
  133. request_module_nowait("nhc_dest");
  134. request_module_nowait("nhc_fragment");
  135. request_module_nowait("nhc_hop");
  136. request_module_nowait("nhc_ipv6");
  137. request_module_nowait("nhc_mobility");
  138. request_module_nowait("nhc_routing");
  139. request_module_nowait("nhc_udp");
  140. return 0;
  141. }
  142. static void __exit lowpan_module_exit(void)
  143. {
  144. lowpan_debugfs_exit();
  145. unregister_netdevice_notifier(&lowpan_notifier);
  146. }
  147. module_init(lowpan_module_init);
  148. module_exit(lowpan_module_exit);
  149. MODULE_LICENSE("GPL");