]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/ipv6/addrconf_core.c
treewide: Add SPDX license identifier for missed files
[thirdparty/kernel/stable.git] / net / ipv6 / addrconf_core.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
8c14b7ce
YH
2/*
3 * IPv6 library code, needed by static components when full IPv6 support is
4 * not configured or static.
5 */
6
bc3b2d7f 7#include <linux/export.h>
8c14b7ce 8#include <net/ipv6.h>
3616d08b 9#include <net/ipv6_stubs.h>
f39dc102 10#include <net/ip.h>
8c14b7ce 11
705f1c86
HFS
12/* if ipv6 module registers this function is used by xfrm to force all
13 * sockets to relookup their nodes - this is fairly expensive, be
14 * careful
15 */
16void (*__fib6_flush_trees)(struct net *);
17EXPORT_SYMBOL(__fib6_flush_trees);
18
8c14b7ce
YH
19#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
20
95c96174 21static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
8c14b7ce 22{
d94d34a0 23 switch (scope) {
8c14b7ce
YH
24 case IPV6_ADDR_SCOPE_NODELOCAL:
25 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
26 IPV6_ADDR_LOOPBACK);
27 case IPV6_ADDR_SCOPE_LINKLOCAL:
28 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
29 IPV6_ADDR_LINKLOCAL);
30 case IPV6_ADDR_SCOPE_SITELOCAL:
31 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
32 IPV6_ADDR_SITELOCAL);
33 }
34 return IPV6_ADDR_SCOPE_TYPE(scope);
35}
36
37int __ipv6_addr_type(const struct in6_addr *addr)
38{
39 __be32 st;
40
41 st = addr->s6_addr32[0];
42
43 /* Consider all addresses with the first three bits different of
44 000 and 111 as unicasts.
45 */
46 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
47 (st & htonl(0xE0000000)) != htonl(0xE0000000))
48 return (IPV6_ADDR_UNICAST |
49 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
50
51 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
52 /* multicast */
53 /* addr-select 3.1 */
54 return (IPV6_ADDR_MULTICAST |
55 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
56 }
57
58 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
59 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
60 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
61 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
62 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
63 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
c61a7d10
DJ
64 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
65 return (IPV6_ADDR_UNICAST |
66 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
8c14b7ce
YH
67
68 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
69 if (addr->s6_addr32[2] == 0) {
70 if (addr->s6_addr32[3] == 0)
71 return IPV6_ADDR_ANY;
72
73 if (addr->s6_addr32[3] == htonl(0x00000001))
74 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
75 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
76
77 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
78 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
79 }
80
81 if (addr->s6_addr32[2] == htonl(0x0000ffff))
82 return (IPV6_ADDR_MAPPED |
83 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
84 }
85
45bb0060 86 return (IPV6_ADDR_UNICAST |
8c14b7ce
YH
87 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
88}
7401055b 89EXPORT_SYMBOL(__ipv6_addr_type);
8c14b7ce 90
f88c91dd 91static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
ff7883ea 92static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain);
f88c91dd
CW
93
94int register_inet6addr_notifier(struct notifier_block *nb)
95{
96 return atomic_notifier_chain_register(&inet6addr_chain, nb);
97}
98EXPORT_SYMBOL(register_inet6addr_notifier);
99
100int unregister_inet6addr_notifier(struct notifier_block *nb)
101{
102 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
103}
104EXPORT_SYMBOL(unregister_inet6addr_notifier);
105
106int inet6addr_notifier_call_chain(unsigned long val, void *v)
107{
108 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
109}
110EXPORT_SYMBOL(inet6addr_notifier_call_chain);
5f81bd2e 111
3ad7d246
KJ
112int register_inet6addr_validator_notifier(struct notifier_block *nb)
113{
ff7883ea 114 return blocking_notifier_chain_register(&inet6addr_validator_chain, nb);
3ad7d246
KJ
115}
116EXPORT_SYMBOL(register_inet6addr_validator_notifier);
117
118int unregister_inet6addr_validator_notifier(struct notifier_block *nb)
119{
ff7883ea
DA
120 return blocking_notifier_chain_unregister(&inet6addr_validator_chain,
121 nb);
3ad7d246
KJ
122}
123EXPORT_SYMBOL(unregister_inet6addr_validator_notifier);
124
125int inet6addr_validator_notifier_call_chain(unsigned long val, void *v)
126{
ff7883ea 127 return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v);
3ad7d246
KJ
128}
129EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain);
130
343d60aa
RP
131static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
132 struct dst_entry **u2,
133 struct flowi6 *u3)
134{
135 return -EAFNOSUPPORT;
136}
137
9b0a6a9d
PO
138static int eafnosupport_ipv6_route_input(struct sk_buff *skb)
139{
140 return -EAFNOSUPPORT;
141}
142
65a2022e
DA
143static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
144{
145 return NULL;
146}
147
effda4dd 148static int
65a2022e 149eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
effda4dd
DA
150 int oif, struct flowi6 *fl6,
151 struct fib6_result *res, int flags)
65a2022e 152{
effda4dd 153 return -EAFNOSUPPORT;
65a2022e
DA
154}
155
effda4dd 156static int
65a2022e 157eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
effda4dd 158 struct fib6_result *res, int flags)
65a2022e 159{
effda4dd 160 return -EAFNOSUPPORT;
65a2022e
DA
161}
162
b1d40991
DA
163static void
164eafnosupport_fib6_select_path(const struct net *net, struct fib6_result *res,
165 struct flowi6 *fl6, int oif, bool have_oif_match,
166 const struct sk_buff *skb, int strict)
65a2022e 167{
65a2022e
DA
168}
169
901731b8 170static u32
b748f260
DA
171eafnosupport_ip6_mtu_from_fib6(const struct fib6_result *res,
172 const struct in6_addr *daddr,
173 const struct in6_addr *saddr)
901731b8
DA
174{
175 return 0;
176}
177
1aefd3de
DA
178static int eafnosupport_fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
179 struct fib6_config *cfg, gfp_t gfp_flags,
180 struct netlink_ext_ack *extack)
181{
182 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
183 return -EAFNOSUPPORT;
184}
185
343d60aa 186const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
65a2022e 187 .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
9b0a6a9d 188 .ipv6_route_input = eafnosupport_ipv6_route_input,
65a2022e
DA
189 .fib6_get_table = eafnosupport_fib6_get_table,
190 .fib6_table_lookup = eafnosupport_fib6_table_lookup,
191 .fib6_lookup = eafnosupport_fib6_lookup,
b1d40991 192 .fib6_select_path = eafnosupport_fib6_select_path,
901731b8 193 .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
1aefd3de 194 .fib6_nh_init = eafnosupport_fib6_nh_init,
343d60aa 195};
5f81bd2e 196EXPORT_SYMBOL_GPL(ipv6_stub);
034dfc5d
CW
197
198/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
199const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
200EXPORT_SYMBOL(in6addr_loopback);
201const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
202EXPORT_SYMBOL(in6addr_any);
203const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
204EXPORT_SYMBOL(in6addr_linklocal_allnodes);
205const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
206EXPORT_SYMBOL(in6addr_linklocal_allrouters);
207const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
208EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
209const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
210EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
211const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
212EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
f39dc102
CW
213
214static void snmp6_free_dev(struct inet6_dev *idev)
215{
216 kfree(idev->stats.icmpv6msgdev);
217 kfree(idev->stats.icmpv6dev);
698365fa 218 free_percpu(idev->stats.ipv6);
f39dc102
CW
219}
220
27e41fcf
RS
221static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
222{
223 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
224
225 snmp6_free_dev(idev);
226 kfree(idev);
227}
228
f39dc102
CW
229/* Nobody refers to this device, we may destroy it. */
230
231void in6_dev_finish_destroy(struct inet6_dev *idev)
232{
233 struct net_device *dev = idev->dev;
234
235 WARN_ON(!list_empty(&idev->addr_list));
53b24b8f 236 WARN_ON(idev->mc_list);
f39dc102
CW
237 WARN_ON(timer_pending(&idev->rs_timer));
238
239#ifdef NET_REFCNT_DEBUG
240 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
241#endif
242 dev_put(dev);
243 if (!idev->dead) {
244 pr_warn("Freeing alive inet6 device %p\n", idev);
245 return;
246 }
27e41fcf 247 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
f39dc102
CW
248}
249EXPORT_SYMBOL(in6_dev_finish_destroy);