addrconf_core.c 2.5 KB

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