addrconf_core.c 2.4 KB

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