core.c 4.0 KB

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