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