addrconf_core.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
  9. static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
  10. {
  11. switch (scope) {
  12. case IPV6_ADDR_SCOPE_NODELOCAL:
  13. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
  14. IPV6_ADDR_LOOPBACK);
  15. case IPV6_ADDR_SCOPE_LINKLOCAL:
  16. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
  17. IPV6_ADDR_LINKLOCAL);
  18. case IPV6_ADDR_SCOPE_SITELOCAL:
  19. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
  20. IPV6_ADDR_SITELOCAL);
  21. }
  22. return IPV6_ADDR_SCOPE_TYPE(scope);
  23. }
  24. int __ipv6_addr_type(const struct in6_addr *addr)
  25. {
  26. __be32 st;
  27. st = addr->s6_addr32[0];
  28. /* Consider all addresses with the first three bits different of
  29. 000 and 111 as unicasts.
  30. */
  31. if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
  32. (st & htonl(0xE0000000)) != htonl(0xE0000000))
  33. return (IPV6_ADDR_UNICAST |
  34. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
  35. if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
  36. /* multicast */
  37. /* addr-select 3.1 */
  38. return (IPV6_ADDR_MULTICAST |
  39. ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
  40. }
  41. if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
  42. return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
  43. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
  44. if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
  45. return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
  46. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
  47. if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
  48. return (IPV6_ADDR_UNICAST |
  49. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
  50. if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
  51. if (addr->s6_addr32[2] == 0) {
  52. if (addr->s6_addr32[3] == 0)
  53. return IPV6_ADDR_ANY;
  54. if (addr->s6_addr32[3] == htonl(0x00000001))
  55. return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
  56. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
  57. return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
  58. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  59. }
  60. if (addr->s6_addr32[2] == htonl(0x0000ffff))
  61. return (IPV6_ADDR_MAPPED |
  62. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  63. }
  64. return (IPV6_ADDR_UNICAST |
  65. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
  66. }
  67. EXPORT_SYMBOL(__ipv6_addr_type);
  68. static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
  69. int register_inet6addr_notifier(struct notifier_block *nb)
  70. {
  71. return atomic_notifier_chain_register(&inet6addr_chain, nb);
  72. }
  73. EXPORT_SYMBOL(register_inet6addr_notifier);
  74. int unregister_inet6addr_notifier(struct notifier_block *nb)
  75. {
  76. return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
  77. }
  78. EXPORT_SYMBOL(unregister_inet6addr_notifier);
  79. int inet6addr_notifier_call_chain(unsigned long val, void *v)
  80. {
  81. return atomic_notifier_call_chain(&inet6addr_chain, val, v);
  82. }
  83. EXPORT_SYMBOL(inet6addr_notifier_call_chain);