YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 library code, needed by static components when full IPv6 support is |
| 3 | * not configured or static. |
| 4 | */ |
| 5 | |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 6 | #include <linux/export.h> |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 7 | #include <net/ipv6.h> |
| 8 | |
| 9 | #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16) |
| 10 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 11 | static inline unsigned int ipv6_addr_scope2type(unsigned int scope) |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 12 | { |
Eldad Zack | d94d34a | 2012-04-01 07:49:02 +0000 | [diff] [blame] | 13 | switch (scope) { |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 14 | case IPV6_ADDR_SCOPE_NODELOCAL: |
| 15 | return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) | |
| 16 | IPV6_ADDR_LOOPBACK); |
| 17 | case IPV6_ADDR_SCOPE_LINKLOCAL: |
| 18 | return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) | |
| 19 | IPV6_ADDR_LINKLOCAL); |
| 20 | case IPV6_ADDR_SCOPE_SITELOCAL: |
| 21 | return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) | |
| 22 | IPV6_ADDR_SITELOCAL); |
| 23 | } |
| 24 | return IPV6_ADDR_SCOPE_TYPE(scope); |
| 25 | } |
| 26 | |
| 27 | int __ipv6_addr_type(const struct in6_addr *addr) |
| 28 | { |
| 29 | __be32 st; |
| 30 | |
| 31 | st = addr->s6_addr32[0]; |
| 32 | |
| 33 | /* Consider all addresses with the first three bits different of |
| 34 | 000 and 111 as unicasts. |
| 35 | */ |
| 36 | if ((st & htonl(0xE0000000)) != htonl(0x00000000) && |
| 37 | (st & htonl(0xE0000000)) != htonl(0xE0000000)) |
| 38 | return (IPV6_ADDR_UNICAST | |
| 39 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); |
| 40 | |
| 41 | if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) { |
| 42 | /* multicast */ |
| 43 | /* addr-select 3.1 */ |
| 44 | return (IPV6_ADDR_MULTICAST | |
| 45 | ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr))); |
| 46 | } |
| 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 */ |
Dave Johnson | c61a7d1 | 2007-07-30 17:19:31 -0700 | [diff] [blame] | 54 | if ((st & htonl(0xFE000000)) == htonl(0xFC000000)) |
| 55 | return (IPV6_ADDR_UNICAST | |
| 56 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */ |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 57 | |
| 58 | if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) { |
| 59 | if (addr->s6_addr32[2] == 0) { |
| 60 | if (addr->s6_addr32[3] == 0) |
| 61 | return IPV6_ADDR_ANY; |
| 62 | |
| 63 | if (addr->s6_addr32[3] == htonl(0x00000001)) |
| 64 | return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST | |
| 65 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */ |
| 66 | |
| 67 | return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST | |
| 68 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */ |
| 69 | } |
| 70 | |
| 71 | if (addr->s6_addr32[2] == htonl(0x0000ffff)) |
| 72 | return (IPV6_ADDR_MAPPED | |
| 73 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */ |
| 74 | } |
| 75 | |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 76 | return (IPV6_ADDR_UNICAST | |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 77 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */ |
| 78 | } |
David S. Miller | 7401055 | 2007-02-21 23:26:56 -0800 | [diff] [blame] | 79 | EXPORT_SYMBOL(__ipv6_addr_type); |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 80 | |