blob: d2f87427244b2ccd00f754048e46d4882f77363d [file] [log] [blame]
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +09001/*
2 * IPv6 library code, needed by static components when full IPv6 support is
3 * not configured or static.
4 */
5
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04006#include <linux/export.h>
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +09007#include <net/ipv6.h>
Eric Dumazet6da334e2013-06-25 01:30:11 -07008#include <net/addrconf.h>
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +09009
10#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
11
Eric Dumazet95c96172012-04-15 05:58:06 +000012static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090013{
Eldad Zackd94d34a2012-04-01 07:49:02 +000014 switch (scope) {
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090015 case IPV6_ADDR_SCOPE_NODELOCAL:
16 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
17 IPV6_ADDR_LOOPBACK);
18 case IPV6_ADDR_SCOPE_LINKLOCAL:
19 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
20 IPV6_ADDR_LINKLOCAL);
21 case IPV6_ADDR_SCOPE_SITELOCAL:
22 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
23 IPV6_ADDR_SITELOCAL);
24 }
25 return IPV6_ADDR_SCOPE_TYPE(scope);
26}
27
28int __ipv6_addr_type(const struct in6_addr *addr)
29{
30 __be32 st;
31
32 st = addr->s6_addr32[0];
33
34 /* Consider all addresses with the first three bits different of
35 000 and 111 as unicasts.
36 */
37 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
38 (st & htonl(0xE0000000)) != htonl(0xE0000000))
39 return (IPV6_ADDR_UNICAST |
40 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
41
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
49 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
50 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
51 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
52 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
53 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
54 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Dave Johnsonc61a7d12007-07-30 17:19:31 -070055 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
56 return (IPV6_ADDR_UNICAST |
57 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090058
59 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
60 if (addr->s6_addr32[2] == 0) {
61 if (addr->s6_addr32[3] == 0)
62 return IPV6_ADDR_ANY;
63
64 if (addr->s6_addr32[3] == htonl(0x00000001))
65 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
66 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
67
68 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
69 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
70 }
71
72 if (addr->s6_addr32[2] == htonl(0x0000ffff))
73 return (IPV6_ADDR_MAPPED |
74 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
75 }
76
Ulrich Weber45bb0062010-02-25 23:28:58 +000077 return (IPV6_ADDR_UNICAST |
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090078 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
79}
David S. Miller74010552007-02-21 23:26:56 -080080EXPORT_SYMBOL(__ipv6_addr_type);
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090081
Cong Wangf88c91d2013-04-14 23:18:43 +080082static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
83
84int register_inet6addr_notifier(struct notifier_block *nb)
85{
86 return atomic_notifier_chain_register(&inet6addr_chain, nb);
87}
88EXPORT_SYMBOL(register_inet6addr_notifier);
89
90int unregister_inet6addr_notifier(struct notifier_block *nb)
91{
92 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
93}
94EXPORT_SYMBOL(unregister_inet6addr_notifier);
95
96int inet6addr_notifier_call_chain(unsigned long val, void *v)
97{
98 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
99}
100EXPORT_SYMBOL(inet6addr_notifier_call_chain);