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