core.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. switch (lltype) {
  22. case LOWPAN_LLTYPE_IEEE802154:
  23. dev->addr_len = EUI64_ADDR_LEN;
  24. break;
  25. case LOWPAN_LLTYPE_BTLE:
  26. dev->addr_len = ETH_ALEN;
  27. break;
  28. }
  29. dev->type = ARPHRD_6LOWPAN;
  30. dev->mtu = IPV6_MIN_MTU;
  31. lowpan_dev(dev)->lltype = lltype;
  32. spin_lock_init(&lowpan_dev(dev)->ctx.lock);
  33. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  34. lowpan_dev(dev)->ctx.table[i].id = i;
  35. dev->ndisc_ops = &lowpan_ndisc_ops;
  36. ret = register_netdevice(dev);
  37. if (ret < 0)
  38. return ret;
  39. ret = lowpan_dev_debugfs_init(dev);
  40. if (ret < 0)
  41. unregister_netdevice(dev);
  42. return ret;
  43. }
  44. EXPORT_SYMBOL(lowpan_register_netdevice);
  45. int lowpan_register_netdev(struct net_device *dev,
  46. enum lowpan_lltypes lltype)
  47. {
  48. int ret;
  49. rtnl_lock();
  50. ret = lowpan_register_netdevice(dev, lltype);
  51. rtnl_unlock();
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(lowpan_register_netdev);
  55. void lowpan_unregister_netdevice(struct net_device *dev)
  56. {
  57. unregister_netdevice(dev);
  58. lowpan_dev_debugfs_exit(dev);
  59. }
  60. EXPORT_SYMBOL(lowpan_unregister_netdevice);
  61. void lowpan_unregister_netdev(struct net_device *dev)
  62. {
  63. rtnl_lock();
  64. lowpan_unregister_netdevice(dev);
  65. rtnl_unlock();
  66. }
  67. EXPORT_SYMBOL(lowpan_unregister_netdev);
  68. int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev)
  69. {
  70. struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
  71. /* Set short_addr autoconfiguration if short_addr is present only */
  72. if (!lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr))
  73. return -1;
  74. /* For either address format, all zero addresses MUST NOT be used */
  75. if (wpan_dev->pan_id == cpu_to_le16(0x0000) &&
  76. wpan_dev->short_addr == cpu_to_le16(0x0000))
  77. return -1;
  78. /* Alternatively, if no PAN ID is known, 16 zero bits may be used */
  79. if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  80. memset(eui, 0, 2);
  81. else
  82. ieee802154_le16_to_be16(eui, &wpan_dev->pan_id);
  83. /* The "Universal/Local" (U/L) bit shall be set to zero */
  84. eui[0] &= ~2;
  85. eui[2] = 0;
  86. eui[3] = 0xFF;
  87. eui[4] = 0xFE;
  88. eui[5] = 0;
  89. ieee802154_le16_to_be16(&eui[6], &wpan_dev->short_addr);
  90. return 0;
  91. }
  92. static int lowpan_event(struct notifier_block *unused,
  93. unsigned long event, void *ptr)
  94. {
  95. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  96. struct inet6_dev *idev;
  97. struct in6_addr addr;
  98. int i;
  99. if (dev->type != ARPHRD_6LOWPAN)
  100. return NOTIFY_DONE;
  101. idev = __in6_dev_get(dev);
  102. if (!idev)
  103. return NOTIFY_DONE;
  104. switch (event) {
  105. case NETDEV_UP:
  106. case NETDEV_CHANGE:
  107. /* (802.15.4 6LoWPAN short address slaac handling */
  108. if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) &&
  109. addrconf_ifid_802154_6lowpan(addr.s6_addr + 8, dev) == 0) {
  110. __ipv6_addr_set_half(&addr.s6_addr32[0],
  111. htonl(0xFE800000), 0);
  112. addrconf_add_linklocal(idev, &addr, 0);
  113. }
  114. break;
  115. case NETDEV_DOWN:
  116. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  117. clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE,
  118. &lowpan_dev(dev)->ctx.table[i].flags);
  119. break;
  120. default:
  121. return NOTIFY_DONE;
  122. }
  123. return NOTIFY_OK;
  124. }
  125. static struct notifier_block lowpan_notifier = {
  126. .notifier_call = lowpan_event,
  127. };
  128. static int __init lowpan_module_init(void)
  129. {
  130. int ret;
  131. ret = lowpan_debugfs_init();
  132. if (ret < 0)
  133. return ret;
  134. ret = register_netdevice_notifier(&lowpan_notifier);
  135. if (ret < 0) {
  136. lowpan_debugfs_exit();
  137. return ret;
  138. }
  139. request_module_nowait("nhc_dest");
  140. request_module_nowait("nhc_fragment");
  141. request_module_nowait("nhc_hop");
  142. request_module_nowait("nhc_ipv6");
  143. request_module_nowait("nhc_mobility");
  144. request_module_nowait("nhc_routing");
  145. request_module_nowait("nhc_udp");
  146. return 0;
  147. }
  148. static void __exit lowpan_module_exit(void)
  149. {
  150. lowpan_debugfs_exit();
  151. unregister_netdevice_notifier(&lowpan_notifier);
  152. }
  153. module_init(lowpan_module_init);
  154. module_exit(lowpan_module_exit);
  155. MODULE_LICENSE("GPL");