core.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "6lowpan_i.h"
  16. int lowpan_register_netdevice(struct net_device *dev,
  17. enum lowpan_lltypes lltype)
  18. {
  19. int i, ret;
  20. dev->addr_len = EUI64_ADDR_LEN;
  21. dev->type = ARPHRD_6LOWPAN;
  22. dev->mtu = IPV6_MIN_MTU;
  23. dev->priv_flags |= IFF_NO_QUEUE;
  24. lowpan_priv(dev)->lltype = lltype;
  25. spin_lock_init(&lowpan_priv(dev)->ctx.lock);
  26. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  27. lowpan_priv(dev)->ctx.table[i].id = i;
  28. ret = register_netdevice(dev);
  29. if (ret < 0)
  30. return ret;
  31. ret = lowpan_dev_debugfs_init(dev);
  32. if (ret < 0)
  33. unregister_netdevice(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. static int lowpan_event(struct notifier_block *unused,
  61. unsigned long event, void *ptr)
  62. {
  63. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  64. int i;
  65. if (dev->type != ARPHRD_6LOWPAN)
  66. return NOTIFY_DONE;
  67. switch (event) {
  68. case NETDEV_DOWN:
  69. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  70. clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE,
  71. &lowpan_priv(dev)->ctx.table[i].flags);
  72. break;
  73. default:
  74. return NOTIFY_DONE;
  75. }
  76. return NOTIFY_OK;
  77. }
  78. static struct notifier_block lowpan_notifier = {
  79. .notifier_call = lowpan_event,
  80. };
  81. static int __init lowpan_module_init(void)
  82. {
  83. int ret;
  84. ret = lowpan_debugfs_init();
  85. if (ret < 0)
  86. return ret;
  87. ret = register_netdevice_notifier(&lowpan_notifier);
  88. if (ret < 0) {
  89. lowpan_debugfs_exit();
  90. return ret;
  91. }
  92. request_module_nowait("ipv6");
  93. request_module_nowait("nhc_dest");
  94. request_module_nowait("nhc_fragment");
  95. request_module_nowait("nhc_hop");
  96. request_module_nowait("nhc_ipv6");
  97. request_module_nowait("nhc_mobility");
  98. request_module_nowait("nhc_routing");
  99. request_module_nowait("nhc_udp");
  100. return 0;
  101. }
  102. static void __exit lowpan_module_exit(void)
  103. {
  104. lowpan_debugfs_exit();
  105. unregister_netdevice_notifier(&lowpan_notifier);
  106. }
  107. module_init(lowpan_module_init);
  108. module_exit(lowpan_module_exit);
  109. MODULE_LICENSE("GPL");