blob: 4c11cbcf83089152052b60072dbe11d147d10803 [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>
Cong Wangf39dc102013-08-31 13:44:35 +08009#include <net/ip.h>
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090010
11#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
12
Eric Dumazet95c96172012-04-15 05:58:06 +000013static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090014{
Eldad Zackd94d34a2012-04-01 07:49:02 +000015 switch (scope) {
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090016 case IPV6_ADDR_SCOPE_NODELOCAL:
17 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
18 IPV6_ADDR_LOOPBACK);
19 case IPV6_ADDR_SCOPE_LINKLOCAL:
20 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
21 IPV6_ADDR_LINKLOCAL);
22 case IPV6_ADDR_SCOPE_SITELOCAL:
23 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
24 IPV6_ADDR_SITELOCAL);
25 }
26 return IPV6_ADDR_SCOPE_TYPE(scope);
27}
28
29int __ipv6_addr_type(const struct in6_addr *addr)
30{
31 __be32 st;
32
33 st = addr->s6_addr32[0];
34
35 /* Consider all addresses with the first three bits different of
36 000 and 111 as unicasts.
37 */
38 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
39 (st & htonl(0xE0000000)) != htonl(0xE0000000))
40 return (IPV6_ADDR_UNICAST |
41 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
42
43 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
44 /* multicast */
45 /* addr-select 3.1 */
46 return (IPV6_ADDR_MULTICAST |
47 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
48 }
49
50 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
51 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
52 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
53 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
54 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
55 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Dave Johnsonc61a7d12007-07-30 17:19:31 -070056 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
57 return (IPV6_ADDR_UNICAST |
58 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090059
60 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
61 if (addr->s6_addr32[2] == 0) {
62 if (addr->s6_addr32[3] == 0)
63 return IPV6_ADDR_ANY;
64
65 if (addr->s6_addr32[3] == htonl(0x00000001))
66 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
67 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
68
69 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
70 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
71 }
72
73 if (addr->s6_addr32[2] == htonl(0x0000ffff))
74 return (IPV6_ADDR_MAPPED |
75 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
76 }
77
Ulrich Weber45bb0062010-02-25 23:28:58 +000078 return (IPV6_ADDR_UNICAST |
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090079 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
80}
David S. Miller74010552007-02-21 23:26:56 -080081EXPORT_SYMBOL(__ipv6_addr_type);
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090082
Cong Wangf88c91d2013-04-14 23:18:43 +080083static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
84
85int register_inet6addr_notifier(struct notifier_block *nb)
86{
87 return atomic_notifier_chain_register(&inet6addr_chain, nb);
88}
89EXPORT_SYMBOL(register_inet6addr_notifier);
90
91int unregister_inet6addr_notifier(struct notifier_block *nb)
92{
93 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
94}
95EXPORT_SYMBOL(unregister_inet6addr_notifier);
96
97int inet6addr_notifier_call_chain(unsigned long val, void *v)
98{
99 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
100}
101EXPORT_SYMBOL(inet6addr_notifier_call_chain);
Cong Wang5f81bd22013-08-31 13:44:30 +0800102
103const struct ipv6_stub *ipv6_stub __read_mostly;
104EXPORT_SYMBOL_GPL(ipv6_stub);
Cong Wang034dfc52013-08-31 13:44:31 +0800105
106/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
107const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
108EXPORT_SYMBOL(in6addr_loopback);
109const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
110EXPORT_SYMBOL(in6addr_any);
111const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
112EXPORT_SYMBOL(in6addr_linklocal_allnodes);
113const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
114EXPORT_SYMBOL(in6addr_linklocal_allrouters);
115const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
116EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
117const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
118EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
119const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
120EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
Cong Wangf39dc102013-08-31 13:44:35 +0800121
122static void snmp6_free_dev(struct inet6_dev *idev)
123{
124 kfree(idev->stats.icmpv6msgdev);
125 kfree(idev->stats.icmpv6dev);
126 snmp_mib_free((void __percpu **)idev->stats.ipv6);
127}
128
129/* Nobody refers to this device, we may destroy it. */
130
131void in6_dev_finish_destroy(struct inet6_dev *idev)
132{
133 struct net_device *dev = idev->dev;
134
135 WARN_ON(!list_empty(&idev->addr_list));
136 WARN_ON(idev->mc_list != NULL);
137 WARN_ON(timer_pending(&idev->rs_timer));
138
139#ifdef NET_REFCNT_DEBUG
140 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
141#endif
142 dev_put(dev);
143 if (!idev->dead) {
144 pr_warn("Freeing alive inet6 device %p\n", idev);
145 return;
146 }
147 snmp6_free_dev(idev);
148 kfree_rcu(idev, rcu);
149}
150EXPORT_SYMBOL(in6_dev_finish_destroy);