blob: 5cd0029d930e2d2cd940cd1c618e5d5b81b952cc [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
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +020011/* if ipv6 module registers this function is used by xfrm to force all
12 * sockets to relookup their nodes - this is fairly expensive, be
13 * careful
14 */
15void (*__fib6_flush_trees)(struct net *);
16EXPORT_SYMBOL(__fib6_flush_trees);
17
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090018#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
19
Eric Dumazet95c96172012-04-15 05:58:06 +000020static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090021{
Eldad Zackd94d34a2012-04-01 07:49:02 +000022 switch (scope) {
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090023 case IPV6_ADDR_SCOPE_NODELOCAL:
24 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
25 IPV6_ADDR_LOOPBACK);
26 case IPV6_ADDR_SCOPE_LINKLOCAL:
27 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
28 IPV6_ADDR_LINKLOCAL);
29 case IPV6_ADDR_SCOPE_SITELOCAL:
30 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
31 IPV6_ADDR_SITELOCAL);
32 }
33 return IPV6_ADDR_SCOPE_TYPE(scope);
34}
35
36int __ipv6_addr_type(const struct in6_addr *addr)
37{
38 __be32 st;
39
40 st = addr->s6_addr32[0];
41
42 /* Consider all addresses with the first three bits different of
43 000 and 111 as unicasts.
44 */
45 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
46 (st & htonl(0xE0000000)) != htonl(0xE0000000))
47 return (IPV6_ADDR_UNICAST |
48 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
49
50 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
51 /* multicast */
52 /* addr-select 3.1 */
53 return (IPV6_ADDR_MULTICAST |
54 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
55 }
56
57 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
58 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
59 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
60 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
61 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
62 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Dave Johnsonc61a7d12007-07-30 17:19:31 -070063 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
64 return (IPV6_ADDR_UNICAST |
65 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090066
67 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
68 if (addr->s6_addr32[2] == 0) {
69 if (addr->s6_addr32[3] == 0)
70 return IPV6_ADDR_ANY;
71
72 if (addr->s6_addr32[3] == htonl(0x00000001))
73 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
74 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
75
76 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
77 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
78 }
79
80 if (addr->s6_addr32[2] == htonl(0x0000ffff))
81 return (IPV6_ADDR_MAPPED |
82 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
83 }
84
Ulrich Weber45bb0062010-02-25 23:28:58 +000085 return (IPV6_ADDR_UNICAST |
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090086 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
87}
David S. Miller74010552007-02-21 23:26:56 -080088EXPORT_SYMBOL(__ipv6_addr_type);
YOSHIFUJI Hideaki8c14b7c2007-02-22 02:25:42 +090089
Cong Wangf88c91d2013-04-14 23:18:43 +080090static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
David Ahernff7883e2017-10-18 09:56:53 -070091static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain);
Cong Wangf88c91d2013-04-14 23:18:43 +080092
93int register_inet6addr_notifier(struct notifier_block *nb)
94{
95 return atomic_notifier_chain_register(&inet6addr_chain, nb);
96}
97EXPORT_SYMBOL(register_inet6addr_notifier);
98
99int unregister_inet6addr_notifier(struct notifier_block *nb)
100{
101 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
102}
103EXPORT_SYMBOL(unregister_inet6addr_notifier);
104
105int inet6addr_notifier_call_chain(unsigned long val, void *v)
106{
107 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
108}
109EXPORT_SYMBOL(inet6addr_notifier_call_chain);
Cong Wang5f81bd22013-08-31 13:44:30 +0800110
Krister Johansen3ad7d242017-06-08 13:12:14 -0700111int register_inet6addr_validator_notifier(struct notifier_block *nb)
112{
David Ahernff7883e2017-10-18 09:56:53 -0700113 return blocking_notifier_chain_register(&inet6addr_validator_chain, nb);
Krister Johansen3ad7d242017-06-08 13:12:14 -0700114}
115EXPORT_SYMBOL(register_inet6addr_validator_notifier);
116
117int unregister_inet6addr_validator_notifier(struct notifier_block *nb)
118{
David Ahernff7883e2017-10-18 09:56:53 -0700119 return blocking_notifier_chain_unregister(&inet6addr_validator_chain,
120 nb);
Krister Johansen3ad7d242017-06-08 13:12:14 -0700121}
122EXPORT_SYMBOL(unregister_inet6addr_validator_notifier);
123
124int inet6addr_validator_notifier_call_chain(unsigned long val, void *v)
125{
David Ahernff7883e2017-10-18 09:56:53 -0700126 return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v);
Krister Johansen3ad7d242017-06-08 13:12:14 -0700127}
128EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain);
129
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700130static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
131 struct dst_entry **u2,
132 struct flowi6 *u3)
133{
134 return -EAFNOSUPPORT;
135}
136
David Ahern65a20222018-05-09 20:34:25 -0700137static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
138{
139 return NULL;
140}
141
142static struct fib6_info *
143eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
144 int oif, struct flowi6 *fl6, int flags)
145{
146 return NULL;
147}
148
149static struct fib6_info *
150eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
151 int flags)
152{
153 return NULL;
154}
155
156static struct fib6_info *
157eafnosupport_fib6_multipath_select(const struct net *net, struct fib6_info *f6i,
158 struct flowi6 *fl6, int oif,
159 const struct sk_buff *skb, int strict)
160{
161 return f6i;
162}
163
David Ahern901731b2018-05-21 09:08:14 -0700164static u32
165eafnosupport_ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
166 struct in6_addr *saddr)
167{
168 return 0;
169}
170
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700171const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
David Ahern65a20222018-05-09 20:34:25 -0700172 .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
173 .fib6_get_table = eafnosupport_fib6_get_table,
174 .fib6_table_lookup = eafnosupport_fib6_table_lookup,
175 .fib6_lookup = eafnosupport_fib6_lookup,
176 .fib6_multipath_select = eafnosupport_fib6_multipath_select,
David Ahern901731b2018-05-21 09:08:14 -0700177 .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700178};
Cong Wang5f81bd22013-08-31 13:44:30 +0800179EXPORT_SYMBOL_GPL(ipv6_stub);
Cong Wang034dfc52013-08-31 13:44:31 +0800180
181/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
182const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
183EXPORT_SYMBOL(in6addr_loopback);
184const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
185EXPORT_SYMBOL(in6addr_any);
186const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
187EXPORT_SYMBOL(in6addr_linklocal_allnodes);
188const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
189EXPORT_SYMBOL(in6addr_linklocal_allrouters);
190const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
191EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
192const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
193EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
194const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
195EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
Cong Wangf39dc102013-08-31 13:44:35 +0800196
197static void snmp6_free_dev(struct inet6_dev *idev)
198{
199 kfree(idev->stats.icmpv6msgdev);
200 kfree(idev->stats.icmpv6dev);
WANG Cong698365f2014-05-05 15:55:55 -0700201 free_percpu(idev->stats.ipv6);
Cong Wangf39dc102013-08-31 13:44:35 +0800202}
203
Robert Shearman27e41fc2015-06-05 18:51:54 +0100204static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
205{
206 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
207
208 snmp6_free_dev(idev);
209 kfree(idev);
210}
211
Cong Wangf39dc102013-08-31 13:44:35 +0800212/* Nobody refers to this device, we may destroy it. */
213
214void in6_dev_finish_destroy(struct inet6_dev *idev)
215{
216 struct net_device *dev = idev->dev;
217
218 WARN_ON(!list_empty(&idev->addr_list));
Ian Morris53b24b82015-03-29 14:00:05 +0100219 WARN_ON(idev->mc_list);
Cong Wangf39dc102013-08-31 13:44:35 +0800220 WARN_ON(timer_pending(&idev->rs_timer));
221
222#ifdef NET_REFCNT_DEBUG
223 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
224#endif
225 dev_put(dev);
226 if (!idev->dead) {
227 pr_warn("Freeing alive inet6 device %p\n", idev);
228 return;
229 }
Robert Shearman27e41fc2015-06-05 18:51:54 +0100230 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Cong Wangf39dc102013-08-31 13:44:35 +0800231}
232EXPORT_SYMBOL(in6_dev_finish_destroy);