net: phy: improve link partner capability detection
[linux-block.git] / net / ipv6 / addrconf_core.c
CommitLineData
8c14b7ce
YH
1/*
2 * IPv6 library code, needed by static components when full IPv6 support is
3 * not configured or static.
4 */
5
bc3b2d7f 6#include <linux/export.h>
8c14b7ce 7#include <net/ipv6.h>
3616d08b 8#include <net/ipv6_stubs.h>
f39dc102 9#include <net/ip.h>
8c14b7ce 10
705f1c86
HFS
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
8c14b7ce
YH
18#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
19
95c96174 20static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
8c14b7ce 21{
d94d34a0 22 switch (scope) {
8c14b7ce
YH
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 */
c61a7d10
DJ
63 if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
64 return (IPV6_ADDR_UNICAST |
65 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
8c14b7ce
YH
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
45bb0060 85 return (IPV6_ADDR_UNICAST |
8c14b7ce
YH
86 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
87}
7401055b 88EXPORT_SYMBOL(__ipv6_addr_type);
8c14b7ce 89
f88c91dd 90static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
ff7883ea 91static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain);
f88c91dd
CW
92
93int register_inet6addr_notifier(struct notifier_block *nb)
94{
95 return atomic_notifier_chain_register(&inet6addr_chain, nb);
96}
97EXPORT_SYMBOL(register_inet6addr_notifier);
98
99int unregister_inet6addr_notifier(struct notifier_block *nb)
100{
101 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
102}
103EXPORT_SYMBOL(unregister_inet6addr_notifier);
104
105int inet6addr_notifier_call_chain(unsigned long val, void *v)
106{
107 return atomic_notifier_call_chain(&inet6addr_chain, val, v);
108}
109EXPORT_SYMBOL(inet6addr_notifier_call_chain);
5f81bd2e 110
3ad7d246
KJ
111int register_inet6addr_validator_notifier(struct notifier_block *nb)
112{
ff7883ea 113 return blocking_notifier_chain_register(&inet6addr_validator_chain, nb);
3ad7d246
KJ
114}
115EXPORT_SYMBOL(register_inet6addr_validator_notifier);
116
117int unregister_inet6addr_validator_notifier(struct notifier_block *nb)
118{
ff7883ea
DA
119 return blocking_notifier_chain_unregister(&inet6addr_validator_chain,
120 nb);
3ad7d246
KJ
121}
122EXPORT_SYMBOL(unregister_inet6addr_validator_notifier);
123
124int inet6addr_validator_notifier_call_chain(unsigned long val, void *v)
125{
ff7883ea 126 return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v);
3ad7d246
KJ
127}
128EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain);
129
343d60aa
RP
130static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
131 struct dst_entry **u2,
132 struct flowi6 *u3)
133{
134 return -EAFNOSUPPORT;
135}
136
9b0a6a9d
PO
137static int eafnosupport_ipv6_route_input(struct sk_buff *skb)
138{
139 return -EAFNOSUPPORT;
140}
141
65a2022e
DA
142static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
143{
144 return NULL;
145}
146
147static struct fib6_info *
148eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
149 int oif, struct flowi6 *fl6, int flags)
150{
151 return NULL;
152}
153
154static struct fib6_info *
155eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
156 int flags)
157{
158 return NULL;
159}
160
161static struct fib6_info *
162eafnosupport_fib6_multipath_select(const struct net *net, struct fib6_info *f6i,
163 struct flowi6 *fl6, int oif,
164 const struct sk_buff *skb, int strict)
165{
166 return f6i;
167}
168
901731b8
DA
169static u32
170eafnosupport_ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
171 struct in6_addr *saddr)
172{
173 return 0;
174}
175
343d60aa 176const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
65a2022e 177 .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
9b0a6a9d 178 .ipv6_route_input = eafnosupport_ipv6_route_input,
65a2022e
DA
179 .fib6_get_table = eafnosupport_fib6_get_table,
180 .fib6_table_lookup = eafnosupport_fib6_table_lookup,
181 .fib6_lookup = eafnosupport_fib6_lookup,
182 .fib6_multipath_select = eafnosupport_fib6_multipath_select,
901731b8 183 .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
343d60aa 184};
5f81bd2e 185EXPORT_SYMBOL_GPL(ipv6_stub);
034dfc5d
CW
186
187/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
188const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
189EXPORT_SYMBOL(in6addr_loopback);
190const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
191EXPORT_SYMBOL(in6addr_any);
192const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
193EXPORT_SYMBOL(in6addr_linklocal_allnodes);
194const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
195EXPORT_SYMBOL(in6addr_linklocal_allrouters);
196const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
197EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
198const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
199EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
200const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
201EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
f39dc102
CW
202
203static void snmp6_free_dev(struct inet6_dev *idev)
204{
205 kfree(idev->stats.icmpv6msgdev);
206 kfree(idev->stats.icmpv6dev);
698365fa 207 free_percpu(idev->stats.ipv6);
f39dc102
CW
208}
209
27e41fcf
RS
210static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
211{
212 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
213
214 snmp6_free_dev(idev);
215 kfree(idev);
216}
217
f39dc102
CW
218/* Nobody refers to this device, we may destroy it. */
219
220void in6_dev_finish_destroy(struct inet6_dev *idev)
221{
222 struct net_device *dev = idev->dev;
223
224 WARN_ON(!list_empty(&idev->addr_list));
53b24b8f 225 WARN_ON(idev->mc_list);
f39dc102
CW
226 WARN_ON(timer_pending(&idev->rs_timer));
227
228#ifdef NET_REFCNT_DEBUG
229 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
230#endif
231 dev_put(dev);
232 if (!idev->dead) {
233 pr_warn("Freeing alive inet6 device %p\n", idev);
234 return;
235 }
27e41fcf 236 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
f39dc102
CW
237}
238EXPORT_SYMBOL(in6_dev_finish_destroy);