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/addrconf.h> 10 #include <net/ip.h> 11 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 */ 16 void (*__fib6_flush_trees)(struct net *); 17 EXPORT_SYMBOL(__fib6_flush_trees); 18 19 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16) 20 21 static inline unsigned int ipv6_addr_scope2type(unsigned int scope) 22 { 23 switch (scope) { 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 37 int __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 */ 64 if ((st & htonl(0xFE000000)) == htonl(0xFC000000)) 65 return (IPV6_ADDR_UNICAST | 66 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */ 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 86 return (IPV6_ADDR_UNICAST | 87 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */ 88 } 89 EXPORT_SYMBOL(__ipv6_addr_type); 90 91 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 92 static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain); 93 94 int register_inet6addr_notifier(struct notifier_block *nb) 95 { 96 return atomic_notifier_chain_register(&inet6addr_chain, nb); 97 } 98 EXPORT_SYMBOL(register_inet6addr_notifier); 99 100 int unregister_inet6addr_notifier(struct notifier_block *nb) 101 { 102 return atomic_notifier_chain_unregister(&inet6addr_chain, nb); 103 } 104 EXPORT_SYMBOL(unregister_inet6addr_notifier); 105 106 int inet6addr_notifier_call_chain(unsigned long val, void *v) 107 { 108 return atomic_notifier_call_chain(&inet6addr_chain, val, v); 109 } 110 EXPORT_SYMBOL(inet6addr_notifier_call_chain); 111 112 int register_inet6addr_validator_notifier(struct notifier_block *nb) 113 { 114 return blocking_notifier_chain_register(&inet6addr_validator_chain, nb); 115 } 116 EXPORT_SYMBOL(register_inet6addr_validator_notifier); 117 118 int unregister_inet6addr_validator_notifier(struct notifier_block *nb) 119 { 120 return blocking_notifier_chain_unregister(&inet6addr_validator_chain, 121 nb); 122 } 123 EXPORT_SYMBOL(unregister_inet6addr_validator_notifier); 124 125 int inet6addr_validator_notifier_call_chain(unsigned long val, void *v) 126 { 127 return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v); 128 } 129 EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain); 130 131 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 132 const struct in6_addr in6addr_loopback __aligned(BITS_PER_LONG/8) 133 = IN6ADDR_LOOPBACK_INIT; 134 EXPORT_SYMBOL(in6addr_loopback); 135 const struct in6_addr in6addr_any __aligned(BITS_PER_LONG/8) 136 = IN6ADDR_ANY_INIT; 137 EXPORT_SYMBOL(in6addr_any); 138 const struct in6_addr in6addr_linklocal_allnodes __aligned(BITS_PER_LONG/8) 139 = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 140 EXPORT_SYMBOL(in6addr_linklocal_allnodes); 141 const struct in6_addr in6addr_linklocal_allrouters __aligned(BITS_PER_LONG/8) 142 = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 143 EXPORT_SYMBOL(in6addr_linklocal_allrouters); 144 const struct in6_addr in6addr_interfacelocal_allnodes __aligned(BITS_PER_LONG/8) 145 = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT; 146 EXPORT_SYMBOL(in6addr_interfacelocal_allnodes); 147 const struct in6_addr in6addr_interfacelocal_allrouters __aligned(BITS_PER_LONG/8) 148 = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT; 149 EXPORT_SYMBOL(in6addr_interfacelocal_allrouters); 150 const struct in6_addr in6addr_sitelocal_allrouters __aligned(BITS_PER_LONG/8) 151 = IN6ADDR_SITELOCAL_ALLROUTERS_INIT; 152 EXPORT_SYMBOL(in6addr_sitelocal_allrouters); 153 154 static void snmp6_free_dev(struct inet6_dev *idev) 155 { 156 kfree(idev->stats.icmpv6msgdev); 157 kfree(idev->stats.icmpv6dev); 158 free_percpu(idev->stats.ipv6); 159 } 160 161 static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 162 { 163 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 164 165 snmp6_free_dev(idev); 166 kfree(idev); 167 } 168 169 /* Nobody refers to this device, we may destroy it. */ 170 171 void in6_dev_finish_destroy(struct inet6_dev *idev) 172 { 173 struct net_device *dev = idev->dev; 174 175 WARN_ON(!list_empty(&idev->addr_list)); 176 WARN_ON(rcu_access_pointer(idev->mc_list)); 177 WARN_ON(timer_pending(&idev->rs_timer)); 178 179 #ifdef NET_REFCNT_DEBUG 180 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL"); 181 #endif 182 netdev_put(dev, &idev->dev_tracker); 183 if (!idev->dead) { 184 pr_warn("Freeing alive inet6 device %p\n", idev); 185 return; 186 } 187 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 188 } 189 EXPORT_SYMBOL(in6_dev_finish_destroy); 190