core.c 4.0 KB

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