YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 library code, needed by static components when full IPv6 support is |
| 3 | * not configured or static. |
| 4 | */ |
| 5 | |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 6 | #include <linux/export.h> |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 7 | #include <net/ipv6.h> |
Eric Dumazet | 6da334e | 2013-06-25 01:30:11 -0700 | [diff] [blame] | 8 | #include <net/addrconf.h> |
Cong Wang | f39dc10 | 2013-08-31 13:44:35 +0800 | [diff] [blame] | 9 | #include <net/ip.h> |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 10 | |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 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 | */ |
| 15 | void (*__fib6_flush_trees)(struct net *); |
| 16 | EXPORT_SYMBOL(__fib6_flush_trees); |
| 17 | |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 18 | #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16) |
| 19 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 20 | static inline unsigned int ipv6_addr_scope2type(unsigned int scope) |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 21 | { |
Eldad Zack | d94d34a | 2012-04-01 07:49:02 +0000 | [diff] [blame] | 22 | switch (scope) { |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 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 | |
| 36 | int __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 */ |
Dave Johnson | c61a7d1 | 2007-07-30 17:19:31 -0700 | [diff] [blame] | 63 | if ((st & htonl(0xFE000000)) == htonl(0xFC000000)) |
| 64 | return (IPV6_ADDR_UNICAST | |
| 65 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */ |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 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 | |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 85 | return (IPV6_ADDR_UNICAST | |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 86 | IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */ |
| 87 | } |
David S. Miller | 7401055 | 2007-02-21 23:26:56 -0800 | [diff] [blame] | 88 | EXPORT_SYMBOL(__ipv6_addr_type); |
YOSHIFUJI Hideaki | 8c14b7c | 2007-02-22 02:25:42 +0900 | [diff] [blame] | 89 | |
Cong Wang | f88c91d | 2013-04-14 23:18:43 +0800 | [diff] [blame] | 90 | static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); |
| 91 | |
| 92 | int register_inet6addr_notifier(struct notifier_block *nb) |
| 93 | { |
| 94 | return atomic_notifier_chain_register(&inet6addr_chain, nb); |
| 95 | } |
| 96 | EXPORT_SYMBOL(register_inet6addr_notifier); |
| 97 | |
| 98 | int unregister_inet6addr_notifier(struct notifier_block *nb) |
| 99 | { |
| 100 | return atomic_notifier_chain_unregister(&inet6addr_chain, nb); |
| 101 | } |
| 102 | EXPORT_SYMBOL(unregister_inet6addr_notifier); |
| 103 | |
| 104 | int inet6addr_notifier_call_chain(unsigned long val, void *v) |
| 105 | { |
| 106 | return atomic_notifier_call_chain(&inet6addr_chain, val, v); |
| 107 | } |
| 108 | EXPORT_SYMBOL(inet6addr_notifier_call_chain); |
Cong Wang | 5f81bd2 | 2013-08-31 13:44:30 +0800 | [diff] [blame] | 109 | |
| 110 | const struct ipv6_stub *ipv6_stub __read_mostly; |
| 111 | EXPORT_SYMBOL_GPL(ipv6_stub); |
Cong Wang | 034dfc5 | 2013-08-31 13:44:31 +0800 | [diff] [blame] | 112 | |
| 113 | /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ |
| 114 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; |
| 115 | EXPORT_SYMBOL(in6addr_loopback); |
| 116 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
| 117 | EXPORT_SYMBOL(in6addr_any); |
| 118 | const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; |
| 119 | EXPORT_SYMBOL(in6addr_linklocal_allnodes); |
| 120 | const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; |
| 121 | EXPORT_SYMBOL(in6addr_linklocal_allrouters); |
| 122 | const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT; |
| 123 | EXPORT_SYMBOL(in6addr_interfacelocal_allnodes); |
| 124 | const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT; |
| 125 | EXPORT_SYMBOL(in6addr_interfacelocal_allrouters); |
| 126 | const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT; |
| 127 | EXPORT_SYMBOL(in6addr_sitelocal_allrouters); |
Cong Wang | f39dc10 | 2013-08-31 13:44:35 +0800 | [diff] [blame] | 128 | |
| 129 | static void snmp6_free_dev(struct inet6_dev *idev) |
| 130 | { |
| 131 | kfree(idev->stats.icmpv6msgdev); |
| 132 | kfree(idev->stats.icmpv6dev); |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 133 | free_percpu(idev->stats.ipv6); |
Cong Wang | f39dc10 | 2013-08-31 13:44:35 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Nobody refers to this device, we may destroy it. */ |
| 137 | |
| 138 | void in6_dev_finish_destroy(struct inet6_dev *idev) |
| 139 | { |
| 140 | struct net_device *dev = idev->dev; |
| 141 | |
| 142 | WARN_ON(!list_empty(&idev->addr_list)); |
| 143 | WARN_ON(idev->mc_list != NULL); |
| 144 | WARN_ON(timer_pending(&idev->rs_timer)); |
| 145 | |
| 146 | #ifdef NET_REFCNT_DEBUG |
| 147 | pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL"); |
| 148 | #endif |
| 149 | dev_put(dev); |
| 150 | if (!idev->dead) { |
| 151 | pr_warn("Freeing alive inet6 device %p\n", idev); |
| 152 | return; |
| 153 | } |
| 154 | snmp6_free_dev(idev); |
| 155 | kfree_rcu(idev, rcu); |
| 156 | } |
| 157 | EXPORT_SYMBOL(in6_dev_finish_destroy); |