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