addrconf_core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * IPv6 library code, needed by static components when full IPv6 support is
  3. * not configured or static.
  4. */
  5. #include <linux/export.h>
  6. #include <net/ipv6.h>
  7. #include <net/addrconf.h>
  8. #include <net/ip.h>
  9. /* if ipv6 module registers this function is used by xfrm to force all
  10. * sockets to relookup their nodes - this is fairly expensive, be
  11. * careful
  12. */
  13. void (*__fib6_flush_trees)(struct net *);
  14. EXPORT_SYMBOL(__fib6_flush_trees);
  15. #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
  16. static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
  17. {
  18. switch (scope) {
  19. case IPV6_ADDR_SCOPE_NODELOCAL:
  20. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
  21. IPV6_ADDR_LOOPBACK);
  22. case IPV6_ADDR_SCOPE_LINKLOCAL:
  23. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
  24. IPV6_ADDR_LINKLOCAL);
  25. case IPV6_ADDR_SCOPE_SITELOCAL:
  26. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
  27. IPV6_ADDR_SITELOCAL);
  28. }
  29. return IPV6_ADDR_SCOPE_TYPE(scope);
  30. }
  31. int __ipv6_addr_type(const struct in6_addr *addr)
  32. {
  33. __be32 st;
  34. st = addr->s6_addr32[0];
  35. /* Consider all addresses with the first three bits different of
  36. 000 and 111 as unicasts.
  37. */
  38. if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
  39. (st & htonl(0xE0000000)) != htonl(0xE0000000))
  40. return (IPV6_ADDR_UNICAST |
  41. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
  42. if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
  43. /* multicast */
  44. /* addr-select 3.1 */
  45. return (IPV6_ADDR_MULTICAST |
  46. ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
  47. }
  48. if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
  49. return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
  50. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
  51. if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
  52. return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
  53. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
  54. if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
  55. return (IPV6_ADDR_UNICAST |
  56. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
  57. if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
  58. if (addr->s6_addr32[2] == 0) {
  59. if (addr->s6_addr32[3] == 0)
  60. return IPV6_ADDR_ANY;
  61. if (addr->s6_addr32[3] == htonl(0x00000001))
  62. return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
  63. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
  64. return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
  65. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  66. }
  67. if (addr->s6_addr32[2] == htonl(0x0000ffff))
  68. return (IPV6_ADDR_MAPPED |
  69. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  70. }
  71. return (IPV6_ADDR_UNICAST |
  72. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
  73. }
  74. EXPORT_SYMBOL(__ipv6_addr_type);
  75. static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
  76. int register_inet6addr_notifier(struct notifier_block *nb)
  77. {
  78. return atomic_notifier_chain_register(&inet6addr_chain, nb);
  79. }
  80. EXPORT_SYMBOL(register_inet6addr_notifier);
  81. int unregister_inet6addr_notifier(struct notifier_block *nb)
  82. {
  83. return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
  84. }
  85. EXPORT_SYMBOL(unregister_inet6addr_notifier);
  86. int inet6addr_notifier_call_chain(unsigned long val, void *v)
  87. {
  88. return atomic_notifier_call_chain(&inet6addr_chain, val, v);
  89. }
  90. EXPORT_SYMBOL(inet6addr_notifier_call_chain);
  91. const struct ipv6_stub *ipv6_stub __read_mostly;
  92. EXPORT_SYMBOL_GPL(ipv6_stub);
  93. /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
  94. const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  95. EXPORT_SYMBOL(in6addr_loopback);
  96. const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  97. EXPORT_SYMBOL(in6addr_any);
  98. const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
  99. EXPORT_SYMBOL(in6addr_linklocal_allnodes);
  100. const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
  101. EXPORT_SYMBOL(in6addr_linklocal_allrouters);
  102. const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
  103. EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
  104. const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
  105. EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
  106. const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
  107. EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
  108. static void snmp6_free_dev(struct inet6_dev *idev)
  109. {
  110. kfree(idev->stats.icmpv6msgdev);
  111. kfree(idev->stats.icmpv6dev);
  112. free_percpu(idev->stats.ipv6);
  113. }
  114. static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
  115. {
  116. struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
  117. snmp6_free_dev(idev);
  118. kfree(idev);
  119. }
  120. /* Nobody refers to this device, we may destroy it. */
  121. void in6_dev_finish_destroy(struct inet6_dev *idev)
  122. {
  123. struct net_device *dev = idev->dev;
  124. WARN_ON(!list_empty(&idev->addr_list));
  125. WARN_ON(idev->mc_list);
  126. WARN_ON(timer_pending(&idev->rs_timer));
  127. #ifdef NET_REFCNT_DEBUG
  128. pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
  129. #endif
  130. dev_put(dev);
  131. if (!idev->dead) {
  132. pr_warn("Freeing alive inet6 device %p\n", idev);
  133. return;
  134. }
  135. call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
  136. }
  137. EXPORT_SYMBOL(in6_dev_finish_destroy);