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