]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Linux INET6 implementation | |
4 | * FIB front-end. | |
5 | * | |
6 | * Authors: | |
1ab1457c | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
8 | */ |
9 | ||
10 | /* Changes: | |
11 | * | |
12 | * YOSHIFUJI Hideaki @USAGI | |
13 | * reworked default router selection. | |
14 | * - respect outgoing interface | |
15 | * - select from (probably) reachable routers (i.e. | |
16 | * routers in REACHABLE, STALE, DELAY or PROBE states). | |
17 | * - always select the same router if it is (probably) | |
18 | * reachable. otherwise, round-robin the list. | |
c0bece9f YH |
19 | * Ville Nuorvala |
20 | * Fixed routing subtrees. | |
1da177e4 LT |
21 | */ |
22 | ||
f3213831 JP |
23 | #define pr_fmt(fmt) "IPv6: " fmt |
24 | ||
4fc268d2 | 25 | #include <linux/capability.h> |
1da177e4 | 26 | #include <linux/errno.h> |
bc3b2d7f | 27 | #include <linux/export.h> |
1da177e4 LT |
28 | #include <linux/types.h> |
29 | #include <linux/times.h> | |
30 | #include <linux/socket.h> | |
31 | #include <linux/sockios.h> | |
32 | #include <linux/net.h> | |
33 | #include <linux/route.h> | |
34 | #include <linux/netdevice.h> | |
35 | #include <linux/in6.h> | |
7bc570c8 | 36 | #include <linux/mroute6.h> |
1da177e4 | 37 | #include <linux/init.h> |
1da177e4 | 38 | #include <linux/if_arp.h> |
1da177e4 LT |
39 | #include <linux/proc_fs.h> |
40 | #include <linux/seq_file.h> | |
5b7c931d | 41 | #include <linux/nsproxy.h> |
5a0e3ad6 | 42 | #include <linux/slab.h> |
35732d01 | 43 | #include <linux/jhash.h> |
457c4cbc | 44 | #include <net/net_namespace.h> |
1da177e4 LT |
45 | #include <net/snmp.h> |
46 | #include <net/ipv6.h> | |
47 | #include <net/ip6_fib.h> | |
48 | #include <net/ip6_route.h> | |
49 | #include <net/ndisc.h> | |
50 | #include <net/addrconf.h> | |
51 | #include <net/tcp.h> | |
52 | #include <linux/rtnetlink.h> | |
53 | #include <net/dst.h> | |
904af04d | 54 | #include <net/dst_metadata.h> |
1da177e4 | 55 | #include <net/xfrm.h> |
8d71740c | 56 | #include <net/netevent.h> |
21713ebc | 57 | #include <net/netlink.h> |
3c618c1d | 58 | #include <net/rtnh.h> |
19e42e45 | 59 | #include <net/lwtunnel.h> |
904af04d | 60 | #include <net/ip_tunnels.h> |
ca254490 | 61 | #include <net/l3mdev.h> |
eacb9384 | 62 | #include <net/ip.h> |
7c0f6ba6 | 63 | #include <linux/uaccess.h> |
1da177e4 LT |
64 | |
65 | #ifdef CONFIG_SYSCTL | |
66 | #include <linux/sysctl.h> | |
67 | #endif | |
68 | ||
30d444d3 DA |
69 | static int ip6_rt_type_to_error(u8 fib6_type); |
70 | ||
71 | #define CREATE_TRACE_POINTS | |
72 | #include <trace/events/fib6.h> | |
73 | EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); | |
74 | #undef CREATE_TRACE_POINTS | |
75 | ||
afc154e9 | 76 | enum rt6_nud_state { |
7e980569 JB |
77 | RT6_NUD_FAIL_HARD = -3, |
78 | RT6_NUD_FAIL_PROBE = -2, | |
79 | RT6_NUD_FAIL_DO_RR = -1, | |
afc154e9 HFS |
80 | RT6_NUD_SUCCEED = 1 |
81 | }; | |
82 | ||
1da177e4 | 83 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); |
0dbaee3b | 84 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
ebb762f2 | 85 | static unsigned int ip6_mtu(const struct dst_entry *dst); |
1da177e4 LT |
86 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); |
87 | static void ip6_dst_destroy(struct dst_entry *); | |
88 | static void ip6_dst_ifdown(struct dst_entry *, | |
89 | struct net_device *dev, int how); | |
569d3645 | 90 | static int ip6_dst_gc(struct dst_ops *ops); |
1da177e4 LT |
91 | |
92 | static int ip6_pkt_discard(struct sk_buff *skb); | |
ede2059d | 93 | static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
7150aede | 94 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
ede2059d | 95 | static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
1da177e4 | 96 | static void ip6_link_failure(struct sk_buff *skb); |
6700c270 | 97 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
bd085ef6 HL |
98 | struct sk_buff *skb, u32 mtu, |
99 | bool confirm_neigh); | |
6700c270 DM |
100 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, |
101 | struct sk_buff *skb); | |
702cea56 DA |
102 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
103 | int strict); | |
a1b7a1f0 | 104 | static size_t rt6_nlmsg_size(struct fib6_info *f6i); |
d4ead6b3 | 105 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
8d1c802b | 106 | struct fib6_info *rt, struct dst_entry *dst, |
d4ead6b3 | 107 | struct in6_addr *dest, struct in6_addr *src, |
16a16cd3 DA |
108 | int iif, int type, u32 portid, u32 seq, |
109 | unsigned int flags); | |
7e4b5128 | 110 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
510e2ced WW |
111 | const struct in6_addr *daddr, |
112 | const struct in6_addr *saddr); | |
1da177e4 | 113 | |
70ceb4f5 | 114 | #ifdef CONFIG_IPV6_ROUTE_INFO |
8d1c802b | 115 | static struct fib6_info *rt6_add_route_info(struct net *net, |
b71d1d42 | 116 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
117 | const struct in6_addr *gwaddr, |
118 | struct net_device *dev, | |
95c96174 | 119 | unsigned int pref); |
8d1c802b | 120 | static struct fib6_info *rt6_get_route_info(struct net *net, |
b71d1d42 | 121 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
122 | const struct in6_addr *gwaddr, |
123 | struct net_device *dev); | |
70ceb4f5 YH |
124 | #endif |
125 | ||
8d0b94af MKL |
126 | struct uncached_list { |
127 | spinlock_t lock; | |
128 | struct list_head head; | |
129 | }; | |
130 | ||
131 | static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); | |
132 | ||
510c321b | 133 | void rt6_uncached_list_add(struct rt6_info *rt) |
8d0b94af MKL |
134 | { |
135 | struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); | |
136 | ||
8d0b94af MKL |
137 | rt->rt6i_uncached_list = ul; |
138 | ||
139 | spin_lock_bh(&ul->lock); | |
140 | list_add_tail(&rt->rt6i_uncached, &ul->head); | |
141 | spin_unlock_bh(&ul->lock); | |
142 | } | |
143 | ||
510c321b | 144 | void rt6_uncached_list_del(struct rt6_info *rt) |
8d0b94af MKL |
145 | { |
146 | if (!list_empty(&rt->rt6i_uncached)) { | |
147 | struct uncached_list *ul = rt->rt6i_uncached_list; | |
81eb8447 | 148 | struct net *net = dev_net(rt->dst.dev); |
8d0b94af MKL |
149 | |
150 | spin_lock_bh(&ul->lock); | |
151 | list_del(&rt->rt6i_uncached); | |
81eb8447 | 152 | atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); |
8d0b94af MKL |
153 | spin_unlock_bh(&ul->lock); |
154 | } | |
155 | } | |
156 | ||
157 | static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) | |
158 | { | |
159 | struct net_device *loopback_dev = net->loopback_dev; | |
160 | int cpu; | |
161 | ||
e332bc67 EB |
162 | if (dev == loopback_dev) |
163 | return; | |
164 | ||
8d0b94af MKL |
165 | for_each_possible_cpu(cpu) { |
166 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); | |
167 | struct rt6_info *rt; | |
168 | ||
169 | spin_lock_bh(&ul->lock); | |
170 | list_for_each_entry(rt, &ul->head, rt6i_uncached) { | |
171 | struct inet6_dev *rt_idev = rt->rt6i_idev; | |
172 | struct net_device *rt_dev = rt->dst.dev; | |
173 | ||
e332bc67 | 174 | if (rt_idev->dev == dev) { |
8d0b94af MKL |
175 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
176 | in6_dev_put(rt_idev); | |
177 | } | |
178 | ||
e332bc67 | 179 | if (rt_dev == dev) { |
8d7017fd | 180 | rt->dst.dev = blackhole_netdev; |
8d0b94af MKL |
181 | dev_hold(rt->dst.dev); |
182 | dev_put(rt_dev); | |
183 | } | |
184 | } | |
185 | spin_unlock_bh(&ul->lock); | |
186 | } | |
187 | } | |
188 | ||
f8a1b43b | 189 | static inline const void *choose_neigh_daddr(const struct in6_addr *p, |
f894cbf8 DM |
190 | struct sk_buff *skb, |
191 | const void *daddr) | |
39232973 | 192 | { |
a7563f34 | 193 | if (!ipv6_addr_any(p)) |
39232973 | 194 | return (const void *) p; |
f894cbf8 DM |
195 | else if (skb) |
196 | return &ipv6_hdr(skb)->daddr; | |
39232973 DM |
197 | return daddr; |
198 | } | |
199 | ||
f8a1b43b DA |
200 | struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, |
201 | struct net_device *dev, | |
202 | struct sk_buff *skb, | |
203 | const void *daddr) | |
d3aaeb38 | 204 | { |
39232973 DM |
205 | struct neighbour *n; |
206 | ||
f8a1b43b DA |
207 | daddr = choose_neigh_daddr(gw, skb, daddr); |
208 | n = __ipv6_neigh_lookup(dev, daddr); | |
f83c7790 DM |
209 | if (n) |
210 | return n; | |
7adf3246 SB |
211 | |
212 | n = neigh_create(&nd_tbl, daddr, dev); | |
213 | return IS_ERR(n) ? NULL : n; | |
f8a1b43b DA |
214 | } |
215 | ||
216 | static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, | |
217 | struct sk_buff *skb, | |
218 | const void *daddr) | |
219 | { | |
220 | const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); | |
221 | ||
2c6b55f4 ND |
222 | return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), |
223 | dst->dev, skb, daddr); | |
f83c7790 DM |
224 | } |
225 | ||
63fca65d JA |
226 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
227 | { | |
228 | struct net_device *dev = dst->dev; | |
229 | struct rt6_info *rt = (struct rt6_info *)dst; | |
230 | ||
cbfd6891 | 231 | daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr); |
63fca65d JA |
232 | if (!daddr) |
233 | return; | |
234 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) | |
235 | return; | |
236 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) | |
237 | return; | |
238 | __ipv6_confirm_neigh(dev, daddr); | |
239 | } | |
240 | ||
9a7ec3a9 | 241 | static struct dst_ops ip6_dst_ops_template = { |
1da177e4 | 242 | .family = AF_INET6, |
1da177e4 LT |
243 | .gc = ip6_dst_gc, |
244 | .gc_thresh = 1024, | |
245 | .check = ip6_dst_check, | |
0dbaee3b | 246 | .default_advmss = ip6_default_advmss, |
ebb762f2 | 247 | .mtu = ip6_mtu, |
d4ead6b3 | 248 | .cow_metrics = dst_cow_metrics_generic, |
1da177e4 LT |
249 | .destroy = ip6_dst_destroy, |
250 | .ifdown = ip6_dst_ifdown, | |
251 | .negative_advice = ip6_negative_advice, | |
252 | .link_failure = ip6_link_failure, | |
253 | .update_pmtu = ip6_rt_update_pmtu, | |
6e157b6a | 254 | .redirect = rt6_do_redirect, |
9f8955cc | 255 | .local_out = __ip6_local_out, |
f8a1b43b | 256 | .neigh_lookup = ip6_dst_neigh_lookup, |
63fca65d | 257 | .confirm_neigh = ip6_confirm_neigh, |
1da177e4 LT |
258 | }; |
259 | ||
ebb762f2 | 260 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
ec831ea7 | 261 | { |
618f9bc7 SK |
262 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
263 | ||
264 | return mtu ? : dst->dev->mtu; | |
ec831ea7 RD |
265 | } |
266 | ||
6700c270 | 267 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
bd085ef6 HL |
268 | struct sk_buff *skb, u32 mtu, |
269 | bool confirm_neigh) | |
14e50e57 DM |
270 | { |
271 | } | |
272 | ||
6700c270 DM |
273 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
274 | struct sk_buff *skb) | |
b587ee3b DM |
275 | { |
276 | } | |
277 | ||
14e50e57 DM |
278 | static struct dst_ops ip6_dst_blackhole_ops = { |
279 | .family = AF_INET6, | |
14e50e57 DM |
280 | .destroy = ip6_dst_destroy, |
281 | .check = ip6_dst_check, | |
ebb762f2 | 282 | .mtu = ip6_blackhole_mtu, |
214f45c9 | 283 | .default_advmss = ip6_default_advmss, |
14e50e57 | 284 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
b587ee3b | 285 | .redirect = ip6_rt_blackhole_redirect, |
0a1f5962 | 286 | .cow_metrics = dst_cow_metrics_generic, |
f8a1b43b | 287 | .neigh_lookup = ip6_dst_neigh_lookup, |
14e50e57 DM |
288 | }; |
289 | ||
62fa8a84 | 290 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
14edd87d | 291 | [RTAX_HOPLIMIT - 1] = 0, |
62fa8a84 DM |
292 | }; |
293 | ||
8d1c802b | 294 | static const struct fib6_info fib6_null_entry_template = { |
93c2fb25 DA |
295 | .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), |
296 | .fib6_protocol = RTPROT_KERNEL, | |
297 | .fib6_metric = ~(u32)0, | |
f05713e0 | 298 | .fib6_ref = REFCOUNT_INIT(1), |
421842ed DA |
299 | .fib6_type = RTN_UNREACHABLE, |
300 | .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, | |
301 | }; | |
302 | ||
fb0af4c7 | 303 | static const struct rt6_info ip6_null_entry_template = { |
d8d1f30b CG |
304 | .dst = { |
305 | .__refcnt = ATOMIC_INIT(1), | |
306 | .__use = 1, | |
2c20cbd7 | 307 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 308 | .error = -ENETUNREACH, |
d8d1f30b CG |
309 | .input = ip6_pkt_discard, |
310 | .output = ip6_pkt_discard_out, | |
1da177e4 LT |
311 | }, |
312 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
1da177e4 LT |
313 | }; |
314 | ||
101367c2 TG |
315 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
316 | ||
fb0af4c7 | 317 | static const struct rt6_info ip6_prohibit_entry_template = { |
d8d1f30b CG |
318 | .dst = { |
319 | .__refcnt = ATOMIC_INIT(1), | |
320 | .__use = 1, | |
2c20cbd7 | 321 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 322 | .error = -EACCES, |
d8d1f30b CG |
323 | .input = ip6_pkt_prohibit, |
324 | .output = ip6_pkt_prohibit_out, | |
101367c2 TG |
325 | }, |
326 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
101367c2 TG |
327 | }; |
328 | ||
fb0af4c7 | 329 | static const struct rt6_info ip6_blk_hole_entry_template = { |
d8d1f30b CG |
330 | .dst = { |
331 | .__refcnt = ATOMIC_INIT(1), | |
332 | .__use = 1, | |
2c20cbd7 | 333 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 334 | .error = -EINVAL, |
d8d1f30b | 335 | .input = dst_discard, |
ede2059d | 336 | .output = dst_discard_out, |
101367c2 TG |
337 | }, |
338 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
101367c2 TG |
339 | }; |
340 | ||
341 | #endif | |
342 | ||
ebfa45f0 MKL |
343 | static void rt6_info_init(struct rt6_info *rt) |
344 | { | |
345 | struct dst_entry *dst = &rt->dst; | |
346 | ||
347 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); | |
ebfa45f0 MKL |
348 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
349 | } | |
350 | ||
1da177e4 | 351 | /* allocate dst with ip6_dst_ops */ |
93531c67 DA |
352 | struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, |
353 | int flags) | |
1da177e4 | 354 | { |
97bab73f | 355 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
b2a9c0ed | 356 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
cf911662 | 357 | |
81eb8447 | 358 | if (rt) { |
ebfa45f0 | 359 | rt6_info_init(rt); |
81eb8447 WW |
360 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
361 | } | |
8104891b | 362 | |
cf911662 | 363 | return rt; |
1da177e4 | 364 | } |
9ab179d8 | 365 | EXPORT_SYMBOL(ip6_dst_alloc); |
d52d3997 | 366 | |
1da177e4 LT |
367 | static void ip6_dst_destroy(struct dst_entry *dst) |
368 | { | |
369 | struct rt6_info *rt = (struct rt6_info *)dst; | |
a68886a6 | 370 | struct fib6_info *from; |
8d0b94af | 371 | struct inet6_dev *idev; |
1da177e4 | 372 | |
1620a336 | 373 | ip_dst_metrics_put(dst); |
8d0b94af MKL |
374 | rt6_uncached_list_del(rt); |
375 | ||
376 | idev = rt->rt6i_idev; | |
38308473 | 377 | if (idev) { |
1da177e4 LT |
378 | rt->rt6i_idev = NULL; |
379 | in6_dev_put(idev); | |
1ab1457c | 380 | } |
1716a961 | 381 | |
0e233874 | 382 | from = xchg((__force struct fib6_info **)&rt->from, NULL); |
93531c67 | 383 | fib6_info_release(from); |
b3419363 DM |
384 | } |
385 | ||
1da177e4 LT |
386 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
387 | int how) | |
388 | { | |
389 | struct rt6_info *rt = (struct rt6_info *)dst; | |
390 | struct inet6_dev *idev = rt->rt6i_idev; | |
5a3e55d6 | 391 | struct net_device *loopback_dev = |
c346dca1 | 392 | dev_net(dev)->loopback_dev; |
1da177e4 | 393 | |
e5645f51 WW |
394 | if (idev && idev->dev != loopback_dev) { |
395 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); | |
396 | if (loopback_idev) { | |
397 | rt->rt6i_idev = loopback_idev; | |
398 | in6_dev_put(idev); | |
97cac082 | 399 | } |
1da177e4 LT |
400 | } |
401 | } | |
402 | ||
5973fb1e MKL |
403 | static bool __rt6_check_expired(const struct rt6_info *rt) |
404 | { | |
405 | if (rt->rt6i_flags & RTF_EXPIRES) | |
406 | return time_after(jiffies, rt->dst.expires); | |
407 | else | |
408 | return false; | |
409 | } | |
410 | ||
a50feda5 | 411 | static bool rt6_check_expired(const struct rt6_info *rt) |
1da177e4 | 412 | { |
a68886a6 DA |
413 | struct fib6_info *from; |
414 | ||
415 | from = rcu_dereference(rt->from); | |
416 | ||
1716a961 G |
417 | if (rt->rt6i_flags & RTF_EXPIRES) { |
418 | if (time_after(jiffies, rt->dst.expires)) | |
a50feda5 | 419 | return true; |
a68886a6 | 420 | } else if (from) { |
1e2ea8ad | 421 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
a68886a6 | 422 | fib6_check_expired(from); |
1716a961 | 423 | } |
a50feda5 | 424 | return false; |
1da177e4 LT |
425 | } |
426 | ||
b1d40991 DA |
427 | void fib6_select_path(const struct net *net, struct fib6_result *res, |
428 | struct flowi6 *fl6, int oif, bool have_oif_match, | |
429 | const struct sk_buff *skb, int strict) | |
51ebd318 | 430 | { |
8d1c802b | 431 | struct fib6_info *sibling, *next_sibling; |
b1d40991 DA |
432 | struct fib6_info *match = res->f6i; |
433 | ||
f88d8ea6 | 434 | if ((!match->fib6_nsiblings && !match->nh) || have_oif_match) |
b1d40991 | 435 | goto out; |
51ebd318 | 436 | |
b673d6cc JS |
437 | /* We might have already computed the hash for ICMPv6 errors. In such |
438 | * case it will always be non-zero. Otherwise now is the time to do it. | |
439 | */ | |
f88d8ea6 DA |
440 | if (!fl6->mp_hash && |
441 | (!match->nh || nexthop_is_multipath(match->nh))) | |
b4bac172 | 442 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
b673d6cc | 443 | |
f88d8ea6 DA |
444 | if (unlikely(match->nh)) { |
445 | nexthop_path_fib6_result(res, fl6->mp_hash); | |
446 | return; | |
447 | } | |
448 | ||
1cf844c7 | 449 | if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) |
b1d40991 | 450 | goto out; |
3d709f69 | 451 | |
93c2fb25 DA |
452 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
453 | fib6_siblings) { | |
1cf844c7 | 454 | const struct fib6_nh *nh = sibling->fib6_nh; |
5e670d84 DA |
455 | int nh_upper_bound; |
456 | ||
702cea56 | 457 | nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); |
5e670d84 | 458 | if (fl6->mp_hash > nh_upper_bound) |
3d709f69 | 459 | continue; |
702cea56 | 460 | if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) |
3d709f69 IS |
461 | break; |
462 | match = sibling; | |
463 | break; | |
464 | } | |
465 | ||
b1d40991 DA |
466 | out: |
467 | res->f6i = match; | |
1cf844c7 | 468 | res->nh = match->fib6_nh; |
51ebd318 ND |
469 | } |
470 | ||
1da177e4 | 471 | /* |
66f5d6ce | 472 | * Route lookup. rcu_read_lock() should be held. |
1da177e4 LT |
473 | */ |
474 | ||
0c59d006 DA |
475 | static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, |
476 | const struct in6_addr *saddr, int oif, int flags) | |
477 | { | |
478 | const struct net_device *dev; | |
479 | ||
480 | if (nh->fib_nh_flags & RTNH_F_DEAD) | |
481 | return false; | |
482 | ||
483 | dev = nh->fib_nh_dev; | |
484 | if (oif) { | |
485 | if (dev->ifindex == oif) | |
486 | return true; | |
487 | } else { | |
488 | if (ipv6_chk_addr(net, saddr, dev, | |
489 | flags & RT6_LOOKUP_F_IFACE)) | |
490 | return true; | |
491 | } | |
492 | ||
493 | return false; | |
494 | } | |
495 | ||
962b6803 DA |
496 | struct fib6_nh_dm_arg { |
497 | struct net *net; | |
498 | const struct in6_addr *saddr; | |
499 | int oif; | |
500 | int flags; | |
501 | struct fib6_nh *nh; | |
502 | }; | |
503 | ||
504 | static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) | |
505 | { | |
506 | struct fib6_nh_dm_arg *arg = _arg; | |
507 | ||
508 | arg->nh = nh; | |
509 | return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, | |
510 | arg->flags); | |
511 | } | |
512 | ||
513 | /* returns fib6_nh from nexthop or NULL */ | |
514 | static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, | |
515 | struct fib6_result *res, | |
516 | const struct in6_addr *saddr, | |
517 | int oif, int flags) | |
518 | { | |
519 | struct fib6_nh_dm_arg arg = { | |
520 | .net = net, | |
521 | .saddr = saddr, | |
522 | .oif = oif, | |
523 | .flags = flags, | |
524 | }; | |
525 | ||
526 | if (nexthop_is_blackhole(nh)) | |
527 | return NULL; | |
528 | ||
529 | if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) | |
530 | return arg.nh; | |
531 | ||
532 | return NULL; | |
533 | } | |
534 | ||
75ef7389 DA |
535 | static void rt6_device_match(struct net *net, struct fib6_result *res, |
536 | const struct in6_addr *saddr, int oif, int flags) | |
1da177e4 | 537 | { |
75ef7389 DA |
538 | struct fib6_info *f6i = res->f6i; |
539 | struct fib6_info *spf6i; | |
540 | struct fib6_nh *nh; | |
1da177e4 | 541 | |
75ef7389 | 542 | if (!oif && ipv6_addr_any(saddr)) { |
f88d8ea6 DA |
543 | if (unlikely(f6i->nh)) { |
544 | nh = nexthop_fib6_nh(f6i->nh); | |
545 | if (nexthop_is_blackhole(f6i->nh)) | |
546 | goto out_blackhole; | |
547 | } else { | |
548 | nh = f6i->fib6_nh; | |
549 | } | |
7d21fec9 DA |
550 | if (!(nh->fib_nh_flags & RTNH_F_DEAD)) |
551 | goto out; | |
75ef7389 | 552 | } |
dd3abc4e | 553 | |
75ef7389 | 554 | for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { |
962b6803 DA |
555 | bool matched = false; |
556 | ||
557 | if (unlikely(spf6i->nh)) { | |
558 | nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, | |
559 | oif, flags); | |
560 | if (nh) | |
561 | matched = true; | |
562 | } else { | |
563 | nh = spf6i->fib6_nh; | |
564 | if (__rt6_device_match(net, nh, saddr, oif, flags)) | |
565 | matched = true; | |
566 | } | |
567 | if (matched) { | |
75ef7389 | 568 | res->f6i = spf6i; |
7d21fec9 | 569 | goto out; |
75ef7389 | 570 | } |
dd3abc4e | 571 | } |
1da177e4 | 572 | |
75ef7389 DA |
573 | if (oif && flags & RT6_LOOKUP_F_IFACE) { |
574 | res->f6i = net->ipv6.fib6_null_entry; | |
1cf844c7 | 575 | nh = res->f6i->fib6_nh; |
7d21fec9 | 576 | goto out; |
75ef7389 | 577 | } |
8067bb8c | 578 | |
f88d8ea6 DA |
579 | if (unlikely(f6i->nh)) { |
580 | nh = nexthop_fib6_nh(f6i->nh); | |
581 | if (nexthop_is_blackhole(f6i->nh)) | |
582 | goto out_blackhole; | |
583 | } else { | |
584 | nh = f6i->fib6_nh; | |
585 | } | |
586 | ||
7d21fec9 | 587 | if (nh->fib_nh_flags & RTNH_F_DEAD) { |
75ef7389 | 588 | res->f6i = net->ipv6.fib6_null_entry; |
1cf844c7 | 589 | nh = res->f6i->fib6_nh; |
75ef7389 | 590 | } |
7d21fec9 DA |
591 | out: |
592 | res->nh = nh; | |
593 | res->fib6_type = res->f6i->fib6_type; | |
594 | res->fib6_flags = res->f6i->fib6_flags; | |
f88d8ea6 DA |
595 | return; |
596 | ||
597 | out_blackhole: | |
598 | res->fib6_flags |= RTF_REJECT; | |
599 | res->fib6_type = RTN_BLACKHOLE; | |
600 | res->nh = nh; | |
1da177e4 LT |
601 | } |
602 | ||
27097255 | 603 | #ifdef CONFIG_IPV6_ROUTER_PREF |
c2f17e82 HFS |
604 | struct __rt6_probe_work { |
605 | struct work_struct work; | |
606 | struct in6_addr target; | |
607 | struct net_device *dev; | |
608 | }; | |
609 | ||
610 | static void rt6_probe_deferred(struct work_struct *w) | |
611 | { | |
612 | struct in6_addr mcaddr; | |
613 | struct __rt6_probe_work *work = | |
614 | container_of(w, struct __rt6_probe_work, work); | |
615 | ||
616 | addrconf_addr_solict_mult(&work->target, &mcaddr); | |
adc176c5 | 617 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
c2f17e82 | 618 | dev_put(work->dev); |
662f5533 | 619 | kfree(work); |
c2f17e82 HFS |
620 | } |
621 | ||
cc3a86c8 | 622 | static void rt6_probe(struct fib6_nh *fib6_nh) |
27097255 | 623 | { |
f547fac6 | 624 | struct __rt6_probe_work *work = NULL; |
5e670d84 | 625 | const struct in6_addr *nh_gw; |
1bef4c22 | 626 | unsigned long last_probe; |
f2c31e32 | 627 | struct neighbour *neigh; |
5e670d84 | 628 | struct net_device *dev; |
f547fac6 | 629 | struct inet6_dev *idev; |
5e670d84 | 630 | |
27097255 YH |
631 | /* |
632 | * Okay, this does not seem to be appropriate | |
633 | * for now, however, we need to check if it | |
634 | * is really so; aka Router Reachability Probing. | |
635 | * | |
636 | * Router Reachability Probe MUST be rate-limited | |
637 | * to no more than one per minute. | |
638 | */ | |
004b3942 | 639 | if (!fib6_nh->fib_nh_gw_family) |
7ff74a59 | 640 | return; |
5e670d84 | 641 | |
cc3a86c8 DA |
642 | nh_gw = &fib6_nh->fib_nh_gw6; |
643 | dev = fib6_nh->fib_nh_dev; | |
2152caea | 644 | rcu_read_lock_bh(); |
1bef4c22 | 645 | last_probe = READ_ONCE(fib6_nh->last_probe); |
f547fac6 | 646 | idev = __in6_dev_get(dev); |
5e670d84 | 647 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
2152caea | 648 | if (neigh) { |
8d6c31bf MKL |
649 | if (neigh->nud_state & NUD_VALID) |
650 | goto out; | |
651 | ||
2152caea | 652 | write_lock(&neigh->lock); |
990edb42 MKL |
653 | if (!(neigh->nud_state & NUD_VALID) && |
654 | time_after(jiffies, | |
dcd1f572 | 655 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
990edb42 MKL |
656 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
657 | if (work) | |
658 | __neigh_set_probe_once(neigh); | |
c2f17e82 | 659 | } |
2152caea | 660 | write_unlock(&neigh->lock); |
1bef4c22 | 661 | } else if (time_after(jiffies, last_probe + |
f547fac6 | 662 | idev->cnf.rtr_probe_interval)) { |
990edb42 | 663 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
f2c31e32 | 664 | } |
990edb42 | 665 | |
1bef4c22 ED |
666 | if (!work || cmpxchg(&fib6_nh->last_probe, |
667 | last_probe, jiffies) != last_probe) { | |
668 | kfree(work); | |
669 | } else { | |
990edb42 | 670 | INIT_WORK(&work->work, rt6_probe_deferred); |
5e670d84 DA |
671 | work->target = *nh_gw; |
672 | dev_hold(dev); | |
673 | work->dev = dev; | |
990edb42 MKL |
674 | schedule_work(&work->work); |
675 | } | |
676 | ||
8d6c31bf | 677 | out: |
2152caea | 678 | rcu_read_unlock_bh(); |
27097255 YH |
679 | } |
680 | #else | |
cc3a86c8 | 681 | static inline void rt6_probe(struct fib6_nh *fib6_nh) |
27097255 | 682 | { |
27097255 YH |
683 | } |
684 | #endif | |
685 | ||
1da177e4 | 686 | /* |
554cfb7e | 687 | * Default Router Selection (RFC 2461 6.3.6) |
1da177e4 | 688 | */ |
1ba9a895 | 689 | static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) |
1da177e4 | 690 | { |
afc154e9 | 691 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
5e670d84 | 692 | struct neighbour *neigh; |
f2c31e32 | 693 | |
145a3621 | 694 | rcu_read_lock_bh(); |
1ba9a895 DA |
695 | neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, |
696 | &fib6_nh->fib_nh_gw6); | |
145a3621 YH |
697 | if (neigh) { |
698 | read_lock(&neigh->lock); | |
554cfb7e | 699 | if (neigh->nud_state & NUD_VALID) |
afc154e9 | 700 | ret = RT6_NUD_SUCCEED; |
398bcbeb | 701 | #ifdef CONFIG_IPV6_ROUTER_PREF |
a5a81f0b | 702 | else if (!(neigh->nud_state & NUD_FAILED)) |
afc154e9 | 703 | ret = RT6_NUD_SUCCEED; |
7e980569 JB |
704 | else |
705 | ret = RT6_NUD_FAIL_PROBE; | |
398bcbeb | 706 | #endif |
145a3621 | 707 | read_unlock(&neigh->lock); |
afc154e9 HFS |
708 | } else { |
709 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? | |
7e980569 | 710 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
a5a81f0b | 711 | } |
145a3621 YH |
712 | rcu_read_unlock_bh(); |
713 | ||
a5a81f0b | 714 | return ret; |
1da177e4 LT |
715 | } |
716 | ||
702cea56 DA |
717 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
718 | int strict) | |
1da177e4 | 719 | { |
6e1809a5 DA |
720 | int m = 0; |
721 | ||
722 | if (!oif || nh->fib_nh_dev->ifindex == oif) | |
723 | m = 2; | |
1ab1457c | 724 | |
77d16f45 | 725 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
afc154e9 | 726 | return RT6_NUD_FAIL_HARD; |
ebacaaa0 | 727 | #ifdef CONFIG_IPV6_ROUTER_PREF |
702cea56 | 728 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; |
ebacaaa0 | 729 | #endif |
1ba9a895 | 730 | if ((strict & RT6_LOOKUP_F_REACHABLE) && |
702cea56 | 731 | !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { |
1ba9a895 | 732 | int n = rt6_check_neigh(nh); |
afc154e9 HFS |
733 | if (n < 0) |
734 | return n; | |
735 | } | |
554cfb7e YH |
736 | return m; |
737 | } | |
738 | ||
28679ed1 DA |
739 | static bool find_match(struct fib6_nh *nh, u32 fib6_flags, |
740 | int oif, int strict, int *mpri, bool *do_rr) | |
554cfb7e | 741 | { |
afc154e9 | 742 | bool match_do_rr = false; |
28679ed1 DA |
743 | bool rc = false; |
744 | int m; | |
35103d11 | 745 | |
28679ed1 | 746 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
8067bb8c IS |
747 | goto out; |
748 | ||
28679ed1 DA |
749 | if (ip6_ignore_linkdown(nh->fib_nh_dev) && |
750 | nh->fib_nh_flags & RTNH_F_LINKDOWN && | |
d5d32e4b | 751 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
35103d11 | 752 | goto out; |
f11e6659 | 753 | |
28679ed1 | 754 | m = rt6_score_route(nh, fib6_flags, oif, strict); |
7e980569 | 755 | if (m == RT6_NUD_FAIL_DO_RR) { |
afc154e9 HFS |
756 | match_do_rr = true; |
757 | m = 0; /* lowest valid score */ | |
7e980569 | 758 | } else if (m == RT6_NUD_FAIL_HARD) { |
f11e6659 | 759 | goto out; |
afc154e9 HFS |
760 | } |
761 | ||
762 | if (strict & RT6_LOOKUP_F_REACHABLE) | |
28679ed1 | 763 | rt6_probe(nh); |
f11e6659 | 764 | |
7e980569 | 765 | /* note that m can be RT6_NUD_FAIL_PROBE at this point */ |
f11e6659 | 766 | if (m > *mpri) { |
afc154e9 | 767 | *do_rr = match_do_rr; |
f11e6659 | 768 | *mpri = m; |
28679ed1 | 769 | rc = true; |
f11e6659 | 770 | } |
f11e6659 | 771 | out: |
28679ed1 | 772 | return rc; |
f11e6659 DM |
773 | } |
774 | ||
17a5984e DA |
775 | struct fib6_nh_frl_arg { |
776 | u32 flags; | |
777 | int oif; | |
778 | int strict; | |
779 | int *mpri; | |
780 | bool *do_rr; | |
781 | struct fib6_nh *nh; | |
782 | }; | |
783 | ||
784 | static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) | |
785 | { | |
786 | struct fib6_nh_frl_arg *arg = _arg; | |
787 | ||
788 | arg->nh = nh; | |
789 | return find_match(nh, arg->flags, arg->oif, arg->strict, | |
790 | arg->mpri, arg->do_rr); | |
791 | } | |
792 | ||
b7bc4b6a | 793 | static void __find_rr_leaf(struct fib6_info *f6i_start, |
30c15f03 | 794 | struct fib6_info *nomatch, u32 metric, |
b7bc4b6a | 795 | struct fib6_result *res, struct fib6_info **cont, |
30c15f03 | 796 | int oif, int strict, bool *do_rr, int *mpri) |
f11e6659 | 797 | { |
b7bc4b6a | 798 | struct fib6_info *f6i; |
1da177e4 | 799 | |
b7bc4b6a DA |
800 | for (f6i = f6i_start; |
801 | f6i && f6i != nomatch; | |
802 | f6i = rcu_dereference(f6i->fib6_next)) { | |
17a5984e | 803 | bool matched = false; |
30c15f03 DA |
804 | struct fib6_nh *nh; |
805 | ||
b7bc4b6a DA |
806 | if (cont && f6i->fib6_metric != metric) { |
807 | *cont = f6i; | |
30c15f03 | 808 | return; |
9fbdcfaf SK |
809 | } |
810 | ||
b7bc4b6a | 811 | if (fib6_check_expired(f6i)) |
28679ed1 DA |
812 | continue; |
813 | ||
17a5984e DA |
814 | if (unlikely(f6i->nh)) { |
815 | struct fib6_nh_frl_arg arg = { | |
816 | .flags = f6i->fib6_flags, | |
817 | .oif = oif, | |
818 | .strict = strict, | |
819 | .mpri = mpri, | |
820 | .do_rr = do_rr | |
821 | }; | |
822 | ||
823 | if (nexthop_is_blackhole(f6i->nh)) { | |
824 | res->fib6_flags = RTF_REJECT; | |
825 | res->fib6_type = RTN_BLACKHOLE; | |
826 | res->f6i = f6i; | |
827 | res->nh = nexthop_fib6_nh(f6i->nh); | |
828 | return; | |
829 | } | |
830 | if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, | |
831 | &arg)) { | |
832 | matched = true; | |
833 | nh = arg.nh; | |
834 | } | |
835 | } else { | |
836 | nh = f6i->fib6_nh; | |
837 | if (find_match(nh, f6i->fib6_flags, oif, strict, | |
838 | mpri, do_rr)) | |
839 | matched = true; | |
840 | } | |
841 | if (matched) { | |
b7bc4b6a DA |
842 | res->f6i = f6i; |
843 | res->nh = nh; | |
7d21fec9 DA |
844 | res->fib6_flags = f6i->fib6_flags; |
845 | res->fib6_type = f6i->fib6_type; | |
b7bc4b6a | 846 | } |
9fbdcfaf | 847 | } |
30c15f03 | 848 | } |
9fbdcfaf | 849 | |
b7bc4b6a DA |
850 | static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, |
851 | struct fib6_info *rr_head, int oif, int strict, | |
852 | bool *do_rr, struct fib6_result *res) | |
30c15f03 | 853 | { |
b7bc4b6a DA |
854 | u32 metric = rr_head->fib6_metric; |
855 | struct fib6_info *cont = NULL; | |
30c15f03 | 856 | int mpri = -1; |
9fbdcfaf | 857 | |
b7bc4b6a | 858 | __find_rr_leaf(rr_head, NULL, metric, res, &cont, |
30c15f03 | 859 | oif, strict, do_rr, &mpri); |
28679ed1 | 860 | |
b7bc4b6a | 861 | __find_rr_leaf(leaf, rr_head, metric, res, &cont, |
30c15f03 | 862 | oif, strict, do_rr, &mpri); |
9fbdcfaf | 863 | |
b7bc4b6a DA |
864 | if (res->f6i || !cont) |
865 | return; | |
9fbdcfaf | 866 | |
b7bc4b6a | 867 | __find_rr_leaf(cont, NULL, metric, res, NULL, |
30c15f03 | 868 | oif, strict, do_rr, &mpri); |
f11e6659 | 869 | } |
1da177e4 | 870 | |
b7bc4b6a DA |
871 | static void rt6_select(struct net *net, struct fib6_node *fn, int oif, |
872 | struct fib6_result *res, int strict) | |
f11e6659 | 873 | { |
8d1c802b | 874 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
b7bc4b6a | 875 | struct fib6_info *rt0; |
afc154e9 | 876 | bool do_rr = false; |
17ecf590 | 877 | int key_plen; |
1da177e4 | 878 | |
b7bc4b6a DA |
879 | /* make sure this function or its helpers sets f6i */ |
880 | res->f6i = NULL; | |
881 | ||
421842ed | 882 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
b7bc4b6a | 883 | goto out; |
8d1040e8 | 884 | |
66f5d6ce | 885 | rt0 = rcu_dereference(fn->rr_ptr); |
f11e6659 | 886 | if (!rt0) |
66f5d6ce | 887 | rt0 = leaf; |
1da177e4 | 888 | |
17ecf590 WW |
889 | /* Double check to make sure fn is not an intermediate node |
890 | * and fn->leaf does not points to its child's leaf | |
891 | * (This might happen if all routes under fn are deleted from | |
892 | * the tree and fib6_repair_tree() is called on the node.) | |
893 | */ | |
93c2fb25 | 894 | key_plen = rt0->fib6_dst.plen; |
17ecf590 | 895 | #ifdef CONFIG_IPV6_SUBTREES |
93c2fb25 DA |
896 | if (rt0->fib6_src.plen) |
897 | key_plen = rt0->fib6_src.plen; | |
17ecf590 WW |
898 | #endif |
899 | if (fn->fn_bit != key_plen) | |
b7bc4b6a | 900 | goto out; |
1da177e4 | 901 | |
b7bc4b6a | 902 | find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); |
afc154e9 | 903 | if (do_rr) { |
8fb11a9a | 904 | struct fib6_info *next = rcu_dereference(rt0->fib6_next); |
f11e6659 | 905 | |
554cfb7e | 906 | /* no entries matched; do round-robin */ |
93c2fb25 | 907 | if (!next || next->fib6_metric != rt0->fib6_metric) |
8d1040e8 | 908 | next = leaf; |
f11e6659 | 909 | |
66f5d6ce | 910 | if (next != rt0) { |
93c2fb25 | 911 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
66f5d6ce | 912 | /* make sure next is not being deleted from the tree */ |
93c2fb25 | 913 | if (next->fib6_node) |
66f5d6ce | 914 | rcu_assign_pointer(fn->rr_ptr, next); |
93c2fb25 | 915 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
66f5d6ce | 916 | } |
1da177e4 | 917 | } |
1da177e4 | 918 | |
b7bc4b6a DA |
919 | out: |
920 | if (!res->f6i) { | |
921 | res->f6i = net->ipv6.fib6_null_entry; | |
1cf844c7 | 922 | res->nh = res->f6i->fib6_nh; |
7d21fec9 DA |
923 | res->fib6_flags = res->f6i->fib6_flags; |
924 | res->fib6_type = res->f6i->fib6_type; | |
b7bc4b6a | 925 | } |
1da177e4 LT |
926 | } |
927 | ||
85bd05de | 928 | static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) |
8b9df265 | 929 | { |
85bd05de DA |
930 | return (res->f6i->fib6_flags & RTF_NONEXTHOP) || |
931 | res->nh->fib_nh_gw_family; | |
8b9df265 MKL |
932 | } |
933 | ||
70ceb4f5 YH |
934 | #ifdef CONFIG_IPV6_ROUTE_INFO |
935 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, | |
b71d1d42 | 936 | const struct in6_addr *gwaddr) |
70ceb4f5 | 937 | { |
c346dca1 | 938 | struct net *net = dev_net(dev); |
70ceb4f5 YH |
939 | struct route_info *rinfo = (struct route_info *) opt; |
940 | struct in6_addr prefix_buf, *prefix; | |
941 | unsigned int pref; | |
4bed72e4 | 942 | unsigned long lifetime; |
8d1c802b | 943 | struct fib6_info *rt; |
70ceb4f5 YH |
944 | |
945 | if (len < sizeof(struct route_info)) { | |
946 | return -EINVAL; | |
947 | } | |
948 | ||
949 | /* Sanity check for prefix_len and length */ | |
950 | if (rinfo->length > 3) { | |
951 | return -EINVAL; | |
952 | } else if (rinfo->prefix_len > 128) { | |
953 | return -EINVAL; | |
954 | } else if (rinfo->prefix_len > 64) { | |
955 | if (rinfo->length < 2) { | |
956 | return -EINVAL; | |
957 | } | |
958 | } else if (rinfo->prefix_len > 0) { | |
959 | if (rinfo->length < 1) { | |
960 | return -EINVAL; | |
961 | } | |
962 | } | |
963 | ||
964 | pref = rinfo->route_pref; | |
965 | if (pref == ICMPV6_ROUTER_PREF_INVALID) | |
3933fc95 | 966 | return -EINVAL; |
70ceb4f5 | 967 | |
4bed72e4 | 968 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
70ceb4f5 YH |
969 | |
970 | if (rinfo->length == 3) | |
971 | prefix = (struct in6_addr *)rinfo->prefix; | |
972 | else { | |
973 | /* this function is safe */ | |
974 | ipv6_addr_prefix(&prefix_buf, | |
975 | (struct in6_addr *)rinfo->prefix, | |
976 | rinfo->prefix_len); | |
977 | prefix = &prefix_buf; | |
978 | } | |
979 | ||
f104a567 | 980 | if (rinfo->prefix_len == 0) |
afb1d4b5 | 981 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
f104a567 DJ |
982 | else |
983 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, | |
830218c1 | 984 | gwaddr, dev); |
70ceb4f5 YH |
985 | |
986 | if (rt && !lifetime) { | |
afb1d4b5 | 987 | ip6_del_rt(net, rt); |
70ceb4f5 YH |
988 | rt = NULL; |
989 | } | |
990 | ||
991 | if (!rt && lifetime) | |
830218c1 DA |
992 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
993 | dev, pref); | |
70ceb4f5 | 994 | else if (rt) |
93c2fb25 DA |
995 | rt->fib6_flags = RTF_ROUTEINFO | |
996 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); | |
70ceb4f5 YH |
997 | |
998 | if (rt) { | |
1716a961 | 999 | if (!addrconf_finite_timeout(lifetime)) |
14895687 | 1000 | fib6_clean_expires(rt); |
1716a961 | 1001 | else |
14895687 | 1002 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
1716a961 | 1003 | |
93531c67 | 1004 | fib6_info_release(rt); |
70ceb4f5 YH |
1005 | } |
1006 | return 0; | |
1007 | } | |
1008 | #endif | |
1009 | ||
ae90d867 DA |
1010 | /* |
1011 | * Misc support functions | |
1012 | */ | |
1013 | ||
1014 | /* called with rcu_lock held */ | |
0d161581 | 1015 | static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) |
ae90d867 | 1016 | { |
0d161581 | 1017 | struct net_device *dev = res->nh->fib_nh_dev; |
ae90d867 | 1018 | |
7d21fec9 | 1019 | if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
ae90d867 DA |
1020 | /* for copies of local routes, dst->dev needs to be the |
1021 | * device if it is a master device, the master device if | |
1022 | * device is enslaved, and the loopback as the default | |
1023 | */ | |
1024 | if (netif_is_l3_slave(dev) && | |
7d21fec9 | 1025 | !rt6_need_strict(&res->f6i->fib6_dst.addr)) |
ae90d867 DA |
1026 | dev = l3mdev_master_dev_rcu(dev); |
1027 | else if (!netif_is_l3_master(dev)) | |
1028 | dev = dev_net(dev)->loopback_dev; | |
1029 | /* last case is netif_is_l3_master(dev) is true in which | |
1030 | * case we want dev returned to be dev | |
1031 | */ | |
1032 | } | |
1033 | ||
1034 | return dev; | |
1035 | } | |
1036 | ||
6edb3c96 DA |
1037 | static const int fib6_prop[RTN_MAX + 1] = { |
1038 | [RTN_UNSPEC] = 0, | |
1039 | [RTN_UNICAST] = 0, | |
1040 | [RTN_LOCAL] = 0, | |
1041 | [RTN_BROADCAST] = 0, | |
1042 | [RTN_ANYCAST] = 0, | |
1043 | [RTN_MULTICAST] = 0, | |
1044 | [RTN_BLACKHOLE] = -EINVAL, | |
1045 | [RTN_UNREACHABLE] = -EHOSTUNREACH, | |
1046 | [RTN_PROHIBIT] = -EACCES, | |
1047 | [RTN_THROW] = -EAGAIN, | |
1048 | [RTN_NAT] = -EINVAL, | |
1049 | [RTN_XRESOLVE] = -EINVAL, | |
1050 | }; | |
1051 | ||
1052 | static int ip6_rt_type_to_error(u8 fib6_type) | |
1053 | { | |
1054 | return fib6_prop[fib6_type]; | |
1055 | } | |
1056 | ||
8d1c802b | 1057 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
3b6761d1 DA |
1058 | { |
1059 | unsigned short flags = 0; | |
1060 | ||
1061 | if (rt->dst_nocount) | |
1062 | flags |= DST_NOCOUNT; | |
1063 | if (rt->dst_nopolicy) | |
1064 | flags |= DST_NOPOLICY; | |
3b6761d1 DA |
1065 | |
1066 | return flags; | |
1067 | } | |
1068 | ||
7d21fec9 | 1069 | static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) |
6edb3c96 | 1070 | { |
7d21fec9 | 1071 | rt->dst.error = ip6_rt_type_to_error(fib6_type); |
6edb3c96 | 1072 | |
7d21fec9 | 1073 | switch (fib6_type) { |
6edb3c96 DA |
1074 | case RTN_BLACKHOLE: |
1075 | rt->dst.output = dst_discard_out; | |
1076 | rt->dst.input = dst_discard; | |
1077 | break; | |
1078 | case RTN_PROHIBIT: | |
1079 | rt->dst.output = ip6_pkt_prohibit_out; | |
1080 | rt->dst.input = ip6_pkt_prohibit; | |
1081 | break; | |
1082 | case RTN_THROW: | |
1083 | case RTN_UNREACHABLE: | |
1084 | default: | |
1085 | rt->dst.output = ip6_pkt_discard_out; | |
1086 | rt->dst.input = ip6_pkt_discard; | |
1087 | break; | |
1088 | } | |
1089 | } | |
1090 | ||
0d161581 | 1091 | static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) |
6edb3c96 | 1092 | { |
7d21fec9 | 1093 | struct fib6_info *f6i = res->f6i; |
0d161581 | 1094 | |
7d21fec9 DA |
1095 | if (res->fib6_flags & RTF_REJECT) { |
1096 | ip6_rt_init_dst_reject(rt, res->fib6_type); | |
6edb3c96 DA |
1097 | return; |
1098 | } | |
1099 | ||
1100 | rt->dst.error = 0; | |
1101 | rt->dst.output = ip6_output; | |
1102 | ||
7d21fec9 | 1103 | if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { |
6edb3c96 | 1104 | rt->dst.input = ip6_input; |
7d21fec9 | 1105 | } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
6edb3c96 DA |
1106 | rt->dst.input = ip6_mc_input; |
1107 | } else { | |
1108 | rt->dst.input = ip6_forward; | |
1109 | } | |
1110 | ||
0d161581 DA |
1111 | if (res->nh->fib_nh_lws) { |
1112 | rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); | |
6edb3c96 DA |
1113 | lwtunnel_set_redirect(&rt->dst); |
1114 | } | |
1115 | ||
1116 | rt->dst.lastuse = jiffies; | |
1117 | } | |
1118 | ||
e873e4b9 | 1119 | /* Caller must already hold reference to @from */ |
8d1c802b | 1120 | static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) |
ae90d867 | 1121 | { |
ae90d867 | 1122 | rt->rt6i_flags &= ~RTF_EXPIRES; |
a68886a6 | 1123 | rcu_assign_pointer(rt->from, from); |
e1255ed4 | 1124 | ip_dst_init_metrics(&rt->dst, from->fib6_metrics); |
ae90d867 DA |
1125 | } |
1126 | ||
0d161581 DA |
1127 | /* Caller must already hold reference to f6i in result */ |
1128 | static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) | |
ae90d867 | 1129 | { |
0d161581 DA |
1130 | const struct fib6_nh *nh = res->nh; |
1131 | const struct net_device *dev = nh->fib_nh_dev; | |
1132 | struct fib6_info *f6i = res->f6i; | |
dcd1f572 | 1133 | |
0d161581 | 1134 | ip6_rt_init_dst(rt, res); |
6edb3c96 | 1135 | |
0d161581 | 1136 | rt->rt6i_dst = f6i->fib6_dst; |
dcd1f572 | 1137 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
7d21fec9 | 1138 | rt->rt6i_flags = res->fib6_flags; |
0d161581 DA |
1139 | if (nh->fib_nh_gw_family) { |
1140 | rt->rt6i_gateway = nh->fib_nh_gw6; | |
2b2450ca DA |
1141 | rt->rt6i_flags |= RTF_GATEWAY; |
1142 | } | |
0d161581 | 1143 | rt6_set_from(rt, f6i); |
ae90d867 | 1144 | #ifdef CONFIG_IPV6_SUBTREES |
0d161581 | 1145 | rt->rt6i_src = f6i->fib6_src; |
ae90d867 | 1146 | #endif |
ae90d867 DA |
1147 | } |
1148 | ||
a3c00e46 MKL |
1149 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
1150 | struct in6_addr *saddr) | |
1151 | { | |
66f5d6ce | 1152 | struct fib6_node *pn, *sn; |
a3c00e46 MKL |
1153 | while (1) { |
1154 | if (fn->fn_flags & RTN_TL_ROOT) | |
1155 | return NULL; | |
66f5d6ce WW |
1156 | pn = rcu_dereference(fn->parent); |
1157 | sn = FIB6_SUBTREE(pn); | |
1158 | if (sn && sn != fn) | |
6454743b | 1159 | fn = fib6_node_lookup(sn, NULL, saddr); |
a3c00e46 MKL |
1160 | else |
1161 | fn = pn; | |
1162 | if (fn->fn_flags & RTN_RTINFO) | |
1163 | return fn; | |
1164 | } | |
1165 | } | |
c71099ac | 1166 | |
10585b43 | 1167 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) |
d3843fe5 WW |
1168 | { |
1169 | struct rt6_info *rt = *prt; | |
1170 | ||
1171 | if (dst_hold_safe(&rt->dst)) | |
1172 | return true; | |
10585b43 | 1173 | if (net) { |
d3843fe5 WW |
1174 | rt = net->ipv6.ip6_null_entry; |
1175 | dst_hold(&rt->dst); | |
1176 | } else { | |
1177 | rt = NULL; | |
1178 | } | |
1179 | *prt = rt; | |
1180 | return false; | |
1181 | } | |
1182 | ||
dec9b0e2 | 1183 | /* called with rcu_lock held */ |
9b6b35ab | 1184 | static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) |
dec9b0e2 | 1185 | { |
9b6b35ab DA |
1186 | struct net_device *dev = res->nh->fib_nh_dev; |
1187 | struct fib6_info *f6i = res->f6i; | |
1188 | unsigned short flags; | |
dec9b0e2 DA |
1189 | struct rt6_info *nrt; |
1190 | ||
9b6b35ab | 1191 | if (!fib6_info_hold_safe(f6i)) |
1c87e79a | 1192 | goto fallback; |
e873e4b9 | 1193 | |
9b6b35ab | 1194 | flags = fib6_info_dst_flags(f6i); |
93531c67 | 1195 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
1c87e79a | 1196 | if (!nrt) { |
9b6b35ab | 1197 | fib6_info_release(f6i); |
1c87e79a XL |
1198 | goto fallback; |
1199 | } | |
dec9b0e2 | 1200 | |
0d161581 | 1201 | ip6_rt_copy_init(nrt, res); |
1c87e79a XL |
1202 | return nrt; |
1203 | ||
1204 | fallback: | |
1205 | nrt = dev_net(dev)->ipv6.ip6_null_entry; | |
1206 | dst_hold(&nrt->dst); | |
dec9b0e2 DA |
1207 | return nrt; |
1208 | } | |
1209 | ||
8ed67789 DL |
1210 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
1211 | struct fib6_table *table, | |
b75cc8f9 DA |
1212 | struct flowi6 *fl6, |
1213 | const struct sk_buff *skb, | |
1214 | int flags) | |
1da177e4 | 1215 | { |
b1d40991 | 1216 | struct fib6_result res = {}; |
1da177e4 | 1217 | struct fib6_node *fn; |
23fb93a4 | 1218 | struct rt6_info *rt; |
1da177e4 | 1219 | |
b6cdbc85 DA |
1220 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
1221 | flags &= ~RT6_LOOKUP_F_IFACE; | |
1222 | ||
66f5d6ce | 1223 | rcu_read_lock(); |
6454743b | 1224 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
c71099ac | 1225 | restart: |
b1d40991 DA |
1226 | res.f6i = rcu_dereference(fn->leaf); |
1227 | if (!res.f6i) | |
1228 | res.f6i = net->ipv6.fib6_null_entry; | |
af52a52c | 1229 | else |
75ef7389 DA |
1230 | rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, |
1231 | flags); | |
af52a52c | 1232 | |
b1d40991 | 1233 | if (res.f6i == net->ipv6.fib6_null_entry) { |
a3c00e46 MKL |
1234 | fn = fib6_backtrack(fn, &fl6->saddr); |
1235 | if (fn) | |
1236 | goto restart; | |
2b760fcf | 1237 | |
af52a52c DA |
1238 | rt = net->ipv6.ip6_null_entry; |
1239 | dst_hold(&rt->dst); | |
1240 | goto out; | |
f88d8ea6 DA |
1241 | } else if (res.fib6_flags & RTF_REJECT) { |
1242 | goto do_create; | |
af52a52c | 1243 | } |
d3843fe5 | 1244 | |
b1d40991 DA |
1245 | fib6_select_path(net, &res, fl6, fl6->flowi6_oif, |
1246 | fl6->flowi6_oif != 0, skb, flags); | |
1247 | ||
2b760fcf | 1248 | /* Search through exception table */ |
7e4b5128 | 1249 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
23fb93a4 | 1250 | if (rt) { |
10585b43 | 1251 | if (ip6_hold_safe(net, &rt)) |
dec9b0e2 | 1252 | dst_use_noref(&rt->dst, jiffies); |
23fb93a4 | 1253 | } else { |
f88d8ea6 | 1254 | do_create: |
9b6b35ab | 1255 | rt = ip6_create_rt_rcu(&res); |
dec9b0e2 | 1256 | } |
b811580d | 1257 | |
af52a52c | 1258 | out: |
8ff2e5b2 | 1259 | trace_fib6_table_lookup(net, &res, table, fl6); |
af52a52c | 1260 | |
66f5d6ce | 1261 | rcu_read_unlock(); |
b811580d | 1262 | |
c71099ac | 1263 | return rt; |
c71099ac TG |
1264 | } |
1265 | ||
67ba4152 | 1266 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
b75cc8f9 | 1267 | const struct sk_buff *skb, int flags) |
ea6e574e | 1268 | { |
b75cc8f9 | 1269 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
ea6e574e FW |
1270 | } |
1271 | EXPORT_SYMBOL_GPL(ip6_route_lookup); | |
1272 | ||
9acd9f3a | 1273 | struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, |
b75cc8f9 DA |
1274 | const struct in6_addr *saddr, int oif, |
1275 | const struct sk_buff *skb, int strict) | |
c71099ac | 1276 | { |
4c9483b2 DM |
1277 | struct flowi6 fl6 = { |
1278 | .flowi6_oif = oif, | |
1279 | .daddr = *daddr, | |
c71099ac TG |
1280 | }; |
1281 | struct dst_entry *dst; | |
77d16f45 | 1282 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
c71099ac | 1283 | |
adaa70bb | 1284 | if (saddr) { |
4c9483b2 | 1285 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
adaa70bb TG |
1286 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
1287 | } | |
1288 | ||
b75cc8f9 | 1289 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
c71099ac TG |
1290 | if (dst->error == 0) |
1291 | return (struct rt6_info *) dst; | |
1292 | ||
1293 | dst_release(dst); | |
1294 | ||
1da177e4 LT |
1295 | return NULL; |
1296 | } | |
7159039a YH |
1297 | EXPORT_SYMBOL(rt6_lookup); |
1298 | ||
c71099ac | 1299 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
1cfb71ee WW |
1300 | * It takes new route entry, the addition fails by any reason the |
1301 | * route is released. | |
1302 | * Caller must hold dst before calling it. | |
1da177e4 LT |
1303 | */ |
1304 | ||
8d1c802b | 1305 | static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, |
333c4301 | 1306 | struct netlink_ext_ack *extack) |
1da177e4 LT |
1307 | { |
1308 | int err; | |
c71099ac | 1309 | struct fib6_table *table; |
1da177e4 | 1310 | |
93c2fb25 | 1311 | table = rt->fib6_table; |
66f5d6ce | 1312 | spin_lock_bh(&table->tb6_lock); |
d4ead6b3 | 1313 | err = fib6_add(&table->tb6_root, rt, info, extack); |
66f5d6ce | 1314 | spin_unlock_bh(&table->tb6_lock); |
1da177e4 LT |
1315 | |
1316 | return err; | |
1317 | } | |
1318 | ||
8d1c802b | 1319 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
40e22e8f | 1320 | { |
afb1d4b5 | 1321 | struct nl_info info = { .nl_net = net, }; |
e715b6d3 | 1322 | |
d4ead6b3 | 1323 | return __ip6_ins_rt(rt, &info, NULL); |
40e22e8f TG |
1324 | } |
1325 | ||
85bd05de | 1326 | static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, |
8b9df265 MKL |
1327 | const struct in6_addr *daddr, |
1328 | const struct in6_addr *saddr) | |
1da177e4 | 1329 | { |
85bd05de | 1330 | struct fib6_info *f6i = res->f6i; |
4832c30d | 1331 | struct net_device *dev; |
1da177e4 LT |
1332 | struct rt6_info *rt; |
1333 | ||
1334 | /* | |
1335 | * Clone the route. | |
1336 | */ | |
1337 | ||
85bd05de | 1338 | if (!fib6_info_hold_safe(f6i)) |
e873e4b9 WW |
1339 | return NULL; |
1340 | ||
0d161581 | 1341 | dev = ip6_rt_get_dev_rcu(res); |
93531c67 | 1342 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
e873e4b9 | 1343 | if (!rt) { |
85bd05de | 1344 | fib6_info_release(f6i); |
83a09abd | 1345 | return NULL; |
e873e4b9 | 1346 | } |
83a09abd | 1347 | |
0d161581 | 1348 | ip6_rt_copy_init(rt, res); |
83a09abd | 1349 | rt->rt6i_flags |= RTF_CACHE; |
83a09abd MKL |
1350 | rt->rt6i_dst.addr = *daddr; |
1351 | rt->rt6i_dst.plen = 128; | |
1da177e4 | 1352 | |
85bd05de DA |
1353 | if (!rt6_is_gw_or_nonexthop(res)) { |
1354 | if (f6i->fib6_dst.plen != 128 && | |
1355 | ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) | |
83a09abd | 1356 | rt->rt6i_flags |= RTF_ANYCAST; |
1da177e4 | 1357 | #ifdef CONFIG_IPV6_SUBTREES |
83a09abd MKL |
1358 | if (rt->rt6i_src.plen && saddr) { |
1359 | rt->rt6i_src.addr = *saddr; | |
1360 | rt->rt6i_src.plen = 128; | |
8b9df265 | 1361 | } |
83a09abd | 1362 | #endif |
95a9a5ba | 1363 | } |
1da177e4 | 1364 | |
95a9a5ba YH |
1365 | return rt; |
1366 | } | |
1da177e4 | 1367 | |
db3fedee | 1368 | static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) |
d52d3997 | 1369 | { |
db3fedee DA |
1370 | struct fib6_info *f6i = res->f6i; |
1371 | unsigned short flags = fib6_info_dst_flags(f6i); | |
4832c30d | 1372 | struct net_device *dev; |
d52d3997 MKL |
1373 | struct rt6_info *pcpu_rt; |
1374 | ||
db3fedee | 1375 | if (!fib6_info_hold_safe(f6i)) |
e873e4b9 WW |
1376 | return NULL; |
1377 | ||
4832c30d | 1378 | rcu_read_lock(); |
0d161581 | 1379 | dev = ip6_rt_get_dev_rcu(res); |
93531c67 | 1380 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); |
4832c30d | 1381 | rcu_read_unlock(); |
e873e4b9 | 1382 | if (!pcpu_rt) { |
db3fedee | 1383 | fib6_info_release(f6i); |
d52d3997 | 1384 | return NULL; |
e873e4b9 | 1385 | } |
0d161581 | 1386 | ip6_rt_copy_init(pcpu_rt, res); |
d52d3997 | 1387 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
8f34e53b DA |
1388 | |
1389 | if (f6i->nh) | |
1390 | pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev)); | |
1391 | ||
d52d3997 MKL |
1392 | return pcpu_rt; |
1393 | } | |
1394 | ||
8f34e53b DA |
1395 | static bool rt6_is_valid(const struct rt6_info *rt6) |
1396 | { | |
1397 | return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev)); | |
1398 | } | |
1399 | ||
66f5d6ce | 1400 | /* It should be called with rcu_read_lock() acquired */ |
db3fedee | 1401 | static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) |
d52d3997 | 1402 | { |
c353071a | 1403 | struct rt6_info *pcpu_rt; |
d52d3997 | 1404 | |
c353071a | 1405 | pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); |
d52d3997 | 1406 | |
8f34e53b DA |
1407 | if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) { |
1408 | struct rt6_info *prev, **p; | |
1409 | ||
1410 | p = this_cpu_ptr(res->nh->rt6i_pcpu); | |
1411 | prev = xchg(p, NULL); | |
1412 | if (prev) { | |
1413 | dst_dev_put(&prev->dst); | |
1414 | dst_release(&prev->dst); | |
1415 | } | |
1416 | ||
1417 | pcpu_rt = NULL; | |
1418 | } | |
1419 | ||
a73e4195 MKL |
1420 | return pcpu_rt; |
1421 | } | |
1422 | ||
afb1d4b5 | 1423 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
db3fedee | 1424 | const struct fib6_result *res) |
a73e4195 MKL |
1425 | { |
1426 | struct rt6_info *pcpu_rt, *prev, **p; | |
d52d3997 | 1427 | |
db3fedee | 1428 | pcpu_rt = ip6_rt_pcpu_alloc(res); |
0e09edcc WW |
1429 | if (!pcpu_rt) |
1430 | return NULL; | |
d52d3997 | 1431 | |
f40b6ae2 | 1432 | p = this_cpu_ptr(res->nh->rt6i_pcpu); |
a94b9367 | 1433 | prev = cmpxchg(p, NULL, pcpu_rt); |
951f788a | 1434 | BUG_ON(prev); |
a94b9367 | 1435 | |
61fb0d01 ED |
1436 | if (res->f6i->fib6_destroying) { |
1437 | struct fib6_info *from; | |
1438 | ||
1439 | from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); | |
1440 | fib6_info_release(from); | |
1441 | } | |
1442 | ||
d52d3997 MKL |
1443 | return pcpu_rt; |
1444 | } | |
1445 | ||
35732d01 WW |
1446 | /* exception hash table implementation |
1447 | */ | |
1448 | static DEFINE_SPINLOCK(rt6_exception_lock); | |
1449 | ||
1450 | /* Remove rt6_ex from hash table and free the memory | |
1451 | * Caller must hold rt6_exception_lock | |
1452 | */ | |
1453 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, | |
1454 | struct rt6_exception *rt6_ex) | |
1455 | { | |
f5b51fe8 | 1456 | struct fib6_info *from; |
b2427e67 | 1457 | struct net *net; |
81eb8447 | 1458 | |
35732d01 WW |
1459 | if (!bucket || !rt6_ex) |
1460 | return; | |
b2427e67 CIK |
1461 | |
1462 | net = dev_net(rt6_ex->rt6i->dst.dev); | |
f5b51fe8 PA |
1463 | net->ipv6.rt6_stats->fib_rt_cache--; |
1464 | ||
1465 | /* purge completely the exception to allow releasing the held resources: | |
1466 | * some [sk] cache may keep the dst around for unlimited time | |
1467 | */ | |
0e233874 | 1468 | from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); |
f5b51fe8 PA |
1469 | fib6_info_release(from); |
1470 | dst_dev_put(&rt6_ex->rt6i->dst); | |
1471 | ||
35732d01 | 1472 | hlist_del_rcu(&rt6_ex->hlist); |
77634cc6 | 1473 | dst_release(&rt6_ex->rt6i->dst); |
35732d01 WW |
1474 | kfree_rcu(rt6_ex, rcu); |
1475 | WARN_ON_ONCE(!bucket->depth); | |
1476 | bucket->depth--; | |
1477 | } | |
1478 | ||
1479 | /* Remove oldest rt6_ex in bucket and free the memory | |
1480 | * Caller must hold rt6_exception_lock | |
1481 | */ | |
1482 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) | |
1483 | { | |
1484 | struct rt6_exception *rt6_ex, *oldest = NULL; | |
1485 | ||
1486 | if (!bucket) | |
1487 | return; | |
1488 | ||
1489 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { | |
1490 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) | |
1491 | oldest = rt6_ex; | |
1492 | } | |
1493 | rt6_remove_exception(bucket, oldest); | |
1494 | } | |
1495 | ||
1496 | static u32 rt6_exception_hash(const struct in6_addr *dst, | |
1497 | const struct in6_addr *src) | |
1498 | { | |
1499 | static u32 seed __read_mostly; | |
1500 | u32 val; | |
1501 | ||
1502 | net_get_random_once(&seed, sizeof(seed)); | |
b6b556af | 1503 | val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed); |
35732d01 WW |
1504 | |
1505 | #ifdef CONFIG_IPV6_SUBTREES | |
1506 | if (src) | |
b6b556af | 1507 | val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val); |
35732d01 WW |
1508 | #endif |
1509 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); | |
1510 | } | |
1511 | ||
1512 | /* Helper function to find the cached rt in the hash table | |
1513 | * and update bucket pointer to point to the bucket for this | |
1514 | * (daddr, saddr) pair | |
1515 | * Caller must hold rt6_exception_lock | |
1516 | */ | |
1517 | static struct rt6_exception * | |
1518 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, | |
1519 | const struct in6_addr *daddr, | |
1520 | const struct in6_addr *saddr) | |
1521 | { | |
1522 | struct rt6_exception *rt6_ex; | |
1523 | u32 hval; | |
1524 | ||
1525 | if (!(*bucket) || !daddr) | |
1526 | return NULL; | |
1527 | ||
1528 | hval = rt6_exception_hash(daddr, saddr); | |
1529 | *bucket += hval; | |
1530 | ||
1531 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { | |
1532 | struct rt6_info *rt6 = rt6_ex->rt6i; | |
1533 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); | |
1534 | ||
1535 | #ifdef CONFIG_IPV6_SUBTREES | |
1536 | if (matched && saddr) | |
1537 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); | |
1538 | #endif | |
1539 | if (matched) | |
1540 | return rt6_ex; | |
1541 | } | |
1542 | return NULL; | |
1543 | } | |
1544 | ||
1545 | /* Helper function to find the cached rt in the hash table | |
1546 | * and update bucket pointer to point to the bucket for this | |
1547 | * (daddr, saddr) pair | |
1548 | * Caller must hold rcu_read_lock() | |
1549 | */ | |
1550 | static struct rt6_exception * | |
1551 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, | |
1552 | const struct in6_addr *daddr, | |
1553 | const struct in6_addr *saddr) | |
1554 | { | |
1555 | struct rt6_exception *rt6_ex; | |
1556 | u32 hval; | |
1557 | ||
1558 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
1559 | ||
1560 | if (!(*bucket) || !daddr) | |
1561 | return NULL; | |
1562 | ||
1563 | hval = rt6_exception_hash(daddr, saddr); | |
1564 | *bucket += hval; | |
1565 | ||
1566 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { | |
1567 | struct rt6_info *rt6 = rt6_ex->rt6i; | |
1568 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); | |
1569 | ||
1570 | #ifdef CONFIG_IPV6_SUBTREES | |
1571 | if (matched && saddr) | |
1572 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); | |
1573 | #endif | |
1574 | if (matched) | |
1575 | return rt6_ex; | |
1576 | } | |
1577 | return NULL; | |
1578 | } | |
1579 | ||
b748f260 | 1580 | static unsigned int fib6_mtu(const struct fib6_result *res) |
d4ead6b3 | 1581 | { |
b748f260 | 1582 | const struct fib6_nh *nh = res->nh; |
d4ead6b3 DA |
1583 | unsigned int mtu; |
1584 | ||
b748f260 DA |
1585 | if (res->f6i->fib6_pmtu) { |
1586 | mtu = res->f6i->fib6_pmtu; | |
dcd1f572 | 1587 | } else { |
b748f260 | 1588 | struct net_device *dev = nh->fib_nh_dev; |
dcd1f572 DA |
1589 | struct inet6_dev *idev; |
1590 | ||
1591 | rcu_read_lock(); | |
1592 | idev = __in6_dev_get(dev); | |
1593 | mtu = idev->cnf.mtu6; | |
1594 | rcu_read_unlock(); | |
1595 | } | |
1596 | ||
d4ead6b3 DA |
1597 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
1598 | ||
b748f260 | 1599 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
d4ead6b3 DA |
1600 | } |
1601 | ||
cc5c073a DA |
1602 | #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL |
1603 | ||
1604 | /* used when the flushed bit is not relevant, only access to the bucket | |
1605 | * (ie., all bucket users except rt6_insert_exception); | |
1606 | * | |
1607 | * called under rcu lock; sometimes called with rt6_exception_lock held | |
1608 | */ | |
1609 | static | |
1610 | struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, | |
1611 | spinlock_t *lock) | |
1612 | { | |
1613 | struct rt6_exception_bucket *bucket; | |
1614 | ||
1615 | if (lock) | |
1616 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, | |
1617 | lockdep_is_held(lock)); | |
1618 | else | |
1619 | bucket = rcu_dereference(nh->rt6i_exception_bucket); | |
1620 | ||
1621 | /* remove bucket flushed bit if set */ | |
1622 | if (bucket) { | |
1623 | unsigned long p = (unsigned long)bucket; | |
1624 | ||
1625 | p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; | |
1626 | bucket = (struct rt6_exception_bucket *)p; | |
1627 | } | |
1628 | ||
1629 | return bucket; | |
1630 | } | |
1631 | ||
1632 | static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) | |
1633 | { | |
1634 | unsigned long p = (unsigned long)bucket; | |
1635 | ||
1636 | return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); | |
1637 | } | |
1638 | ||
1639 | /* called with rt6_exception_lock held */ | |
1640 | static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, | |
1641 | spinlock_t *lock) | |
1642 | { | |
1643 | struct rt6_exception_bucket *bucket; | |
1644 | unsigned long p; | |
1645 | ||
1646 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, | |
1647 | lockdep_is_held(lock)); | |
1648 | ||
1649 | p = (unsigned long)bucket; | |
1650 | p |= FIB6_EXCEPTION_BUCKET_FLUSHED; | |
1651 | bucket = (struct rt6_exception_bucket *)p; | |
1652 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); | |
1653 | } | |
1654 | ||
35732d01 | 1655 | static int rt6_insert_exception(struct rt6_info *nrt, |
5012f0a5 | 1656 | const struct fib6_result *res) |
35732d01 | 1657 | { |
5e670d84 | 1658 | struct net *net = dev_net(nrt->dst.dev); |
35732d01 | 1659 | struct rt6_exception_bucket *bucket; |
cc5c073a | 1660 | struct fib6_info *f6i = res->f6i; |
35732d01 WW |
1661 | struct in6_addr *src_key = NULL; |
1662 | struct rt6_exception *rt6_ex; | |
cc5c073a | 1663 | struct fib6_nh *nh = res->nh; |
35732d01 WW |
1664 | int err = 0; |
1665 | ||
35732d01 WW |
1666 | spin_lock_bh(&rt6_exception_lock); |
1667 | ||
cc5c073a DA |
1668 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
1669 | lockdep_is_held(&rt6_exception_lock)); | |
35732d01 WW |
1670 | if (!bucket) { |
1671 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), | |
1672 | GFP_ATOMIC); | |
1673 | if (!bucket) { | |
1674 | err = -ENOMEM; | |
1675 | goto out; | |
1676 | } | |
cc5c073a DA |
1677 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
1678 | } else if (fib6_nh_excptn_bucket_flushed(bucket)) { | |
1679 | err = -EINVAL; | |
1680 | goto out; | |
35732d01 WW |
1681 | } |
1682 | ||
1683 | #ifdef CONFIG_IPV6_SUBTREES | |
5012f0a5 | 1684 | /* fib6_src.plen != 0 indicates f6i is in subtree |
35732d01 | 1685 | * and exception table is indexed by a hash of |
5012f0a5 | 1686 | * both fib6_dst and fib6_src. |
35732d01 | 1687 | * Otherwise, the exception table is indexed by |
5012f0a5 | 1688 | * a hash of only fib6_dst. |
35732d01 | 1689 | */ |
5012f0a5 | 1690 | if (f6i->fib6_src.plen) |
35732d01 WW |
1691 | src_key = &nrt->rt6i_src.addr; |
1692 | #endif | |
5012f0a5 | 1693 | /* rt6_mtu_change() might lower mtu on f6i. |
f5bbe7ee | 1694 | * Only insert this exception route if its mtu |
5012f0a5 | 1695 | * is less than f6i's mtu value. |
f5bbe7ee | 1696 | */ |
b748f260 | 1697 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { |
f5bbe7ee WW |
1698 | err = -EINVAL; |
1699 | goto out; | |
1700 | } | |
60006a48 | 1701 | |
35732d01 WW |
1702 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
1703 | src_key); | |
1704 | if (rt6_ex) | |
1705 | rt6_remove_exception(bucket, rt6_ex); | |
1706 | ||
1707 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); | |
1708 | if (!rt6_ex) { | |
1709 | err = -ENOMEM; | |
1710 | goto out; | |
1711 | } | |
1712 | rt6_ex->rt6i = nrt; | |
1713 | rt6_ex->stamp = jiffies; | |
35732d01 WW |
1714 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
1715 | bucket->depth++; | |
81eb8447 | 1716 | net->ipv6.rt6_stats->fib_rt_cache++; |
35732d01 WW |
1717 | |
1718 | if (bucket->depth > FIB6_MAX_DEPTH) | |
1719 | rt6_exception_remove_oldest(bucket); | |
1720 | ||
1721 | out: | |
1722 | spin_unlock_bh(&rt6_exception_lock); | |
1723 | ||
1724 | /* Update fn->fn_sernum to invalidate all cached dst */ | |
b886d5f2 | 1725 | if (!err) { |
5012f0a5 DA |
1726 | spin_lock_bh(&f6i->fib6_table->tb6_lock); |
1727 | fib6_update_sernum(net, f6i); | |
1728 | spin_unlock_bh(&f6i->fib6_table->tb6_lock); | |
b886d5f2 PA |
1729 | fib6_force_start_gc(net); |
1730 | } | |
35732d01 WW |
1731 | |
1732 | return err; | |
1733 | } | |
1734 | ||
c0b220cf | 1735 | static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) |
35732d01 WW |
1736 | { |
1737 | struct rt6_exception_bucket *bucket; | |
1738 | struct rt6_exception *rt6_ex; | |
1739 | struct hlist_node *tmp; | |
1740 | int i; | |
1741 | ||
1742 | spin_lock_bh(&rt6_exception_lock); | |
35732d01 | 1743 | |
cc5c073a | 1744 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
35732d01 WW |
1745 | if (!bucket) |
1746 | goto out; | |
1747 | ||
cc5c073a DA |
1748 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
1749 | if (!from) | |
1750 | fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); | |
1751 | ||
35732d01 | 1752 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
cc5c073a DA |
1753 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { |
1754 | if (!from || | |
1755 | rcu_access_pointer(rt6_ex->rt6i->from) == from) | |
1756 | rt6_remove_exception(bucket, rt6_ex); | |
1757 | } | |
1758 | WARN_ON_ONCE(!from && bucket->depth); | |
35732d01 WW |
1759 | bucket++; |
1760 | } | |
35732d01 WW |
1761 | out: |
1762 | spin_unlock_bh(&rt6_exception_lock); | |
1763 | } | |
1764 | ||
e659ba31 DA |
1765 | static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) |
1766 | { | |
1767 | struct fib6_info *f6i = arg; | |
1768 | ||
1769 | fib6_nh_flush_exceptions(nh, f6i); | |
1770 | ||
1771 | return 0; | |
1772 | } | |
1773 | ||
c0b220cf DA |
1774 | void rt6_flush_exceptions(struct fib6_info *f6i) |
1775 | { | |
e659ba31 DA |
1776 | if (f6i->nh) |
1777 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, | |
1778 | f6i); | |
1779 | else | |
1780 | fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); | |
c0b220cf DA |
1781 | } |
1782 | ||
35732d01 WW |
1783 | /* Find cached rt in the hash table inside passed in rt |
1784 | * Caller has to hold rcu_read_lock() | |
1785 | */ | |
7e4b5128 | 1786 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
510e2ced WW |
1787 | const struct in6_addr *daddr, |
1788 | const struct in6_addr *saddr) | |
35732d01 | 1789 | { |
510e2ced | 1790 | const struct in6_addr *src_key = NULL; |
35732d01 | 1791 | struct rt6_exception_bucket *bucket; |
35732d01 | 1792 | struct rt6_exception *rt6_ex; |
7e4b5128 | 1793 | struct rt6_info *ret = NULL; |
35732d01 | 1794 | |
35732d01 | 1795 | #ifdef CONFIG_IPV6_SUBTREES |
7e4b5128 | 1796 | /* fib6i_src.plen != 0 indicates f6i is in subtree |
35732d01 | 1797 | * and exception table is indexed by a hash of |
7e4b5128 | 1798 | * both fib6_dst and fib6_src. |
510e2ced WW |
1799 | * However, the src addr used to create the hash |
1800 | * might not be exactly the passed in saddr which | |
1801 | * is a /128 addr from the flow. | |
1802 | * So we need to use f6i->fib6_src to redo lookup | |
1803 | * if the passed in saddr does not find anything. | |
1804 | * (See the logic in ip6_rt_cache_alloc() on how | |
1805 | * rt->rt6i_src is updated.) | |
35732d01 | 1806 | */ |
7e4b5128 | 1807 | if (res->f6i->fib6_src.plen) |
35732d01 | 1808 | src_key = saddr; |
510e2ced | 1809 | find_ex: |
35732d01 | 1810 | #endif |
cc5c073a | 1811 | bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); |
35732d01 WW |
1812 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
1813 | ||
1814 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) | |
7e4b5128 | 1815 | ret = rt6_ex->rt6i; |
35732d01 | 1816 | |
510e2ced WW |
1817 | #ifdef CONFIG_IPV6_SUBTREES |
1818 | /* Use fib6_src as src_key and redo lookup */ | |
1819 | if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { | |
1820 | src_key = &res->f6i->fib6_src.addr; | |
1821 | goto find_ex; | |
1822 | } | |
1823 | #endif | |
1824 | ||
7e4b5128 | 1825 | return ret; |
35732d01 WW |
1826 | } |
1827 | ||
1828 | /* Remove the passed in cached rt from the hash table that contains it */ | |
cc5c073a | 1829 | static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, |
c0b220cf | 1830 | const struct rt6_info *rt) |
35732d01 | 1831 | { |
c0b220cf | 1832 | const struct in6_addr *src_key = NULL; |
35732d01 | 1833 | struct rt6_exception_bucket *bucket; |
35732d01 WW |
1834 | struct rt6_exception *rt6_ex; |
1835 | int err; | |
1836 | ||
cc5c073a | 1837 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
35732d01 WW |
1838 | return -ENOENT; |
1839 | ||
1840 | spin_lock_bh(&rt6_exception_lock); | |
cc5c073a DA |
1841 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
1842 | ||
35732d01 | 1843 | #ifdef CONFIG_IPV6_SUBTREES |
cc5c073a DA |
1844 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
1845 | * and exception table is indexed by a hash of | |
1846 | * both rt6i_dst and rt6i_src. | |
35732d01 WW |
1847 | * Otherwise, the exception table is indexed by |
1848 | * a hash of only rt6i_dst. | |
1849 | */ | |
c0b220cf | 1850 | if (plen) |
35732d01 WW |
1851 | src_key = &rt->rt6i_src.addr; |
1852 | #endif | |
1853 | rt6_ex = __rt6_find_exception_spinlock(&bucket, | |
1854 | &rt->rt6i_dst.addr, | |
1855 | src_key); | |
1856 | if (rt6_ex) { | |
1857 | rt6_remove_exception(bucket, rt6_ex); | |
1858 | err = 0; | |
1859 | } else { | |
1860 | err = -ENOENT; | |
1861 | } | |
1862 | ||
1863 | spin_unlock_bh(&rt6_exception_lock); | |
1864 | return err; | |
1865 | } | |
1866 | ||
e659ba31 DA |
1867 | struct fib6_nh_excptn_arg { |
1868 | struct rt6_info *rt; | |
1869 | int plen; | |
1870 | }; | |
1871 | ||
1872 | static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) | |
1873 | { | |
1874 | struct fib6_nh_excptn_arg *arg = _arg; | |
1875 | int err; | |
1876 | ||
1877 | err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); | |
1878 | if (err == 0) | |
1879 | return 1; | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | ||
c0b220cf DA |
1884 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
1885 | { | |
1886 | struct fib6_info *from; | |
1887 | ||
1888 | from = rcu_dereference(rt->from); | |
cc5c073a | 1889 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
c0b220cf DA |
1890 | return -EINVAL; |
1891 | ||
e659ba31 DA |
1892 | if (from->nh) { |
1893 | struct fib6_nh_excptn_arg arg = { | |
1894 | .rt = rt, | |
1895 | .plen = from->fib6_src.plen | |
1896 | }; | |
1897 | int rc; | |
1898 | ||
1899 | /* rc = 1 means an entry was found */ | |
1900 | rc = nexthop_for_each_fib6_nh(from->nh, | |
1901 | rt6_nh_remove_exception_rt, | |
1902 | &arg); | |
1903 | return rc ? 0 : -ENOENT; | |
1904 | } | |
1905 | ||
1cf844c7 | 1906 | return fib6_nh_remove_exception(from->fib6_nh, |
cc5c073a | 1907 | from->fib6_src.plen, rt); |
c0b220cf DA |
1908 | } |
1909 | ||
35732d01 WW |
1910 | /* Find rt6_ex which contains the passed in rt cache and |
1911 | * refresh its stamp | |
1912 | */ | |
cc5c073a | 1913 | static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, |
c0b220cf | 1914 | const struct rt6_info *rt) |
35732d01 | 1915 | { |
c0b220cf | 1916 | const struct in6_addr *src_key = NULL; |
35732d01 | 1917 | struct rt6_exception_bucket *bucket; |
35732d01 | 1918 | struct rt6_exception *rt6_ex; |
193f3685 | 1919 | |
cc5c073a | 1920 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
35732d01 | 1921 | #ifdef CONFIG_IPV6_SUBTREES |
cc5c073a DA |
1922 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
1923 | * and exception table is indexed by a hash of | |
1924 | * both rt6i_dst and rt6i_src. | |
35732d01 WW |
1925 | * Otherwise, the exception table is indexed by |
1926 | * a hash of only rt6i_dst. | |
1927 | */ | |
c0b220cf | 1928 | if (plen) |
35732d01 WW |
1929 | src_key = &rt->rt6i_src.addr; |
1930 | #endif | |
cc5c073a | 1931 | rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); |
35732d01 WW |
1932 | if (rt6_ex) |
1933 | rt6_ex->stamp = jiffies; | |
c0b220cf DA |
1934 | } |
1935 | ||
e659ba31 DA |
1936 | struct fib6_nh_match_arg { |
1937 | const struct net_device *dev; | |
1938 | const struct in6_addr *gw; | |
1939 | struct fib6_nh *match; | |
1940 | }; | |
1941 | ||
1942 | /* determine if fib6_nh has given device and gateway */ | |
1943 | static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) | |
1944 | { | |
1945 | struct fib6_nh_match_arg *arg = _arg; | |
1946 | ||
1947 | if (arg->dev != nh->fib_nh_dev || | |
1948 | (arg->gw && !nh->fib_nh_gw_family) || | |
1949 | (!arg->gw && nh->fib_nh_gw_family) || | |
1950 | (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) | |
1951 | return 0; | |
1952 | ||
1953 | arg->match = nh; | |
1954 | ||
1955 | /* found a match, break the loop */ | |
1956 | return 1; | |
1957 | } | |
1958 | ||
c0b220cf DA |
1959 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
1960 | { | |
1961 | struct fib6_info *from; | |
e659ba31 | 1962 | struct fib6_nh *fib6_nh; |
35732d01 | 1963 | |
c0b220cf DA |
1964 | rcu_read_lock(); |
1965 | ||
1966 | from = rcu_dereference(rt->from); | |
1967 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) | |
1968 | goto unlock; | |
1969 | ||
e659ba31 DA |
1970 | if (from->nh) { |
1971 | struct fib6_nh_match_arg arg = { | |
1972 | .dev = rt->dst.dev, | |
1973 | .gw = &rt->rt6i_gateway, | |
1974 | }; | |
1975 | ||
1976 | nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); | |
1977 | ||
1978 | if (!arg.match) | |
cff6a327 | 1979 | goto unlock; |
e659ba31 DA |
1980 | fib6_nh = arg.match; |
1981 | } else { | |
1982 | fib6_nh = from->fib6_nh; | |
1983 | } | |
1984 | fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); | |
193f3685 | 1985 | unlock: |
35732d01 WW |
1986 | rcu_read_unlock(); |
1987 | } | |
1988 | ||
e9fa1495 SB |
1989 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
1990 | struct rt6_info *rt, int mtu) | |
1991 | { | |
1992 | /* If the new MTU is lower than the route PMTU, this new MTU will be the | |
1993 | * lowest MTU in the path: always allow updating the route PMTU to | |
1994 | * reflect PMTU decreases. | |
1995 | * | |
1996 | * If the new MTU is higher, and the route PMTU is equal to the local | |
1997 | * MTU, this means the old MTU is the lowest in the path, so allow | |
1998 | * updating it: if other nodes now have lower MTUs, PMTU discovery will | |
1999 | * handle this. | |
2000 | */ | |
2001 | ||
2002 | if (dst_mtu(&rt->dst) >= mtu) | |
2003 | return true; | |
2004 | ||
2005 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) | |
2006 | return true; | |
2007 | ||
2008 | return false; | |
2009 | } | |
2010 | ||
2011 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, | |
cc5c073a | 2012 | const struct fib6_nh *nh, int mtu) |
f5bbe7ee WW |
2013 | { |
2014 | struct rt6_exception_bucket *bucket; | |
2015 | struct rt6_exception *rt6_ex; | |
2016 | int i; | |
2017 | ||
cc5c073a | 2018 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
e9fa1495 SB |
2019 | if (!bucket) |
2020 | return; | |
2021 | ||
2022 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { | |
2023 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { | |
2024 | struct rt6_info *entry = rt6_ex->rt6i; | |
2025 | ||
2026 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected | |
d4ead6b3 | 2027 | * route), the metrics of its rt->from have already |
e9fa1495 SB |
2028 | * been updated. |
2029 | */ | |
d4ead6b3 | 2030 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
e9fa1495 | 2031 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
d4ead6b3 | 2032 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
f5bbe7ee | 2033 | } |
e9fa1495 | 2034 | bucket++; |
f5bbe7ee WW |
2035 | } |
2036 | } | |
2037 | ||
b16cb459 WW |
2038 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
2039 | ||
cc5c073a DA |
2040 | static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, |
2041 | const struct in6_addr *gateway) | |
b16cb459 WW |
2042 | { |
2043 | struct rt6_exception_bucket *bucket; | |
2044 | struct rt6_exception *rt6_ex; | |
2045 | struct hlist_node *tmp; | |
2046 | int i; | |
2047 | ||
cc5c073a | 2048 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
b16cb459 WW |
2049 | return; |
2050 | ||
2051 | spin_lock_bh(&rt6_exception_lock); | |
cc5c073a | 2052 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
b16cb459 WW |
2053 | if (bucket) { |
2054 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { | |
2055 | hlist_for_each_entry_safe(rt6_ex, tmp, | |
2056 | &bucket->chain, hlist) { | |
2057 | struct rt6_info *entry = rt6_ex->rt6i; | |
2058 | ||
2059 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == | |
2060 | RTF_CACHE_GATEWAY && | |
2061 | ipv6_addr_equal(gateway, | |
2062 | &entry->rt6i_gateway)) { | |
2063 | rt6_remove_exception(bucket, rt6_ex); | |
2064 | } | |
2065 | } | |
2066 | bucket++; | |
2067 | } | |
2068 | } | |
2069 | ||
2070 | spin_unlock_bh(&rt6_exception_lock); | |
2071 | } | |
2072 | ||
c757faa8 WW |
2073 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
2074 | struct rt6_exception *rt6_ex, | |
2075 | struct fib6_gc_args *gc_args, | |
2076 | unsigned long now) | |
2077 | { | |
2078 | struct rt6_info *rt = rt6_ex->rt6i; | |
2079 | ||
1859bac0 PA |
2080 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
2081 | * even if others have still references to them, so that on next | |
2082 | * dst_check() such references can be dropped. | |
2083 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when | |
2084 | * expired, independently from their aging, as per RFC 8201 section 4 | |
2085 | */ | |
31afeb42 WW |
2086 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
2087 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { | |
2088 | RT6_TRACE("aging clone %p\n", rt); | |
2089 | rt6_remove_exception(bucket, rt6_ex); | |
2090 | return; | |
2091 | } | |
2092 | } else if (time_after(jiffies, rt->dst.expires)) { | |
2093 | RT6_TRACE("purging expired route %p\n", rt); | |
c757faa8 WW |
2094 | rt6_remove_exception(bucket, rt6_ex); |
2095 | return; | |
31afeb42 WW |
2096 | } |
2097 | ||
2098 | if (rt->rt6i_flags & RTF_GATEWAY) { | |
c757faa8 WW |
2099 | struct neighbour *neigh; |
2100 | __u8 neigh_flags = 0; | |
2101 | ||
1bfa26ff ED |
2102 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
2103 | if (neigh) | |
c757faa8 | 2104 | neigh_flags = neigh->flags; |
1bfa26ff | 2105 | |
c757faa8 WW |
2106 | if (!(neigh_flags & NTF_ROUTER)) { |
2107 | RT6_TRACE("purging route %p via non-router but gateway\n", | |
2108 | rt); | |
2109 | rt6_remove_exception(bucket, rt6_ex); | |
2110 | return; | |
2111 | } | |
2112 | } | |
31afeb42 | 2113 | |
c757faa8 WW |
2114 | gc_args->more++; |
2115 | } | |
2116 | ||
cc5c073a | 2117 | static void fib6_nh_age_exceptions(const struct fib6_nh *nh, |
c0b220cf DA |
2118 | struct fib6_gc_args *gc_args, |
2119 | unsigned long now) | |
c757faa8 WW |
2120 | { |
2121 | struct rt6_exception_bucket *bucket; | |
2122 | struct rt6_exception *rt6_ex; | |
2123 | struct hlist_node *tmp; | |
2124 | int i; | |
2125 | ||
cc5c073a | 2126 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
c757faa8 WW |
2127 | return; |
2128 | ||
1bfa26ff ED |
2129 | rcu_read_lock_bh(); |
2130 | spin_lock(&rt6_exception_lock); | |
cc5c073a | 2131 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
c757faa8 WW |
2132 | if (bucket) { |
2133 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { | |
2134 | hlist_for_each_entry_safe(rt6_ex, tmp, | |
2135 | &bucket->chain, hlist) { | |
2136 | rt6_age_examine_exception(bucket, rt6_ex, | |
2137 | gc_args, now); | |
2138 | } | |
2139 | bucket++; | |
2140 | } | |
2141 | } | |
1bfa26ff ED |
2142 | spin_unlock(&rt6_exception_lock); |
2143 | rcu_read_unlock_bh(); | |
c757faa8 WW |
2144 | } |
2145 | ||
e659ba31 DA |
2146 | struct fib6_nh_age_excptn_arg { |
2147 | struct fib6_gc_args *gc_args; | |
2148 | unsigned long now; | |
2149 | }; | |
2150 | ||
2151 | static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) | |
2152 | { | |
2153 | struct fib6_nh_age_excptn_arg *arg = _arg; | |
2154 | ||
2155 | fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); | |
2156 | return 0; | |
2157 | } | |
2158 | ||
cc5c073a | 2159 | void rt6_age_exceptions(struct fib6_info *f6i, |
c0b220cf DA |
2160 | struct fib6_gc_args *gc_args, |
2161 | unsigned long now) | |
2162 | { | |
e659ba31 DA |
2163 | if (f6i->nh) { |
2164 | struct fib6_nh_age_excptn_arg arg = { | |
2165 | .gc_args = gc_args, | |
2166 | .now = now | |
2167 | }; | |
2168 | ||
2169 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, | |
2170 | &arg); | |
2171 | } else { | |
2172 | fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); | |
2173 | } | |
c0b220cf DA |
2174 | } |
2175 | ||
1d053da9 | 2176 | /* must be called with rcu lock held */ |
effda4dd DA |
2177 | int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, |
2178 | struct flowi6 *fl6, struct fib6_result *res, int strict) | |
1da177e4 | 2179 | { |
367efcb9 | 2180 | struct fib6_node *fn, *saved_fn; |
1da177e4 | 2181 | |
6454743b | 2182 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
367efcb9 | 2183 | saved_fn = fn; |
1da177e4 | 2184 | |
ca254490 DA |
2185 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
2186 | oif = 0; | |
2187 | ||
a3c00e46 | 2188 | redo_rt6_select: |
effda4dd DA |
2189 | rt6_select(net, fn, oif, res, strict); |
2190 | if (res->f6i == net->ipv6.fib6_null_entry) { | |
a3c00e46 MKL |
2191 | fn = fib6_backtrack(fn, &fl6->saddr); |
2192 | if (fn) | |
2193 | goto redo_rt6_select; | |
367efcb9 MKL |
2194 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
2195 | /* also consider unreachable route */ | |
2196 | strict &= ~RT6_LOOKUP_F_REACHABLE; | |
2197 | fn = saved_fn; | |
2198 | goto redo_rt6_select; | |
367efcb9 | 2199 | } |
a3c00e46 MKL |
2200 | } |
2201 | ||
effda4dd | 2202 | trace_fib6_table_lookup(net, res, table, fl6); |
fb9de91e | 2203 | |
effda4dd | 2204 | return 0; |
1d053da9 DA |
2205 | } |
2206 | ||
2207 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, | |
2208 | int oif, struct flowi6 *fl6, | |
2209 | const struct sk_buff *skb, int flags) | |
2210 | { | |
b1d40991 | 2211 | struct fib6_result res = {}; |
0e09edcc | 2212 | struct rt6_info *rt = NULL; |
1d053da9 DA |
2213 | int strict = 0; |
2214 | ||
0e09edcc WW |
2215 | WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && |
2216 | !rcu_read_lock_held()); | |
2217 | ||
1d053da9 DA |
2218 | strict |= flags & RT6_LOOKUP_F_IFACE; |
2219 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; | |
2220 | if (net->ipv6.devconf_all->forwarding == 0) | |
2221 | strict |= RT6_LOOKUP_F_REACHABLE; | |
2222 | ||
2223 | rcu_read_lock(); | |
2224 | ||
effda4dd | 2225 | fib6_table_lookup(net, table, oif, fl6, &res, strict); |
0e09edcc WW |
2226 | if (res.f6i == net->ipv6.fib6_null_entry) |
2227 | goto out; | |
23fb93a4 | 2228 | |
b1d40991 | 2229 | fib6_select_path(net, &res, fl6, oif, false, skb, strict); |
d83009d4 | 2230 | |
23fb93a4 | 2231 | /*Search through exception table */ |
7e4b5128 | 2232 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
23fb93a4 | 2233 | if (rt) { |
0e09edcc | 2234 | goto out; |
3da59bd9 | 2235 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
b1d40991 | 2236 | !res.nh->fib_nh_gw_family)) { |
3da59bd9 MKL |
2237 | /* Create a RTF_CACHE clone which will not be |
2238 | * owned by the fib6 tree. It is for the special case where | |
2239 | * the daddr in the skb during the neighbor look-up is different | |
2240 | * from the fl6->daddr used to look-up route here. | |
2241 | */ | |
0e09edcc | 2242 | rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); |
c71099ac | 2243 | |
0e09edcc WW |
2244 | if (rt) { |
2245 | /* 1 refcnt is taken during ip6_rt_cache_alloc(). | |
2246 | * As rt6_uncached_list_add() does not consume refcnt, | |
2247 | * this refcnt is always returned to the caller even | |
2248 | * if caller sets RT6_LOOKUP_F_DST_NOREF flag. | |
1cfb71ee | 2249 | */ |
0e09edcc | 2250 | rt6_uncached_list_add(rt); |
81eb8447 | 2251 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
0e09edcc | 2252 | rcu_read_unlock(); |
b811580d | 2253 | |
0e09edcc WW |
2254 | return rt; |
2255 | } | |
d52d3997 MKL |
2256 | } else { |
2257 | /* Get a percpu copy */ | |
951f788a | 2258 | local_bh_disable(); |
0e09edcc | 2259 | rt = rt6_get_pcpu_route(&res); |
d52d3997 | 2260 | |
0e09edcc WW |
2261 | if (!rt) |
2262 | rt = rt6_make_pcpu_route(net, &res); | |
93531c67 | 2263 | |
951f788a | 2264 | local_bh_enable(); |
d52d3997 | 2265 | } |
0e09edcc WW |
2266 | out: |
2267 | if (!rt) | |
2268 | rt = net->ipv6.ip6_null_entry; | |
2269 | if (!(flags & RT6_LOOKUP_F_DST_NOREF)) | |
2270 | ip6_hold_safe(net, &rt); | |
2271 | rcu_read_unlock(); | |
2272 | ||
2273 | return rt; | |
1da177e4 | 2274 | } |
9ff74384 | 2275 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
1da177e4 | 2276 | |
b75cc8f9 DA |
2277 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
2278 | struct fib6_table *table, | |
2279 | struct flowi6 *fl6, | |
2280 | const struct sk_buff *skb, | |
2281 | int flags) | |
4acad72d | 2282 | { |
b75cc8f9 | 2283 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
4acad72d PE |
2284 | } |
2285 | ||
d409b847 MB |
2286 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
2287 | struct net_device *dev, | |
b75cc8f9 DA |
2288 | struct flowi6 *fl6, |
2289 | const struct sk_buff *skb, | |
2290 | int flags) | |
72331bc0 SL |
2291 | { |
2292 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) | |
2293 | flags |= RT6_LOOKUP_F_IFACE; | |
2294 | ||
b75cc8f9 | 2295 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
72331bc0 | 2296 | } |
d409b847 | 2297 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
72331bc0 | 2298 | |
23aebdac | 2299 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
5e5d6fed RP |
2300 | struct flow_keys *keys, |
2301 | struct flow_keys *flkeys) | |
23aebdac JS |
2302 | { |
2303 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); | |
2304 | const struct ipv6hdr *key_iph = outer_iph; | |
5e5d6fed | 2305 | struct flow_keys *_flkeys = flkeys; |
23aebdac JS |
2306 | const struct ipv6hdr *inner_iph; |
2307 | const struct icmp6hdr *icmph; | |
2308 | struct ipv6hdr _inner_iph; | |
cea67a2d | 2309 | struct icmp6hdr _icmph; |
23aebdac JS |
2310 | |
2311 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) | |
2312 | goto out; | |
2313 | ||
cea67a2d ED |
2314 | icmph = skb_header_pointer(skb, skb_transport_offset(skb), |
2315 | sizeof(_icmph), &_icmph); | |
2316 | if (!icmph) | |
2317 | goto out; | |
2318 | ||
54074f1d | 2319 | if (!icmpv6_is_err(icmph->icmp6_type)) |
23aebdac JS |
2320 | goto out; |
2321 | ||
2322 | inner_iph = skb_header_pointer(skb, | |
2323 | skb_transport_offset(skb) + sizeof(*icmph), | |
2324 | sizeof(_inner_iph), &_inner_iph); | |
2325 | if (!inner_iph) | |
2326 | goto out; | |
2327 | ||
2328 | key_iph = inner_iph; | |
5e5d6fed | 2329 | _flkeys = NULL; |
23aebdac | 2330 | out: |
5e5d6fed RP |
2331 | if (_flkeys) { |
2332 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; | |
2333 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; | |
2334 | keys->tags.flow_label = _flkeys->tags.flow_label; | |
2335 | keys->basic.ip_proto = _flkeys->basic.ip_proto; | |
2336 | } else { | |
2337 | keys->addrs.v6addrs.src = key_iph->saddr; | |
2338 | keys->addrs.v6addrs.dst = key_iph->daddr; | |
fa1be7e0 | 2339 | keys->tags.flow_label = ip6_flowlabel(key_iph); |
5e5d6fed RP |
2340 | keys->basic.ip_proto = key_iph->nexthdr; |
2341 | } | |
23aebdac JS |
2342 | } |
2343 | ||
2344 | /* if skb is set it will be used and fl6 can be NULL */ | |
b4bac172 DA |
2345 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
2346 | const struct sk_buff *skb, struct flow_keys *flkeys) | |
23aebdac JS |
2347 | { |
2348 | struct flow_keys hash_keys; | |
9a2a537a | 2349 | u32 mhash; |
23aebdac | 2350 | |
bbfa047a | 2351 | switch (ip6_multipath_hash_policy(net)) { |
b4bac172 DA |
2352 | case 0: |
2353 | memset(&hash_keys, 0, sizeof(hash_keys)); | |
2354 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2355 | if (skb) { | |
2356 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); | |
2357 | } else { | |
2358 | hash_keys.addrs.v6addrs.src = fl6->saddr; | |
2359 | hash_keys.addrs.v6addrs.dst = fl6->daddr; | |
fa1be7e0 | 2360 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
b4bac172 DA |
2361 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
2362 | } | |
2363 | break; | |
2364 | case 1: | |
2365 | if (skb) { | |
2366 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; | |
2367 | struct flow_keys keys; | |
2368 | ||
2369 | /* short-circuit if we already have L4 hash present */ | |
2370 | if (skb->l4_hash) | |
2371 | return skb_get_hash_raw(skb) >> 1; | |
2372 | ||
2373 | memset(&hash_keys, 0, sizeof(hash_keys)); | |
2374 | ||
2375 | if (!flkeys) { | |
2376 | skb_flow_dissect_flow_keys(skb, &keys, flag); | |
2377 | flkeys = &keys; | |
2378 | } | |
2379 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2380 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; | |
2381 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; | |
2382 | hash_keys.ports.src = flkeys->ports.src; | |
2383 | hash_keys.ports.dst = flkeys->ports.dst; | |
2384 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; | |
2385 | } else { | |
2386 | memset(&hash_keys, 0, sizeof(hash_keys)); | |
2387 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2388 | hash_keys.addrs.v6addrs.src = fl6->saddr; | |
2389 | hash_keys.addrs.v6addrs.dst = fl6->daddr; | |
2390 | hash_keys.ports.src = fl6->fl6_sport; | |
2391 | hash_keys.ports.dst = fl6->fl6_dport; | |
2392 | hash_keys.basic.ip_proto = fl6->flowi6_proto; | |
2393 | } | |
2394 | break; | |
d8f74f09 SS |
2395 | case 2: |
2396 | memset(&hash_keys, 0, sizeof(hash_keys)); | |
2397 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2398 | if (skb) { | |
2399 | struct flow_keys keys; | |
2400 | ||
2401 | if (!flkeys) { | |
2402 | skb_flow_dissect_flow_keys(skb, &keys, 0); | |
2403 | flkeys = &keys; | |
2404 | } | |
2405 | ||
2406 | /* Inner can be v4 or v6 */ | |
2407 | if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { | |
2408 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
2409 | hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; | |
2410 | hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; | |
2411 | } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { | |
2412 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2413 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; | |
2414 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; | |
2415 | hash_keys.tags.flow_label = flkeys->tags.flow_label; | |
2416 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; | |
2417 | } else { | |
2418 | /* Same as case 0 */ | |
2419 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2420 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); | |
2421 | } | |
2422 | } else { | |
2423 | /* Same as case 0 */ | |
2424 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
2425 | hash_keys.addrs.v6addrs.src = fl6->saddr; | |
2426 | hash_keys.addrs.v6addrs.dst = fl6->daddr; | |
2427 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); | |
2428 | hash_keys.basic.ip_proto = fl6->flowi6_proto; | |
2429 | } | |
2430 | break; | |
23aebdac | 2431 | } |
9a2a537a | 2432 | mhash = flow_hash_from_keys(&hash_keys); |
23aebdac | 2433 | |
9a2a537a | 2434 | return mhash >> 1; |
23aebdac JS |
2435 | } |
2436 | ||
67f415dd | 2437 | /* Called with rcu held */ |
c71099ac TG |
2438 | void ip6_route_input(struct sk_buff *skb) |
2439 | { | |
b71d1d42 | 2440 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
c346dca1 | 2441 | struct net *net = dev_net(skb->dev); |
67f415dd | 2442 | int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; |
904af04d | 2443 | struct ip_tunnel_info *tun_info; |
4c9483b2 | 2444 | struct flowi6 fl6 = { |
e0d56fdd | 2445 | .flowi6_iif = skb->dev->ifindex, |
4c9483b2 DM |
2446 | .daddr = iph->daddr, |
2447 | .saddr = iph->saddr, | |
6502ca52 | 2448 | .flowlabel = ip6_flowinfo(iph), |
4c9483b2 DM |
2449 | .flowi6_mark = skb->mark, |
2450 | .flowi6_proto = iph->nexthdr, | |
c71099ac | 2451 | }; |
5e5d6fed | 2452 | struct flow_keys *flkeys = NULL, _flkeys; |
adaa70bb | 2453 | |
904af04d | 2454 | tun_info = skb_tunnel_info(skb); |
46fa062a | 2455 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
904af04d | 2456 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
5e5d6fed RP |
2457 | |
2458 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) | |
2459 | flkeys = &_flkeys; | |
2460 | ||
23aebdac | 2461 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
b4bac172 | 2462 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
06e9d040 | 2463 | skb_dst_drop(skb); |
67f415dd WW |
2464 | skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, |
2465 | &fl6, skb, flags)); | |
c71099ac TG |
2466 | } |
2467 | ||
b75cc8f9 DA |
2468 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
2469 | struct fib6_table *table, | |
2470 | struct flowi6 *fl6, | |
2471 | const struct sk_buff *skb, | |
2472 | int flags) | |
1da177e4 | 2473 | { |
b75cc8f9 | 2474 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
c71099ac TG |
2475 | } |
2476 | ||
7d9e5f42 WW |
2477 | struct dst_entry *ip6_route_output_flags_noref(struct net *net, |
2478 | const struct sock *sk, | |
2479 | struct flowi6 *fl6, int flags) | |
c71099ac | 2480 | { |
d46a9d67 | 2481 | bool any_src; |
c71099ac | 2482 | |
3ede0bbc RS |
2483 | if (ipv6_addr_type(&fl6->daddr) & |
2484 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { | |
4c1feac5 DA |
2485 | struct dst_entry *dst; |
2486 | ||
7d9e5f42 | 2487 | /* This function does not take refcnt on the dst */ |
4c1feac5 DA |
2488 | dst = l3mdev_link_scope_lookup(net, fl6); |
2489 | if (dst) | |
2490 | return dst; | |
2491 | } | |
ca254490 | 2492 | |
1fb9489b | 2493 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
4dc27d1c | 2494 | |
7d9e5f42 | 2495 | flags |= RT6_LOOKUP_F_DST_NOREF; |
d46a9d67 | 2496 | any_src = ipv6_addr_any(&fl6->saddr); |
741a11d9 | 2497 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
d46a9d67 | 2498 | (fl6->flowi6_oif && any_src)) |
77d16f45 | 2499 | flags |= RT6_LOOKUP_F_IFACE; |
c71099ac | 2500 | |
d46a9d67 | 2501 | if (!any_src) |
adaa70bb | 2502 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
0c9a2ac1 YH |
2503 | else if (sk) |
2504 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); | |
adaa70bb | 2505 | |
b75cc8f9 | 2506 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
1da177e4 | 2507 | } |
7d9e5f42 WW |
2508 | EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref); |
2509 | ||
2510 | struct dst_entry *ip6_route_output_flags(struct net *net, | |
2511 | const struct sock *sk, | |
2512 | struct flowi6 *fl6, | |
2513 | int flags) | |
2514 | { | |
2515 | struct dst_entry *dst; | |
2516 | struct rt6_info *rt6; | |
2517 | ||
2518 | rcu_read_lock(); | |
2519 | dst = ip6_route_output_flags_noref(net, sk, fl6, flags); | |
2520 | rt6 = (struct rt6_info *)dst; | |
2521 | /* For dst cached in uncached_list, refcnt is already taken. */ | |
2522 | if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) { | |
2523 | dst = &net->ipv6.ip6_null_entry->dst; | |
2524 | dst_hold(dst); | |
2525 | } | |
2526 | rcu_read_unlock(); | |
2527 | ||
2528 | return dst; | |
2529 | } | |
6f21c96a | 2530 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
1da177e4 | 2531 | |
2774c131 | 2532 | struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) |
14e50e57 | 2533 | { |
5c1e6aa3 | 2534 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
1dbe3252 | 2535 | struct net_device *loopback_dev = net->loopback_dev; |
14e50e57 DM |
2536 | struct dst_entry *new = NULL; |
2537 | ||
1dbe3252 | 2538 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
62cf27e5 | 2539 | DST_OBSOLETE_DEAD, 0); |
14e50e57 | 2540 | if (rt) { |
0a1f5962 | 2541 | rt6_info_init(rt); |
81eb8447 | 2542 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
8104891b | 2543 | |
0a1f5962 | 2544 | new = &rt->dst; |
14e50e57 | 2545 | new->__use = 1; |
352e512c | 2546 | new->input = dst_discard; |
ede2059d | 2547 | new->output = dst_discard_out; |
14e50e57 | 2548 | |
0a1f5962 | 2549 | dst_copy_metrics(new, &ort->dst); |
14e50e57 | 2550 | |
1dbe3252 | 2551 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
4e3fd7a0 | 2552 | rt->rt6i_gateway = ort->rt6i_gateway; |
0a1f5962 | 2553 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
14e50e57 DM |
2554 | |
2555 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); | |
2556 | #ifdef CONFIG_IPV6_SUBTREES | |
2557 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); | |
2558 | #endif | |
14e50e57 DM |
2559 | } |
2560 | ||
69ead7af DM |
2561 | dst_release(dst_orig); |
2562 | return new ? new : ERR_PTR(-ENOMEM); | |
14e50e57 | 2563 | } |
14e50e57 | 2564 | |
1da177e4 LT |
2565 | /* |
2566 | * Destination cache support functions | |
2567 | */ | |
2568 | ||
8d1c802b | 2569 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
4b32b5ad | 2570 | { |
93531c67 DA |
2571 | u32 rt_cookie = 0; |
2572 | ||
8ae86971 | 2573 | if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) |
93531c67 DA |
2574 | return false; |
2575 | ||
2576 | if (fib6_check_expired(f6i)) | |
2577 | return false; | |
2578 | ||
2579 | return true; | |
4b32b5ad MKL |
2580 | } |
2581 | ||
a68886a6 DA |
2582 | static struct dst_entry *rt6_check(struct rt6_info *rt, |
2583 | struct fib6_info *from, | |
2584 | u32 cookie) | |
3da59bd9 | 2585 | { |
36143645 | 2586 | u32 rt_cookie = 0; |
c5cff856 | 2587 | |
49d05fe2 | 2588 | if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || |
93531c67 | 2589 | rt_cookie != cookie) |
3da59bd9 MKL |
2590 | return NULL; |
2591 | ||
2592 | if (rt6_check_expired(rt)) | |
2593 | return NULL; | |
2594 | ||
2595 | return &rt->dst; | |
2596 | } | |
2597 | ||
a68886a6 DA |
2598 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, |
2599 | struct fib6_info *from, | |
2600 | u32 cookie) | |
3da59bd9 | 2601 | { |
5973fb1e MKL |
2602 | if (!__rt6_check_expired(rt) && |
2603 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && | |
a68886a6 | 2604 | fib6_check(from, cookie)) |
3da59bd9 MKL |
2605 | return &rt->dst; |
2606 | else | |
2607 | return NULL; | |
2608 | } | |
2609 | ||
1da177e4 LT |
2610 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
2611 | { | |
a87b7dc9 | 2612 | struct dst_entry *dst_ret; |
a68886a6 | 2613 | struct fib6_info *from; |
1da177e4 LT |
2614 | struct rt6_info *rt; |
2615 | ||
a87b7dc9 DA |
2616 | rt = container_of(dst, struct rt6_info, dst); |
2617 | ||
8f34e53b DA |
2618 | if (rt->sernum) |
2619 | return rt6_is_valid(rt) ? dst : NULL; | |
2620 | ||
a87b7dc9 | 2621 | rcu_read_lock(); |
1da177e4 | 2622 | |
6f3118b5 ND |
2623 | /* All IPV6 dsts are created with ->obsolete set to the value |
2624 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down | |
2625 | * into this function always. | |
2626 | */ | |
e3bc10bd | 2627 | |
a68886a6 | 2628 | from = rcu_dereference(rt->from); |
4b32b5ad | 2629 | |
a68886a6 DA |
2630 | if (from && (rt->rt6i_flags & RTF_PCPU || |
2631 | unlikely(!list_empty(&rt->rt6i_uncached)))) | |
2632 | dst_ret = rt6_dst_from_check(rt, from, cookie); | |
3da59bd9 | 2633 | else |
a68886a6 | 2634 | dst_ret = rt6_check(rt, from, cookie); |
a87b7dc9 DA |
2635 | |
2636 | rcu_read_unlock(); | |
2637 | ||
2638 | return dst_ret; | |
1da177e4 LT |
2639 | } |
2640 | ||
2641 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) | |
2642 | { | |
2643 | struct rt6_info *rt = (struct rt6_info *) dst; | |
2644 | ||
2645 | if (rt) { | |
54c1a859 | 2646 | if (rt->rt6i_flags & RTF_CACHE) { |
c3c14da0 | 2647 | rcu_read_lock(); |
54c1a859 | 2648 | if (rt6_check_expired(rt)) { |
93531c67 | 2649 | rt6_remove_exception_rt(rt); |
54c1a859 YH |
2650 | dst = NULL; |
2651 | } | |
c3c14da0 | 2652 | rcu_read_unlock(); |
54c1a859 | 2653 | } else { |
1da177e4 | 2654 | dst_release(dst); |
54c1a859 YH |
2655 | dst = NULL; |
2656 | } | |
1da177e4 | 2657 | } |
54c1a859 | 2658 | return dst; |
1da177e4 LT |
2659 | } |
2660 | ||
2661 | static void ip6_link_failure(struct sk_buff *skb) | |
2662 | { | |
2663 | struct rt6_info *rt; | |
2664 | ||
3ffe533c | 2665 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
1da177e4 | 2666 | |
adf30907 | 2667 | rt = (struct rt6_info *) skb_dst(skb); |
1da177e4 | 2668 | if (rt) { |
8a14e46f | 2669 | rcu_read_lock(); |
1eb4f758 | 2670 | if (rt->rt6i_flags & RTF_CACHE) { |
761f6026 | 2671 | rt6_remove_exception_rt(rt); |
c5cff856 | 2672 | } else { |
a68886a6 | 2673 | struct fib6_info *from; |
c5cff856 WW |
2674 | struct fib6_node *fn; |
2675 | ||
a68886a6 DA |
2676 | from = rcu_dereference(rt->from); |
2677 | if (from) { | |
2678 | fn = rcu_dereference(from->fib6_node); | |
2679 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) | |
2680 | fn->fn_sernum = -1; | |
2681 | } | |
1eb4f758 | 2682 | } |
8a14e46f | 2683 | rcu_read_unlock(); |
1da177e4 LT |
2684 | } |
2685 | } | |
2686 | ||
6a3e030f DA |
2687 | static void rt6_update_expires(struct rt6_info *rt0, int timeout) |
2688 | { | |
a68886a6 DA |
2689 | if (!(rt0->rt6i_flags & RTF_EXPIRES)) { |
2690 | struct fib6_info *from; | |
2691 | ||
2692 | rcu_read_lock(); | |
2693 | from = rcu_dereference(rt0->from); | |
2694 | if (from) | |
2695 | rt0->dst.expires = from->expires; | |
2696 | rcu_read_unlock(); | |
2697 | } | |
6a3e030f DA |
2698 | |
2699 | dst_set_expires(&rt0->dst, timeout); | |
2700 | rt0->rt6i_flags |= RTF_EXPIRES; | |
2701 | } | |
2702 | ||
45e4fd26 MKL |
2703 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
2704 | { | |
2705 | struct net *net = dev_net(rt->dst.dev); | |
2706 | ||
d4ead6b3 | 2707 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
45e4fd26 | 2708 | rt->rt6i_flags |= RTF_MODIFIED; |
45e4fd26 MKL |
2709 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
2710 | } | |
2711 | ||
0d3f6d29 MKL |
2712 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
2713 | { | |
2714 | return !(rt->rt6i_flags & RTF_CACHE) && | |
1490ed2a | 2715 | (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); |
0d3f6d29 MKL |
2716 | } |
2717 | ||
45e4fd26 | 2718 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
bd085ef6 HL |
2719 | const struct ipv6hdr *iph, u32 mtu, |
2720 | bool confirm_neigh) | |
1da177e4 | 2721 | { |
0dec879f | 2722 | const struct in6_addr *daddr, *saddr; |
67ba4152 | 2723 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
1da177e4 | 2724 | |
09454fd0 MŻ |
2725 | /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU) |
2726 | * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it. | |
2727 | * [see also comment in rt6_mtu_change_route()] | |
2728 | */ | |
19bda36c | 2729 | |
0dec879f JA |
2730 | if (iph) { |
2731 | daddr = &iph->daddr; | |
2732 | saddr = &iph->saddr; | |
2733 | } else if (sk) { | |
2734 | daddr = &sk->sk_v6_daddr; | |
2735 | saddr = &inet6_sk(sk)->saddr; | |
2736 | } else { | |
2737 | daddr = NULL; | |
2738 | saddr = NULL; | |
2739 | } | |
bd085ef6 HL |
2740 | |
2741 | if (confirm_neigh) | |
2742 | dst_confirm_neigh(dst, daddr); | |
2743 | ||
45e4fd26 MKL |
2744 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
2745 | if (mtu >= dst_mtu(dst)) | |
2746 | return; | |
9d289715 | 2747 | |
0d3f6d29 | 2748 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
45e4fd26 | 2749 | rt6_do_update_pmtu(rt6, mtu); |
2b760fcf WW |
2750 | /* update rt6_ex->stamp for cache */ |
2751 | if (rt6->rt6i_flags & RTF_CACHE) | |
2752 | rt6_update_exception_stamp_rt(rt6); | |
0dec879f | 2753 | } else if (daddr) { |
85bd05de | 2754 | struct fib6_result res = {}; |
45e4fd26 MKL |
2755 | struct rt6_info *nrt6; |
2756 | ||
4d85cd0c | 2757 | rcu_read_lock(); |
85bd05de | 2758 | res.f6i = rcu_dereference(rt6->from); |
43a4b60d DA |
2759 | if (!res.f6i) |
2760 | goto out_unlock; | |
2761 | ||
7d21fec9 DA |
2762 | res.fib6_flags = res.f6i->fib6_flags; |
2763 | res.fib6_type = res.f6i->fib6_type; | |
2764 | ||
2d44234b DA |
2765 | if (res.f6i->nh) { |
2766 | struct fib6_nh_match_arg arg = { | |
2767 | .dev = dst->dev, | |
2768 | .gw = &rt6->rt6i_gateway, | |
2769 | }; | |
2770 | ||
2771 | nexthop_for_each_fib6_nh(res.f6i->nh, | |
2772 | fib6_nh_find_match, &arg); | |
2773 | ||
2774 | /* fib6_info uses a nexthop that does not have fib6_nh | |
2775 | * using the dst->dev + gw. Should be impossible. | |
2776 | */ | |
43a4b60d DA |
2777 | if (!arg.match) |
2778 | goto out_unlock; | |
2d44234b DA |
2779 | |
2780 | res.nh = arg.match; | |
2781 | } else { | |
2782 | res.nh = res.f6i->fib6_nh; | |
2783 | } | |
2784 | ||
85bd05de | 2785 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
45e4fd26 MKL |
2786 | if (nrt6) { |
2787 | rt6_do_update_pmtu(nrt6, mtu); | |
5012f0a5 | 2788 | if (rt6_insert_exception(nrt6, &res)) |
2b760fcf | 2789 | dst_release_immediate(&nrt6->dst); |
45e4fd26 | 2790 | } |
43a4b60d | 2791 | out_unlock: |
a68886a6 | 2792 | rcu_read_unlock(); |
1da177e4 LT |
2793 | } |
2794 | } | |
2795 | ||
45e4fd26 | 2796 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
bd085ef6 HL |
2797 | struct sk_buff *skb, u32 mtu, |
2798 | bool confirm_neigh) | |
45e4fd26 | 2799 | { |
bd085ef6 HL |
2800 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, |
2801 | confirm_neigh); | |
45e4fd26 MKL |
2802 | } |
2803 | ||
42ae66c8 | 2804 | void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, |
e2d118a1 | 2805 | int oif, u32 mark, kuid_t uid) |
81aded24 DM |
2806 | { |
2807 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
2808 | struct dst_entry *dst; | |
dc92095d MŻ |
2809 | struct flowi6 fl6 = { |
2810 | .flowi6_oif = oif, | |
2811 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), | |
2812 | .daddr = iph->daddr, | |
2813 | .saddr = iph->saddr, | |
2814 | .flowlabel = ip6_flowinfo(iph), | |
2815 | .flowi6_uid = uid, | |
2816 | }; | |
81aded24 DM |
2817 | |
2818 | dst = ip6_route_output(net, NULL, &fl6); | |
2819 | if (!dst->error) | |
bd085ef6 | 2820 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); |
81aded24 DM |
2821 | dst_release(dst); |
2822 | } | |
2823 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); | |
2824 | ||
2825 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) | |
2826 | { | |
7ddacfa5 | 2827 | int oif = sk->sk_bound_dev_if; |
33c162a9 MKL |
2828 | struct dst_entry *dst; |
2829 | ||
7ddacfa5 DA |
2830 | if (!oif && skb->dev) |
2831 | oif = l3mdev_master_ifindex(skb->dev); | |
2832 | ||
2833 | ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); | |
33c162a9 MKL |
2834 | |
2835 | dst = __sk_dst_get(sk); | |
2836 | if (!dst || !dst->obsolete || | |
2837 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) | |
2838 | return; | |
2839 | ||
2840 | bh_lock_sock(sk); | |
2841 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) | |
2842 | ip6_datagram_dst_update(sk, false); | |
2843 | bh_unlock_sock(sk); | |
81aded24 DM |
2844 | } |
2845 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); | |
2846 | ||
7d6850f7 AK |
2847 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
2848 | const struct flowi6 *fl6) | |
2849 | { | |
2850 | #ifdef CONFIG_IPV6_SUBTREES | |
2851 | struct ipv6_pinfo *np = inet6_sk(sk); | |
2852 | #endif | |
2853 | ||
2854 | ip6_dst_store(sk, dst, | |
2855 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? | |
2856 | &sk->sk_v6_daddr : NULL, | |
2857 | #ifdef CONFIG_IPV6_SUBTREES | |
2858 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? | |
2859 | &np->saddr : | |
2860 | #endif | |
2861 | NULL); | |
2862 | } | |
2863 | ||
9b6b35ab | 2864 | static bool ip6_redirect_nh_match(const struct fib6_result *res, |
0b34eb00 DA |
2865 | struct flowi6 *fl6, |
2866 | const struct in6_addr *gw, | |
2867 | struct rt6_info **ret) | |
2868 | { | |
9b6b35ab DA |
2869 | const struct fib6_nh *nh = res->nh; |
2870 | ||
0b34eb00 DA |
2871 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
2872 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) | |
2873 | return false; | |
2874 | ||
2875 | /* rt_cache's gateway might be different from its 'parent' | |
2876 | * in the case of an ip redirect. | |
2877 | * So we keep searching in the exception table if the gateway | |
2878 | * is different. | |
2879 | */ | |
2880 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { | |
2881 | struct rt6_info *rt_cache; | |
2882 | ||
9b6b35ab | 2883 | rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); |
0b34eb00 DA |
2884 | if (rt_cache && |
2885 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { | |
2886 | *ret = rt_cache; | |
2887 | return true; | |
2888 | } | |
2889 | return false; | |
2890 | } | |
2891 | return true; | |
2892 | } | |
2893 | ||
c55c8988 DA |
2894 | struct fib6_nh_rd_arg { |
2895 | struct fib6_result *res; | |
2896 | struct flowi6 *fl6; | |
2897 | const struct in6_addr *gw; | |
2898 | struct rt6_info **ret; | |
2899 | }; | |
2900 | ||
2901 | static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) | |
2902 | { | |
2903 | struct fib6_nh_rd_arg *arg = _arg; | |
2904 | ||
2905 | arg->res->nh = nh; | |
2906 | return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); | |
2907 | } | |
2908 | ||
b55b76b2 DJ |
2909 | /* Handle redirects */ |
2910 | struct ip6rd_flowi { | |
2911 | struct flowi6 fl6; | |
2912 | struct in6_addr gateway; | |
2913 | }; | |
2914 | ||
2915 | static struct rt6_info *__ip6_route_redirect(struct net *net, | |
2916 | struct fib6_table *table, | |
2917 | struct flowi6 *fl6, | |
b75cc8f9 | 2918 | const struct sk_buff *skb, |
b55b76b2 DJ |
2919 | int flags) |
2920 | { | |
2921 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; | |
0b34eb00 | 2922 | struct rt6_info *ret = NULL; |
9b6b35ab | 2923 | struct fib6_result res = {}; |
c55c8988 DA |
2924 | struct fib6_nh_rd_arg arg = { |
2925 | .res = &res, | |
2926 | .fl6 = fl6, | |
2927 | .gw = &rdfl->gateway, | |
2928 | .ret = &ret | |
2929 | }; | |
8d1c802b | 2930 | struct fib6_info *rt; |
b55b76b2 DJ |
2931 | struct fib6_node *fn; |
2932 | ||
31680ac2 DA |
2933 | /* l3mdev_update_flow overrides oif if the device is enslaved; in |
2934 | * this case we must match on the real ingress device, so reset it | |
2935 | */ | |
2936 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) | |
2937 | fl6->flowi6_oif = skb->dev->ifindex; | |
2938 | ||
b55b76b2 | 2939 | /* Get the "current" route for this destination and |
67c408cf | 2940 | * check if the redirect has come from appropriate router. |
b55b76b2 DJ |
2941 | * |
2942 | * RFC 4861 specifies that redirects should only be | |
2943 | * accepted if they come from the nexthop to the target. | |
2944 | * Due to the way the routes are chosen, this notion | |
2945 | * is a bit fuzzy and one might need to check all possible | |
2946 | * routes. | |
2947 | */ | |
2948 | ||
66f5d6ce | 2949 | rcu_read_lock(); |
6454743b | 2950 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
b55b76b2 | 2951 | restart: |
66f5d6ce | 2952 | for_each_fib6_node_rt_rcu(fn) { |
9b6b35ab | 2953 | res.f6i = rt; |
14895687 | 2954 | if (fib6_check_expired(rt)) |
b55b76b2 | 2955 | continue; |
93c2fb25 | 2956 | if (rt->fib6_flags & RTF_REJECT) |
b55b76b2 | 2957 | break; |
c55c8988 DA |
2958 | if (unlikely(rt->nh)) { |
2959 | if (nexthop_is_blackhole(rt->nh)) | |
2960 | continue; | |
2961 | /* on match, res->nh is filled in and potentially ret */ | |
2962 | if (nexthop_for_each_fib6_nh(rt->nh, | |
2963 | fib6_nh_redirect_match, | |
2964 | &arg)) | |
2965 | goto out; | |
2966 | } else { | |
2967 | res.nh = rt->fib6_nh; | |
2968 | if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, | |
2969 | &ret)) | |
2970 | goto out; | |
2971 | } | |
b55b76b2 DJ |
2972 | } |
2973 | ||
2974 | if (!rt) | |
421842ed | 2975 | rt = net->ipv6.fib6_null_entry; |
93c2fb25 | 2976 | else if (rt->fib6_flags & RTF_REJECT) { |
23fb93a4 | 2977 | ret = net->ipv6.ip6_null_entry; |
b0a1ba59 MKL |
2978 | goto out; |
2979 | } | |
2980 | ||
421842ed | 2981 | if (rt == net->ipv6.fib6_null_entry) { |
a3c00e46 MKL |
2982 | fn = fib6_backtrack(fn, &fl6->saddr); |
2983 | if (fn) | |
2984 | goto restart; | |
b55b76b2 | 2985 | } |
a3c00e46 | 2986 | |
9b6b35ab | 2987 | res.f6i = rt; |
1cf844c7 | 2988 | res.nh = rt->fib6_nh; |
b0a1ba59 | 2989 | out: |
7d21fec9 | 2990 | if (ret) { |
10585b43 | 2991 | ip6_hold_safe(net, &ret); |
7d21fec9 DA |
2992 | } else { |
2993 | res.fib6_flags = res.f6i->fib6_flags; | |
2994 | res.fib6_type = res.f6i->fib6_type; | |
9b6b35ab | 2995 | ret = ip6_create_rt_rcu(&res); |
7d21fec9 | 2996 | } |
b55b76b2 | 2997 | |
66f5d6ce | 2998 | rcu_read_unlock(); |
b55b76b2 | 2999 | |
8ff2e5b2 | 3000 | trace_fib6_table_lookup(net, &res, table, fl6); |
23fb93a4 | 3001 | return ret; |
b55b76b2 DJ |
3002 | }; |
3003 | ||
3004 | static struct dst_entry *ip6_route_redirect(struct net *net, | |
b75cc8f9 DA |
3005 | const struct flowi6 *fl6, |
3006 | const struct sk_buff *skb, | |
3007 | const struct in6_addr *gateway) | |
b55b76b2 DJ |
3008 | { |
3009 | int flags = RT6_LOOKUP_F_HAS_SADDR; | |
3010 | struct ip6rd_flowi rdfl; | |
3011 | ||
3012 | rdfl.fl6 = *fl6; | |
3013 | rdfl.gateway = *gateway; | |
3014 | ||
b75cc8f9 | 3015 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
b55b76b2 DJ |
3016 | flags, __ip6_route_redirect); |
3017 | } | |
3018 | ||
e2d118a1 LC |
3019 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
3020 | kuid_t uid) | |
3a5ad2ee DM |
3021 | { |
3022 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
3023 | struct dst_entry *dst; | |
1f7f10ac MŻ |
3024 | struct flowi6 fl6 = { |
3025 | .flowi6_iif = LOOPBACK_IFINDEX, | |
3026 | .flowi6_oif = oif, | |
3027 | .flowi6_mark = mark, | |
3028 | .daddr = iph->daddr, | |
3029 | .saddr = iph->saddr, | |
3030 | .flowlabel = ip6_flowinfo(iph), | |
3031 | .flowi6_uid = uid, | |
3032 | }; | |
3a5ad2ee | 3033 | |
b75cc8f9 | 3034 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
b55b76b2 | 3035 | rt6_do_redirect(dst, NULL, skb); |
3a5ad2ee DM |
3036 | dst_release(dst); |
3037 | } | |
3038 | EXPORT_SYMBOL_GPL(ip6_redirect); | |
3039 | ||
d456336d | 3040 | void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) |
c92a59ec DJ |
3041 | { |
3042 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
3043 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); | |
3044 | struct dst_entry *dst; | |
0b26fb17 MŻ |
3045 | struct flowi6 fl6 = { |
3046 | .flowi6_iif = LOOPBACK_IFINDEX, | |
3047 | .flowi6_oif = oif, | |
0b26fb17 MŻ |
3048 | .daddr = msg->dest, |
3049 | .saddr = iph->daddr, | |
3050 | .flowi6_uid = sock_net_uid(net, NULL), | |
3051 | }; | |
c92a59ec | 3052 | |
b75cc8f9 | 3053 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
b55b76b2 | 3054 | rt6_do_redirect(dst, NULL, skb); |
c92a59ec DJ |
3055 | dst_release(dst); |
3056 | } | |
3057 | ||
3a5ad2ee DM |
3058 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
3059 | { | |
e2d118a1 LC |
3060 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
3061 | sk->sk_uid); | |
3a5ad2ee DM |
3062 | } |
3063 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); | |
3064 | ||
0dbaee3b | 3065 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
1da177e4 | 3066 | { |
0dbaee3b DM |
3067 | struct net_device *dev = dst->dev; |
3068 | unsigned int mtu = dst_mtu(dst); | |
3069 | struct net *net = dev_net(dev); | |
3070 | ||
1da177e4 LT |
3071 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
3072 | ||
5578689a DL |
3073 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
3074 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; | |
1da177e4 LT |
3075 | |
3076 | /* | |
1ab1457c YH |
3077 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
3078 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. | |
3079 | * IPV6_MAXPLEN is also valid and means: "any MSS, | |
1da177e4 LT |
3080 | * rely only on pmtu discovery" |
3081 | */ | |
3082 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) | |
3083 | mtu = IPV6_MAXPLEN; | |
3084 | return mtu; | |
3085 | } | |
3086 | ||
ebb762f2 | 3087 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
d33e4553 | 3088 | { |
d33e4553 | 3089 | struct inet6_dev *idev; |
d4ead6b3 | 3090 | unsigned int mtu; |
4b32b5ad MKL |
3091 | |
3092 | mtu = dst_metric_raw(dst, RTAX_MTU); | |
618f9bc7 | 3093 | if (mtu) |
30f78d8e | 3094 | goto out; |
618f9bc7 SK |
3095 | |
3096 | mtu = IPV6_MIN_MTU; | |
d33e4553 DM |
3097 | |
3098 | rcu_read_lock(); | |
3099 | idev = __in6_dev_get(dst->dev); | |
3100 | if (idev) | |
3101 | mtu = idev->cnf.mtu6; | |
3102 | rcu_read_unlock(); | |
3103 | ||
30f78d8e | 3104 | out: |
14972cbd RP |
3105 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
3106 | ||
3107 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); | |
d33e4553 DM |
3108 | } |
3109 | ||
901731b8 DA |
3110 | /* MTU selection: |
3111 | * 1. mtu on route is locked - use it | |
3112 | * 2. mtu from nexthop exception | |
3113 | * 3. mtu from egress device | |
3114 | * | |
3115 | * based on ip6_dst_mtu_forward and exception logic of | |
3116 | * rt6_find_cached_rt; called with rcu_read_lock | |
3117 | */ | |
b748f260 DA |
3118 | u32 ip6_mtu_from_fib6(const struct fib6_result *res, |
3119 | const struct in6_addr *daddr, | |
3120 | const struct in6_addr *saddr) | |
901731b8 | 3121 | { |
b748f260 DA |
3122 | const struct fib6_nh *nh = res->nh; |
3123 | struct fib6_info *f6i = res->f6i; | |
901731b8 | 3124 | struct inet6_dev *idev; |
510e2ced | 3125 | struct rt6_info *rt; |
901731b8 DA |
3126 | u32 mtu = 0; |
3127 | ||
3128 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { | |
3129 | mtu = f6i->fib6_pmtu; | |
3130 | if (mtu) | |
3131 | goto out; | |
3132 | } | |
3133 | ||
510e2ced WW |
3134 | rt = rt6_find_cached_rt(res, daddr, saddr); |
3135 | if (unlikely(rt)) { | |
3136 | mtu = dst_metric_raw(&rt->dst, RTAX_MTU); | |
3137 | } else { | |
b748f260 | 3138 | struct net_device *dev = nh->fib_nh_dev; |
901731b8 DA |
3139 | |
3140 | mtu = IPV6_MIN_MTU; | |
3141 | idev = __in6_dev_get(dev); | |
3142 | if (idev && idev->cnf.mtu6 > mtu) | |
3143 | mtu = idev->cnf.mtu6; | |
3144 | } | |
3145 | ||
3146 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); | |
3147 | out: | |
b748f260 | 3148 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
901731b8 DA |
3149 | } |
3150 | ||
3b00944c | 3151 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
87a11578 | 3152 | struct flowi6 *fl6) |
1da177e4 | 3153 | { |
87a11578 | 3154 | struct dst_entry *dst; |
1da177e4 LT |
3155 | struct rt6_info *rt; |
3156 | struct inet6_dev *idev = in6_dev_get(dev); | |
c346dca1 | 3157 | struct net *net = dev_net(dev); |
1da177e4 | 3158 | |
38308473 | 3159 | if (unlikely(!idev)) |
122bdf67 | 3160 | return ERR_PTR(-ENODEV); |
1da177e4 | 3161 | |
ad706862 | 3162 | rt = ip6_dst_alloc(net, dev, 0); |
38308473 | 3163 | if (unlikely(!rt)) { |
1da177e4 | 3164 | in6_dev_put(idev); |
87a11578 | 3165 | dst = ERR_PTR(-ENOMEM); |
1da177e4 LT |
3166 | goto out; |
3167 | } | |
3168 | ||
588753f1 | 3169 | rt->dst.input = ip6_input; |
8e2ec639 | 3170 | rt->dst.output = ip6_output; |
550bab42 | 3171 | rt->rt6i_gateway = fl6->daddr; |
87a11578 | 3172 | rt->rt6i_dst.addr = fl6->daddr; |
8e2ec639 YZ |
3173 | rt->rt6i_dst.plen = 128; |
3174 | rt->rt6i_idev = idev; | |
14edd87d | 3175 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
1da177e4 | 3176 | |
4c981e28 | 3177 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
587fea74 WW |
3178 | * do proper release of the net_device |
3179 | */ | |
3180 | rt6_uncached_list_add(rt); | |
81eb8447 | 3181 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
1da177e4 | 3182 | |
87a11578 DM |
3183 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
3184 | ||
1da177e4 | 3185 | out: |
87a11578 | 3186 | return dst; |
1da177e4 LT |
3187 | } |
3188 | ||
569d3645 | 3189 | static int ip6_dst_gc(struct dst_ops *ops) |
1da177e4 | 3190 | { |
86393e52 | 3191 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
7019b78e DL |
3192 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
3193 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; | |
3194 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
3195 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; | |
3196 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; | |
fc66f95c | 3197 | int entries; |
7019b78e | 3198 | |
fc66f95c | 3199 | entries = dst_entries_get_fast(ops); |
49a18d86 | 3200 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
fc66f95c | 3201 | entries <= rt_max_size) |
1da177e4 LT |
3202 | goto out; |
3203 | ||
6891a346 | 3204 | net->ipv6.ip6_rt_gc_expire++; |
14956643 | 3205 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
fc66f95c ED |
3206 | entries = dst_entries_get_slow(ops); |
3207 | if (entries < ops->gc_thresh) | |
7019b78e | 3208 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
1da177e4 | 3209 | out: |
7019b78e | 3210 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; |
fc66f95c | 3211 | return entries > rt_max_size; |
1da177e4 LT |
3212 | } |
3213 | ||
b2c709cc DA |
3214 | static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, |
3215 | const struct in6_addr *gw_addr, u32 tbid, | |
3216 | int flags, struct fib6_result *res) | |
8c14586f DA |
3217 | { |
3218 | struct flowi6 fl6 = { | |
3219 | .flowi6_oif = cfg->fc_ifindex, | |
3220 | .daddr = *gw_addr, | |
3221 | .saddr = cfg->fc_prefsrc, | |
3222 | }; | |
3223 | struct fib6_table *table; | |
b2c709cc | 3224 | int err; |
8c14586f | 3225 | |
f4797b33 | 3226 | table = fib6_get_table(net, tbid); |
8c14586f | 3227 | if (!table) |
b2c709cc | 3228 | return -EINVAL; |
8c14586f DA |
3229 | |
3230 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) | |
3231 | flags |= RT6_LOOKUP_F_HAS_SADDR; | |
3232 | ||
f4797b33 | 3233 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
8c14586f | 3234 | |
b2c709cc DA |
3235 | err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); |
3236 | if (!err && res->f6i != net->ipv6.fib6_null_entry) | |
3237 | fib6_select_path(net, res, &fl6, cfg->fc_ifindex, | |
3238 | cfg->fc_ifindex != 0, NULL, flags); | |
8c14586f | 3239 | |
b2c709cc | 3240 | return err; |
8c14586f DA |
3241 | } |
3242 | ||
fc1e64e1 DA |
3243 | static int ip6_route_check_nh_onlink(struct net *net, |
3244 | struct fib6_config *cfg, | |
9fbb704c | 3245 | const struct net_device *dev, |
fc1e64e1 DA |
3246 | struct netlink_ext_ack *extack) |
3247 | { | |
b2c709cc | 3248 | u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; |
fc1e64e1 | 3249 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
b2c709cc | 3250 | struct fib6_result res = {}; |
fc1e64e1 DA |
3251 | int err; |
3252 | ||
b2c709cc DA |
3253 | err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); |
3254 | if (!err && !(res.fib6_flags & RTF_REJECT) && | |
3255 | /* ignore match if it is the default route */ | |
3256 | !ipv6_addr_any(&res.f6i->fib6_dst.addr) && | |
3257 | (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { | |
3258 | NL_SET_ERR_MSG(extack, | |
3259 | "Nexthop has invalid gateway or device mismatch"); | |
3260 | err = -EINVAL; | |
fc1e64e1 DA |
3261 | } |
3262 | ||
3263 | return err; | |
3264 | } | |
3265 | ||
1edce99f DA |
3266 | static int ip6_route_check_nh(struct net *net, |
3267 | struct fib6_config *cfg, | |
3268 | struct net_device **_dev, | |
3269 | struct inet6_dev **idev) | |
3270 | { | |
3271 | const struct in6_addr *gw_addr = &cfg->fc_gateway; | |
3272 | struct net_device *dev = _dev ? *_dev : NULL; | |
b2c709cc DA |
3273 | int flags = RT6_LOOKUP_F_IFACE; |
3274 | struct fib6_result res = {}; | |
1edce99f DA |
3275 | int err = -EHOSTUNREACH; |
3276 | ||
3277 | if (cfg->fc_table) { | |
b2c709cc DA |
3278 | err = ip6_nh_lookup_table(net, cfg, gw_addr, |
3279 | cfg->fc_table, flags, &res); | |
3280 | /* gw_addr can not require a gateway or resolve to a reject | |
3281 | * route. If a device is given, it must match the result. | |
3282 | */ | |
3283 | if (err || res.fib6_flags & RTF_REJECT || | |
3284 | res.nh->fib_nh_gw_family || | |
3285 | (dev && dev != res.nh->fib_nh_dev)) | |
3286 | err = -EHOSTUNREACH; | |
1edce99f DA |
3287 | } |
3288 | ||
b2c709cc DA |
3289 | if (err < 0) { |
3290 | struct flowi6 fl6 = { | |
3291 | .flowi6_oif = cfg->fc_ifindex, | |
3292 | .daddr = *gw_addr, | |
3293 | }; | |
1edce99f | 3294 | |
b2c709cc DA |
3295 | err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); |
3296 | if (err || res.fib6_flags & RTF_REJECT || | |
3297 | res.nh->fib_nh_gw_family) | |
3298 | err = -EHOSTUNREACH; | |
3299 | ||
3300 | if (err) | |
3301 | return err; | |
3302 | ||
3303 | fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, | |
3304 | cfg->fc_ifindex != 0, NULL, flags); | |
3305 | } | |
1edce99f | 3306 | |
b2c709cc | 3307 | err = 0; |
1edce99f | 3308 | if (dev) { |
b2c709cc DA |
3309 | if (dev != res.nh->fib_nh_dev) |
3310 | err = -EHOSTUNREACH; | |
1edce99f | 3311 | } else { |
b2c709cc | 3312 | *_dev = dev = res.nh->fib_nh_dev; |
1edce99f | 3313 | dev_hold(dev); |
b2c709cc | 3314 | *idev = in6_dev_get(dev); |
1edce99f DA |
3315 | } |
3316 | ||
1edce99f DA |
3317 | return err; |
3318 | } | |
3319 | ||
9fbb704c DA |
3320 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
3321 | struct net_device **_dev, struct inet6_dev **idev, | |
3322 | struct netlink_ext_ack *extack) | |
3323 | { | |
3324 | const struct in6_addr *gw_addr = &cfg->fc_gateway; | |
3325 | int gwa_type = ipv6_addr_type(gw_addr); | |
232378e8 | 3326 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
9fbb704c | 3327 | const struct net_device *dev = *_dev; |
232378e8 | 3328 | bool need_addr_check = !dev; |
9fbb704c DA |
3329 | int err = -EINVAL; |
3330 | ||
3331 | /* if gw_addr is local we will fail to detect this in case | |
3332 | * address is still TENTATIVE (DAD in progress). rt6_lookup() | |
3333 | * will return already-added prefix route via interface that | |
3334 | * prefix route was assigned to, which might be non-loopback. | |
3335 | */ | |
232378e8 DA |
3336 | if (dev && |
3337 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { | |
3338 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); | |
9fbb704c DA |
3339 | goto out; |
3340 | } | |
3341 | ||
3342 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { | |
3343 | /* IPv6 strictly inhibits using not link-local | |
3344 | * addresses as nexthop address. | |
3345 | * Otherwise, router will not able to send redirects. | |
3346 | * It is very good, but in some (rare!) circumstances | |
3347 | * (SIT, PtP, NBMA NOARP links) it is handy to allow | |
3348 | * some exceptions. --ANK | |
3349 | * We allow IPv4-mapped nexthops to support RFC4798-type | |
3350 | * addressing | |
3351 | */ | |
3352 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { | |
3353 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); | |
3354 | goto out; | |
3355 | } | |
3356 | ||
b2c709cc DA |
3357 | rcu_read_lock(); |
3358 | ||
9fbb704c DA |
3359 | if (cfg->fc_flags & RTNH_F_ONLINK) |
3360 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); | |
3361 | else | |
3362 | err = ip6_route_check_nh(net, cfg, _dev, idev); | |
3363 | ||
b2c709cc DA |
3364 | rcu_read_unlock(); |
3365 | ||
9fbb704c DA |
3366 | if (err) |
3367 | goto out; | |
3368 | } | |
3369 | ||
3370 | /* reload in case device was changed */ | |
3371 | dev = *_dev; | |
3372 | ||
3373 | err = -EINVAL; | |
3374 | if (!dev) { | |
3375 | NL_SET_ERR_MSG(extack, "Egress device not specified"); | |
3376 | goto out; | |
3377 | } else if (dev->flags & IFF_LOOPBACK) { | |
3378 | NL_SET_ERR_MSG(extack, | |
3379 | "Egress device can not be loopback device for this route"); | |
3380 | goto out; | |
3381 | } | |
232378e8 DA |
3382 | |
3383 | /* if we did not check gw_addr above, do so now that the | |
3384 | * egress device has been resolved. | |
3385 | */ | |
3386 | if (need_addr_check && | |
3387 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { | |
3388 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); | |
3389 | goto out; | |
3390 | } | |
3391 | ||
9fbb704c DA |
3392 | err = 0; |
3393 | out: | |
3394 | return err; | |
3395 | } | |
3396 | ||
83c44251 DA |
3397 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
3398 | { | |
3399 | if ((flags & RTF_REJECT) || | |
3400 | (dev && (dev->flags & IFF_LOOPBACK) && | |
3401 | !(addr_type & IPV6_ADDR_LOOPBACK) && | |
3402 | !(flags & RTF_LOCAL))) | |
3403 | return true; | |
3404 | ||
3405 | return false; | |
3406 | } | |
3407 | ||
3408 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, | |
3409 | struct fib6_config *cfg, gfp_t gfp_flags, | |
3410 | struct netlink_ext_ack *extack) | |
3411 | { | |
3412 | struct net_device *dev = NULL; | |
3413 | struct inet6_dev *idev = NULL; | |
3414 | int addr_type; | |
3415 | int err; | |
3416 | ||
f1741730 | 3417 | fib6_nh->fib_nh_family = AF_INET6; |
1bef4c22 ED |
3418 | #ifdef CONFIG_IPV6_ROUTER_PREF |
3419 | fib6_nh->last_probe = jiffies; | |
3420 | #endif | |
f1741730 | 3421 | |
83c44251 DA |
3422 | err = -ENODEV; |
3423 | if (cfg->fc_ifindex) { | |
3424 | dev = dev_get_by_index(net, cfg->fc_ifindex); | |
3425 | if (!dev) | |
3426 | goto out; | |
3427 | idev = in6_dev_get(dev); | |
3428 | if (!idev) | |
3429 | goto out; | |
3430 | } | |
3431 | ||
3432 | if (cfg->fc_flags & RTNH_F_ONLINK) { | |
3433 | if (!dev) { | |
3434 | NL_SET_ERR_MSG(extack, | |
3435 | "Nexthop device required for onlink"); | |
3436 | goto out; | |
3437 | } | |
3438 | ||
3439 | if (!(dev->flags & IFF_UP)) { | |
3440 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); | |
3441 | err = -ENETDOWN; | |
3442 | goto out; | |
3443 | } | |
3444 | ||
ad1601ae | 3445 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
83c44251 DA |
3446 | } |
3447 | ||
ad1601ae | 3448 | fib6_nh->fib_nh_weight = 1; |
83c44251 DA |
3449 | |
3450 | /* We cannot add true routes via loopback here, | |
3451 | * they would result in kernel looping; promote them to reject routes | |
3452 | */ | |
3453 | addr_type = ipv6_addr_type(&cfg->fc_dst); | |
3454 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { | |
3455 | /* hold loopback dev/idev if we haven't done so. */ | |
3456 | if (dev != net->loopback_dev) { | |
3457 | if (dev) { | |
3458 | dev_put(dev); | |
3459 | in6_dev_put(idev); | |
3460 | } | |
3461 | dev = net->loopback_dev; | |
3462 | dev_hold(dev); | |
3463 | idev = in6_dev_get(dev); | |
3464 | if (!idev) { | |
3465 | err = -ENODEV; | |
3466 | goto out; | |
3467 | } | |
3468 | } | |
7dd73168 | 3469 | goto pcpu_alloc; |
83c44251 DA |
3470 | } |
3471 | ||
3472 | if (cfg->fc_flags & RTF_GATEWAY) { | |
3473 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); | |
3474 | if (err) | |
3475 | goto out; | |
3476 | ||
ad1601ae | 3477 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
bdf00467 | 3478 | fib6_nh->fib_nh_gw_family = AF_INET6; |
83c44251 DA |
3479 | } |
3480 | ||
3481 | err = -ENODEV; | |
3482 | if (!dev) | |
3483 | goto out; | |
3484 | ||
3485 | if (idev->cnf.disable_ipv6) { | |
3486 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); | |
3487 | err = -EACCES; | |
3488 | goto out; | |
3489 | } | |
3490 | ||
3491 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { | |
3492 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); | |
3493 | err = -ENETDOWN; | |
3494 | goto out; | |
3495 | } | |
3496 | ||
3497 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && | |
3498 | !netif_carrier_ok(dev)) | |
ad1601ae | 3499 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
83c44251 | 3500 | |
faee6769 | 3501 | err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap, |
7dd73168 DA |
3502 | cfg->fc_encap_type, cfg, gfp_flags, extack); |
3503 | if (err) | |
3504 | goto out; | |
3505 | ||
3506 | pcpu_alloc: | |
f40b6ae2 DA |
3507 | fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); |
3508 | if (!fib6_nh->rt6i_pcpu) { | |
3509 | err = -ENOMEM; | |
3510 | goto out; | |
3511 | } | |
3512 | ||
ad1601ae | 3513 | fib6_nh->fib_nh_dev = dev; |
f1741730 | 3514 | fib6_nh->fib_nh_oif = dev->ifindex; |
83c44251 DA |
3515 | err = 0; |
3516 | out: | |
3517 | if (idev) | |
3518 | in6_dev_put(idev); | |
3519 | ||
3520 | if (err) { | |
ad1601ae DA |
3521 | lwtstate_put(fib6_nh->fib_nh_lws); |
3522 | fib6_nh->fib_nh_lws = NULL; | |
83c44251 DA |
3523 | if (dev) |
3524 | dev_put(dev); | |
3525 | } | |
3526 | ||
3527 | return err; | |
3528 | } | |
3529 | ||
dac7d0f2 DA |
3530 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
3531 | { | |
cc5c073a DA |
3532 | struct rt6_exception_bucket *bucket; |
3533 | ||
3534 | rcu_read_lock(); | |
3535 | ||
3536 | fib6_nh_flush_exceptions(fib6_nh, NULL); | |
3537 | bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); | |
3538 | if (bucket) { | |
3539 | rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); | |
3540 | kfree(bucket); | |
3541 | } | |
3542 | ||
3543 | rcu_read_unlock(); | |
3544 | ||
f40b6ae2 DA |
3545 | if (fib6_nh->rt6i_pcpu) { |
3546 | int cpu; | |
3547 | ||
3548 | for_each_possible_cpu(cpu) { | |
3549 | struct rt6_info **ppcpu_rt; | |
3550 | struct rt6_info *pcpu_rt; | |
3551 | ||
3552 | ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); | |
3553 | pcpu_rt = *ppcpu_rt; | |
3554 | if (pcpu_rt) { | |
3555 | dst_dev_put(&pcpu_rt->dst); | |
3556 | dst_release(&pcpu_rt->dst); | |
3557 | *ppcpu_rt = NULL; | |
3558 | } | |
3559 | } | |
3560 | ||
3561 | free_percpu(fib6_nh->rt6i_pcpu); | |
3562 | } | |
3563 | ||
979e276e | 3564 | fib_nh_common_release(&fib6_nh->nh_common); |
dac7d0f2 DA |
3565 | } |
3566 | ||
8d1c802b | 3567 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
acb54e3c | 3568 | gfp_t gfp_flags, |
333c4301 | 3569 | struct netlink_ext_ack *extack) |
1da177e4 | 3570 | { |
5578689a | 3571 | struct net *net = cfg->fc_nlinfo.nl_net; |
8d1c802b | 3572 | struct fib6_info *rt = NULL; |
f88d8ea6 | 3573 | struct nexthop *nh = NULL; |
c71099ac | 3574 | struct fib6_table *table; |
f88d8ea6 | 3575 | struct fib6_nh *fib6_nh; |
8c5b83f0 | 3576 | int err = -EINVAL; |
83c44251 | 3577 | int addr_type; |
1da177e4 | 3578 | |
557c44be | 3579 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
d5d531cb DA |
3580 | if (cfg->fc_flags & RTF_PCPU) { |
3581 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); | |
557c44be | 3582 | goto out; |
d5d531cb | 3583 | } |
557c44be | 3584 | |
2ea2352e WW |
3585 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
3586 | if (cfg->fc_flags & RTF_CACHE) { | |
3587 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); | |
3588 | goto out; | |
3589 | } | |
3590 | ||
e8478e80 DA |
3591 | if (cfg->fc_type > RTN_MAX) { |
3592 | NL_SET_ERR_MSG(extack, "Invalid route type"); | |
3593 | goto out; | |
3594 | } | |
3595 | ||
d5d531cb DA |
3596 | if (cfg->fc_dst_len > 128) { |
3597 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); | |
3598 | goto out; | |
3599 | } | |
3600 | if (cfg->fc_src_len > 128) { | |
3601 | NL_SET_ERR_MSG(extack, "Invalid source address length"); | |
8c5b83f0 | 3602 | goto out; |
d5d531cb | 3603 | } |
1da177e4 | 3604 | #ifndef CONFIG_IPV6_SUBTREES |
d5d531cb DA |
3605 | if (cfg->fc_src_len) { |
3606 | NL_SET_ERR_MSG(extack, | |
3607 | "Specifying source address requires IPV6_SUBTREES to be enabled"); | |
8c5b83f0 | 3608 | goto out; |
d5d531cb | 3609 | } |
1da177e4 | 3610 | #endif |
5b98324e DA |
3611 | if (cfg->fc_nh_id) { |
3612 | nh = nexthop_find_by_id(net, cfg->fc_nh_id); | |
3613 | if (!nh) { | |
3614 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); | |
3615 | goto out; | |
3616 | } | |
3617 | err = fib6_check_nexthop(nh, cfg, extack); | |
3618 | if (err) | |
3619 | goto out; | |
3620 | } | |
fc1e64e1 | 3621 | |
d71314b4 | 3622 | err = -ENOBUFS; |
38308473 DM |
3623 | if (cfg->fc_nlinfo.nlh && |
3624 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { | |
d71314b4 | 3625 | table = fib6_get_table(net, cfg->fc_table); |
38308473 | 3626 | if (!table) { |
f3213831 | 3627 | pr_warn("NLM_F_CREATE should be specified when creating new route\n"); |
d71314b4 MV |
3628 | table = fib6_new_table(net, cfg->fc_table); |
3629 | } | |
3630 | } else { | |
3631 | table = fib6_new_table(net, cfg->fc_table); | |
3632 | } | |
38308473 DM |
3633 | |
3634 | if (!table) | |
c71099ac | 3635 | goto out; |
c71099ac | 3636 | |
93531c67 | 3637 | err = -ENOMEM; |
f88d8ea6 | 3638 | rt = fib6_info_alloc(gfp_flags, !nh); |
93531c67 | 3639 | if (!rt) |
1da177e4 | 3640 | goto out; |
93531c67 | 3641 | |
d7e774f3 DA |
3642 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
3643 | extack); | |
767a2217 DA |
3644 | if (IS_ERR(rt->fib6_metrics)) { |
3645 | err = PTR_ERR(rt->fib6_metrics); | |
fda21d46 ED |
3646 | /* Do not leave garbage there. */ |
3647 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; | |
767a2217 DA |
3648 | goto out; |
3649 | } | |
3650 | ||
93531c67 DA |
3651 | if (cfg->fc_flags & RTF_ADDRCONF) |
3652 | rt->dst_nocount = true; | |
1da177e4 | 3653 | |
1716a961 | 3654 | if (cfg->fc_flags & RTF_EXPIRES) |
14895687 | 3655 | fib6_set_expires(rt, jiffies + |
1716a961 G |
3656 | clock_t_to_jiffies(cfg->fc_expires)); |
3657 | else | |
14895687 | 3658 | fib6_clean_expires(rt); |
1da177e4 | 3659 | |
86872cb5 TG |
3660 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
3661 | cfg->fc_protocol = RTPROT_BOOT; | |
93c2fb25 | 3662 | rt->fib6_protocol = cfg->fc_protocol; |
86872cb5 | 3663 | |
83c44251 DA |
3664 | rt->fib6_table = table; |
3665 | rt->fib6_metric = cfg->fc_metric; | |
c7036d97 | 3666 | rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; |
2b2450ca | 3667 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
19e42e45 | 3668 | |
93c2fb25 DA |
3669 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
3670 | rt->fib6_dst.plen = cfg->fc_dst_len; | |
e5fd387a | 3671 | |
1da177e4 | 3672 | #ifdef CONFIG_IPV6_SUBTREES |
93c2fb25 DA |
3673 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
3674 | rt->fib6_src.plen = cfg->fc_src_len; | |
1da177e4 | 3675 | #endif |
f88d8ea6 DA |
3676 | if (nh) { |
3677 | if (!nexthop_get(nh)) { | |
3678 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); | |
3679 | goto out; | |
3680 | } | |
3681 | if (rt->fib6_src.plen) { | |
4daa95af | 3682 | NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); |
f88d8ea6 DA |
3683 | goto out; |
3684 | } | |
3685 | rt->nh = nh; | |
3686 | fib6_nh = nexthop_fib6_nh(rt->nh); | |
3687 | } else { | |
3688 | err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); | |
3689 | if (err) | |
3690 | goto out; | |
1da177e4 | 3691 | |
f88d8ea6 DA |
3692 | fib6_nh = rt->fib6_nh; |
3693 | ||
3694 | /* We cannot add true routes via loopback here, they would | |
3695 | * result in kernel looping; promote them to reject routes | |
3696 | */ | |
3697 | addr_type = ipv6_addr_type(&cfg->fc_dst); | |
3698 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, | |
3699 | addr_type)) | |
3700 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; | |
3701 | } | |
955ec4cb | 3702 | |
c3968a85 | 3703 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
f88d8ea6 | 3704 | struct net_device *dev = fib6_nh->fib_nh_dev; |
83c44251 | 3705 | |
c3968a85 | 3706 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
d5d531cb | 3707 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
c3968a85 DW |
3708 | err = -EINVAL; |
3709 | goto out; | |
3710 | } | |
93c2fb25 DA |
3711 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
3712 | rt->fib6_prefsrc.plen = 128; | |
c3968a85 | 3713 | } else |
93c2fb25 | 3714 | rt->fib6_prefsrc.plen = 0; |
c3968a85 | 3715 | |
8c5b83f0 | 3716 | return rt; |
6b9ea5a6 | 3717 | out: |
93531c67 | 3718 | fib6_info_release(rt); |
8c5b83f0 | 3719 | return ERR_PTR(err); |
6b9ea5a6 RP |
3720 | } |
3721 | ||
acb54e3c | 3722 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
333c4301 | 3723 | struct netlink_ext_ack *extack) |
6b9ea5a6 | 3724 | { |
8d1c802b | 3725 | struct fib6_info *rt; |
6b9ea5a6 RP |
3726 | int err; |
3727 | ||
acb54e3c | 3728 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
d4ead6b3 DA |
3729 | if (IS_ERR(rt)) |
3730 | return PTR_ERR(rt); | |
6b9ea5a6 | 3731 | |
d4ead6b3 | 3732 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
93531c67 | 3733 | fib6_info_release(rt); |
6b9ea5a6 | 3734 | |
1da177e4 LT |
3735 | return err; |
3736 | } | |
3737 | ||
8d1c802b | 3738 | static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) |
1da177e4 | 3739 | { |
afb1d4b5 | 3740 | struct net *net = info->nl_net; |
c71099ac | 3741 | struct fib6_table *table; |
afb1d4b5 | 3742 | int err; |
1da177e4 | 3743 | |
421842ed | 3744 | if (rt == net->ipv6.fib6_null_entry) { |
6825a26c G |
3745 | err = -ENOENT; |
3746 | goto out; | |
3747 | } | |
6c813a72 | 3748 | |
93c2fb25 | 3749 | table = rt->fib6_table; |
66f5d6ce | 3750 | spin_lock_bh(&table->tb6_lock); |
86872cb5 | 3751 | err = fib6_del(rt, info); |
66f5d6ce | 3752 | spin_unlock_bh(&table->tb6_lock); |
1da177e4 | 3753 | |
6825a26c | 3754 | out: |
93531c67 | 3755 | fib6_info_release(rt); |
1da177e4 LT |
3756 | return err; |
3757 | } | |
3758 | ||
8d1c802b | 3759 | int ip6_del_rt(struct net *net, struct fib6_info *rt) |
e0a1ad73 | 3760 | { |
afb1d4b5 DA |
3761 | struct nl_info info = { .nl_net = net }; |
3762 | ||
528c4ceb | 3763 | return __ip6_del_rt(rt, &info); |
e0a1ad73 TG |
3764 | } |
3765 | ||
8d1c802b | 3766 | static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) |
0ae81335 DA |
3767 | { |
3768 | struct nl_info *info = &cfg->fc_nlinfo; | |
e3330039 | 3769 | struct net *net = info->nl_net; |
16a16cd3 | 3770 | struct sk_buff *skb = NULL; |
0ae81335 | 3771 | struct fib6_table *table; |
e3330039 | 3772 | int err = -ENOENT; |
0ae81335 | 3773 | |
421842ed | 3774 | if (rt == net->ipv6.fib6_null_entry) |
e3330039 | 3775 | goto out_put; |
93c2fb25 | 3776 | table = rt->fib6_table; |
66f5d6ce | 3777 | spin_lock_bh(&table->tb6_lock); |
0ae81335 | 3778 | |
93c2fb25 | 3779 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
8d1c802b | 3780 | struct fib6_info *sibling, *next_sibling; |
0284696b | 3781 | struct fib6_node *fn; |
0ae81335 | 3782 | |
16a16cd3 DA |
3783 | /* prefer to send a single notification with all hops */ |
3784 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); | |
3785 | if (skb) { | |
3786 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; | |
3787 | ||
d4ead6b3 | 3788 | if (rt6_fill_node(net, skb, rt, NULL, |
16a16cd3 DA |
3789 | NULL, NULL, 0, RTM_DELROUTE, |
3790 | info->portid, seq, 0) < 0) { | |
3791 | kfree_skb(skb); | |
3792 | skb = NULL; | |
3793 | } else | |
3794 | info->skip_notify = 1; | |
3795 | } | |
3796 | ||
0284696b IS |
3797 | /* 'rt' points to the first sibling route. If it is not the |
3798 | * leaf, then we do not need to send a notification. Otherwise, | |
3799 | * we need to check if the last sibling has a next route or not | |
3800 | * and emit a replace or delete notification, respectively. | |
3801 | */ | |
2881fd61 | 3802 | info->skip_notify_kernel = 1; |
0284696b IS |
3803 | fn = rcu_dereference_protected(rt->fib6_node, |
3804 | lockdep_is_held(&table->tb6_lock)); | |
3805 | if (rcu_access_pointer(fn->leaf) == rt) { | |
3806 | struct fib6_info *last_sibling, *replace_rt; | |
3807 | ||
3808 | last_sibling = list_last_entry(&rt->fib6_siblings, | |
3809 | struct fib6_info, | |
3810 | fib6_siblings); | |
3811 | replace_rt = rcu_dereference_protected( | |
3812 | last_sibling->fib6_next, | |
3813 | lockdep_is_held(&table->tb6_lock)); | |
3814 | if (replace_rt) | |
3815 | call_fib6_entry_notifiers_replace(net, | |
3816 | replace_rt); | |
3817 | else | |
3818 | call_fib6_multipath_entry_notifiers(net, | |
caafb250 | 3819 | FIB_EVENT_ENTRY_DEL, |
0284696b IS |
3820 | rt, rt->fib6_nsiblings, |
3821 | NULL); | |
3822 | } | |
0ae81335 | 3823 | list_for_each_entry_safe(sibling, next_sibling, |
93c2fb25 DA |
3824 | &rt->fib6_siblings, |
3825 | fib6_siblings) { | |
0ae81335 DA |
3826 | err = fib6_del(sibling, info); |
3827 | if (err) | |
e3330039 | 3828 | goto out_unlock; |
0ae81335 DA |
3829 | } |
3830 | } | |
3831 | ||
3832 | err = fib6_del(rt, info); | |
e3330039 | 3833 | out_unlock: |
66f5d6ce | 3834 | spin_unlock_bh(&table->tb6_lock); |
e3330039 | 3835 | out_put: |
93531c67 | 3836 | fib6_info_release(rt); |
16a16cd3 DA |
3837 | |
3838 | if (skb) { | |
e3330039 | 3839 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
16a16cd3 DA |
3840 | info->nlh, gfp_any()); |
3841 | } | |
0ae81335 DA |
3842 | return err; |
3843 | } | |
3844 | ||
0fa6efc5 | 3845 | static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) |
23fb93a4 DA |
3846 | { |
3847 | int rc = -ESRCH; | |
3848 | ||
3849 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) | |
3850 | goto out; | |
3851 | ||
3852 | if (cfg->fc_flags & RTF_GATEWAY && | |
3853 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) | |
3854 | goto out; | |
761f6026 XL |
3855 | |
3856 | rc = rt6_remove_exception_rt(rt); | |
23fb93a4 DA |
3857 | out: |
3858 | return rc; | |
3859 | } | |
3860 | ||
0fa6efc5 DA |
3861 | static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, |
3862 | struct fib6_nh *nh) | |
3863 | { | |
3864 | struct fib6_result res = { | |
3865 | .f6i = rt, | |
3866 | .nh = nh, | |
3867 | }; | |
3868 | struct rt6_info *rt_cache; | |
3869 | ||
3870 | rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); | |
3871 | if (rt_cache) | |
3872 | return __ip6_del_cached_rt(rt_cache, cfg); | |
3873 | ||
3874 | return 0; | |
3875 | } | |
3876 | ||
5b98324e DA |
3877 | struct fib6_nh_del_cached_rt_arg { |
3878 | struct fib6_config *cfg; | |
3879 | struct fib6_info *f6i; | |
3880 | }; | |
3881 | ||
3882 | static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) | |
3883 | { | |
3884 | struct fib6_nh_del_cached_rt_arg *arg = _arg; | |
3885 | int rc; | |
3886 | ||
3887 | rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); | |
3888 | return rc != -ESRCH ? rc : 0; | |
3889 | } | |
3890 | ||
3891 | static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) | |
3892 | { | |
3893 | struct fib6_nh_del_cached_rt_arg arg = { | |
3894 | .cfg = cfg, | |
3895 | .f6i = f6i | |
3896 | }; | |
3897 | ||
3898 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); | |
3899 | } | |
3900 | ||
333c4301 DA |
3901 | static int ip6_route_del(struct fib6_config *cfg, |
3902 | struct netlink_ext_ack *extack) | |
1da177e4 | 3903 | { |
c71099ac | 3904 | struct fib6_table *table; |
8d1c802b | 3905 | struct fib6_info *rt; |
1da177e4 | 3906 | struct fib6_node *fn; |
1da177e4 LT |
3907 | int err = -ESRCH; |
3908 | ||
5578689a | 3909 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
d5d531cb DA |
3910 | if (!table) { |
3911 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); | |
c71099ac | 3912 | return err; |
d5d531cb | 3913 | } |
c71099ac | 3914 | |
66f5d6ce | 3915 | rcu_read_lock(); |
1da177e4 | 3916 | |
c71099ac | 3917 | fn = fib6_locate(&table->tb6_root, |
86872cb5 | 3918 | &cfg->fc_dst, cfg->fc_dst_len, |
38fbeeee | 3919 | &cfg->fc_src, cfg->fc_src_len, |
2b760fcf | 3920 | !(cfg->fc_flags & RTF_CACHE)); |
1ab1457c | 3921 | |
1da177e4 | 3922 | if (fn) { |
66f5d6ce | 3923 | for_each_fib6_node_rt_rcu(fn) { |
ad1601ae DA |
3924 | struct fib6_nh *nh; |
3925 | ||
3401bfb1 SB |
3926 | if (rt->nh && cfg->fc_nh_id && |
3927 | rt->nh->id != cfg->fc_nh_id) | |
5b98324e | 3928 | continue; |
23fb93a4 | 3929 | |
5b98324e DA |
3930 | if (cfg->fc_flags & RTF_CACHE) { |
3931 | int rc = 0; | |
3932 | ||
3933 | if (rt->nh) { | |
3934 | rc = ip6_del_cached_rt_nh(cfg, rt); | |
3935 | } else if (cfg->fc_nh_id) { | |
3936 | continue; | |
3937 | } else { | |
3938 | nh = rt->fib6_nh; | |
3939 | rc = ip6_del_cached_rt(cfg, rt, nh); | |
3940 | } | |
0fa6efc5 DA |
3941 | if (rc != -ESRCH) { |
3942 | rcu_read_unlock(); | |
3943 | return rc; | |
23fb93a4 DA |
3944 | } |
3945 | continue; | |
2b760fcf | 3946 | } |
ad1601ae | 3947 | |
5b98324e DA |
3948 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
3949 | continue; | |
3950 | if (cfg->fc_protocol && | |
3951 | cfg->fc_protocol != rt->fib6_protocol) | |
3952 | continue; | |
3953 | ||
3954 | if (rt->nh) { | |
3955 | if (!fib6_info_hold_safe(rt)) | |
3956 | continue; | |
3957 | rcu_read_unlock(); | |
3958 | ||
3959 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); | |
3960 | } | |
3961 | if (cfg->fc_nh_id) | |
3962 | continue; | |
3963 | ||
3964 | nh = rt->fib6_nh; | |
86872cb5 | 3965 | if (cfg->fc_ifindex && |
ad1601ae DA |
3966 | (!nh->fib_nh_dev || |
3967 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) | |
1da177e4 | 3968 | continue; |
86872cb5 | 3969 | if (cfg->fc_flags & RTF_GATEWAY && |
ad1601ae | 3970 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
1da177e4 | 3971 | continue; |
e873e4b9 WW |
3972 | if (!fib6_info_hold_safe(rt)) |
3973 | continue; | |
66f5d6ce | 3974 | rcu_read_unlock(); |
1da177e4 | 3975 | |
0ae81335 DA |
3976 | /* if gateway was specified only delete the one hop */ |
3977 | if (cfg->fc_flags & RTF_GATEWAY) | |
3978 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); | |
3979 | ||
3980 | return __ip6_del_rt_siblings(rt, cfg); | |
1da177e4 LT |
3981 | } |
3982 | } | |
66f5d6ce | 3983 | rcu_read_unlock(); |
1da177e4 LT |
3984 | |
3985 | return err; | |
3986 | } | |
3987 | ||
6700c270 | 3988 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) |
a6279458 | 3989 | { |
a6279458 | 3990 | struct netevent_redirect netevent; |
e8599ff4 | 3991 | struct rt6_info *rt, *nrt = NULL; |
85bd05de | 3992 | struct fib6_result res = {}; |
e8599ff4 DM |
3993 | struct ndisc_options ndopts; |
3994 | struct inet6_dev *in6_dev; | |
3995 | struct neighbour *neigh; | |
71bcdba0 | 3996 | struct rd_msg *msg; |
6e157b6a DM |
3997 | int optlen, on_link; |
3998 | u8 *lladdr; | |
e8599ff4 | 3999 | |
29a3cad5 | 4000 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
71bcdba0 | 4001 | optlen -= sizeof(*msg); |
e8599ff4 DM |
4002 | |
4003 | if (optlen < 0) { | |
6e157b6a | 4004 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
e8599ff4 DM |
4005 | return; |
4006 | } | |
4007 | ||
71bcdba0 | 4008 | msg = (struct rd_msg *)icmp6_hdr(skb); |
e8599ff4 | 4009 | |
71bcdba0 | 4010 | if (ipv6_addr_is_multicast(&msg->dest)) { |
6e157b6a | 4011 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
e8599ff4 DM |
4012 | return; |
4013 | } | |
4014 | ||
6e157b6a | 4015 | on_link = 0; |
71bcdba0 | 4016 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
e8599ff4 | 4017 | on_link = 1; |
71bcdba0 | 4018 | } else if (ipv6_addr_type(&msg->target) != |
e8599ff4 | 4019 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
6e157b6a | 4020 | net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); |
e8599ff4 DM |
4021 | return; |
4022 | } | |
4023 | ||
4024 | in6_dev = __in6_dev_get(skb->dev); | |
4025 | if (!in6_dev) | |
4026 | return; | |
4027 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) | |
4028 | return; | |
4029 | ||
4030 | /* RFC2461 8.1: | |
4031 | * The IP source address of the Redirect MUST be the same as the current | |
4032 | * first-hop router for the specified ICMP Destination Address. | |
4033 | */ | |
4034 | ||
f997c55c | 4035 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
e8599ff4 DM |
4036 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
4037 | return; | |
4038 | } | |
6e157b6a DM |
4039 | |
4040 | lladdr = NULL; | |
e8599ff4 DM |
4041 | if (ndopts.nd_opts_tgt_lladdr) { |
4042 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, | |
4043 | skb->dev); | |
4044 | if (!lladdr) { | |
4045 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); | |
4046 | return; | |
4047 | } | |
4048 | } | |
4049 | ||
6e157b6a | 4050 | rt = (struct rt6_info *) dst; |
ec13ad1d | 4051 | if (rt->rt6i_flags & RTF_REJECT) { |
6e157b6a | 4052 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
e8599ff4 | 4053 | return; |
6e157b6a | 4054 | } |
e8599ff4 | 4055 | |
6e157b6a DM |
4056 | /* Redirect received -> path was valid. |
4057 | * Look, redirects are sent only in response to data packets, | |
4058 | * so that this nexthop apparently is reachable. --ANK | |
4059 | */ | |
0dec879f | 4060 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
a6279458 | 4061 | |
71bcdba0 | 4062 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
6e157b6a DM |
4063 | if (!neigh) |
4064 | return; | |
a6279458 | 4065 | |
1da177e4 LT |
4066 | /* |
4067 | * We have finally decided to accept it. | |
4068 | */ | |
4069 | ||
f997c55c | 4070 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
1da177e4 LT |
4071 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
4072 | NEIGH_UPDATE_F_OVERRIDE| | |
4073 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| | |
f997c55c AA |
4074 | NEIGH_UPDATE_F_ISROUTER)), |
4075 | NDISC_REDIRECT, &ndopts); | |
1da177e4 | 4076 | |
4d85cd0c | 4077 | rcu_read_lock(); |
85bd05de | 4078 | res.f6i = rcu_dereference(rt->from); |
ff24e498 | 4079 | if (!res.f6i) |
886b7a50 | 4080 | goto out; |
8a14e46f | 4081 | |
49d5b8ef DA |
4082 | if (res.f6i->nh) { |
4083 | struct fib6_nh_match_arg arg = { | |
4084 | .dev = dst->dev, | |
4085 | .gw = &rt->rt6i_gateway, | |
4086 | }; | |
4087 | ||
4088 | nexthop_for_each_fib6_nh(res.f6i->nh, | |
4089 | fib6_nh_find_match, &arg); | |
4090 | ||
4091 | /* fib6_info uses a nexthop that does not have fib6_nh | |
4092 | * using the dst->dev. Should be impossible | |
4093 | */ | |
4094 | if (!arg.match) | |
4095 | goto out; | |
4096 | res.nh = arg.match; | |
4097 | } else { | |
4098 | res.nh = res.f6i->fib6_nh; | |
4099 | } | |
4100 | ||
7d21fec9 DA |
4101 | res.fib6_flags = res.f6i->fib6_flags; |
4102 | res.fib6_type = res.f6i->fib6_type; | |
85bd05de | 4103 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
38308473 | 4104 | if (!nrt) |
1da177e4 LT |
4105 | goto out; |
4106 | ||
4107 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; | |
4108 | if (on_link) | |
4109 | nrt->rt6i_flags &= ~RTF_GATEWAY; | |
4110 | ||
4e3fd7a0 | 4111 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
1da177e4 | 4112 | |
886b7a50 | 4113 | /* rt6_insert_exception() will take care of duplicated exceptions */ |
5012f0a5 | 4114 | if (rt6_insert_exception(nrt, &res)) { |
2b760fcf WW |
4115 | dst_release_immediate(&nrt->dst); |
4116 | goto out; | |
4117 | } | |
1da177e4 | 4118 | |
d8d1f30b CG |
4119 | netevent.old = &rt->dst; |
4120 | netevent.new = &nrt->dst; | |
71bcdba0 | 4121 | netevent.daddr = &msg->dest; |
60592833 | 4122 | netevent.neigh = neigh; |
8d71740c TT |
4123 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
4124 | ||
1da177e4 | 4125 | out: |
886b7a50 | 4126 | rcu_read_unlock(); |
e8599ff4 | 4127 | neigh_release(neigh); |
6e157b6a DM |
4128 | } |
4129 | ||
70ceb4f5 | 4130 | #ifdef CONFIG_IPV6_ROUTE_INFO |
8d1c802b | 4131 | static struct fib6_info *rt6_get_route_info(struct net *net, |
b71d1d42 | 4132 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
4133 | const struct in6_addr *gwaddr, |
4134 | struct net_device *dev) | |
70ceb4f5 | 4135 | { |
830218c1 DA |
4136 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
4137 | int ifindex = dev->ifindex; | |
70ceb4f5 | 4138 | struct fib6_node *fn; |
8d1c802b | 4139 | struct fib6_info *rt = NULL; |
c71099ac TG |
4140 | struct fib6_table *table; |
4141 | ||
830218c1 | 4142 | table = fib6_get_table(net, tb_id); |
38308473 | 4143 | if (!table) |
c71099ac | 4144 | return NULL; |
70ceb4f5 | 4145 | |
66f5d6ce | 4146 | rcu_read_lock(); |
38fbeeee | 4147 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
70ceb4f5 YH |
4148 | if (!fn) |
4149 | goto out; | |
4150 | ||
66f5d6ce | 4151 | for_each_fib6_node_rt_rcu(fn) { |
f88d8ea6 DA |
4152 | /* these routes do not use nexthops */ |
4153 | if (rt->nh) | |
4154 | continue; | |
1cf844c7 | 4155 | if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) |
70ceb4f5 | 4156 | continue; |
2b2450ca | 4157 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
1cf844c7 | 4158 | !rt->fib6_nh->fib_nh_gw_family) |
70ceb4f5 | 4159 | continue; |
1cf844c7 | 4160 | if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) |
70ceb4f5 | 4161 | continue; |
e873e4b9 WW |
4162 | if (!fib6_info_hold_safe(rt)) |
4163 | continue; | |
70ceb4f5 YH |
4164 | break; |
4165 | } | |
4166 | out: | |
66f5d6ce | 4167 | rcu_read_unlock(); |
70ceb4f5 YH |
4168 | return rt; |
4169 | } | |
4170 | ||
8d1c802b | 4171 | static struct fib6_info *rt6_add_route_info(struct net *net, |
b71d1d42 | 4172 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
4173 | const struct in6_addr *gwaddr, |
4174 | struct net_device *dev, | |
95c96174 | 4175 | unsigned int pref) |
70ceb4f5 | 4176 | { |
86872cb5 | 4177 | struct fib6_config cfg = { |
238fc7ea | 4178 | .fc_metric = IP6_RT_PRIO_USER, |
830218c1 | 4179 | .fc_ifindex = dev->ifindex, |
86872cb5 TG |
4180 | .fc_dst_len = prefixlen, |
4181 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | | |
4182 | RTF_UP | RTF_PREF(pref), | |
b91d5329 | 4183 | .fc_protocol = RTPROT_RA, |
e8478e80 | 4184 | .fc_type = RTN_UNICAST, |
15e47304 | 4185 | .fc_nlinfo.portid = 0, |
efa2cea0 DL |
4186 | .fc_nlinfo.nlh = NULL, |
4187 | .fc_nlinfo.nl_net = net, | |
86872cb5 TG |
4188 | }; |
4189 | ||
830218c1 | 4190 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
4e3fd7a0 AD |
4191 | cfg.fc_dst = *prefix; |
4192 | cfg.fc_gateway = *gwaddr; | |
70ceb4f5 | 4193 | |
e317da96 YH |
4194 | /* We should treat it as a default route if prefix length is 0. */ |
4195 | if (!prefixlen) | |
86872cb5 | 4196 | cfg.fc_flags |= RTF_DEFAULT; |
70ceb4f5 | 4197 | |
acb54e3c | 4198 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
70ceb4f5 | 4199 | |
830218c1 | 4200 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
70ceb4f5 YH |
4201 | } |
4202 | #endif | |
4203 | ||
8d1c802b | 4204 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
afb1d4b5 DA |
4205 | const struct in6_addr *addr, |
4206 | struct net_device *dev) | |
1ab1457c | 4207 | { |
830218c1 | 4208 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
8d1c802b | 4209 | struct fib6_info *rt; |
c71099ac | 4210 | struct fib6_table *table; |
1da177e4 | 4211 | |
afb1d4b5 | 4212 | table = fib6_get_table(net, tb_id); |
38308473 | 4213 | if (!table) |
c71099ac | 4214 | return NULL; |
1da177e4 | 4215 | |
66f5d6ce WW |
4216 | rcu_read_lock(); |
4217 | for_each_fib6_node_rt_rcu(&table->tb6_root) { | |
f88d8ea6 | 4218 | struct fib6_nh *nh; |
ad1601ae | 4219 | |
f88d8ea6 DA |
4220 | /* RA routes do not use nexthops */ |
4221 | if (rt->nh) | |
4222 | continue; | |
4223 | ||
4224 | nh = rt->fib6_nh; | |
ad1601ae | 4225 | if (dev == nh->fib_nh_dev && |
93c2fb25 | 4226 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
ad1601ae | 4227 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
1da177e4 LT |
4228 | break; |
4229 | } | |
e873e4b9 WW |
4230 | if (rt && !fib6_info_hold_safe(rt)) |
4231 | rt = NULL; | |
66f5d6ce | 4232 | rcu_read_unlock(); |
1da177e4 LT |
4233 | return rt; |
4234 | } | |
4235 | ||
8d1c802b | 4236 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
afb1d4b5 | 4237 | const struct in6_addr *gwaddr, |
ebacaaa0 YH |
4238 | struct net_device *dev, |
4239 | unsigned int pref) | |
1da177e4 | 4240 | { |
86872cb5 | 4241 | struct fib6_config cfg = { |
ca254490 | 4242 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
238fc7ea | 4243 | .fc_metric = IP6_RT_PRIO_USER, |
86872cb5 TG |
4244 | .fc_ifindex = dev->ifindex, |
4245 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | | |
4246 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), | |
b91d5329 | 4247 | .fc_protocol = RTPROT_RA, |
e8478e80 | 4248 | .fc_type = RTN_UNICAST, |
15e47304 | 4249 | .fc_nlinfo.portid = 0, |
5578689a | 4250 | .fc_nlinfo.nlh = NULL, |
afb1d4b5 | 4251 | .fc_nlinfo.nl_net = net, |
86872cb5 | 4252 | }; |
1da177e4 | 4253 | |
4e3fd7a0 | 4254 | cfg.fc_gateway = *gwaddr; |
1da177e4 | 4255 | |
acb54e3c | 4256 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
830218c1 DA |
4257 | struct fib6_table *table; |
4258 | ||
4259 | table = fib6_get_table(dev_net(dev), cfg.fc_table); | |
4260 | if (table) | |
4261 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; | |
4262 | } | |
1da177e4 | 4263 | |
afb1d4b5 | 4264 | return rt6_get_dflt_router(net, gwaddr, dev); |
1da177e4 LT |
4265 | } |
4266 | ||
afb1d4b5 DA |
4267 | static void __rt6_purge_dflt_routers(struct net *net, |
4268 | struct fib6_table *table) | |
1da177e4 | 4269 | { |
8d1c802b | 4270 | struct fib6_info *rt; |
1da177e4 LT |
4271 | |
4272 | restart: | |
66f5d6ce WW |
4273 | rcu_read_lock(); |
4274 | for_each_fib6_node_rt_rcu(&table->tb6_root) { | |
dcd1f572 DA |
4275 | struct net_device *dev = fib6_info_nh_dev(rt); |
4276 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; | |
4277 | ||
93c2fb25 | 4278 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
e873e4b9 WW |
4279 | (!idev || idev->cnf.accept_ra != 2) && |
4280 | fib6_info_hold_safe(rt)) { | |
93531c67 DA |
4281 | rcu_read_unlock(); |
4282 | ip6_del_rt(net, rt); | |
1da177e4 LT |
4283 | goto restart; |
4284 | } | |
4285 | } | |
66f5d6ce | 4286 | rcu_read_unlock(); |
830218c1 DA |
4287 | |
4288 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; | |
4289 | } | |
4290 | ||
4291 | void rt6_purge_dflt_routers(struct net *net) | |
4292 | { | |
4293 | struct fib6_table *table; | |
4294 | struct hlist_head *head; | |
4295 | unsigned int h; | |
4296 | ||
4297 | rcu_read_lock(); | |
4298 | ||
4299 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { | |
4300 | head = &net->ipv6.fib_table_hash[h]; | |
4301 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { | |
4302 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) | |
afb1d4b5 | 4303 | __rt6_purge_dflt_routers(net, table); |
830218c1 DA |
4304 | } |
4305 | } | |
4306 | ||
4307 | rcu_read_unlock(); | |
1da177e4 LT |
4308 | } |
4309 | ||
5578689a DL |
4310 | static void rtmsg_to_fib6_config(struct net *net, |
4311 | struct in6_rtmsg *rtmsg, | |
86872cb5 TG |
4312 | struct fib6_config *cfg) |
4313 | { | |
8823a3ac MŻ |
4314 | *cfg = (struct fib6_config){ |
4315 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? | |
4316 | : RT6_TABLE_MAIN, | |
4317 | .fc_ifindex = rtmsg->rtmsg_ifindex, | |
67f69513 | 4318 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
8823a3ac MŻ |
4319 | .fc_expires = rtmsg->rtmsg_info, |
4320 | .fc_dst_len = rtmsg->rtmsg_dst_len, | |
4321 | .fc_src_len = rtmsg->rtmsg_src_len, | |
4322 | .fc_flags = rtmsg->rtmsg_flags, | |
4323 | .fc_type = rtmsg->rtmsg_type, | |
4324 | ||
4325 | .fc_nlinfo.nl_net = net, | |
4326 | ||
4327 | .fc_dst = rtmsg->rtmsg_dst, | |
4328 | .fc_src = rtmsg->rtmsg_src, | |
4329 | .fc_gateway = rtmsg->rtmsg_gateway, | |
4330 | }; | |
86872cb5 TG |
4331 | } |
4332 | ||
5578689a | 4333 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
1da177e4 | 4334 | { |
86872cb5 | 4335 | struct fib6_config cfg; |
1da177e4 LT |
4336 | struct in6_rtmsg rtmsg; |
4337 | int err; | |
4338 | ||
67ba4152 | 4339 | switch (cmd) { |
1da177e4 LT |
4340 | case SIOCADDRT: /* Add a route */ |
4341 | case SIOCDELRT: /* Delete a route */ | |
af31f412 | 4342 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
4343 | return -EPERM; |
4344 | err = copy_from_user(&rtmsg, arg, | |
4345 | sizeof(struct in6_rtmsg)); | |
4346 | if (err) | |
4347 | return -EFAULT; | |
86872cb5 | 4348 | |
5578689a | 4349 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
86872cb5 | 4350 | |
1da177e4 LT |
4351 | rtnl_lock(); |
4352 | switch (cmd) { | |
4353 | case SIOCADDRT: | |
acb54e3c | 4354 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
1da177e4 LT |
4355 | break; |
4356 | case SIOCDELRT: | |
333c4301 | 4357 | err = ip6_route_del(&cfg, NULL); |
1da177e4 LT |
4358 | break; |
4359 | default: | |
4360 | err = -EINVAL; | |
4361 | } | |
4362 | rtnl_unlock(); | |
4363 | ||
4364 | return err; | |
3ff50b79 | 4365 | } |
1da177e4 LT |
4366 | |
4367 | return -EINVAL; | |
4368 | } | |
4369 | ||
4370 | /* | |
4371 | * Drop the packet on the floor | |
4372 | */ | |
4373 | ||
d5fdd6ba | 4374 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) |
1da177e4 | 4375 | { |
adf30907 | 4376 | struct dst_entry *dst = skb_dst(skb); |
1d3fd8a1 SS |
4377 | struct net *net = dev_net(dst->dev); |
4378 | struct inet6_dev *idev; | |
4379 | int type; | |
4380 | ||
4381 | if (netif_is_l3_master(skb->dev) && | |
4382 | dst->dev == net->loopback_dev) | |
4383 | idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); | |
4384 | else | |
4385 | idev = ip6_dst_idev(dst); | |
4386 | ||
612f09e8 YH |
4387 | switch (ipstats_mib_noroutes) { |
4388 | case IPSTATS_MIB_INNOROUTES: | |
0660e03f | 4389 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
45bb0060 | 4390 | if (type == IPV6_ADDR_ANY) { |
1d3fd8a1 | 4391 | IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
612f09e8 YH |
4392 | break; |
4393 | } | |
a8eceea8 | 4394 | fallthrough; |
612f09e8 | 4395 | case IPSTATS_MIB_OUTNOROUTES: |
1d3fd8a1 | 4396 | IP6_INC_STATS(net, idev, ipstats_mib_noroutes); |
612f09e8 YH |
4397 | break; |
4398 | } | |
1d3fd8a1 SS |
4399 | |
4400 | /* Start over by dropping the dst for l3mdev case */ | |
4401 | if (netif_is_l3_master(skb->dev)) | |
4402 | skb_dst_drop(skb); | |
4403 | ||
3ffe533c | 4404 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
1da177e4 LT |
4405 | kfree_skb(skb); |
4406 | return 0; | |
4407 | } | |
4408 | ||
9ce8ade0 TG |
4409 | static int ip6_pkt_discard(struct sk_buff *skb) |
4410 | { | |
612f09e8 | 4411 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
4412 | } |
4413 | ||
ede2059d | 4414 | static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 4415 | { |
adf30907 | 4416 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 4417 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
1da177e4 LT |
4418 | } |
4419 | ||
9ce8ade0 TG |
4420 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
4421 | { | |
612f09e8 | 4422 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
4423 | } |
4424 | ||
ede2059d | 4425 | static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
9ce8ade0 | 4426 | { |
adf30907 | 4427 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 4428 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
9ce8ade0 TG |
4429 | } |
4430 | ||
1da177e4 LT |
4431 | /* |
4432 | * Allocate a dst for local (unicast / anycast) address. | |
4433 | */ | |
4434 | ||
360a9887 DA |
4435 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
4436 | struct inet6_dev *idev, | |
4437 | const struct in6_addr *addr, | |
4438 | bool anycast, gfp_t gfp_flags) | |
1da177e4 | 4439 | { |
c7a1ce39 DA |
4440 | struct fib6_config cfg = { |
4441 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, | |
4442 | .fc_ifindex = idev->dev->ifindex, | |
d55a2e37 | 4443 | .fc_flags = RTF_UP | RTF_NONEXTHOP, |
c7a1ce39 DA |
4444 | .fc_dst = *addr, |
4445 | .fc_dst_len = 128, | |
4446 | .fc_protocol = RTPROT_KERNEL, | |
4447 | .fc_nlinfo.nl_net = net, | |
4448 | .fc_ignore_dev_down = true, | |
4449 | }; | |
d55a2e37 | 4450 | struct fib6_info *f6i; |
1da177e4 | 4451 | |
e8478e80 | 4452 | if (anycast) { |
c7a1ce39 DA |
4453 | cfg.fc_type = RTN_ANYCAST; |
4454 | cfg.fc_flags |= RTF_ANYCAST; | |
e8478e80 | 4455 | } else { |
c7a1ce39 DA |
4456 | cfg.fc_type = RTN_LOCAL; |
4457 | cfg.fc_flags |= RTF_LOCAL; | |
e8478e80 | 4458 | } |
1da177e4 | 4459 | |
d55a2e37 | 4460 | f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); |
8652f17c | 4461 | if (!IS_ERR(f6i)) |
d55a2e37 MŻ |
4462 | f6i->dst_nocount = true; |
4463 | return f6i; | |
1da177e4 LT |
4464 | } |
4465 | ||
c3968a85 DW |
4466 | /* remove deleted ip from prefsrc entries */ |
4467 | struct arg_dev_net_ip { | |
4468 | struct net_device *dev; | |
4469 | struct net *net; | |
4470 | struct in6_addr *addr; | |
4471 | }; | |
4472 | ||
8d1c802b | 4473 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
c3968a85 DW |
4474 | { |
4475 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; | |
4476 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; | |
4477 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; | |
4478 | ||
f88d8ea6 DA |
4479 | if (!rt->nh && |
4480 | ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && | |
421842ed | 4481 | rt != net->ipv6.fib6_null_entry && |
93c2fb25 | 4482 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
60006a48 | 4483 | spin_lock_bh(&rt6_exception_lock); |
c3968a85 | 4484 | /* remove prefsrc entry */ |
93c2fb25 | 4485 | rt->fib6_prefsrc.plen = 0; |
60006a48 | 4486 | spin_unlock_bh(&rt6_exception_lock); |
c3968a85 DW |
4487 | } |
4488 | return 0; | |
4489 | } | |
4490 | ||
4491 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) | |
4492 | { | |
4493 | struct net *net = dev_net(ifp->idev->dev); | |
4494 | struct arg_dev_net_ip adni = { | |
4495 | .dev = ifp->idev->dev, | |
4496 | .net = net, | |
4497 | .addr = &ifp->addr, | |
4498 | }; | |
0c3584d5 | 4499 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
c3968a85 DW |
4500 | } |
4501 | ||
2b2450ca | 4502 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
be7a010d DJ |
4503 | |
4504 | /* Remove routers and update dst entries when gateway turn into host. */ | |
8d1c802b | 4505 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
be7a010d DJ |
4506 | { |
4507 | struct in6_addr *gateway = (struct in6_addr *)arg; | |
f88d8ea6 DA |
4508 | struct fib6_nh *nh; |
4509 | ||
4510 | /* RA routes do not use nexthops */ | |
4511 | if (rt->nh) | |
4512 | return 0; | |
be7a010d | 4513 | |
f88d8ea6 | 4514 | nh = rt->fib6_nh; |
93c2fb25 | 4515 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
cc5c073a | 4516 | nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) |
be7a010d | 4517 | return -1; |
b16cb459 WW |
4518 | |
4519 | /* Further clean up cached routes in exception table. | |
4520 | * This is needed because cached route may have a different | |
4521 | * gateway than its 'parent' in the case of an ip redirect. | |
4522 | */ | |
cc5c073a | 4523 | fib6_nh_exceptions_clean_tohost(nh, gateway); |
b16cb459 | 4524 | |
be7a010d DJ |
4525 | return 0; |
4526 | } | |
4527 | ||
4528 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) | |
4529 | { | |
4530 | fib6_clean_all(net, fib6_clean_tohost, gateway); | |
4531 | } | |
4532 | ||
2127d95a IS |
4533 | struct arg_netdev_event { |
4534 | const struct net_device *dev; | |
4c981e28 | 4535 | union { |
ecc5663c | 4536 | unsigned char nh_flags; |
4c981e28 IS |
4537 | unsigned long event; |
4538 | }; | |
2127d95a IS |
4539 | }; |
4540 | ||
8d1c802b | 4541 | static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) |
d7dedee1 | 4542 | { |
8d1c802b | 4543 | struct fib6_info *iter; |
d7dedee1 IS |
4544 | struct fib6_node *fn; |
4545 | ||
93c2fb25 DA |
4546 | fn = rcu_dereference_protected(rt->fib6_node, |
4547 | lockdep_is_held(&rt->fib6_table->tb6_lock)); | |
d7dedee1 | 4548 | iter = rcu_dereference_protected(fn->leaf, |
93c2fb25 | 4549 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
d7dedee1 | 4550 | while (iter) { |
93c2fb25 | 4551 | if (iter->fib6_metric == rt->fib6_metric && |
33bd5ac5 | 4552 | rt6_qualify_for_ecmp(iter)) |
d7dedee1 | 4553 | return iter; |
8fb11a9a | 4554 | iter = rcu_dereference_protected(iter->fib6_next, |
93c2fb25 | 4555 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
d7dedee1 IS |
4556 | } |
4557 | ||
4558 | return NULL; | |
4559 | } | |
4560 | ||
f88d8ea6 | 4561 | /* only called for fib entries with builtin fib6_nh */ |
8d1c802b | 4562 | static bool rt6_is_dead(const struct fib6_info *rt) |
d7dedee1 | 4563 | { |
1cf844c7 DA |
4564 | if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || |
4565 | (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && | |
4566 | ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) | |
d7dedee1 IS |
4567 | return true; |
4568 | ||
4569 | return false; | |
4570 | } | |
4571 | ||
8d1c802b | 4572 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
d7dedee1 | 4573 | { |
8d1c802b | 4574 | struct fib6_info *iter; |
d7dedee1 IS |
4575 | int total = 0; |
4576 | ||
4577 | if (!rt6_is_dead(rt)) | |
1cf844c7 | 4578 | total += rt->fib6_nh->fib_nh_weight; |
d7dedee1 | 4579 | |
93c2fb25 | 4580 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
d7dedee1 | 4581 | if (!rt6_is_dead(iter)) |
1cf844c7 | 4582 | total += iter->fib6_nh->fib_nh_weight; |
d7dedee1 IS |
4583 | } |
4584 | ||
4585 | return total; | |
4586 | } | |
4587 | ||
8d1c802b | 4588 | static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) |
d7dedee1 IS |
4589 | { |
4590 | int upper_bound = -1; | |
4591 | ||
4592 | if (!rt6_is_dead(rt)) { | |
1cf844c7 | 4593 | *weight += rt->fib6_nh->fib_nh_weight; |
d7dedee1 IS |
4594 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
4595 | total) - 1; | |
4596 | } | |
1cf844c7 | 4597 | atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); |
d7dedee1 IS |
4598 | } |
4599 | ||
8d1c802b | 4600 | static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) |
d7dedee1 | 4601 | { |
8d1c802b | 4602 | struct fib6_info *iter; |
d7dedee1 IS |
4603 | int weight = 0; |
4604 | ||
4605 | rt6_upper_bound_set(rt, &weight, total); | |
4606 | ||
93c2fb25 | 4607 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
d7dedee1 IS |
4608 | rt6_upper_bound_set(iter, &weight, total); |
4609 | } | |
4610 | ||
8d1c802b | 4611 | void rt6_multipath_rebalance(struct fib6_info *rt) |
d7dedee1 | 4612 | { |
8d1c802b | 4613 | struct fib6_info *first; |
d7dedee1 IS |
4614 | int total; |
4615 | ||
4616 | /* In case the entire multipath route was marked for flushing, | |
4617 | * then there is no need to rebalance upon the removal of every | |
4618 | * sibling route. | |
4619 | */ | |
93c2fb25 | 4620 | if (!rt->fib6_nsiblings || rt->should_flush) |
d7dedee1 IS |
4621 | return; |
4622 | ||
4623 | /* During lookup routes are evaluated in order, so we need to | |
4624 | * make sure upper bounds are assigned from the first sibling | |
4625 | * onwards. | |
4626 | */ | |
4627 | first = rt6_multipath_first_sibling(rt); | |
4628 | if (WARN_ON_ONCE(!first)) | |
4629 | return; | |
4630 | ||
4631 | total = rt6_multipath_total_weight(first); | |
4632 | rt6_multipath_upper_bound_set(first, total); | |
4633 | } | |
4634 | ||
8d1c802b | 4635 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
2127d95a IS |
4636 | { |
4637 | const struct arg_netdev_event *arg = p_arg; | |
7aef6859 | 4638 | struct net *net = dev_net(arg->dev); |
2127d95a | 4639 | |
f88d8ea6 | 4640 | if (rt != net->ipv6.fib6_null_entry && !rt->nh && |
1cf844c7 DA |
4641 | rt->fib6_nh->fib_nh_dev == arg->dev) { |
4642 | rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; | |
7aef6859 | 4643 | fib6_update_sernum_upto_root(net, rt); |
d7dedee1 | 4644 | rt6_multipath_rebalance(rt); |
1de178ed | 4645 | } |
2127d95a IS |
4646 | |
4647 | return 0; | |
4648 | } | |
4649 | ||
ecc5663c | 4650 | void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) |
2127d95a IS |
4651 | { |
4652 | struct arg_netdev_event arg = { | |
4653 | .dev = dev, | |
6802f3ad IS |
4654 | { |
4655 | .nh_flags = nh_flags, | |
4656 | }, | |
2127d95a IS |
4657 | }; |
4658 | ||
4659 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) | |
4660 | arg.nh_flags |= RTNH_F_LINKDOWN; | |
4661 | ||
4662 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); | |
4663 | } | |
4664 | ||
f88d8ea6 | 4665 | /* only called for fib entries with inline fib6_nh */ |
8d1c802b | 4666 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
1de178ed IS |
4667 | const struct net_device *dev) |
4668 | { | |
8d1c802b | 4669 | struct fib6_info *iter; |
1de178ed | 4670 | |
1cf844c7 | 4671 | if (rt->fib6_nh->fib_nh_dev == dev) |
1de178ed | 4672 | return true; |
93c2fb25 | 4673 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 | 4674 | if (iter->fib6_nh->fib_nh_dev == dev) |
1de178ed IS |
4675 | return true; |
4676 | ||
4677 | return false; | |
4678 | } | |
4679 | ||
8d1c802b | 4680 | static void rt6_multipath_flush(struct fib6_info *rt) |
1de178ed | 4681 | { |
8d1c802b | 4682 | struct fib6_info *iter; |
1de178ed IS |
4683 | |
4684 | rt->should_flush = 1; | |
93c2fb25 | 4685 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1de178ed IS |
4686 | iter->should_flush = 1; |
4687 | } | |
4688 | ||
8d1c802b | 4689 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
1de178ed IS |
4690 | const struct net_device *down_dev) |
4691 | { | |
8d1c802b | 4692 | struct fib6_info *iter; |
1de178ed IS |
4693 | unsigned int dead = 0; |
4694 | ||
1cf844c7 DA |
4695 | if (rt->fib6_nh->fib_nh_dev == down_dev || |
4696 | rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) | |
1de178ed | 4697 | dead++; |
93c2fb25 | 4698 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 DA |
4699 | if (iter->fib6_nh->fib_nh_dev == down_dev || |
4700 | iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) | |
1de178ed IS |
4701 | dead++; |
4702 | ||
4703 | return dead; | |
4704 | } | |
4705 | ||
8d1c802b | 4706 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
1de178ed | 4707 | const struct net_device *dev, |
ecc5663c | 4708 | unsigned char nh_flags) |
1de178ed | 4709 | { |
8d1c802b | 4710 | struct fib6_info *iter; |
1de178ed | 4711 | |
1cf844c7 DA |
4712 | if (rt->fib6_nh->fib_nh_dev == dev) |
4713 | rt->fib6_nh->fib_nh_flags |= nh_flags; | |
93c2fb25 | 4714 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 DA |
4715 | if (iter->fib6_nh->fib_nh_dev == dev) |
4716 | iter->fib6_nh->fib_nh_flags |= nh_flags; | |
1de178ed IS |
4717 | } |
4718 | ||
a1a22c12 | 4719 | /* called with write lock held for table with rt */ |
8d1c802b | 4720 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
1da177e4 | 4721 | { |
4c981e28 IS |
4722 | const struct arg_netdev_event *arg = p_arg; |
4723 | const struct net_device *dev = arg->dev; | |
7aef6859 | 4724 | struct net *net = dev_net(dev); |
8ed67789 | 4725 | |
f88d8ea6 | 4726 | if (rt == net->ipv6.fib6_null_entry || rt->nh) |
27c6fa73 IS |
4727 | return 0; |
4728 | ||
4729 | switch (arg->event) { | |
4730 | case NETDEV_UNREGISTER: | |
1cf844c7 | 4731 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
27c6fa73 | 4732 | case NETDEV_DOWN: |
1de178ed | 4733 | if (rt->should_flush) |
27c6fa73 | 4734 | return -1; |
93c2fb25 | 4735 | if (!rt->fib6_nsiblings) |
1cf844c7 | 4736 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
1de178ed IS |
4737 | if (rt6_multipath_uses_dev(rt, dev)) { |
4738 | unsigned int count; | |
4739 | ||
4740 | count = rt6_multipath_dead_count(rt, dev); | |
93c2fb25 | 4741 | if (rt->fib6_nsiblings + 1 == count) { |
1de178ed IS |
4742 | rt6_multipath_flush(rt); |
4743 | return -1; | |
4744 | } | |
4745 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | | |
4746 | RTNH_F_LINKDOWN); | |
7aef6859 | 4747 | fib6_update_sernum(net, rt); |
d7dedee1 | 4748 | rt6_multipath_rebalance(rt); |
1de178ed IS |
4749 | } |
4750 | return -2; | |
27c6fa73 | 4751 | case NETDEV_CHANGE: |
1cf844c7 | 4752 | if (rt->fib6_nh->fib_nh_dev != dev || |
93c2fb25 | 4753 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
27c6fa73 | 4754 | break; |
1cf844c7 | 4755 | rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
d7dedee1 | 4756 | rt6_multipath_rebalance(rt); |
27c6fa73 | 4757 | break; |
2b241361 | 4758 | } |
c159d30c | 4759 | |
1da177e4 LT |
4760 | return 0; |
4761 | } | |
4762 | ||
27c6fa73 | 4763 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
1da177e4 | 4764 | { |
4c981e28 | 4765 | struct arg_netdev_event arg = { |
8ed67789 | 4766 | .dev = dev, |
6802f3ad IS |
4767 | { |
4768 | .event = event, | |
4769 | }, | |
8ed67789 | 4770 | }; |
7c6bb7d2 | 4771 | struct net *net = dev_net(dev); |
8ed67789 | 4772 | |
7c6bb7d2 DA |
4773 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
4774 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); | |
4775 | else | |
4776 | fib6_clean_all(net, fib6_ifdown, &arg); | |
4c981e28 IS |
4777 | } |
4778 | ||
4779 | void rt6_disable_ip(struct net_device *dev, unsigned long event) | |
4780 | { | |
4781 | rt6_sync_down_dev(dev, event); | |
4782 | rt6_uncached_list_flush_dev(dev_net(dev), dev); | |
4783 | neigh_ifdown(&nd_tbl, dev); | |
1da177e4 LT |
4784 | } |
4785 | ||
95c96174 | 4786 | struct rt6_mtu_change_arg { |
1da177e4 | 4787 | struct net_device *dev; |
95c96174 | 4788 | unsigned int mtu; |
c0b220cf | 4789 | struct fib6_info *f6i; |
1da177e4 LT |
4790 | }; |
4791 | ||
cc5c073a | 4792 | static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) |
c0b220cf DA |
4793 | { |
4794 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; | |
cc5c073a | 4795 | struct fib6_info *f6i = arg->f6i; |
c0b220cf DA |
4796 | |
4797 | /* For administrative MTU increase, there is no way to discover | |
4798 | * IPv6 PMTU increase, so PMTU increase should be updated here. | |
4799 | * Since RFC 1981 doesn't include administrative MTU increase | |
4800 | * update PMTU increase is a MUST. (i.e. jumbo frame) | |
4801 | */ | |
4802 | if (nh->fib_nh_dev == arg->dev) { | |
4803 | struct inet6_dev *idev = __in6_dev_get(arg->dev); | |
4804 | u32 mtu = f6i->fib6_pmtu; | |
4805 | ||
4806 | if (mtu >= arg->mtu || | |
4807 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) | |
4808 | fib6_metric_set(f6i, RTAX_MTU, arg->mtu); | |
4809 | ||
4810 | spin_lock_bh(&rt6_exception_lock); | |
cc5c073a | 4811 | rt6_exceptions_update_pmtu(idev, nh, arg->mtu); |
c0b220cf DA |
4812 | spin_unlock_bh(&rt6_exception_lock); |
4813 | } | |
4814 | ||
4815 | return 0; | |
4816 | } | |
4817 | ||
4818 | static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) | |
1da177e4 LT |
4819 | { |
4820 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; | |
4821 | struct inet6_dev *idev; | |
4822 | ||
4823 | /* In IPv6 pmtu discovery is not optional, | |
4824 | so that RTAX_MTU lock cannot disable it. | |
4825 | We still use this lock to block changes | |
4826 | caused by addrconf/ndisc. | |
4827 | */ | |
4828 | ||
4829 | idev = __in6_dev_get(arg->dev); | |
38308473 | 4830 | if (!idev) |
1da177e4 LT |
4831 | return 0; |
4832 | ||
c0b220cf DA |
4833 | if (fib6_metric_locked(f6i, RTAX_MTU)) |
4834 | return 0; | |
d4ead6b3 | 4835 | |
c0b220cf | 4836 | arg->f6i = f6i; |
2d44234b DA |
4837 | if (f6i->nh) { |
4838 | /* fib6_nh_mtu_change only returns 0, so this is safe */ | |
4839 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, | |
4840 | arg); | |
4841 | } | |
4842 | ||
1cf844c7 | 4843 | return fib6_nh_mtu_change(f6i->fib6_nh, arg); |
1da177e4 LT |
4844 | } |
4845 | ||
95c96174 | 4846 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
1da177e4 | 4847 | { |
c71099ac TG |
4848 | struct rt6_mtu_change_arg arg = { |
4849 | .dev = dev, | |
4850 | .mtu = mtu, | |
4851 | }; | |
1da177e4 | 4852 | |
0c3584d5 | 4853 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
1da177e4 LT |
4854 | } |
4855 | ||
ef7c79ed | 4856 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
75425657 | 4857 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
5176f91e | 4858 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
aa8f8778 | 4859 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
86872cb5 | 4860 | [RTA_OIF] = { .type = NLA_U32 }, |
ab364a6f | 4861 | [RTA_IIF] = { .type = NLA_U32 }, |
86872cb5 TG |
4862 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
4863 | [RTA_METRICS] = { .type = NLA_NESTED }, | |
51ebd318 | 4864 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
c78ba6d6 | 4865 | [RTA_PREF] = { .type = NLA_U8 }, |
19e42e45 RP |
4866 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
4867 | [RTA_ENCAP] = { .type = NLA_NESTED }, | |
32bc201e | 4868 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
622ec2c9 | 4869 | [RTA_UID] = { .type = NLA_U32 }, |
3b45a410 | 4870 | [RTA_MARK] = { .type = NLA_U32 }, |
aa8f8778 | 4871 | [RTA_TABLE] = { .type = NLA_U32 }, |
eacb9384 RP |
4872 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
4873 | [RTA_SPORT] = { .type = NLA_U16 }, | |
4874 | [RTA_DPORT] = { .type = NLA_U16 }, | |
5b98324e | 4875 | [RTA_NH_ID] = { .type = NLA_U32 }, |
86872cb5 TG |
4876 | }; |
4877 | ||
4878 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, | |
333c4301 DA |
4879 | struct fib6_config *cfg, |
4880 | struct netlink_ext_ack *extack) | |
1da177e4 | 4881 | { |
86872cb5 TG |
4882 | struct rtmsg *rtm; |
4883 | struct nlattr *tb[RTA_MAX+1]; | |
c78ba6d6 | 4884 | unsigned int pref; |
86872cb5 | 4885 | int err; |
1da177e4 | 4886 | |
8cb08174 JB |
4887 | err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
4888 | rtm_ipv6_policy, extack); | |
86872cb5 TG |
4889 | if (err < 0) |
4890 | goto errout; | |
1da177e4 | 4891 | |
86872cb5 TG |
4892 | err = -EINVAL; |
4893 | rtm = nlmsg_data(nlh); | |
86872cb5 | 4894 | |
84db8407 MŻ |
4895 | *cfg = (struct fib6_config){ |
4896 | .fc_table = rtm->rtm_table, | |
4897 | .fc_dst_len = rtm->rtm_dst_len, | |
4898 | .fc_src_len = rtm->rtm_src_len, | |
4899 | .fc_flags = RTF_UP, | |
4900 | .fc_protocol = rtm->rtm_protocol, | |
4901 | .fc_type = rtm->rtm_type, | |
4902 | ||
4903 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, | |
4904 | .fc_nlinfo.nlh = nlh, | |
4905 | .fc_nlinfo.nl_net = sock_net(skb->sk), | |
4906 | }; | |
86872cb5 | 4907 | |
ef2c7d7b ND |
4908 | if (rtm->rtm_type == RTN_UNREACHABLE || |
4909 | rtm->rtm_type == RTN_BLACKHOLE || | |
b4949ab2 ND |
4910 | rtm->rtm_type == RTN_PROHIBIT || |
4911 | rtm->rtm_type == RTN_THROW) | |
86872cb5 TG |
4912 | cfg->fc_flags |= RTF_REJECT; |
4913 | ||
ab79ad14 MŻ |
4914 | if (rtm->rtm_type == RTN_LOCAL) |
4915 | cfg->fc_flags |= RTF_LOCAL; | |
4916 | ||
1f56a01f MKL |
4917 | if (rtm->rtm_flags & RTM_F_CLONED) |
4918 | cfg->fc_flags |= RTF_CACHE; | |
4919 | ||
fc1e64e1 DA |
4920 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
4921 | ||
5b98324e DA |
4922 | if (tb[RTA_NH_ID]) { |
4923 | if (tb[RTA_GATEWAY] || tb[RTA_OIF] || | |
4924 | tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { | |
4925 | NL_SET_ERR_MSG(extack, | |
4926 | "Nexthop specification and nexthop id are mutually exclusive"); | |
4927 | goto errout; | |
4928 | } | |
4929 | cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); | |
4930 | } | |
4931 | ||
86872cb5 | 4932 | if (tb[RTA_GATEWAY]) { |
67b61f6c | 4933 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
86872cb5 | 4934 | cfg->fc_flags |= RTF_GATEWAY; |
1da177e4 | 4935 | } |
e3818541 DA |
4936 | if (tb[RTA_VIA]) { |
4937 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); | |
4938 | goto errout; | |
4939 | } | |
86872cb5 TG |
4940 | |
4941 | if (tb[RTA_DST]) { | |
4942 | int plen = (rtm->rtm_dst_len + 7) >> 3; | |
4943 | ||
4944 | if (nla_len(tb[RTA_DST]) < plen) | |
4945 | goto errout; | |
4946 | ||
4947 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); | |
1da177e4 | 4948 | } |
86872cb5 TG |
4949 | |
4950 | if (tb[RTA_SRC]) { | |
4951 | int plen = (rtm->rtm_src_len + 7) >> 3; | |
4952 | ||
4953 | if (nla_len(tb[RTA_SRC]) < plen) | |
4954 | goto errout; | |
4955 | ||
4956 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); | |
1da177e4 | 4957 | } |
86872cb5 | 4958 | |
c3968a85 | 4959 | if (tb[RTA_PREFSRC]) |
67b61f6c | 4960 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
c3968a85 | 4961 | |
86872cb5 TG |
4962 | if (tb[RTA_OIF]) |
4963 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); | |
4964 | ||
4965 | if (tb[RTA_PRIORITY]) | |
4966 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); | |
4967 | ||
4968 | if (tb[RTA_METRICS]) { | |
4969 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); | |
4970 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); | |
1da177e4 | 4971 | } |
86872cb5 TG |
4972 | |
4973 | if (tb[RTA_TABLE]) | |
4974 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); | |
4975 | ||
51ebd318 ND |
4976 | if (tb[RTA_MULTIPATH]) { |
4977 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); | |
4978 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); | |
9ed59592 DA |
4979 | |
4980 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, | |
c255bd68 | 4981 | cfg->fc_mp_len, extack); |
9ed59592 DA |
4982 | if (err < 0) |
4983 | goto errout; | |
51ebd318 ND |
4984 | } |
4985 | ||
c78ba6d6 LR |
4986 | if (tb[RTA_PREF]) { |
4987 | pref = nla_get_u8(tb[RTA_PREF]); | |
4988 | if (pref != ICMPV6_ROUTER_PREF_LOW && | |
4989 | pref != ICMPV6_ROUTER_PREF_HIGH) | |
4990 | pref = ICMPV6_ROUTER_PREF_MEDIUM; | |
4991 | cfg->fc_flags |= RTF_PREF(pref); | |
4992 | } | |
4993 | ||
19e42e45 RP |
4994 | if (tb[RTA_ENCAP]) |
4995 | cfg->fc_encap = tb[RTA_ENCAP]; | |
4996 | ||
9ed59592 | 4997 | if (tb[RTA_ENCAP_TYPE]) { |
19e42e45 RP |
4998 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
4999 | ||
c255bd68 | 5000 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
9ed59592 DA |
5001 | if (err < 0) |
5002 | goto errout; | |
5003 | } | |
5004 | ||
32bc201e XL |
5005 | if (tb[RTA_EXPIRES]) { |
5006 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); | |
5007 | ||
5008 | if (addrconf_finite_timeout(timeout)) { | |
5009 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); | |
5010 | cfg->fc_flags |= RTF_EXPIRES; | |
5011 | } | |
5012 | } | |
5013 | ||
86872cb5 TG |
5014 | err = 0; |
5015 | errout: | |
5016 | return err; | |
1da177e4 LT |
5017 | } |
5018 | ||
6b9ea5a6 | 5019 | struct rt6_nh { |
8d1c802b | 5020 | struct fib6_info *fib6_info; |
6b9ea5a6 | 5021 | struct fib6_config r_cfg; |
6b9ea5a6 RP |
5022 | struct list_head next; |
5023 | }; | |
5024 | ||
d4ead6b3 DA |
5025 | static int ip6_route_info_append(struct net *net, |
5026 | struct list_head *rt6_nh_list, | |
8d1c802b DA |
5027 | struct fib6_info *rt, |
5028 | struct fib6_config *r_cfg) | |
6b9ea5a6 RP |
5029 | { |
5030 | struct rt6_nh *nh; | |
6b9ea5a6 RP |
5031 | int err = -EEXIST; |
5032 | ||
5033 | list_for_each_entry(nh, rt6_nh_list, next) { | |
8d1c802b DA |
5034 | /* check if fib6_info already exists */ |
5035 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) | |
6b9ea5a6 RP |
5036 | return err; |
5037 | } | |
5038 | ||
5039 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); | |
5040 | if (!nh) | |
5041 | return -ENOMEM; | |
8d1c802b | 5042 | nh->fib6_info = rt; |
6b9ea5a6 RP |
5043 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
5044 | list_add_tail(&nh->next, rt6_nh_list); | |
5045 | ||
5046 | return 0; | |
5047 | } | |
5048 | ||
8d1c802b DA |
5049 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
5050 | struct fib6_info *rt_last, | |
3b1137fe DA |
5051 | struct nl_info *info, |
5052 | __u16 nlflags) | |
5053 | { | |
5054 | /* if this is an APPEND route, then rt points to the first route | |
5055 | * inserted and rt_last points to last route inserted. Userspace | |
5056 | * wants a consistent dump of the route which starts at the first | |
5057 | * nexthop. Since sibling routes are always added at the end of | |
5058 | * the list, find the first sibling of the last route appended | |
5059 | */ | |
93c2fb25 DA |
5060 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
5061 | rt = list_first_entry(&rt_last->fib6_siblings, | |
8d1c802b | 5062 | struct fib6_info, |
93c2fb25 | 5063 | fib6_siblings); |
3b1137fe DA |
5064 | } |
5065 | ||
5066 | if (rt) | |
5067 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); | |
5068 | } | |
5069 | ||
0ee0f47c IS |
5070 | static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) |
5071 | { | |
5072 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); | |
5073 | bool should_notify = false; | |
5074 | struct fib6_info *leaf; | |
5075 | struct fib6_node *fn; | |
5076 | ||
5077 | rcu_read_lock(); | |
5078 | fn = rcu_dereference(rt->fib6_node); | |
5079 | if (!fn) | |
5080 | goto out; | |
5081 | ||
5082 | leaf = rcu_dereference(fn->leaf); | |
5083 | if (!leaf) | |
5084 | goto out; | |
5085 | ||
5086 | if (rt == leaf || | |
5087 | (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && | |
5088 | rt6_qualify_for_ecmp(leaf))) | |
5089 | should_notify = true; | |
5090 | out: | |
5091 | rcu_read_unlock(); | |
5092 | ||
5093 | return should_notify; | |
5094 | } | |
5095 | ||
333c4301 DA |
5096 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
5097 | struct netlink_ext_ack *extack) | |
51ebd318 | 5098 | { |
8d1c802b | 5099 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
3b1137fe | 5100 | struct nl_info *info = &cfg->fc_nlinfo; |
51ebd318 ND |
5101 | struct fib6_config r_cfg; |
5102 | struct rtnexthop *rtnh; | |
8d1c802b | 5103 | struct fib6_info *rt; |
6b9ea5a6 RP |
5104 | struct rt6_nh *err_nh; |
5105 | struct rt6_nh *nh, *nh_safe; | |
3b1137fe | 5106 | __u16 nlflags; |
51ebd318 ND |
5107 | int remaining; |
5108 | int attrlen; | |
6b9ea5a6 RP |
5109 | int err = 1; |
5110 | int nhn = 0; | |
5111 | int replace = (cfg->fc_nlinfo.nlh && | |
5112 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); | |
5113 | LIST_HEAD(rt6_nh_list); | |
51ebd318 | 5114 | |
3b1137fe DA |
5115 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
5116 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) | |
5117 | nlflags |= NLM_F_APPEND; | |
5118 | ||
35f1b4e9 | 5119 | remaining = cfg->fc_mp_len; |
51ebd318 | 5120 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
51ebd318 | 5121 | |
6b9ea5a6 | 5122 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
8d1c802b | 5123 | * fib6_info structs per nexthop |
6b9ea5a6 | 5124 | */ |
51ebd318 ND |
5125 | while (rtnh_ok(rtnh, remaining)) { |
5126 | memcpy(&r_cfg, cfg, sizeof(*cfg)); | |
5127 | if (rtnh->rtnh_ifindex) | |
5128 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; | |
5129 | ||
5130 | attrlen = rtnh_attrlen(rtnh); | |
5131 | if (attrlen > 0) { | |
5132 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
5133 | ||
5134 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
5135 | if (nla) { | |
67b61f6c | 5136 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
51ebd318 ND |
5137 | r_cfg.fc_flags |= RTF_GATEWAY; |
5138 | } | |
19e42e45 RP |
5139 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
5140 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); | |
5141 | if (nla) | |
5142 | r_cfg.fc_encap_type = nla_get_u16(nla); | |
51ebd318 | 5143 | } |
6b9ea5a6 | 5144 | |
68e2ffde | 5145 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
acb54e3c | 5146 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
8c5b83f0 RP |
5147 | if (IS_ERR(rt)) { |
5148 | err = PTR_ERR(rt); | |
5149 | rt = NULL; | |
6b9ea5a6 | 5150 | goto cleanup; |
8c5b83f0 | 5151 | } |
b5d2d75e DA |
5152 | if (!rt6_qualify_for_ecmp(rt)) { |
5153 | err = -EINVAL; | |
5154 | NL_SET_ERR_MSG(extack, | |
5155 | "Device only routes can not be added for IPv6 using the multipath API."); | |
5156 | fib6_info_release(rt); | |
5157 | goto cleanup; | |
5158 | } | |
6b9ea5a6 | 5159 | |
1cf844c7 | 5160 | rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; |
398958ae | 5161 | |
d4ead6b3 DA |
5162 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
5163 | rt, &r_cfg); | |
51ebd318 | 5164 | if (err) { |
93531c67 | 5165 | fib6_info_release(rt); |
6b9ea5a6 RP |
5166 | goto cleanup; |
5167 | } | |
5168 | ||
5169 | rtnh = rtnh_next(rtnh, &remaining); | |
5170 | } | |
5171 | ||
9eee3b49 IS |
5172 | if (list_empty(&rt6_nh_list)) { |
5173 | NL_SET_ERR_MSG(extack, | |
5174 | "Invalid nexthop configuration - no valid nexthops"); | |
5175 | return -EINVAL; | |
5176 | } | |
5177 | ||
3b1137fe DA |
5178 | /* for add and replace send one notification with all nexthops. |
5179 | * Skip the notification in fib6_add_rt2node and send one with | |
5180 | * the full route when done | |
5181 | */ | |
5182 | info->skip_notify = 1; | |
5183 | ||
ebee3cad IS |
5184 | /* For add and replace, send one notification with all nexthops. For |
5185 | * append, send one notification with all appended nexthops. | |
5186 | */ | |
5187 | info->skip_notify_kernel = 1; | |
5188 | ||
6b9ea5a6 RP |
5189 | err_nh = NULL; |
5190 | list_for_each_entry(nh, &rt6_nh_list, next) { | |
8d1c802b DA |
5191 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
5192 | fib6_info_release(nh->fib6_info); | |
93531c67 | 5193 | |
f7225172 DA |
5194 | if (!err) { |
5195 | /* save reference to last route successfully inserted */ | |
5196 | rt_last = nh->fib6_info; | |
5197 | ||
5198 | /* save reference to first route for notification */ | |
5199 | if (!rt_notif) | |
5200 | rt_notif = nh->fib6_info; | |
5201 | } | |
3b1137fe | 5202 | |
8d1c802b DA |
5203 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
5204 | nh->fib6_info = NULL; | |
6b9ea5a6 RP |
5205 | if (err) { |
5206 | if (replace && nhn) | |
a5a82d84 JK |
5207 | NL_SET_ERR_MSG_MOD(extack, |
5208 | "multipath route replace failed (check consistency of installed routes)"); | |
6b9ea5a6 RP |
5209 | err_nh = nh; |
5210 | goto add_errout; | |
51ebd318 | 5211 | } |
6b9ea5a6 | 5212 | |
1a72418b | 5213 | /* Because each route is added like a single route we remove |
27596472 MK |
5214 | * these flags after the first nexthop: if there is a collision, |
5215 | * we have already failed to add the first nexthop: | |
5216 | * fib6_add_rt2node() has rejected it; when replacing, old | |
5217 | * nexthops have been replaced by first new, the rest should | |
5218 | * be added to it. | |
1a72418b | 5219 | */ |
27596472 MK |
5220 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
5221 | NLM_F_REPLACE); | |
afecdb37 | 5222 | cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; |
6b9ea5a6 RP |
5223 | nhn++; |
5224 | } | |
5225 | ||
0ee0f47c IS |
5226 | /* An in-kernel notification should only be sent in case the new |
5227 | * multipath route is added as the first route in the node, or if | |
5228 | * it was appended to it. We pass 'rt_notif' since it is the first | |
5229 | * sibling and might allow us to skip some checks in the replace case. | |
5230 | */ | |
5231 | if (ip6_route_mpath_should_notify(rt_notif)) { | |
5232 | enum fib_event_type fib_event; | |
5233 | ||
5234 | if (rt_notif->fib6_nsiblings != nhn - 1) | |
5235 | fib_event = FIB_EVENT_ENTRY_APPEND; | |
5236 | else | |
caafb250 | 5237 | fib_event = FIB_EVENT_ENTRY_REPLACE; |
0ee0f47c IS |
5238 | |
5239 | err = call_fib6_multipath_entry_notifiers(info->nl_net, | |
5240 | fib_event, rt_notif, | |
5241 | nhn - 1, extack); | |
5242 | if (err) { | |
5243 | /* Delete all the siblings that were just added */ | |
5244 | err_nh = NULL; | |
5245 | goto add_errout; | |
5246 | } | |
5247 | } | |
ebee3cad | 5248 | |
3b1137fe DA |
5249 | /* success ... tell user about new route */ |
5250 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); | |
6b9ea5a6 RP |
5251 | goto cleanup; |
5252 | ||
5253 | add_errout: | |
3b1137fe DA |
5254 | /* send notification for routes that were added so that |
5255 | * the delete notifications sent by ip6_route_del are | |
5256 | * coherent | |
5257 | */ | |
5258 | if (rt_notif) | |
5259 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); | |
5260 | ||
6b9ea5a6 RP |
5261 | /* Delete routes that were already added */ |
5262 | list_for_each_entry(nh, &rt6_nh_list, next) { | |
5263 | if (err_nh == nh) | |
5264 | break; | |
333c4301 | 5265 | ip6_route_del(&nh->r_cfg, extack); |
6b9ea5a6 RP |
5266 | } |
5267 | ||
5268 | cleanup: | |
5269 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { | |
8d1c802b DA |
5270 | if (nh->fib6_info) |
5271 | fib6_info_release(nh->fib6_info); | |
6b9ea5a6 RP |
5272 | list_del(&nh->next); |
5273 | kfree(nh); | |
5274 | } | |
5275 | ||
5276 | return err; | |
5277 | } | |
5278 | ||
333c4301 DA |
5279 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
5280 | struct netlink_ext_ack *extack) | |
6b9ea5a6 RP |
5281 | { |
5282 | struct fib6_config r_cfg; | |
5283 | struct rtnexthop *rtnh; | |
5284 | int remaining; | |
5285 | int attrlen; | |
5286 | int err = 1, last_err = 0; | |
5287 | ||
5288 | remaining = cfg->fc_mp_len; | |
5289 | rtnh = (struct rtnexthop *)cfg->fc_mp; | |
5290 | ||
5291 | /* Parse a Multipath Entry */ | |
5292 | while (rtnh_ok(rtnh, remaining)) { | |
5293 | memcpy(&r_cfg, cfg, sizeof(*cfg)); | |
5294 | if (rtnh->rtnh_ifindex) | |
5295 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; | |
5296 | ||
5297 | attrlen = rtnh_attrlen(rtnh); | |
5298 | if (attrlen > 0) { | |
5299 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
5300 | ||
5301 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
5302 | if (nla) { | |
5303 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); | |
5304 | r_cfg.fc_flags |= RTF_GATEWAY; | |
5305 | } | |
5306 | } | |
333c4301 | 5307 | err = ip6_route_del(&r_cfg, extack); |
6b9ea5a6 RP |
5308 | if (err) |
5309 | last_err = err; | |
5310 | ||
51ebd318 ND |
5311 | rtnh = rtnh_next(rtnh, &remaining); |
5312 | } | |
5313 | ||
5314 | return last_err; | |
5315 | } | |
5316 | ||
c21ef3e3 DA |
5317 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
5318 | struct netlink_ext_ack *extack) | |
1da177e4 | 5319 | { |
86872cb5 TG |
5320 | struct fib6_config cfg; |
5321 | int err; | |
1da177e4 | 5322 | |
333c4301 | 5323 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
86872cb5 TG |
5324 | if (err < 0) |
5325 | return err; | |
5326 | ||
5b98324e DA |
5327 | if (cfg.fc_nh_id && |
5328 | !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { | |
5329 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); | |
5330 | return -EINVAL; | |
5331 | } | |
5332 | ||
51ebd318 | 5333 | if (cfg.fc_mp) |
333c4301 | 5334 | return ip6_route_multipath_del(&cfg, extack); |
0ae81335 DA |
5335 | else { |
5336 | cfg.fc_delete_all_nh = 1; | |
333c4301 | 5337 | return ip6_route_del(&cfg, extack); |
0ae81335 | 5338 | } |
1da177e4 LT |
5339 | } |
5340 | ||
c21ef3e3 DA |
5341 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
5342 | struct netlink_ext_ack *extack) | |
1da177e4 | 5343 | { |
86872cb5 TG |
5344 | struct fib6_config cfg; |
5345 | int err; | |
1da177e4 | 5346 | |
333c4301 | 5347 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
86872cb5 TG |
5348 | if (err < 0) |
5349 | return err; | |
5350 | ||
67f69513 DA |
5351 | if (cfg.fc_metric == 0) |
5352 | cfg.fc_metric = IP6_RT_PRIO_USER; | |
5353 | ||
51ebd318 | 5354 | if (cfg.fc_mp) |
333c4301 | 5355 | return ip6_route_multipath_add(&cfg, extack); |
51ebd318 | 5356 | else |
acb54e3c | 5357 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
1da177e4 LT |
5358 | } |
5359 | ||
a1b7a1f0 DA |
5360 | /* add the overhead of this fib6_nh to nexthop_len */ |
5361 | static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) | |
339bf98f | 5362 | { |
a1b7a1f0 | 5363 | int *nexthop_len = arg; |
beb1afac | 5364 | |
a1b7a1f0 DA |
5365 | *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ |
5366 | + NLA_ALIGN(sizeof(struct rtnexthop)) | |
5367 | + nla_total_size(16); /* RTA_GATEWAY */ | |
5368 | ||
5369 | if (nh->fib_nh_lws) { | |
5370 | /* RTA_ENCAP_TYPE */ | |
5371 | *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); | |
5372 | /* RTA_ENCAP */ | |
5373 | *nexthop_len += nla_total_size(2); | |
5374 | } | |
f88d8ea6 | 5375 | |
a1b7a1f0 DA |
5376 | return 0; |
5377 | } | |
beb1afac | 5378 | |
a1b7a1f0 DA |
5379 | static size_t rt6_nlmsg_size(struct fib6_info *f6i) |
5380 | { | |
5381 | int nexthop_len; | |
5382 | ||
5383 | if (f6i->nh) { | |
5384 | nexthop_len = nla_total_size(4); /* RTA_NH_ID */ | |
5385 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, | |
5386 | &nexthop_len); | |
5387 | } else { | |
5388 | struct fib6_nh *nh = f6i->fib6_nh; | |
5389 | ||
5390 | nexthop_len = 0; | |
5391 | if (f6i->fib6_nsiblings) { | |
5392 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ | |
5393 | + NLA_ALIGN(sizeof(struct rtnexthop)) | |
5394 | + nla_total_size(16) /* RTA_GATEWAY */ | |
5395 | + lwtunnel_get_encap_size(nh->fib_nh_lws); | |
5396 | ||
5397 | nexthop_len *= f6i->fib6_nsiblings; | |
5398 | } | |
5399 | nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); | |
beb1afac DA |
5400 | } |
5401 | ||
339bf98f TG |
5402 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
5403 | + nla_total_size(16) /* RTA_SRC */ | |
5404 | + nla_total_size(16) /* RTA_DST */ | |
5405 | + nla_total_size(16) /* RTA_GATEWAY */ | |
5406 | + nla_total_size(16) /* RTA_PREFSRC */ | |
5407 | + nla_total_size(4) /* RTA_TABLE */ | |
5408 | + nla_total_size(4) /* RTA_IIF */ | |
5409 | + nla_total_size(4) /* RTA_OIF */ | |
5410 | + nla_total_size(4) /* RTA_PRIORITY */ | |
6a2b9ce0 | 5411 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
ea697639 | 5412 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
c78ba6d6 | 5413 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
19e42e45 | 5414 | + nla_total_size(1) /* RTA_PREF */ |
beb1afac DA |
5415 | + nexthop_len; |
5416 | } | |
5417 | ||
f88d8ea6 DA |
5418 | static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, |
5419 | unsigned char *flags) | |
5420 | { | |
5421 | if (nexthop_is_multipath(nh)) { | |
5422 | struct nlattr *mp; | |
5423 | ||
4255ff05 | 5424 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
f88d8ea6 DA |
5425 | if (!mp) |
5426 | goto nla_put_failure; | |
5427 | ||
7bdf4de1 | 5428 | if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) |
f88d8ea6 DA |
5429 | goto nla_put_failure; |
5430 | ||
5431 | nla_nest_end(skb, mp); | |
5432 | } else { | |
5433 | struct fib6_nh *fib6_nh; | |
5434 | ||
5435 | fib6_nh = nexthop_fib6_nh(nh); | |
7bdf4de1 | 5436 | if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, |
f88d8ea6 DA |
5437 | flags, false) < 0) |
5438 | goto nla_put_failure; | |
5439 | } | |
5440 | ||
5441 | return 0; | |
5442 | ||
5443 | nla_put_failure: | |
5444 | return -EMSGSIZE; | |
5445 | } | |
5446 | ||
d4ead6b3 | 5447 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
8d1c802b | 5448 | struct fib6_info *rt, struct dst_entry *dst, |
d4ead6b3 | 5449 | struct in6_addr *dest, struct in6_addr *src, |
15e47304 | 5450 | int iif, int type, u32 portid, u32 seq, |
f8cfe2ce | 5451 | unsigned int flags) |
1da177e4 | 5452 | { |
22d0bd82 XL |
5453 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
5454 | struct rt6key *rt6_dst, *rt6_src; | |
5455 | u32 *pmetrics, table, rt6_flags; | |
f88d8ea6 | 5456 | unsigned char nh_flags = 0; |
2d7202bf | 5457 | struct nlmsghdr *nlh; |
22d0bd82 | 5458 | struct rtmsg *rtm; |
d4ead6b3 | 5459 | long expires = 0; |
1da177e4 | 5460 | |
15e47304 | 5461 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
38308473 | 5462 | if (!nlh) |
26932566 | 5463 | return -EMSGSIZE; |
2d7202bf | 5464 | |
22d0bd82 XL |
5465 | if (rt6) { |
5466 | rt6_dst = &rt6->rt6i_dst; | |
5467 | rt6_src = &rt6->rt6i_src; | |
5468 | rt6_flags = rt6->rt6i_flags; | |
5469 | } else { | |
5470 | rt6_dst = &rt->fib6_dst; | |
5471 | rt6_src = &rt->fib6_src; | |
5472 | rt6_flags = rt->fib6_flags; | |
5473 | } | |
5474 | ||
2d7202bf | 5475 | rtm = nlmsg_data(nlh); |
1da177e4 | 5476 | rtm->rtm_family = AF_INET6; |
22d0bd82 XL |
5477 | rtm->rtm_dst_len = rt6_dst->plen; |
5478 | rtm->rtm_src_len = rt6_src->plen; | |
1da177e4 | 5479 | rtm->rtm_tos = 0; |
93c2fb25 DA |
5480 | if (rt->fib6_table) |
5481 | table = rt->fib6_table->tb6_id; | |
c71099ac | 5482 | else |
9e762a4a | 5483 | table = RT6_TABLE_UNSPEC; |
97f0082a | 5484 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
c78679e8 DM |
5485 | if (nla_put_u32(skb, RTA_TABLE, table)) |
5486 | goto nla_put_failure; | |
e8478e80 DA |
5487 | |
5488 | rtm->rtm_type = rt->fib6_type; | |
1da177e4 LT |
5489 | rtm->rtm_flags = 0; |
5490 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
93c2fb25 | 5491 | rtm->rtm_protocol = rt->fib6_protocol; |
1da177e4 | 5492 | |
22d0bd82 | 5493 | if (rt6_flags & RTF_CACHE) |
1da177e4 LT |
5494 | rtm->rtm_flags |= RTM_F_CLONED; |
5495 | ||
d4ead6b3 DA |
5496 | if (dest) { |
5497 | if (nla_put_in6_addr(skb, RTA_DST, dest)) | |
c78679e8 | 5498 | goto nla_put_failure; |
1ab1457c | 5499 | rtm->rtm_dst_len = 128; |
1da177e4 | 5500 | } else if (rtm->rtm_dst_len) |
22d0bd82 | 5501 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
c78679e8 | 5502 | goto nla_put_failure; |
1da177e4 LT |
5503 | #ifdef CONFIG_IPV6_SUBTREES |
5504 | if (src) { | |
930345ea | 5505 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
c78679e8 | 5506 | goto nla_put_failure; |
1ab1457c | 5507 | rtm->rtm_src_len = 128; |
c78679e8 | 5508 | } else if (rtm->rtm_src_len && |
22d0bd82 | 5509 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
c78679e8 | 5510 | goto nla_put_failure; |
1da177e4 | 5511 | #endif |
7bc570c8 YH |
5512 | if (iif) { |
5513 | #ifdef CONFIG_IPV6_MROUTE | |
22d0bd82 | 5514 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
fd61c6ba DA |
5515 | int err = ip6mr_get_route(net, skb, rtm, portid); |
5516 | ||
5517 | if (err == 0) | |
5518 | return 0; | |
5519 | if (err < 0) | |
5520 | goto nla_put_failure; | |
7bc570c8 YH |
5521 | } else |
5522 | #endif | |
c78679e8 DM |
5523 | if (nla_put_u32(skb, RTA_IIF, iif)) |
5524 | goto nla_put_failure; | |
d4ead6b3 | 5525 | } else if (dest) { |
1da177e4 | 5526 | struct in6_addr saddr_buf; |
d4ead6b3 | 5527 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
930345ea | 5528 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
c78679e8 | 5529 | goto nla_put_failure; |
1da177e4 | 5530 | } |
2d7202bf | 5531 | |
93c2fb25 | 5532 | if (rt->fib6_prefsrc.plen) { |
c3968a85 | 5533 | struct in6_addr saddr_buf; |
93c2fb25 | 5534 | saddr_buf = rt->fib6_prefsrc.addr; |
930345ea | 5535 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
c78679e8 | 5536 | goto nla_put_failure; |
c3968a85 DW |
5537 | } |
5538 | ||
d4ead6b3 DA |
5539 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
5540 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) | |
2d7202bf TG |
5541 | goto nla_put_failure; |
5542 | ||
93c2fb25 | 5543 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
c78679e8 | 5544 | goto nla_put_failure; |
8253947e | 5545 | |
beb1afac DA |
5546 | /* For multipath routes, walk the siblings list and add |
5547 | * each as a nexthop within RTA_MULTIPATH. | |
5548 | */ | |
22d0bd82 XL |
5549 | if (rt6) { |
5550 | if (rt6_flags & RTF_GATEWAY && | |
5551 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) | |
5552 | goto nla_put_failure; | |
5553 | ||
5554 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) | |
5555 | goto nla_put_failure; | |
5556 | } else if (rt->fib6_nsiblings) { | |
8d1c802b | 5557 | struct fib6_info *sibling, *next_sibling; |
beb1afac DA |
5558 | struct nlattr *mp; |
5559 | ||
ae0be8de | 5560 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
beb1afac DA |
5561 | if (!mp) |
5562 | goto nla_put_failure; | |
5563 | ||
1cf844c7 | 5564 | if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, |
7bdf4de1 | 5565 | rt->fib6_nh->fib_nh_weight, AF_INET6) < 0) |
beb1afac DA |
5566 | goto nla_put_failure; |
5567 | ||
5568 | list_for_each_entry_safe(sibling, next_sibling, | |
93c2fb25 | 5569 | &rt->fib6_siblings, fib6_siblings) { |
1cf844c7 | 5570 | if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, |
7bdf4de1 DS |
5571 | sibling->fib6_nh->fib_nh_weight, |
5572 | AF_INET6) < 0) | |
beb1afac DA |
5573 | goto nla_put_failure; |
5574 | } | |
5575 | ||
5576 | nla_nest_end(skb, mp); | |
f88d8ea6 DA |
5577 | } else if (rt->nh) { |
5578 | if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) | |
5579 | goto nla_put_failure; | |
ecc5663c | 5580 | |
f88d8ea6 DA |
5581 | if (nexthop_is_blackhole(rt->nh)) |
5582 | rtm->rtm_type = RTN_BLACKHOLE; | |
5583 | ||
5584 | if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) | |
5585 | goto nla_put_failure; | |
5586 | ||
5587 | rtm->rtm_flags |= nh_flags; | |
5588 | } else { | |
7bdf4de1 | 5589 | if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, |
ecc5663c | 5590 | &nh_flags, false) < 0) |
beb1afac | 5591 | goto nla_put_failure; |
ecc5663c DA |
5592 | |
5593 | rtm->rtm_flags |= nh_flags; | |
beb1afac DA |
5594 | } |
5595 | ||
22d0bd82 | 5596 | if (rt6_flags & RTF_EXPIRES) { |
14895687 DA |
5597 | expires = dst ? dst->expires : rt->expires; |
5598 | expires -= jiffies; | |
5599 | } | |
69cdf8f9 | 5600 | |
bb3c4ab9 IS |
5601 | if (!dst) { |
5602 | if (rt->offload) | |
5603 | rtm->rtm_flags |= RTM_F_OFFLOAD; | |
5604 | if (rt->trap) | |
5605 | rtm->rtm_flags |= RTM_F_TRAP; | |
5606 | } | |
5607 | ||
d4ead6b3 | 5608 | if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) |
e3703b3d | 5609 | goto nla_put_failure; |
2d7202bf | 5610 | |
22d0bd82 | 5611 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
c78ba6d6 LR |
5612 | goto nla_put_failure; |
5613 | ||
19e42e45 | 5614 | |
053c095a JB |
5615 | nlmsg_end(skb, nlh); |
5616 | return 0; | |
2d7202bf TG |
5617 | |
5618 | nla_put_failure: | |
26932566 PM |
5619 | nlmsg_cancel(skb, nlh); |
5620 | return -EMSGSIZE; | |
1da177e4 LT |
5621 | } |
5622 | ||
2c170e07 DA |
5623 | static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) |
5624 | { | |
5625 | const struct net_device *dev = arg; | |
5626 | ||
5627 | if (nh->fib_nh_dev == dev) | |
5628 | return 1; | |
5629 | ||
5630 | return 0; | |
5631 | } | |
5632 | ||
13e38901 DA |
5633 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
5634 | const struct net_device *dev) | |
5635 | { | |
2c170e07 DA |
5636 | if (f6i->nh) { |
5637 | struct net_device *_dev = (struct net_device *)dev; | |
5638 | ||
5639 | return !!nexthop_for_each_fib6_nh(f6i->nh, | |
5640 | fib6_info_nh_uses_dev, | |
5641 | _dev); | |
5642 | } | |
5643 | ||
1cf844c7 | 5644 | if (f6i->fib6_nh->fib_nh_dev == dev) |
13e38901 DA |
5645 | return true; |
5646 | ||
5647 | if (f6i->fib6_nsiblings) { | |
5648 | struct fib6_info *sibling, *next_sibling; | |
5649 | ||
5650 | list_for_each_entry_safe(sibling, next_sibling, | |
5651 | &f6i->fib6_siblings, fib6_siblings) { | |
1cf844c7 | 5652 | if (sibling->fib6_nh->fib_nh_dev == dev) |
13e38901 DA |
5653 | return true; |
5654 | } | |
5655 | } | |
5656 | ||
5657 | return false; | |
5658 | } | |
5659 | ||
1e47b483 SB |
5660 | struct fib6_nh_exception_dump_walker { |
5661 | struct rt6_rtnl_dump_arg *dump; | |
5662 | struct fib6_info *rt; | |
5663 | unsigned int flags; | |
5664 | unsigned int skip; | |
5665 | unsigned int count; | |
5666 | }; | |
5667 | ||
5668 | static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) | |
5669 | { | |
5670 | struct fib6_nh_exception_dump_walker *w = arg; | |
5671 | struct rt6_rtnl_dump_arg *dump = w->dump; | |
5672 | struct rt6_exception_bucket *bucket; | |
5673 | struct rt6_exception *rt6_ex; | |
5674 | int i, err; | |
5675 | ||
5676 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); | |
5677 | if (!bucket) | |
5678 | return 0; | |
5679 | ||
5680 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { | |
5681 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { | |
5682 | if (w->skip) { | |
5683 | w->skip--; | |
5684 | continue; | |
5685 | } | |
5686 | ||
5687 | /* Expiration of entries doesn't bump sernum, insertion | |
5688 | * does. Removal is triggered by insertion, so we can | |
5689 | * rely on the fact that if entries change between two | |
5690 | * partial dumps, this node is scanned again completely, | |
5691 | * see rt6_insert_exception() and fib6_dump_table(). | |
5692 | * | |
5693 | * Count expired entries we go through as handled | |
5694 | * entries that we'll skip next time, in case of partial | |
5695 | * node dump. Otherwise, if entries expire meanwhile, | |
5696 | * we'll skip the wrong amount. | |
5697 | */ | |
5698 | if (rt6_check_expired(rt6_ex->rt6i)) { | |
5699 | w->count++; | |
5700 | continue; | |
5701 | } | |
5702 | ||
5703 | err = rt6_fill_node(dump->net, dump->skb, w->rt, | |
5704 | &rt6_ex->rt6i->dst, NULL, NULL, 0, | |
5705 | RTM_NEWROUTE, | |
5706 | NETLINK_CB(dump->cb->skb).portid, | |
5707 | dump->cb->nlh->nlmsg_seq, w->flags); | |
5708 | if (err) | |
5709 | return err; | |
5710 | ||
5711 | w->count++; | |
5712 | } | |
5713 | bucket++; | |
5714 | } | |
5715 | ||
5716 | return 0; | |
5717 | } | |
5718 | ||
bf9a8a06 | 5719 | /* Return -1 if done with node, number of handled routes on partial dump */ |
1e47b483 | 5720 | int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) |
1da177e4 LT |
5721 | { |
5722 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; | |
13e38901 DA |
5723 | struct fib_dump_filter *filter = &arg->filter; |
5724 | unsigned int flags = NLM_F_MULTI; | |
1f17e2f2 | 5725 | struct net *net = arg->net; |
1e47b483 | 5726 | int count = 0; |
1f17e2f2 | 5727 | |
421842ed | 5728 | if (rt == net->ipv6.fib6_null_entry) |
bf9a8a06 | 5729 | return -1; |
1da177e4 | 5730 | |
13e38901 DA |
5731 | if ((filter->flags & RTM_F_PREFIX) && |
5732 | !(rt->fib6_flags & RTF_PREFIX_RT)) { | |
5733 | /* success since this is not a prefix route */ | |
bf9a8a06 | 5734 | return -1; |
13e38901 | 5735 | } |
1e47b483 SB |
5736 | if (filter->filter_set && |
5737 | ((filter->rt_type && rt->fib6_type != filter->rt_type) || | |
5738 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || | |
5739 | (filter->protocol && rt->fib6_protocol != filter->protocol))) { | |
5740 | return -1; | |
5741 | } | |
5742 | ||
5743 | if (filter->filter_set || | |
5744 | !filter->dump_routes || !filter->dump_exceptions) { | |
13e38901 | 5745 | flags |= NLM_F_DUMP_FILTERED; |
f8cfe2ce | 5746 | } |
1da177e4 | 5747 | |
1e47b483 SB |
5748 | if (filter->dump_routes) { |
5749 | if (skip) { | |
5750 | skip--; | |
5751 | } else { | |
5752 | if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, | |
5753 | 0, RTM_NEWROUTE, | |
5754 | NETLINK_CB(arg->cb->skb).portid, | |
5755 | arg->cb->nlh->nlmsg_seq, flags)) { | |
5756 | return 0; | |
5757 | } | |
5758 | count++; | |
5759 | } | |
5760 | } | |
5761 | ||
5762 | if (filter->dump_exceptions) { | |
5763 | struct fib6_nh_exception_dump_walker w = { .dump = arg, | |
5764 | .rt = rt, | |
5765 | .flags = flags, | |
5766 | .skip = skip, | |
5767 | .count = 0 }; | |
5768 | int err; | |
5769 | ||
3b525691 | 5770 | rcu_read_lock(); |
1e47b483 SB |
5771 | if (rt->nh) { |
5772 | err = nexthop_for_each_fib6_nh(rt->nh, | |
5773 | rt6_nh_dump_exceptions, | |
5774 | &w); | |
5775 | } else { | |
5776 | err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); | |
5777 | } | |
3b525691 | 5778 | rcu_read_unlock(); |
1e47b483 SB |
5779 | |
5780 | if (err) | |
5781 | return count += w.count; | |
5782 | } | |
bf9a8a06 SB |
5783 | |
5784 | return -1; | |
1da177e4 LT |
5785 | } |
5786 | ||
0eff0a27 JK |
5787 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
5788 | const struct nlmsghdr *nlh, | |
5789 | struct nlattr **tb, | |
5790 | struct netlink_ext_ack *extack) | |
5791 | { | |
5792 | struct rtmsg *rtm; | |
5793 | int i, err; | |
5794 | ||
5795 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { | |
5796 | NL_SET_ERR_MSG_MOD(extack, | |
5797 | "Invalid header for get route request"); | |
5798 | return -EINVAL; | |
5799 | } | |
5800 | ||
5801 | if (!netlink_strict_get_check(skb)) | |
8cb08174 JB |
5802 | return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
5803 | rtm_ipv6_policy, extack); | |
0eff0a27 JK |
5804 | |
5805 | rtm = nlmsg_data(nlh); | |
5806 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || | |
5807 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || | |
5808 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || | |
5809 | rtm->rtm_type) { | |
5810 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); | |
5811 | return -EINVAL; | |
5812 | } | |
5813 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { | |
5814 | NL_SET_ERR_MSG_MOD(extack, | |
5815 | "Invalid flags for get route request"); | |
5816 | return -EINVAL; | |
5817 | } | |
5818 | ||
8cb08174 JB |
5819 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
5820 | rtm_ipv6_policy, extack); | |
0eff0a27 JK |
5821 | if (err) |
5822 | return err; | |
5823 | ||
5824 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || | |
5825 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { | |
5826 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); | |
5827 | return -EINVAL; | |
5828 | } | |
5829 | ||
5830 | for (i = 0; i <= RTA_MAX; i++) { | |
5831 | if (!tb[i]) | |
5832 | continue; | |
5833 | ||
5834 | switch (i) { | |
5835 | case RTA_SRC: | |
5836 | case RTA_DST: | |
5837 | case RTA_IIF: | |
5838 | case RTA_OIF: | |
5839 | case RTA_MARK: | |
5840 | case RTA_UID: | |
5841 | case RTA_SPORT: | |
5842 | case RTA_DPORT: | |
5843 | case RTA_IP_PROTO: | |
5844 | break; | |
5845 | default: | |
5846 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); | |
5847 | return -EINVAL; | |
5848 | } | |
5849 | } | |
5850 | ||
5851 | return 0; | |
5852 | } | |
5853 | ||
c21ef3e3 DA |
5854 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
5855 | struct netlink_ext_ack *extack) | |
1da177e4 | 5856 | { |
3b1e0a65 | 5857 | struct net *net = sock_net(in_skb->sk); |
ab364a6f | 5858 | struct nlattr *tb[RTA_MAX+1]; |
18c3a61c | 5859 | int err, iif = 0, oif = 0; |
a68886a6 | 5860 | struct fib6_info *from; |
18c3a61c | 5861 | struct dst_entry *dst; |
ab364a6f | 5862 | struct rt6_info *rt; |
1da177e4 | 5863 | struct sk_buff *skb; |
ab364a6f | 5864 | struct rtmsg *rtm; |
744486d4 | 5865 | struct flowi6 fl6 = {}; |
18c3a61c | 5866 | bool fibmatch; |
1da177e4 | 5867 | |
0eff0a27 | 5868 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
ab364a6f TG |
5869 | if (err < 0) |
5870 | goto errout; | |
1da177e4 | 5871 | |
ab364a6f | 5872 | err = -EINVAL; |
38b7097b HFS |
5873 | rtm = nlmsg_data(nlh); |
5874 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); | |
18c3a61c | 5875 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
1da177e4 | 5876 | |
ab364a6f TG |
5877 | if (tb[RTA_SRC]) { |
5878 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) | |
5879 | goto errout; | |
5880 | ||
4e3fd7a0 | 5881 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
ab364a6f TG |
5882 | } |
5883 | ||
5884 | if (tb[RTA_DST]) { | |
5885 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) | |
5886 | goto errout; | |
5887 | ||
4e3fd7a0 | 5888 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
ab364a6f TG |
5889 | } |
5890 | ||
5891 | if (tb[RTA_IIF]) | |
5892 | iif = nla_get_u32(tb[RTA_IIF]); | |
5893 | ||
5894 | if (tb[RTA_OIF]) | |
72331bc0 | 5895 | oif = nla_get_u32(tb[RTA_OIF]); |
1da177e4 | 5896 | |
2e47b291 LC |
5897 | if (tb[RTA_MARK]) |
5898 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); | |
5899 | ||
622ec2c9 LC |
5900 | if (tb[RTA_UID]) |
5901 | fl6.flowi6_uid = make_kuid(current_user_ns(), | |
5902 | nla_get_u32(tb[RTA_UID])); | |
5903 | else | |
5904 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); | |
5905 | ||
eacb9384 RP |
5906 | if (tb[RTA_SPORT]) |
5907 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); | |
5908 | ||
5909 | if (tb[RTA_DPORT]) | |
5910 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); | |
5911 | ||
5912 | if (tb[RTA_IP_PROTO]) { | |
5913 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], | |
5e1a99ea HL |
5914 | &fl6.flowi6_proto, AF_INET6, |
5915 | extack); | |
eacb9384 RP |
5916 | if (err) |
5917 | goto errout; | |
5918 | } | |
5919 | ||
1da177e4 LT |
5920 | if (iif) { |
5921 | struct net_device *dev; | |
72331bc0 SL |
5922 | int flags = 0; |
5923 | ||
121622db FW |
5924 | rcu_read_lock(); |
5925 | ||
5926 | dev = dev_get_by_index_rcu(net, iif); | |
1da177e4 | 5927 | if (!dev) { |
121622db | 5928 | rcu_read_unlock(); |
1da177e4 | 5929 | err = -ENODEV; |
ab364a6f | 5930 | goto errout; |
1da177e4 | 5931 | } |
72331bc0 SL |
5932 | |
5933 | fl6.flowi6_iif = iif; | |
5934 | ||
5935 | if (!ipv6_addr_any(&fl6.saddr)) | |
5936 | flags |= RT6_LOOKUP_F_HAS_SADDR; | |
5937 | ||
b75cc8f9 | 5938 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
121622db FW |
5939 | |
5940 | rcu_read_unlock(); | |
72331bc0 SL |
5941 | } else { |
5942 | fl6.flowi6_oif = oif; | |
5943 | ||
58acfd71 | 5944 | dst = ip6_route_output(net, NULL, &fl6); |
18c3a61c RP |
5945 | } |
5946 | ||
18c3a61c RP |
5947 | |
5948 | rt = container_of(dst, struct rt6_info, dst); | |
5949 | if (rt->dst.error) { | |
5950 | err = rt->dst.error; | |
5951 | ip6_rt_put(rt); | |
5952 | goto errout; | |
1da177e4 LT |
5953 | } |
5954 | ||
9d6acb3b WC |
5955 | if (rt == net->ipv6.ip6_null_entry) { |
5956 | err = rt->dst.error; | |
5957 | ip6_rt_put(rt); | |
5958 | goto errout; | |
5959 | } | |
5960 | ||
ab364a6f | 5961 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
38308473 | 5962 | if (!skb) { |
94e187c0 | 5963 | ip6_rt_put(rt); |
ab364a6f TG |
5964 | err = -ENOBUFS; |
5965 | goto errout; | |
5966 | } | |
1da177e4 | 5967 | |
d8d1f30b | 5968 | skb_dst_set(skb, &rt->dst); |
a68886a6 DA |
5969 | |
5970 | rcu_read_lock(); | |
5971 | from = rcu_dereference(rt->from); | |
886b7a50 MKL |
5972 | if (from) { |
5973 | if (fibmatch) | |
5974 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, | |
5975 | iif, RTM_NEWROUTE, | |
5976 | NETLINK_CB(in_skb).portid, | |
5977 | nlh->nlmsg_seq, 0); | |
5978 | else | |
5979 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, | |
5980 | &fl6.saddr, iif, RTM_NEWROUTE, | |
5981 | NETLINK_CB(in_skb).portid, | |
5982 | nlh->nlmsg_seq, 0); | |
5983 | } else { | |
5984 | err = -ENETUNREACH; | |
5985 | } | |
a68886a6 DA |
5986 | rcu_read_unlock(); |
5987 | ||
1da177e4 | 5988 | if (err < 0) { |
ab364a6f TG |
5989 | kfree_skb(skb); |
5990 | goto errout; | |
1da177e4 LT |
5991 | } |
5992 | ||
15e47304 | 5993 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
ab364a6f | 5994 | errout: |
1da177e4 | 5995 | return err; |
1da177e4 LT |
5996 | } |
5997 | ||
8d1c802b | 5998 | void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, |
37a1d361 | 5999 | unsigned int nlm_flags) |
1da177e4 LT |
6000 | { |
6001 | struct sk_buff *skb; | |
5578689a | 6002 | struct net *net = info->nl_net; |
528c4ceb DL |
6003 | u32 seq; |
6004 | int err; | |
6005 | ||
6006 | err = -ENOBUFS; | |
38308473 | 6007 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
86872cb5 | 6008 | |
19e42e45 | 6009 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
38308473 | 6010 | if (!skb) |
21713ebc TG |
6011 | goto errout; |
6012 | ||
d4ead6b3 DA |
6013 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
6014 | event, info->portid, seq, nlm_flags); | |
26932566 PM |
6015 | if (err < 0) { |
6016 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | |
6017 | WARN_ON(err == -EMSGSIZE); | |
6018 | kfree_skb(skb); | |
6019 | goto errout; | |
6020 | } | |
15e47304 | 6021 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
1ce85fe4 PNA |
6022 | info->nlh, gfp_any()); |
6023 | return; | |
21713ebc TG |
6024 | errout: |
6025 | if (err < 0) | |
5578689a | 6026 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
1da177e4 LT |
6027 | } |
6028 | ||
19a3b7ee DA |
6029 | void fib6_rt_update(struct net *net, struct fib6_info *rt, |
6030 | struct nl_info *info) | |
6031 | { | |
6032 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; | |
6033 | struct sk_buff *skb; | |
6034 | int err = -ENOBUFS; | |
6035 | ||
6036 | /* call_fib6_entry_notifiers will be removed when in-kernel notifier | |
6037 | * is implemented and supported for nexthop objects | |
6038 | */ | |
6039 | call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); | |
6040 | ||
6041 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); | |
6042 | if (!skb) | |
6043 | goto errout; | |
6044 | ||
6045 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, | |
6046 | RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); | |
6047 | if (err < 0) { | |
6048 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | |
6049 | WARN_ON(err == -EMSGSIZE); | |
6050 | kfree_skb(skb); | |
6051 | goto errout; | |
6052 | } | |
6053 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, | |
6054 | info->nlh, gfp_any()); | |
6055 | return; | |
6056 | errout: | |
6057 | if (err < 0) | |
6058 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); | |
6059 | } | |
6060 | ||
8ed67789 | 6061 | static int ip6_route_dev_notify(struct notifier_block *this, |
351638e7 | 6062 | unsigned long event, void *ptr) |
8ed67789 | 6063 | { |
351638e7 | 6064 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
c346dca1 | 6065 | struct net *net = dev_net(dev); |
8ed67789 | 6066 | |
242d3a49 WC |
6067 | if (!(dev->flags & IFF_LOOPBACK)) |
6068 | return NOTIFY_OK; | |
6069 | ||
6070 | if (event == NETDEV_REGISTER) { | |
1cf844c7 | 6071 | net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; |
d8d1f30b | 6072 | net->ipv6.ip6_null_entry->dst.dev = dev; |
8ed67789 DL |
6073 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
6074 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
d8d1f30b | 6075 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
8ed67789 | 6076 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
d8d1f30b | 6077 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
8ed67789 | 6078 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
242d3a49 | 6079 | #endif |
76da0704 WC |
6080 | } else if (event == NETDEV_UNREGISTER && |
6081 | dev->reg_state != NETREG_UNREGISTERED) { | |
6082 | /* NETDEV_UNREGISTER could be fired for multiple times by | |
6083 | * netdev_wait_allrefs(). Make sure we only call this once. | |
6084 | */ | |
12d94a80 | 6085 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
242d3a49 | 6086 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
12d94a80 ED |
6087 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
6088 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); | |
8ed67789 DL |
6089 | #endif |
6090 | } | |
6091 | ||
6092 | return NOTIFY_OK; | |
6093 | } | |
6094 | ||
1da177e4 LT |
6095 | /* |
6096 | * /proc | |
6097 | */ | |
6098 | ||
6099 | #ifdef CONFIG_PROC_FS | |
1da177e4 LT |
6100 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
6101 | { | |
69ddb805 | 6102 | struct net *net = (struct net *)seq->private; |
1da177e4 | 6103 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
69ddb805 DL |
6104 | net->ipv6.rt6_stats->fib_nodes, |
6105 | net->ipv6.rt6_stats->fib_route_nodes, | |
81eb8447 | 6106 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
69ddb805 DL |
6107 | net->ipv6.rt6_stats->fib_rt_entries, |
6108 | net->ipv6.rt6_stats->fib_rt_cache, | |
fc66f95c | 6109 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
69ddb805 | 6110 | net->ipv6.rt6_stats->fib_discarded_routes); |
1da177e4 LT |
6111 | |
6112 | return 0; | |
6113 | } | |
1da177e4 LT |
6114 | #endif /* CONFIG_PROC_FS */ |
6115 | ||
6116 | #ifdef CONFIG_SYSCTL | |
6117 | ||
1da177e4 | 6118 | static |
fe2c6338 | 6119 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
1da177e4 LT |
6120 | void __user *buffer, size_t *lenp, loff_t *ppos) |
6121 | { | |
c486da34 LAG |
6122 | struct net *net; |
6123 | int delay; | |
f0fb9b28 | 6124 | int ret; |
c486da34 | 6125 | if (!write) |
1da177e4 | 6126 | return -EINVAL; |
c486da34 LAG |
6127 | |
6128 | net = (struct net *)ctl->extra1; | |
6129 | delay = net->ipv6.sysctl.flush_delay; | |
f0fb9b28 AP |
6130 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
6131 | if (ret) | |
6132 | return ret; | |
6133 | ||
2ac3ac8f | 6134 | fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); |
c486da34 | 6135 | return 0; |
1da177e4 LT |
6136 | } |
6137 | ||
ed792e28 | 6138 | static struct ctl_table ipv6_route_table_template[] = { |
1ab1457c | 6139 | { |
1da177e4 | 6140 | .procname = "flush", |
4990509f | 6141 | .data = &init_net.ipv6.sysctl.flush_delay, |
1da177e4 | 6142 | .maxlen = sizeof(int), |
89c8b3a1 | 6143 | .mode = 0200, |
6d9f239a | 6144 | .proc_handler = ipv6_sysctl_rtcache_flush |
1da177e4 LT |
6145 | }, |
6146 | { | |
1da177e4 | 6147 | .procname = "gc_thresh", |
9a7ec3a9 | 6148 | .data = &ip6_dst_ops_template.gc_thresh, |
1da177e4 LT |
6149 | .maxlen = sizeof(int), |
6150 | .mode = 0644, | |
6d9f239a | 6151 | .proc_handler = proc_dointvec, |
1da177e4 LT |
6152 | }, |
6153 | { | |
1da177e4 | 6154 | .procname = "max_size", |
4990509f | 6155 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
1da177e4 LT |
6156 | .maxlen = sizeof(int), |
6157 | .mode = 0644, | |
6d9f239a | 6158 | .proc_handler = proc_dointvec, |
1da177e4 LT |
6159 | }, |
6160 | { | |
1da177e4 | 6161 | .procname = "gc_min_interval", |
4990509f | 6162 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
6163 | .maxlen = sizeof(int), |
6164 | .mode = 0644, | |
6d9f239a | 6165 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
6166 | }, |
6167 | { | |
1da177e4 | 6168 | .procname = "gc_timeout", |
4990509f | 6169 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
1da177e4 LT |
6170 | .maxlen = sizeof(int), |
6171 | .mode = 0644, | |
6d9f239a | 6172 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
6173 | }, |
6174 | { | |
1da177e4 | 6175 | .procname = "gc_interval", |
4990509f | 6176 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
1da177e4 LT |
6177 | .maxlen = sizeof(int), |
6178 | .mode = 0644, | |
6d9f239a | 6179 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
6180 | }, |
6181 | { | |
1da177e4 | 6182 | .procname = "gc_elasticity", |
4990509f | 6183 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
1da177e4 LT |
6184 | .maxlen = sizeof(int), |
6185 | .mode = 0644, | |
f3d3f616 | 6186 | .proc_handler = proc_dointvec, |
1da177e4 LT |
6187 | }, |
6188 | { | |
1da177e4 | 6189 | .procname = "mtu_expires", |
4990509f | 6190 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
1da177e4 LT |
6191 | .maxlen = sizeof(int), |
6192 | .mode = 0644, | |
6d9f239a | 6193 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
6194 | }, |
6195 | { | |
1da177e4 | 6196 | .procname = "min_adv_mss", |
4990509f | 6197 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
1da177e4 LT |
6198 | .maxlen = sizeof(int), |
6199 | .mode = 0644, | |
f3d3f616 | 6200 | .proc_handler = proc_dointvec, |
1da177e4 LT |
6201 | }, |
6202 | { | |
1da177e4 | 6203 | .procname = "gc_min_interval_ms", |
4990509f | 6204 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
6205 | .maxlen = sizeof(int), |
6206 | .mode = 0644, | |
6d9f239a | 6207 | .proc_handler = proc_dointvec_ms_jiffies, |
1da177e4 | 6208 | }, |
7c6bb7d2 DA |
6209 | { |
6210 | .procname = "skip_notify_on_dev_down", | |
6211 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, | |
6212 | .maxlen = sizeof(int), | |
6213 | .mode = 0644, | |
b8e8a863 | 6214 | .proc_handler = proc_dointvec_minmax, |
eec4844f MC |
6215 | .extra1 = SYSCTL_ZERO, |
6216 | .extra2 = SYSCTL_ONE, | |
7c6bb7d2 | 6217 | }, |
f8572d8f | 6218 | { } |
1da177e4 LT |
6219 | }; |
6220 | ||
2c8c1e72 | 6221 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
760f2d01 DL |
6222 | { |
6223 | struct ctl_table *table; | |
6224 | ||
6225 | table = kmemdup(ipv6_route_table_template, | |
6226 | sizeof(ipv6_route_table_template), | |
6227 | GFP_KERNEL); | |
5ee09105 YH |
6228 | |
6229 | if (table) { | |
6230 | table[0].data = &net->ipv6.sysctl.flush_delay; | |
c486da34 | 6231 | table[0].extra1 = net; |
86393e52 | 6232 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
5ee09105 YH |
6233 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
6234 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; | |
6235 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; | |
6236 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; | |
6237 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
6238 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; | |
6239 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; | |
9c69fabe | 6240 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
7c6bb7d2 | 6241 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
464dc801 EB |
6242 | |
6243 | /* Don't export sysctls to unprivileged users */ | |
6244 | if (net->user_ns != &init_user_ns) | |
6245 | table[0].procname = NULL; | |
5ee09105 YH |
6246 | } |
6247 | ||
760f2d01 DL |
6248 | return table; |
6249 | } | |
1da177e4 LT |
6250 | #endif |
6251 | ||
2c8c1e72 | 6252 | static int __net_init ip6_route_net_init(struct net *net) |
cdb18761 | 6253 | { |
633d424b | 6254 | int ret = -ENOMEM; |
8ed67789 | 6255 | |
86393e52 AD |
6256 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
6257 | sizeof(net->ipv6.ip6_dst_ops)); | |
f2fc6a54 | 6258 | |
fc66f95c ED |
6259 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
6260 | goto out_ip6_dst_ops; | |
6261 | ||
1cf844c7 | 6262 | net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); |
421842ed DA |
6263 | if (!net->ipv6.fib6_null_entry) |
6264 | goto out_ip6_dst_entries; | |
1cf844c7 DA |
6265 | memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, |
6266 | sizeof(*net->ipv6.fib6_null_entry)); | |
421842ed | 6267 | |
8ed67789 DL |
6268 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
6269 | sizeof(*net->ipv6.ip6_null_entry), | |
6270 | GFP_KERNEL); | |
6271 | if (!net->ipv6.ip6_null_entry) | |
421842ed | 6272 | goto out_fib6_null_entry; |
d8d1f30b | 6273 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
6274 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
6275 | ip6_template_metrics, true); | |
74109218 | 6276 | INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached); |
8ed67789 DL |
6277 | |
6278 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
feca7d8c | 6279 | net->ipv6.fib6_has_custom_rules = false; |
8ed67789 DL |
6280 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
6281 | sizeof(*net->ipv6.ip6_prohibit_entry), | |
6282 | GFP_KERNEL); | |
68fffc67 PZ |
6283 | if (!net->ipv6.ip6_prohibit_entry) |
6284 | goto out_ip6_null_entry; | |
d8d1f30b | 6285 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
6286 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
6287 | ip6_template_metrics, true); | |
74109218 | 6288 | INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached); |
8ed67789 DL |
6289 | |
6290 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, | |
6291 | sizeof(*net->ipv6.ip6_blk_hole_entry), | |
6292 | GFP_KERNEL); | |
68fffc67 PZ |
6293 | if (!net->ipv6.ip6_blk_hole_entry) |
6294 | goto out_ip6_prohibit_entry; | |
d8d1f30b | 6295 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
6296 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
6297 | ip6_template_metrics, true); | |
74109218 | 6298 | INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached); |
b9b33e7c PA |
6299 | #ifdef CONFIG_IPV6_SUBTREES |
6300 | net->ipv6.fib6_routes_require_src = 0; | |
6301 | #endif | |
8ed67789 DL |
6302 | #endif |
6303 | ||
b339a47c PZ |
6304 | net->ipv6.sysctl.flush_delay = 0; |
6305 | net->ipv6.sysctl.ip6_rt_max_size = 4096; | |
6306 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; | |
6307 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; | |
6308 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; | |
6309 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; | |
6310 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; | |
6311 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; | |
7c6bb7d2 | 6312 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
b339a47c | 6313 | |
6891a346 BT |
6314 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
6315 | ||
8ed67789 DL |
6316 | ret = 0; |
6317 | out: | |
6318 | return ret; | |
f2fc6a54 | 6319 | |
68fffc67 PZ |
6320 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
6321 | out_ip6_prohibit_entry: | |
6322 | kfree(net->ipv6.ip6_prohibit_entry); | |
6323 | out_ip6_null_entry: | |
6324 | kfree(net->ipv6.ip6_null_entry); | |
6325 | #endif | |
421842ed DA |
6326 | out_fib6_null_entry: |
6327 | kfree(net->ipv6.fib6_null_entry); | |
fc66f95c ED |
6328 | out_ip6_dst_entries: |
6329 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); | |
f2fc6a54 | 6330 | out_ip6_dst_ops: |
f2fc6a54 | 6331 | goto out; |
cdb18761 DL |
6332 | } |
6333 | ||
2c8c1e72 | 6334 | static void __net_exit ip6_route_net_exit(struct net *net) |
cdb18761 | 6335 | { |
421842ed | 6336 | kfree(net->ipv6.fib6_null_entry); |
8ed67789 DL |
6337 | kfree(net->ipv6.ip6_null_entry); |
6338 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
6339 | kfree(net->ipv6.ip6_prohibit_entry); | |
6340 | kfree(net->ipv6.ip6_blk_hole_entry); | |
6341 | #endif | |
41bb78b4 | 6342 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
cdb18761 DL |
6343 | } |
6344 | ||
d189634e TG |
6345 | static int __net_init ip6_route_net_init_late(struct net *net) |
6346 | { | |
6347 | #ifdef CONFIG_PROC_FS | |
c3506372 CH |
6348 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
6349 | sizeof(struct ipv6_route_iter)); | |
3617d949 CH |
6350 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
6351 | rt6_stats_seq_show, NULL); | |
d189634e TG |
6352 | #endif |
6353 | return 0; | |
6354 | } | |
6355 | ||
6356 | static void __net_exit ip6_route_net_exit_late(struct net *net) | |
6357 | { | |
6358 | #ifdef CONFIG_PROC_FS | |
ece31ffd G |
6359 | remove_proc_entry("ipv6_route", net->proc_net); |
6360 | remove_proc_entry("rt6_stats", net->proc_net); | |
d189634e TG |
6361 | #endif |
6362 | } | |
6363 | ||
cdb18761 DL |
6364 | static struct pernet_operations ip6_route_net_ops = { |
6365 | .init = ip6_route_net_init, | |
6366 | .exit = ip6_route_net_exit, | |
6367 | }; | |
6368 | ||
c3426b47 DM |
6369 | static int __net_init ipv6_inetpeer_init(struct net *net) |
6370 | { | |
6371 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); | |
6372 | ||
6373 | if (!bp) | |
6374 | return -ENOMEM; | |
6375 | inet_peer_base_init(bp); | |
6376 | net->ipv6.peers = bp; | |
6377 | return 0; | |
6378 | } | |
6379 | ||
6380 | static void __net_exit ipv6_inetpeer_exit(struct net *net) | |
6381 | { | |
6382 | struct inet_peer_base *bp = net->ipv6.peers; | |
6383 | ||
6384 | net->ipv6.peers = NULL; | |
56a6b248 | 6385 | inetpeer_invalidate_tree(bp); |
c3426b47 DM |
6386 | kfree(bp); |
6387 | } | |
6388 | ||
2b823f72 | 6389 | static struct pernet_operations ipv6_inetpeer_ops = { |
c3426b47 DM |
6390 | .init = ipv6_inetpeer_init, |
6391 | .exit = ipv6_inetpeer_exit, | |
6392 | }; | |
6393 | ||
d189634e TG |
6394 | static struct pernet_operations ip6_route_net_late_ops = { |
6395 | .init = ip6_route_net_init_late, | |
6396 | .exit = ip6_route_net_exit_late, | |
6397 | }; | |
6398 | ||
8ed67789 DL |
6399 | static struct notifier_block ip6_route_dev_notifier = { |
6400 | .notifier_call = ip6_route_dev_notify, | |
242d3a49 | 6401 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
8ed67789 DL |
6402 | }; |
6403 | ||
2f460933 WC |
6404 | void __init ip6_route_init_special_entries(void) |
6405 | { | |
6406 | /* Registering of the loopback is done before this portion of code, | |
6407 | * the loopback reference in rt6_info will not be taken, do it | |
6408 | * manually for init_net */ | |
1cf844c7 | 6409 | init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; |
2f460933 WC |
6410 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
6411 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6412 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
6413 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; | |
6414 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6415 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; | |
6416 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6417 | #endif | |
6418 | } | |
6419 | ||
433d49c3 | 6420 | int __init ip6_route_init(void) |
1da177e4 | 6421 | { |
433d49c3 | 6422 | int ret; |
8d0b94af | 6423 | int cpu; |
433d49c3 | 6424 | |
9a7ec3a9 DL |
6425 | ret = -ENOMEM; |
6426 | ip6_dst_ops_template.kmem_cachep = | |
e5d679f3 | 6427 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
f845ab6b | 6428 | SLAB_HWCACHE_ALIGN, NULL); |
9a7ec3a9 | 6429 | if (!ip6_dst_ops_template.kmem_cachep) |
c19a28e1 | 6430 | goto out; |
14e50e57 | 6431 | |
fc66f95c | 6432 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
8ed67789 | 6433 | if (ret) |
bdb3289f | 6434 | goto out_kmem_cache; |
bdb3289f | 6435 | |
c3426b47 DM |
6436 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
6437 | if (ret) | |
e8803b6c | 6438 | goto out_dst_entries; |
2a0c451a | 6439 | |
7e52b33b DM |
6440 | ret = register_pernet_subsys(&ip6_route_net_ops); |
6441 | if (ret) | |
6442 | goto out_register_inetpeer; | |
c3426b47 | 6443 | |
5dc121e9 AE |
6444 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
6445 | ||
e8803b6c | 6446 | ret = fib6_init(); |
433d49c3 | 6447 | if (ret) |
8ed67789 | 6448 | goto out_register_subsys; |
433d49c3 | 6449 | |
433d49c3 DL |
6450 | ret = xfrm6_init(); |
6451 | if (ret) | |
e8803b6c | 6452 | goto out_fib6_init; |
c35b7e72 | 6453 | |
433d49c3 DL |
6454 | ret = fib6_rules_init(); |
6455 | if (ret) | |
6456 | goto xfrm6_init; | |
7e5449c2 | 6457 | |
d189634e TG |
6458 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
6459 | if (ret) | |
6460 | goto fib6_rules_init; | |
6461 | ||
16feebcf FW |
6462 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
6463 | inet6_rtm_newroute, NULL, 0); | |
6464 | if (ret < 0) | |
6465 | goto out_register_late_subsys; | |
6466 | ||
6467 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, | |
6468 | inet6_rtm_delroute, NULL, 0); | |
6469 | if (ret < 0) | |
6470 | goto out_register_late_subsys; | |
6471 | ||
6472 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, | |
6473 | inet6_rtm_getroute, NULL, | |
6474 | RTNL_FLAG_DOIT_UNLOCKED); | |
6475 | if (ret < 0) | |
d189634e | 6476 | goto out_register_late_subsys; |
c127ea2c | 6477 | |
8ed67789 | 6478 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
cdb18761 | 6479 | if (ret) |
d189634e | 6480 | goto out_register_late_subsys; |
8ed67789 | 6481 | |
8d0b94af MKL |
6482 | for_each_possible_cpu(cpu) { |
6483 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); | |
6484 | ||
6485 | INIT_LIST_HEAD(&ul->head); | |
6486 | spin_lock_init(&ul->lock); | |
6487 | } | |
6488 | ||
433d49c3 DL |
6489 | out: |
6490 | return ret; | |
6491 | ||
d189634e | 6492 | out_register_late_subsys: |
16feebcf | 6493 | rtnl_unregister_all(PF_INET6); |
d189634e | 6494 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
433d49c3 | 6495 | fib6_rules_init: |
433d49c3 DL |
6496 | fib6_rules_cleanup(); |
6497 | xfrm6_init: | |
433d49c3 | 6498 | xfrm6_fini(); |
2a0c451a TG |
6499 | out_fib6_init: |
6500 | fib6_gc_cleanup(); | |
8ed67789 DL |
6501 | out_register_subsys: |
6502 | unregister_pernet_subsys(&ip6_route_net_ops); | |
7e52b33b DM |
6503 | out_register_inetpeer: |
6504 | unregister_pernet_subsys(&ipv6_inetpeer_ops); | |
fc66f95c ED |
6505 | out_dst_entries: |
6506 | dst_entries_destroy(&ip6_dst_blackhole_ops); | |
433d49c3 | 6507 | out_kmem_cache: |
f2fc6a54 | 6508 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
433d49c3 | 6509 | goto out; |
1da177e4 LT |
6510 | } |
6511 | ||
6512 | void ip6_route_cleanup(void) | |
6513 | { | |
8ed67789 | 6514 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
d189634e | 6515 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
101367c2 | 6516 | fib6_rules_cleanup(); |
1da177e4 | 6517 | xfrm6_fini(); |
1da177e4 | 6518 | fib6_gc_cleanup(); |
c3426b47 | 6519 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
8ed67789 | 6520 | unregister_pernet_subsys(&ip6_route_net_ops); |
41bb78b4 | 6521 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
f2fc6a54 | 6522 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
1da177e4 | 6523 | } |