1 // SPDX-License-Identifier: GPL-2.0-only
3 * IPv6 library code, needed by static components when full IPv6 support is
4 * not configured or static.
7 #include <linux/export.h>
9 #include <net/ipv6_stubs.h>
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
16 void (*__fib6_flush_trees)(struct net *);
17 EXPORT_SYMBOL(__fib6_flush_trees);
19 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
21 static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
24 case IPV6_ADDR_SCOPE_NODELOCAL:
25 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
27 case IPV6_ADDR_SCOPE_LINKLOCAL:
28 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
30 case IPV6_ADDR_SCOPE_SITELOCAL:
31 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
34 return IPV6_ADDR_SCOPE_TYPE(scope);
37 int __ipv6_addr_type(const struct in6_addr *addr)
41 st = addr->s6_addr32[0];
43 /* Consider all addresses with the first three bits different of
44 000 and 111 as unicasts.
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));
51 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
54 return (IPV6_ADDR_MULTICAST |
55 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
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 */
64 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
65 return (IPV6_ADDR_UNICAST |
66 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
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)
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 */
77 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
78 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
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 */
86 return (IPV6_ADDR_UNICAST |
87 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
89 EXPORT_SYMBOL(__ipv6_addr_type);
91 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
92 static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain);
94 int register_inet6addr_notifier(struct notifier_block *nb)
96 return atomic_notifier_chain_register(&inet6addr_chain, nb);
98 EXPORT_SYMBOL(register_inet6addr_notifier);
100 int unregister_inet6addr_notifier(struct notifier_block *nb)
102 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
104 EXPORT_SYMBOL(unregister_inet6addr_notifier);
106 int inet6addr_notifier_call_chain(unsigned long val, void *v)
108 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
110 EXPORT_SYMBOL(inet6addr_notifier_call_chain);
112 int register_inet6addr_validator_notifier(struct notifier_block *nb)
114 return blocking_notifier_chain_register(&inet6addr_validator_chain, nb);
116 EXPORT_SYMBOL(register_inet6addr_validator_notifier);
118 int unregister_inet6addr_validator_notifier(struct notifier_block *nb)
120 return blocking_notifier_chain_unregister(&inet6addr_validator_chain,
123 EXPORT_SYMBOL(unregister_inet6addr_validator_notifier);
125 int inet6addr_validator_notifier_call_chain(unsigned long val, void *v)
127 return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v);
129 EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain);
131 static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
132 struct dst_entry **u2,
135 return -EAFNOSUPPORT;
138 static int eafnosupport_ipv6_route_input(struct sk_buff *skb)
140 return -EAFNOSUPPORT;
143 static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
149 eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
150 int oif, struct flowi6 *fl6,
151 struct fib6_result *res, int flags)
153 return -EAFNOSUPPORT;
157 eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
158 struct fib6_result *res, int flags)
160 return -EAFNOSUPPORT;
164 eafnosupport_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)
171 eafnosupport_ip6_mtu_from_fib6(const struct fib6_result *res,
172 const struct in6_addr *daddr,
173 const struct in6_addr *saddr)
178 static 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)
182 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
183 return -EAFNOSUPPORT;
186 static int eafnosupport_ip6_del_rt(struct net *net, struct fib6_info *rt)
188 return -EAFNOSUPPORT;
191 const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
192 .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
193 .ipv6_route_input = eafnosupport_ipv6_route_input,
194 .fib6_get_table = eafnosupport_fib6_get_table,
195 .fib6_table_lookup = eafnosupport_fib6_table_lookup,
196 .fib6_lookup = eafnosupport_fib6_lookup,
197 .fib6_select_path = eafnosupport_fib6_select_path,
198 .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
199 .fib6_nh_init = eafnosupport_fib6_nh_init,
200 .ip6_del_rt = eafnosupport_ip6_del_rt,
202 EXPORT_SYMBOL_GPL(ipv6_stub);
204 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
205 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
206 EXPORT_SYMBOL(in6addr_loopback);
207 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
208 EXPORT_SYMBOL(in6addr_any);
209 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
210 EXPORT_SYMBOL(in6addr_linklocal_allnodes);
211 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
212 EXPORT_SYMBOL(in6addr_linklocal_allrouters);
213 const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
214 EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
215 const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
216 EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
217 const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
218 EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
220 static void snmp6_free_dev(struct inet6_dev *idev)
222 kfree(idev->stats.icmpv6msgdev);
223 kfree(idev->stats.icmpv6dev);
224 free_percpu(idev->stats.ipv6);
227 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
229 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
231 snmp6_free_dev(idev);
235 /* Nobody refers to this device, we may destroy it. */
237 void in6_dev_finish_destroy(struct inet6_dev *idev)
239 struct net_device *dev = idev->dev;
241 WARN_ON(!list_empty(&idev->addr_list));
242 WARN_ON(idev->mc_list);
243 WARN_ON(timer_pending(&idev->rs_timer));
245 #ifdef NET_REFCNT_DEBUG
246 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
250 pr_warn("Freeing alive inet6 device %p\n", idev);
253 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
255 EXPORT_SYMBOL(in6_dev_finish_destroy);