Loading...
1/*
2 * IPv6 library code, needed by static components when full IPv6 support is
3 * not configured or static.
4 */
5
6#include <linux/export.h>
7#include <net/ipv6.h>
8#include <net/addrconf.h>
9#include <net/ip.h>
10
11/* 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
18#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
19
20static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
21{
22 switch (scope) {
23 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 */
63 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
64 return (IPV6_ADDR_UNICAST |
65 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
66
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
85 return (IPV6_ADDR_UNICAST |
86 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
87}
88EXPORT_SYMBOL(__ipv6_addr_type);
89
90static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
91
92int register_inet6addr_notifier(struct notifier_block *nb)
93{
94 return atomic_notifier_chain_register(&inet6addr_chain, nb);
95}
96EXPORT_SYMBOL(register_inet6addr_notifier);
97
98int unregister_inet6addr_notifier(struct notifier_block *nb)
99{
100 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
101}
102EXPORT_SYMBOL(unregister_inet6addr_notifier);
103
104int inet6addr_notifier_call_chain(unsigned long val, void *v)
105{
106 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
107}
108EXPORT_SYMBOL(inet6addr_notifier_call_chain);
109
110static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
111 struct dst_entry **u2,
112 struct flowi6 *u3)
113{
114 return -EAFNOSUPPORT;
115}
116
117const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
118 .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
119};
120EXPORT_SYMBOL_GPL(ipv6_stub);
121
122/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
123const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
124EXPORT_SYMBOL(in6addr_loopback);
125const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
126EXPORT_SYMBOL(in6addr_any);
127const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
128EXPORT_SYMBOL(in6addr_linklocal_allnodes);
129const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
130EXPORT_SYMBOL(in6addr_linklocal_allrouters);
131const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
132EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
133const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
134EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
135const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
136EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
137
138static void snmp6_free_dev(struct inet6_dev *idev)
139{
140 kfree(idev->stats.icmpv6msgdev);
141 kfree(idev->stats.icmpv6dev);
142 free_percpu(idev->stats.ipv6);
143}
144
145static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
146{
147 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
148
149 snmp6_free_dev(idev);
150 kfree(idev);
151}
152
153/* Nobody refers to this device, we may destroy it. */
154
155void in6_dev_finish_destroy(struct inet6_dev *idev)
156{
157 struct net_device *dev = idev->dev;
158
159 WARN_ON(!list_empty(&idev->addr_list));
160 WARN_ON(idev->mc_list);
161 WARN_ON(timer_pending(&idev->rs_timer));
162
163#ifdef NET_REFCNT_DEBUG
164 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
165#endif
166 dev_put(dev);
167 if (!idev->dead) {
168 pr_warn("Freeing alive inet6 device %p\n", idev);
169 return;
170 }
171 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
172}
173EXPORT_SYMBOL(in6_dev_finish_destroy);
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 struct dst_entry *eafnosupport_ipv6_dst_lookup_flow(struct net *net,
133 const struct sock *sk,
134 struct flowi6 *fl6,
135 const struct in6_addr *final_dst)
136{
137 return ERR_PTR(-EAFNOSUPPORT);
138}
139
140static int eafnosupport_ipv6_route_input(struct sk_buff *skb)
141{
142 return -EAFNOSUPPORT;
143}
144
145static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
146{
147 return NULL;
148}
149
150static int
151eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
152 int oif, struct flowi6 *fl6,
153 struct fib6_result *res, int flags)
154{
155 return -EAFNOSUPPORT;
156}
157
158static int
159eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
160 struct fib6_result *res, int flags)
161{
162 return -EAFNOSUPPORT;
163}
164
165static void
166eafnosupport_fib6_select_path(const struct net *net, struct fib6_result *res,
167 struct flowi6 *fl6, int oif, bool have_oif_match,
168 const struct sk_buff *skb, int strict)
169{
170}
171
172static u32
173eafnosupport_ip6_mtu_from_fib6(const struct fib6_result *res,
174 const struct in6_addr *daddr,
175 const struct in6_addr *saddr)
176{
177 return 0;
178}
179
180static int eafnosupport_fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
181 struct fib6_config *cfg, gfp_t gfp_flags,
182 struct netlink_ext_ack *extack)
183{
184 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
185 return -EAFNOSUPPORT;
186}
187
188static int eafnosupport_ip6_del_rt(struct net *net, struct fib6_info *rt,
189 bool skip_notify)
190{
191 return -EAFNOSUPPORT;
192}
193
194static int eafnosupport_ipv6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
195 int (*output)(struct net *, struct sock *, struct sk_buff *))
196{
197 kfree_skb(skb);
198 return -EAFNOSUPPORT;
199}
200
201static struct net_device *eafnosupport_ipv6_dev_find(struct net *net, const struct in6_addr *addr,
202 struct net_device *dev)
203{
204 return ERR_PTR(-EAFNOSUPPORT);
205}
206
207const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
208 .ipv6_dst_lookup_flow = eafnosupport_ipv6_dst_lookup_flow,
209 .ipv6_route_input = eafnosupport_ipv6_route_input,
210 .fib6_get_table = eafnosupport_fib6_get_table,
211 .fib6_table_lookup = eafnosupport_fib6_table_lookup,
212 .fib6_lookup = eafnosupport_fib6_lookup,
213 .fib6_select_path = eafnosupport_fib6_select_path,
214 .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
215 .fib6_nh_init = eafnosupport_fib6_nh_init,
216 .ip6_del_rt = eafnosupport_ip6_del_rt,
217 .ipv6_fragment = eafnosupport_ipv6_fragment,
218 .ipv6_dev_find = eafnosupport_ipv6_dev_find,
219};
220EXPORT_SYMBOL_GPL(ipv6_stub);
221
222/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
223const struct in6_addr in6addr_loopback __aligned(BITS_PER_LONG/8)
224 = IN6ADDR_LOOPBACK_INIT;
225EXPORT_SYMBOL(in6addr_loopback);
226const struct in6_addr in6addr_any __aligned(BITS_PER_LONG/8)
227 = IN6ADDR_ANY_INIT;
228EXPORT_SYMBOL(in6addr_any);
229const struct in6_addr in6addr_linklocal_allnodes __aligned(BITS_PER_LONG/8)
230 = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
231EXPORT_SYMBOL(in6addr_linklocal_allnodes);
232const struct in6_addr in6addr_linklocal_allrouters __aligned(BITS_PER_LONG/8)
233 = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
234EXPORT_SYMBOL(in6addr_linklocal_allrouters);
235const struct in6_addr in6addr_interfacelocal_allnodes __aligned(BITS_PER_LONG/8)
236 = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
237EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
238const struct in6_addr in6addr_interfacelocal_allrouters __aligned(BITS_PER_LONG/8)
239 = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
240EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
241const struct in6_addr in6addr_sitelocal_allrouters __aligned(BITS_PER_LONG/8)
242 = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
243EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
244
245static void snmp6_free_dev(struct inet6_dev *idev)
246{
247 kfree(idev->stats.icmpv6msgdev);
248 kfree(idev->stats.icmpv6dev);
249 free_percpu(idev->stats.ipv6);
250}
251
252static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
253{
254 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
255
256 snmp6_free_dev(idev);
257 kfree(idev);
258}
259
260/* Nobody refers to this device, we may destroy it. */
261
262void in6_dev_finish_destroy(struct inet6_dev *idev)
263{
264 struct net_device *dev = idev->dev;
265
266 WARN_ON(!list_empty(&idev->addr_list));
267 WARN_ON(rcu_access_pointer(idev->mc_list));
268 WARN_ON(timer_pending(&idev->rs_timer));
269
270#ifdef NET_REFCNT_DEBUG
271 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
272#endif
273 netdev_put(dev, &idev->dev_tracker);
274 if (!idev->dead) {
275 pr_warn("Freeing alive inet6 device %p\n", idev);
276 return;
277 }
278 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
279}
280EXPORT_SYMBOL(in6_dev_finish_destroy);