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 | } | |
1cf844c7 | 2682 | res.nh = res.f6i->fib6_nh; |
7d21fec9 DA |
2683 | res.fib6_flags = res.f6i->fib6_flags; |
2684 | res.fib6_type = res.f6i->fib6_type; | |
2685 | ||
85bd05de | 2686 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
45e4fd26 MKL |
2687 | if (nrt6) { |
2688 | rt6_do_update_pmtu(nrt6, mtu); | |
5012f0a5 | 2689 | if (rt6_insert_exception(nrt6, &res)) |
2b760fcf | 2690 | dst_release_immediate(&nrt6->dst); |
45e4fd26 | 2691 | } |
a68886a6 | 2692 | rcu_read_unlock(); |
1da177e4 LT |
2693 | } |
2694 | } | |
2695 | ||
45e4fd26 MKL |
2696 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
2697 | struct sk_buff *skb, u32 mtu) | |
2698 | { | |
2699 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); | |
2700 | } | |
2701 | ||
42ae66c8 | 2702 | void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, |
e2d118a1 | 2703 | int oif, u32 mark, kuid_t uid) |
81aded24 DM |
2704 | { |
2705 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
2706 | struct dst_entry *dst; | |
dc92095d MŻ |
2707 | struct flowi6 fl6 = { |
2708 | .flowi6_oif = oif, | |
2709 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), | |
2710 | .daddr = iph->daddr, | |
2711 | .saddr = iph->saddr, | |
2712 | .flowlabel = ip6_flowinfo(iph), | |
2713 | .flowi6_uid = uid, | |
2714 | }; | |
81aded24 DM |
2715 | |
2716 | dst = ip6_route_output(net, NULL, &fl6); | |
2717 | if (!dst->error) | |
45e4fd26 | 2718 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); |
81aded24 DM |
2719 | dst_release(dst); |
2720 | } | |
2721 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); | |
2722 | ||
2723 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) | |
2724 | { | |
7ddacfa5 | 2725 | int oif = sk->sk_bound_dev_if; |
33c162a9 MKL |
2726 | struct dst_entry *dst; |
2727 | ||
7ddacfa5 DA |
2728 | if (!oif && skb->dev) |
2729 | oif = l3mdev_master_ifindex(skb->dev); | |
2730 | ||
2731 | ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); | |
33c162a9 MKL |
2732 | |
2733 | dst = __sk_dst_get(sk); | |
2734 | if (!dst || !dst->obsolete || | |
2735 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) | |
2736 | return; | |
2737 | ||
2738 | bh_lock_sock(sk); | |
2739 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) | |
2740 | ip6_datagram_dst_update(sk, false); | |
2741 | bh_unlock_sock(sk); | |
81aded24 DM |
2742 | } |
2743 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); | |
2744 | ||
7d6850f7 AK |
2745 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
2746 | const struct flowi6 *fl6) | |
2747 | { | |
2748 | #ifdef CONFIG_IPV6_SUBTREES | |
2749 | struct ipv6_pinfo *np = inet6_sk(sk); | |
2750 | #endif | |
2751 | ||
2752 | ip6_dst_store(sk, dst, | |
2753 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? | |
2754 | &sk->sk_v6_daddr : NULL, | |
2755 | #ifdef CONFIG_IPV6_SUBTREES | |
2756 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? | |
2757 | &np->saddr : | |
2758 | #endif | |
2759 | NULL); | |
2760 | } | |
2761 | ||
9b6b35ab | 2762 | static bool ip6_redirect_nh_match(const struct fib6_result *res, |
0b34eb00 DA |
2763 | struct flowi6 *fl6, |
2764 | const struct in6_addr *gw, | |
2765 | struct rt6_info **ret) | |
2766 | { | |
9b6b35ab DA |
2767 | const struct fib6_nh *nh = res->nh; |
2768 | ||
0b34eb00 DA |
2769 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
2770 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) | |
2771 | return false; | |
2772 | ||
2773 | /* rt_cache's gateway might be different from its 'parent' | |
2774 | * in the case of an ip redirect. | |
2775 | * So we keep searching in the exception table if the gateway | |
2776 | * is different. | |
2777 | */ | |
2778 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { | |
2779 | struct rt6_info *rt_cache; | |
2780 | ||
9b6b35ab | 2781 | rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); |
0b34eb00 DA |
2782 | if (rt_cache && |
2783 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { | |
2784 | *ret = rt_cache; | |
2785 | return true; | |
2786 | } | |
2787 | return false; | |
2788 | } | |
2789 | return true; | |
2790 | } | |
2791 | ||
c55c8988 DA |
2792 | struct fib6_nh_rd_arg { |
2793 | struct fib6_result *res; | |
2794 | struct flowi6 *fl6; | |
2795 | const struct in6_addr *gw; | |
2796 | struct rt6_info **ret; | |
2797 | }; | |
2798 | ||
2799 | static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) | |
2800 | { | |
2801 | struct fib6_nh_rd_arg *arg = _arg; | |
2802 | ||
2803 | arg->res->nh = nh; | |
2804 | return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); | |
2805 | } | |
2806 | ||
b55b76b2 DJ |
2807 | /* Handle redirects */ |
2808 | struct ip6rd_flowi { | |
2809 | struct flowi6 fl6; | |
2810 | struct in6_addr gateway; | |
2811 | }; | |
2812 | ||
2813 | static struct rt6_info *__ip6_route_redirect(struct net *net, | |
2814 | struct fib6_table *table, | |
2815 | struct flowi6 *fl6, | |
b75cc8f9 | 2816 | const struct sk_buff *skb, |
b55b76b2 DJ |
2817 | int flags) |
2818 | { | |
2819 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; | |
0b34eb00 | 2820 | struct rt6_info *ret = NULL; |
9b6b35ab | 2821 | struct fib6_result res = {}; |
c55c8988 DA |
2822 | struct fib6_nh_rd_arg arg = { |
2823 | .res = &res, | |
2824 | .fl6 = fl6, | |
2825 | .gw = &rdfl->gateway, | |
2826 | .ret = &ret | |
2827 | }; | |
8d1c802b | 2828 | struct fib6_info *rt; |
b55b76b2 DJ |
2829 | struct fib6_node *fn; |
2830 | ||
31680ac2 DA |
2831 | /* l3mdev_update_flow overrides oif if the device is enslaved; in |
2832 | * this case we must match on the real ingress device, so reset it | |
2833 | */ | |
2834 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) | |
2835 | fl6->flowi6_oif = skb->dev->ifindex; | |
2836 | ||
b55b76b2 | 2837 | /* Get the "current" route for this destination and |
67c408cf | 2838 | * check if the redirect has come from appropriate router. |
b55b76b2 DJ |
2839 | * |
2840 | * RFC 4861 specifies that redirects should only be | |
2841 | * accepted if they come from the nexthop to the target. | |
2842 | * Due to the way the routes are chosen, this notion | |
2843 | * is a bit fuzzy and one might need to check all possible | |
2844 | * routes. | |
2845 | */ | |
2846 | ||
66f5d6ce | 2847 | rcu_read_lock(); |
6454743b | 2848 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
b55b76b2 | 2849 | restart: |
66f5d6ce | 2850 | for_each_fib6_node_rt_rcu(fn) { |
9b6b35ab | 2851 | res.f6i = rt; |
14895687 | 2852 | if (fib6_check_expired(rt)) |
b55b76b2 | 2853 | continue; |
93c2fb25 | 2854 | if (rt->fib6_flags & RTF_REJECT) |
b55b76b2 | 2855 | break; |
c55c8988 DA |
2856 | if (unlikely(rt->nh)) { |
2857 | if (nexthop_is_blackhole(rt->nh)) | |
2858 | continue; | |
2859 | /* on match, res->nh is filled in and potentially ret */ | |
2860 | if (nexthop_for_each_fib6_nh(rt->nh, | |
2861 | fib6_nh_redirect_match, | |
2862 | &arg)) | |
2863 | goto out; | |
2864 | } else { | |
2865 | res.nh = rt->fib6_nh; | |
2866 | if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, | |
2867 | &ret)) | |
2868 | goto out; | |
2869 | } | |
b55b76b2 DJ |
2870 | } |
2871 | ||
2872 | if (!rt) | |
421842ed | 2873 | rt = net->ipv6.fib6_null_entry; |
93c2fb25 | 2874 | else if (rt->fib6_flags & RTF_REJECT) { |
23fb93a4 | 2875 | ret = net->ipv6.ip6_null_entry; |
b0a1ba59 MKL |
2876 | goto out; |
2877 | } | |
2878 | ||
421842ed | 2879 | if (rt == net->ipv6.fib6_null_entry) { |
a3c00e46 MKL |
2880 | fn = fib6_backtrack(fn, &fl6->saddr); |
2881 | if (fn) | |
2882 | goto restart; | |
b55b76b2 | 2883 | } |
a3c00e46 | 2884 | |
9b6b35ab | 2885 | res.f6i = rt; |
1cf844c7 | 2886 | res.nh = rt->fib6_nh; |
b0a1ba59 | 2887 | out: |
7d21fec9 | 2888 | if (ret) { |
10585b43 | 2889 | ip6_hold_safe(net, &ret); |
7d21fec9 DA |
2890 | } else { |
2891 | res.fib6_flags = res.f6i->fib6_flags; | |
2892 | res.fib6_type = res.f6i->fib6_type; | |
9b6b35ab | 2893 | ret = ip6_create_rt_rcu(&res); |
7d21fec9 | 2894 | } |
b55b76b2 | 2895 | |
66f5d6ce | 2896 | rcu_read_unlock(); |
b55b76b2 | 2897 | |
8ff2e5b2 | 2898 | trace_fib6_table_lookup(net, &res, table, fl6); |
23fb93a4 | 2899 | return ret; |
b55b76b2 DJ |
2900 | }; |
2901 | ||
2902 | static struct dst_entry *ip6_route_redirect(struct net *net, | |
b75cc8f9 DA |
2903 | const struct flowi6 *fl6, |
2904 | const struct sk_buff *skb, | |
2905 | const struct in6_addr *gateway) | |
b55b76b2 DJ |
2906 | { |
2907 | int flags = RT6_LOOKUP_F_HAS_SADDR; | |
2908 | struct ip6rd_flowi rdfl; | |
2909 | ||
2910 | rdfl.fl6 = *fl6; | |
2911 | rdfl.gateway = *gateway; | |
2912 | ||
b75cc8f9 | 2913 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
b55b76b2 DJ |
2914 | flags, __ip6_route_redirect); |
2915 | } | |
2916 | ||
e2d118a1 LC |
2917 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
2918 | kuid_t uid) | |
3a5ad2ee DM |
2919 | { |
2920 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
2921 | struct dst_entry *dst; | |
1f7f10ac MŻ |
2922 | struct flowi6 fl6 = { |
2923 | .flowi6_iif = LOOPBACK_IFINDEX, | |
2924 | .flowi6_oif = oif, | |
2925 | .flowi6_mark = mark, | |
2926 | .daddr = iph->daddr, | |
2927 | .saddr = iph->saddr, | |
2928 | .flowlabel = ip6_flowinfo(iph), | |
2929 | .flowi6_uid = uid, | |
2930 | }; | |
3a5ad2ee | 2931 | |
b75cc8f9 | 2932 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
b55b76b2 | 2933 | rt6_do_redirect(dst, NULL, skb); |
3a5ad2ee DM |
2934 | dst_release(dst); |
2935 | } | |
2936 | EXPORT_SYMBOL_GPL(ip6_redirect); | |
2937 | ||
d456336d | 2938 | void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) |
c92a59ec DJ |
2939 | { |
2940 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
2941 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); | |
2942 | struct dst_entry *dst; | |
0b26fb17 MŻ |
2943 | struct flowi6 fl6 = { |
2944 | .flowi6_iif = LOOPBACK_IFINDEX, | |
2945 | .flowi6_oif = oif, | |
0b26fb17 MŻ |
2946 | .daddr = msg->dest, |
2947 | .saddr = iph->daddr, | |
2948 | .flowi6_uid = sock_net_uid(net, NULL), | |
2949 | }; | |
c92a59ec | 2950 | |
b75cc8f9 | 2951 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
b55b76b2 | 2952 | rt6_do_redirect(dst, NULL, skb); |
c92a59ec DJ |
2953 | dst_release(dst); |
2954 | } | |
2955 | ||
3a5ad2ee DM |
2956 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
2957 | { | |
e2d118a1 LC |
2958 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
2959 | sk->sk_uid); | |
3a5ad2ee DM |
2960 | } |
2961 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); | |
2962 | ||
0dbaee3b | 2963 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
1da177e4 | 2964 | { |
0dbaee3b DM |
2965 | struct net_device *dev = dst->dev; |
2966 | unsigned int mtu = dst_mtu(dst); | |
2967 | struct net *net = dev_net(dev); | |
2968 | ||
1da177e4 LT |
2969 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
2970 | ||
5578689a DL |
2971 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
2972 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; | |
1da177e4 LT |
2973 | |
2974 | /* | |
1ab1457c YH |
2975 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
2976 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. | |
2977 | * IPV6_MAXPLEN is also valid and means: "any MSS, | |
1da177e4 LT |
2978 | * rely only on pmtu discovery" |
2979 | */ | |
2980 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) | |
2981 | mtu = IPV6_MAXPLEN; | |
2982 | return mtu; | |
2983 | } | |
2984 | ||
ebb762f2 | 2985 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
d33e4553 | 2986 | { |
d33e4553 | 2987 | struct inet6_dev *idev; |
d4ead6b3 | 2988 | unsigned int mtu; |
4b32b5ad MKL |
2989 | |
2990 | mtu = dst_metric_raw(dst, RTAX_MTU); | |
618f9bc7 | 2991 | if (mtu) |
30f78d8e | 2992 | goto out; |
618f9bc7 SK |
2993 | |
2994 | mtu = IPV6_MIN_MTU; | |
d33e4553 DM |
2995 | |
2996 | rcu_read_lock(); | |
2997 | idev = __in6_dev_get(dst->dev); | |
2998 | if (idev) | |
2999 | mtu = idev->cnf.mtu6; | |
3000 | rcu_read_unlock(); | |
3001 | ||
30f78d8e | 3002 | out: |
14972cbd RP |
3003 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
3004 | ||
3005 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); | |
d33e4553 DM |
3006 | } |
3007 | ||
901731b8 DA |
3008 | /* MTU selection: |
3009 | * 1. mtu on route is locked - use it | |
3010 | * 2. mtu from nexthop exception | |
3011 | * 3. mtu from egress device | |
3012 | * | |
3013 | * based on ip6_dst_mtu_forward and exception logic of | |
3014 | * rt6_find_cached_rt; called with rcu_read_lock | |
3015 | */ | |
b748f260 DA |
3016 | u32 ip6_mtu_from_fib6(const struct fib6_result *res, |
3017 | const struct in6_addr *daddr, | |
3018 | const struct in6_addr *saddr) | |
901731b8 | 3019 | { |
b748f260 DA |
3020 | const struct fib6_nh *nh = res->nh; |
3021 | struct fib6_info *f6i = res->f6i; | |
901731b8 | 3022 | struct inet6_dev *idev; |
510e2ced | 3023 | struct rt6_info *rt; |
901731b8 DA |
3024 | u32 mtu = 0; |
3025 | ||
3026 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { | |
3027 | mtu = f6i->fib6_pmtu; | |
3028 | if (mtu) | |
3029 | goto out; | |
3030 | } | |
3031 | ||
510e2ced WW |
3032 | rt = rt6_find_cached_rt(res, daddr, saddr); |
3033 | if (unlikely(rt)) { | |
3034 | mtu = dst_metric_raw(&rt->dst, RTAX_MTU); | |
3035 | } else { | |
b748f260 | 3036 | struct net_device *dev = nh->fib_nh_dev; |
901731b8 DA |
3037 | |
3038 | mtu = IPV6_MIN_MTU; | |
3039 | idev = __in6_dev_get(dev); | |
3040 | if (idev && idev->cnf.mtu6 > mtu) | |
3041 | mtu = idev->cnf.mtu6; | |
3042 | } | |
3043 | ||
3044 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); | |
3045 | out: | |
b748f260 | 3046 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
901731b8 DA |
3047 | } |
3048 | ||
3b00944c | 3049 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
87a11578 | 3050 | struct flowi6 *fl6) |
1da177e4 | 3051 | { |
87a11578 | 3052 | struct dst_entry *dst; |
1da177e4 LT |
3053 | struct rt6_info *rt; |
3054 | struct inet6_dev *idev = in6_dev_get(dev); | |
c346dca1 | 3055 | struct net *net = dev_net(dev); |
1da177e4 | 3056 | |
38308473 | 3057 | if (unlikely(!idev)) |
122bdf67 | 3058 | return ERR_PTR(-ENODEV); |
1da177e4 | 3059 | |
ad706862 | 3060 | rt = ip6_dst_alloc(net, dev, 0); |
38308473 | 3061 | if (unlikely(!rt)) { |
1da177e4 | 3062 | in6_dev_put(idev); |
87a11578 | 3063 | dst = ERR_PTR(-ENOMEM); |
1da177e4 LT |
3064 | goto out; |
3065 | } | |
3066 | ||
8e2ec639 | 3067 | rt->dst.flags |= DST_HOST; |
588753f1 | 3068 | rt->dst.input = ip6_input; |
8e2ec639 | 3069 | rt->dst.output = ip6_output; |
550bab42 | 3070 | rt->rt6i_gateway = fl6->daddr; |
87a11578 | 3071 | rt->rt6i_dst.addr = fl6->daddr; |
8e2ec639 YZ |
3072 | rt->rt6i_dst.plen = 128; |
3073 | rt->rt6i_idev = idev; | |
14edd87d | 3074 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
1da177e4 | 3075 | |
4c981e28 | 3076 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
587fea74 WW |
3077 | * do proper release of the net_device |
3078 | */ | |
3079 | rt6_uncached_list_add(rt); | |
81eb8447 | 3080 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
1da177e4 | 3081 | |
87a11578 DM |
3082 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
3083 | ||
1da177e4 | 3084 | out: |
87a11578 | 3085 | return dst; |
1da177e4 LT |
3086 | } |
3087 | ||
569d3645 | 3088 | static int ip6_dst_gc(struct dst_ops *ops) |
1da177e4 | 3089 | { |
86393e52 | 3090 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
7019b78e DL |
3091 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
3092 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; | |
3093 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
3094 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; | |
3095 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; | |
fc66f95c | 3096 | int entries; |
7019b78e | 3097 | |
fc66f95c | 3098 | entries = dst_entries_get_fast(ops); |
49a18d86 | 3099 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
fc66f95c | 3100 | entries <= rt_max_size) |
1da177e4 LT |
3101 | goto out; |
3102 | ||
6891a346 | 3103 | net->ipv6.ip6_rt_gc_expire++; |
14956643 | 3104 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
fc66f95c ED |
3105 | entries = dst_entries_get_slow(ops); |
3106 | if (entries < ops->gc_thresh) | |
7019b78e | 3107 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
1da177e4 | 3108 | out: |
7019b78e | 3109 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; |
fc66f95c | 3110 | return entries > rt_max_size; |
1da177e4 LT |
3111 | } |
3112 | ||
8c14586f DA |
3113 | static struct rt6_info *ip6_nh_lookup_table(struct net *net, |
3114 | struct fib6_config *cfg, | |
f4797b33 DA |
3115 | const struct in6_addr *gw_addr, |
3116 | u32 tbid, int flags) | |
8c14586f DA |
3117 | { |
3118 | struct flowi6 fl6 = { | |
3119 | .flowi6_oif = cfg->fc_ifindex, | |
3120 | .daddr = *gw_addr, | |
3121 | .saddr = cfg->fc_prefsrc, | |
3122 | }; | |
3123 | struct fib6_table *table; | |
3124 | struct rt6_info *rt; | |
8c14586f | 3125 | |
f4797b33 | 3126 | table = fib6_get_table(net, tbid); |
8c14586f DA |
3127 | if (!table) |
3128 | return NULL; | |
3129 | ||
3130 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) | |
3131 | flags |= RT6_LOOKUP_F_HAS_SADDR; | |
3132 | ||
f4797b33 | 3133 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
b75cc8f9 | 3134 | rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); |
8c14586f DA |
3135 | |
3136 | /* if table lookup failed, fall back to full lookup */ | |
3137 | if (rt == net->ipv6.ip6_null_entry) { | |
3138 | ip6_rt_put(rt); | |
3139 | rt = NULL; | |
3140 | } | |
3141 | ||
3142 | return rt; | |
3143 | } | |
3144 | ||
fc1e64e1 DA |
3145 | static int ip6_route_check_nh_onlink(struct net *net, |
3146 | struct fib6_config *cfg, | |
9fbb704c | 3147 | const struct net_device *dev, |
fc1e64e1 DA |
3148 | struct netlink_ext_ack *extack) |
3149 | { | |
44750f84 | 3150 | u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
fc1e64e1 DA |
3151 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
3152 | u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; | |
bf1dc8ba | 3153 | struct fib6_info *from; |
fc1e64e1 DA |
3154 | struct rt6_info *grt; |
3155 | int err; | |
3156 | ||
3157 | err = 0; | |
3158 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); | |
3159 | if (grt) { | |
bf1dc8ba PA |
3160 | rcu_read_lock(); |
3161 | from = rcu_dereference(grt->from); | |
58e354c0 | 3162 | if (!grt->dst.error && |
4ed591c8 | 3163 | /* ignore match if it is the default route */ |
bf1dc8ba | 3164 | from && !ipv6_addr_any(&from->fib6_dst.addr) && |
58e354c0 | 3165 | (grt->rt6i_flags & flags || dev != grt->dst.dev)) { |
44750f84 DA |
3166 | NL_SET_ERR_MSG(extack, |
3167 | "Nexthop has invalid gateway or device mismatch"); | |
fc1e64e1 DA |
3168 | err = -EINVAL; |
3169 | } | |
bf1dc8ba | 3170 | rcu_read_unlock(); |
fc1e64e1 DA |
3171 | |
3172 | ip6_rt_put(grt); | |
3173 | } | |
3174 | ||
3175 | return err; | |
3176 | } | |
3177 | ||
1edce99f DA |
3178 | static int ip6_route_check_nh(struct net *net, |
3179 | struct fib6_config *cfg, | |
3180 | struct net_device **_dev, | |
3181 | struct inet6_dev **idev) | |
3182 | { | |
3183 | const struct in6_addr *gw_addr = &cfg->fc_gateway; | |
3184 | struct net_device *dev = _dev ? *_dev : NULL; | |
3185 | struct rt6_info *grt = NULL; | |
3186 | int err = -EHOSTUNREACH; | |
3187 | ||
3188 | if (cfg->fc_table) { | |
f4797b33 DA |
3189 | int flags = RT6_LOOKUP_F_IFACE; |
3190 | ||
3191 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, | |
3192 | cfg->fc_table, flags); | |
1edce99f DA |
3193 | if (grt) { |
3194 | if (grt->rt6i_flags & RTF_GATEWAY || | |
3195 | (dev && dev != grt->dst.dev)) { | |
3196 | ip6_rt_put(grt); | |
3197 | grt = NULL; | |
3198 | } | |
3199 | } | |
3200 | } | |
3201 | ||
3202 | if (!grt) | |
b75cc8f9 | 3203 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); |
1edce99f DA |
3204 | |
3205 | if (!grt) | |
3206 | goto out; | |
3207 | ||
3208 | if (dev) { | |
3209 | if (dev != grt->dst.dev) { | |
3210 | ip6_rt_put(grt); | |
3211 | goto out; | |
3212 | } | |
3213 | } else { | |
3214 | *_dev = dev = grt->dst.dev; | |
3215 | *idev = grt->rt6i_idev; | |
3216 | dev_hold(dev); | |
3217 | in6_dev_hold(grt->rt6i_idev); | |
3218 | } | |
3219 | ||
3220 | if (!(grt->rt6i_flags & RTF_GATEWAY)) | |
3221 | err = 0; | |
3222 | ||
3223 | ip6_rt_put(grt); | |
3224 | ||
3225 | out: | |
3226 | return err; | |
3227 | } | |
3228 | ||
9fbb704c DA |
3229 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
3230 | struct net_device **_dev, struct inet6_dev **idev, | |
3231 | struct netlink_ext_ack *extack) | |
3232 | { | |
3233 | const struct in6_addr *gw_addr = &cfg->fc_gateway; | |
3234 | int gwa_type = ipv6_addr_type(gw_addr); | |
232378e8 | 3235 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
9fbb704c | 3236 | const struct net_device *dev = *_dev; |
232378e8 | 3237 | bool need_addr_check = !dev; |
9fbb704c DA |
3238 | int err = -EINVAL; |
3239 | ||
3240 | /* if gw_addr is local we will fail to detect this in case | |
3241 | * address is still TENTATIVE (DAD in progress). rt6_lookup() | |
3242 | * will return already-added prefix route via interface that | |
3243 | * prefix route was assigned to, which might be non-loopback. | |
3244 | */ | |
232378e8 DA |
3245 | if (dev && |
3246 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { | |
3247 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); | |
9fbb704c DA |
3248 | goto out; |
3249 | } | |
3250 | ||
3251 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { | |
3252 | /* IPv6 strictly inhibits using not link-local | |
3253 | * addresses as nexthop address. | |
3254 | * Otherwise, router will not able to send redirects. | |
3255 | * It is very good, but in some (rare!) circumstances | |
3256 | * (SIT, PtP, NBMA NOARP links) it is handy to allow | |
3257 | * some exceptions. --ANK | |
3258 | * We allow IPv4-mapped nexthops to support RFC4798-type | |
3259 | * addressing | |
3260 | */ | |
3261 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { | |
3262 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); | |
3263 | goto out; | |
3264 | } | |
3265 | ||
3266 | if (cfg->fc_flags & RTNH_F_ONLINK) | |
3267 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); | |
3268 | else | |
3269 | err = ip6_route_check_nh(net, cfg, _dev, idev); | |
3270 | ||
3271 | if (err) | |
3272 | goto out; | |
3273 | } | |
3274 | ||
3275 | /* reload in case device was changed */ | |
3276 | dev = *_dev; | |
3277 | ||
3278 | err = -EINVAL; | |
3279 | if (!dev) { | |
3280 | NL_SET_ERR_MSG(extack, "Egress device not specified"); | |
3281 | goto out; | |
3282 | } else if (dev->flags & IFF_LOOPBACK) { | |
3283 | NL_SET_ERR_MSG(extack, | |
3284 | "Egress device can not be loopback device for this route"); | |
3285 | goto out; | |
3286 | } | |
232378e8 DA |
3287 | |
3288 | /* if we did not check gw_addr above, do so now that the | |
3289 | * egress device has been resolved. | |
3290 | */ | |
3291 | if (need_addr_check && | |
3292 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { | |
3293 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); | |
3294 | goto out; | |
3295 | } | |
3296 | ||
9fbb704c DA |
3297 | err = 0; |
3298 | out: | |
3299 | return err; | |
3300 | } | |
3301 | ||
83c44251 DA |
3302 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
3303 | { | |
3304 | if ((flags & RTF_REJECT) || | |
3305 | (dev && (dev->flags & IFF_LOOPBACK) && | |
3306 | !(addr_type & IPV6_ADDR_LOOPBACK) && | |
3307 | !(flags & RTF_LOCAL))) | |
3308 | return true; | |
3309 | ||
3310 | return false; | |
3311 | } | |
3312 | ||
3313 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, | |
3314 | struct fib6_config *cfg, gfp_t gfp_flags, | |
3315 | struct netlink_ext_ack *extack) | |
3316 | { | |
3317 | struct net_device *dev = NULL; | |
3318 | struct inet6_dev *idev = NULL; | |
3319 | int addr_type; | |
3320 | int err; | |
3321 | ||
f1741730 DA |
3322 | fib6_nh->fib_nh_family = AF_INET6; |
3323 | ||
83c44251 DA |
3324 | err = -ENODEV; |
3325 | if (cfg->fc_ifindex) { | |
3326 | dev = dev_get_by_index(net, cfg->fc_ifindex); | |
3327 | if (!dev) | |
3328 | goto out; | |
3329 | idev = in6_dev_get(dev); | |
3330 | if (!idev) | |
3331 | goto out; | |
3332 | } | |
3333 | ||
3334 | if (cfg->fc_flags & RTNH_F_ONLINK) { | |
3335 | if (!dev) { | |
3336 | NL_SET_ERR_MSG(extack, | |
3337 | "Nexthop device required for onlink"); | |
3338 | goto out; | |
3339 | } | |
3340 | ||
3341 | if (!(dev->flags & IFF_UP)) { | |
3342 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); | |
3343 | err = -ENETDOWN; | |
3344 | goto out; | |
3345 | } | |
3346 | ||
ad1601ae | 3347 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
83c44251 DA |
3348 | } |
3349 | ||
ad1601ae | 3350 | fib6_nh->fib_nh_weight = 1; |
83c44251 DA |
3351 | |
3352 | /* We cannot add true routes via loopback here, | |
3353 | * they would result in kernel looping; promote them to reject routes | |
3354 | */ | |
3355 | addr_type = ipv6_addr_type(&cfg->fc_dst); | |
3356 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { | |
3357 | /* hold loopback dev/idev if we haven't done so. */ | |
3358 | if (dev != net->loopback_dev) { | |
3359 | if (dev) { | |
3360 | dev_put(dev); | |
3361 | in6_dev_put(idev); | |
3362 | } | |
3363 | dev = net->loopback_dev; | |
3364 | dev_hold(dev); | |
3365 | idev = in6_dev_get(dev); | |
3366 | if (!idev) { | |
3367 | err = -ENODEV; | |
3368 | goto out; | |
3369 | } | |
3370 | } | |
7dd73168 | 3371 | goto pcpu_alloc; |
83c44251 DA |
3372 | } |
3373 | ||
3374 | if (cfg->fc_flags & RTF_GATEWAY) { | |
3375 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); | |
3376 | if (err) | |
3377 | goto out; | |
3378 | ||
ad1601ae | 3379 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
bdf00467 | 3380 | fib6_nh->fib_nh_gw_family = AF_INET6; |
83c44251 DA |
3381 | } |
3382 | ||
3383 | err = -ENODEV; | |
3384 | if (!dev) | |
3385 | goto out; | |
3386 | ||
3387 | if (idev->cnf.disable_ipv6) { | |
3388 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); | |
3389 | err = -EACCES; | |
3390 | goto out; | |
3391 | } | |
3392 | ||
3393 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { | |
3394 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); | |
3395 | err = -ENETDOWN; | |
3396 | goto out; | |
3397 | } | |
3398 | ||
3399 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && | |
3400 | !netif_carrier_ok(dev)) | |
ad1601ae | 3401 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
83c44251 | 3402 | |
7dd73168 DA |
3403 | err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, |
3404 | cfg->fc_encap_type, cfg, gfp_flags, extack); | |
3405 | if (err) | |
3406 | goto out; | |
3407 | ||
3408 | pcpu_alloc: | |
f40b6ae2 DA |
3409 | fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); |
3410 | if (!fib6_nh->rt6i_pcpu) { | |
3411 | err = -ENOMEM; | |
3412 | goto out; | |
3413 | } | |
3414 | ||
ad1601ae | 3415 | fib6_nh->fib_nh_dev = dev; |
f1741730 | 3416 | fib6_nh->fib_nh_oif = dev->ifindex; |
83c44251 DA |
3417 | err = 0; |
3418 | out: | |
3419 | if (idev) | |
3420 | in6_dev_put(idev); | |
3421 | ||
3422 | if (err) { | |
ad1601ae DA |
3423 | lwtstate_put(fib6_nh->fib_nh_lws); |
3424 | fib6_nh->fib_nh_lws = NULL; | |
83c44251 DA |
3425 | if (dev) |
3426 | dev_put(dev); | |
3427 | } | |
3428 | ||
3429 | return err; | |
3430 | } | |
3431 | ||
dac7d0f2 DA |
3432 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
3433 | { | |
cc5c073a DA |
3434 | struct rt6_exception_bucket *bucket; |
3435 | ||
3436 | rcu_read_lock(); | |
3437 | ||
3438 | fib6_nh_flush_exceptions(fib6_nh, NULL); | |
3439 | bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); | |
3440 | if (bucket) { | |
3441 | rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); | |
3442 | kfree(bucket); | |
3443 | } | |
3444 | ||
3445 | rcu_read_unlock(); | |
3446 | ||
f40b6ae2 DA |
3447 | if (fib6_nh->rt6i_pcpu) { |
3448 | int cpu; | |
3449 | ||
3450 | for_each_possible_cpu(cpu) { | |
3451 | struct rt6_info **ppcpu_rt; | |
3452 | struct rt6_info *pcpu_rt; | |
3453 | ||
3454 | ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); | |
3455 | pcpu_rt = *ppcpu_rt; | |
3456 | if (pcpu_rt) { | |
3457 | dst_dev_put(&pcpu_rt->dst); | |
3458 | dst_release(&pcpu_rt->dst); | |
3459 | *ppcpu_rt = NULL; | |
3460 | } | |
3461 | } | |
3462 | ||
3463 | free_percpu(fib6_nh->rt6i_pcpu); | |
3464 | } | |
3465 | ||
979e276e | 3466 | fib_nh_common_release(&fib6_nh->nh_common); |
dac7d0f2 DA |
3467 | } |
3468 | ||
8d1c802b | 3469 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
acb54e3c | 3470 | gfp_t gfp_flags, |
333c4301 | 3471 | struct netlink_ext_ack *extack) |
1da177e4 | 3472 | { |
5578689a | 3473 | struct net *net = cfg->fc_nlinfo.nl_net; |
8d1c802b | 3474 | struct fib6_info *rt = NULL; |
f88d8ea6 | 3475 | struct nexthop *nh = NULL; |
c71099ac | 3476 | struct fib6_table *table; |
f88d8ea6 | 3477 | struct fib6_nh *fib6_nh; |
8c5b83f0 | 3478 | int err = -EINVAL; |
83c44251 | 3479 | int addr_type; |
1da177e4 | 3480 | |
557c44be | 3481 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
d5d531cb DA |
3482 | if (cfg->fc_flags & RTF_PCPU) { |
3483 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); | |
557c44be | 3484 | goto out; |
d5d531cb | 3485 | } |
557c44be | 3486 | |
2ea2352e WW |
3487 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
3488 | if (cfg->fc_flags & RTF_CACHE) { | |
3489 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); | |
3490 | goto out; | |
3491 | } | |
3492 | ||
e8478e80 DA |
3493 | if (cfg->fc_type > RTN_MAX) { |
3494 | NL_SET_ERR_MSG(extack, "Invalid route type"); | |
3495 | goto out; | |
3496 | } | |
3497 | ||
d5d531cb DA |
3498 | if (cfg->fc_dst_len > 128) { |
3499 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); | |
3500 | goto out; | |
3501 | } | |
3502 | if (cfg->fc_src_len > 128) { | |
3503 | NL_SET_ERR_MSG(extack, "Invalid source address length"); | |
8c5b83f0 | 3504 | goto out; |
d5d531cb | 3505 | } |
1da177e4 | 3506 | #ifndef CONFIG_IPV6_SUBTREES |
d5d531cb DA |
3507 | if (cfg->fc_src_len) { |
3508 | NL_SET_ERR_MSG(extack, | |
3509 | "Specifying source address requires IPV6_SUBTREES to be enabled"); | |
8c5b83f0 | 3510 | goto out; |
d5d531cb | 3511 | } |
1da177e4 | 3512 | #endif |
fc1e64e1 | 3513 | |
d71314b4 | 3514 | err = -ENOBUFS; |
38308473 DM |
3515 | if (cfg->fc_nlinfo.nlh && |
3516 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { | |
d71314b4 | 3517 | table = fib6_get_table(net, cfg->fc_table); |
38308473 | 3518 | if (!table) { |
f3213831 | 3519 | pr_warn("NLM_F_CREATE should be specified when creating new route\n"); |
d71314b4 MV |
3520 | table = fib6_new_table(net, cfg->fc_table); |
3521 | } | |
3522 | } else { | |
3523 | table = fib6_new_table(net, cfg->fc_table); | |
3524 | } | |
38308473 DM |
3525 | |
3526 | if (!table) | |
c71099ac | 3527 | goto out; |
c71099ac | 3528 | |
93531c67 | 3529 | err = -ENOMEM; |
f88d8ea6 | 3530 | rt = fib6_info_alloc(gfp_flags, !nh); |
93531c67 | 3531 | if (!rt) |
1da177e4 | 3532 | goto out; |
93531c67 | 3533 | |
d7e774f3 DA |
3534 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
3535 | extack); | |
767a2217 DA |
3536 | if (IS_ERR(rt->fib6_metrics)) { |
3537 | err = PTR_ERR(rt->fib6_metrics); | |
fda21d46 ED |
3538 | /* Do not leave garbage there. */ |
3539 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; | |
767a2217 DA |
3540 | goto out; |
3541 | } | |
3542 | ||
93531c67 DA |
3543 | if (cfg->fc_flags & RTF_ADDRCONF) |
3544 | rt->dst_nocount = true; | |
1da177e4 | 3545 | |
1716a961 | 3546 | if (cfg->fc_flags & RTF_EXPIRES) |
14895687 | 3547 | fib6_set_expires(rt, jiffies + |
1716a961 G |
3548 | clock_t_to_jiffies(cfg->fc_expires)); |
3549 | else | |
14895687 | 3550 | fib6_clean_expires(rt); |
1da177e4 | 3551 | |
86872cb5 TG |
3552 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
3553 | cfg->fc_protocol = RTPROT_BOOT; | |
93c2fb25 | 3554 | rt->fib6_protocol = cfg->fc_protocol; |
86872cb5 | 3555 | |
83c44251 DA |
3556 | rt->fib6_table = table; |
3557 | rt->fib6_metric = cfg->fc_metric; | |
3558 | rt->fib6_type = cfg->fc_type; | |
2b2450ca | 3559 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
19e42e45 | 3560 | |
93c2fb25 DA |
3561 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
3562 | rt->fib6_dst.plen = cfg->fc_dst_len; | |
3563 | if (rt->fib6_dst.plen == 128) | |
3b6761d1 | 3564 | rt->dst_host = true; |
e5fd387a | 3565 | |
1da177e4 | 3566 | #ifdef CONFIG_IPV6_SUBTREES |
93c2fb25 DA |
3567 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
3568 | rt->fib6_src.plen = cfg->fc_src_len; | |
1da177e4 | 3569 | #endif |
f88d8ea6 DA |
3570 | if (nh) { |
3571 | if (!nexthop_get(nh)) { | |
3572 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); | |
3573 | goto out; | |
3574 | } | |
3575 | if (rt->fib6_src.plen) { | |
4daa95af | 3576 | NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); |
f88d8ea6 DA |
3577 | goto out; |
3578 | } | |
3579 | rt->nh = nh; | |
3580 | fib6_nh = nexthop_fib6_nh(rt->nh); | |
3581 | } else { | |
3582 | err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); | |
3583 | if (err) | |
3584 | goto out; | |
1da177e4 | 3585 | |
f88d8ea6 DA |
3586 | fib6_nh = rt->fib6_nh; |
3587 | ||
3588 | /* We cannot add true routes via loopback here, they would | |
3589 | * result in kernel looping; promote them to reject routes | |
3590 | */ | |
3591 | addr_type = ipv6_addr_type(&cfg->fc_dst); | |
3592 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, | |
3593 | addr_type)) | |
3594 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; | |
3595 | } | |
955ec4cb | 3596 | |
c3968a85 | 3597 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
f88d8ea6 | 3598 | struct net_device *dev = fib6_nh->fib_nh_dev; |
83c44251 | 3599 | |
c3968a85 | 3600 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
d5d531cb | 3601 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
c3968a85 DW |
3602 | err = -EINVAL; |
3603 | goto out; | |
3604 | } | |
93c2fb25 DA |
3605 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
3606 | rt->fib6_prefsrc.plen = 128; | |
c3968a85 | 3607 | } else |
93c2fb25 | 3608 | rt->fib6_prefsrc.plen = 0; |
c3968a85 | 3609 | |
8c5b83f0 | 3610 | return rt; |
6b9ea5a6 | 3611 | out: |
93531c67 | 3612 | fib6_info_release(rt); |
8c5b83f0 | 3613 | return ERR_PTR(err); |
6b9ea5a6 RP |
3614 | } |
3615 | ||
acb54e3c | 3616 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
333c4301 | 3617 | struct netlink_ext_ack *extack) |
6b9ea5a6 | 3618 | { |
8d1c802b | 3619 | struct fib6_info *rt; |
6b9ea5a6 RP |
3620 | int err; |
3621 | ||
acb54e3c | 3622 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
d4ead6b3 DA |
3623 | if (IS_ERR(rt)) |
3624 | return PTR_ERR(rt); | |
6b9ea5a6 | 3625 | |
d4ead6b3 | 3626 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
93531c67 | 3627 | fib6_info_release(rt); |
6b9ea5a6 | 3628 | |
1da177e4 LT |
3629 | return err; |
3630 | } | |
3631 | ||
8d1c802b | 3632 | static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) |
1da177e4 | 3633 | { |
afb1d4b5 | 3634 | struct net *net = info->nl_net; |
c71099ac | 3635 | struct fib6_table *table; |
afb1d4b5 | 3636 | int err; |
1da177e4 | 3637 | |
421842ed | 3638 | if (rt == net->ipv6.fib6_null_entry) { |
6825a26c G |
3639 | err = -ENOENT; |
3640 | goto out; | |
3641 | } | |
6c813a72 | 3642 | |
93c2fb25 | 3643 | table = rt->fib6_table; |
66f5d6ce | 3644 | spin_lock_bh(&table->tb6_lock); |
86872cb5 | 3645 | err = fib6_del(rt, info); |
66f5d6ce | 3646 | spin_unlock_bh(&table->tb6_lock); |
1da177e4 | 3647 | |
6825a26c | 3648 | out: |
93531c67 | 3649 | fib6_info_release(rt); |
1da177e4 LT |
3650 | return err; |
3651 | } | |
3652 | ||
8d1c802b | 3653 | int ip6_del_rt(struct net *net, struct fib6_info *rt) |
e0a1ad73 | 3654 | { |
afb1d4b5 DA |
3655 | struct nl_info info = { .nl_net = net }; |
3656 | ||
528c4ceb | 3657 | return __ip6_del_rt(rt, &info); |
e0a1ad73 TG |
3658 | } |
3659 | ||
8d1c802b | 3660 | static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) |
0ae81335 DA |
3661 | { |
3662 | struct nl_info *info = &cfg->fc_nlinfo; | |
e3330039 | 3663 | struct net *net = info->nl_net; |
16a16cd3 | 3664 | struct sk_buff *skb = NULL; |
0ae81335 | 3665 | struct fib6_table *table; |
e3330039 | 3666 | int err = -ENOENT; |
0ae81335 | 3667 | |
421842ed | 3668 | if (rt == net->ipv6.fib6_null_entry) |
e3330039 | 3669 | goto out_put; |
93c2fb25 | 3670 | table = rt->fib6_table; |
66f5d6ce | 3671 | spin_lock_bh(&table->tb6_lock); |
0ae81335 | 3672 | |
93c2fb25 | 3673 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
8d1c802b | 3674 | struct fib6_info *sibling, *next_sibling; |
0ae81335 | 3675 | |
16a16cd3 DA |
3676 | /* prefer to send a single notification with all hops */ |
3677 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); | |
3678 | if (skb) { | |
3679 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; | |
3680 | ||
d4ead6b3 | 3681 | if (rt6_fill_node(net, skb, rt, NULL, |
16a16cd3 DA |
3682 | NULL, NULL, 0, RTM_DELROUTE, |
3683 | info->portid, seq, 0) < 0) { | |
3684 | kfree_skb(skb); | |
3685 | skb = NULL; | |
3686 | } else | |
3687 | info->skip_notify = 1; | |
3688 | } | |
3689 | ||
0ae81335 | 3690 | list_for_each_entry_safe(sibling, next_sibling, |
93c2fb25 DA |
3691 | &rt->fib6_siblings, |
3692 | fib6_siblings) { | |
0ae81335 DA |
3693 | err = fib6_del(sibling, info); |
3694 | if (err) | |
e3330039 | 3695 | goto out_unlock; |
0ae81335 DA |
3696 | } |
3697 | } | |
3698 | ||
3699 | err = fib6_del(rt, info); | |
e3330039 | 3700 | out_unlock: |
66f5d6ce | 3701 | spin_unlock_bh(&table->tb6_lock); |
e3330039 | 3702 | out_put: |
93531c67 | 3703 | fib6_info_release(rt); |
16a16cd3 DA |
3704 | |
3705 | if (skb) { | |
e3330039 | 3706 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
16a16cd3 DA |
3707 | info->nlh, gfp_any()); |
3708 | } | |
0ae81335 DA |
3709 | return err; |
3710 | } | |
3711 | ||
0fa6efc5 | 3712 | static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) |
23fb93a4 DA |
3713 | { |
3714 | int rc = -ESRCH; | |
3715 | ||
3716 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) | |
3717 | goto out; | |
3718 | ||
3719 | if (cfg->fc_flags & RTF_GATEWAY && | |
3720 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) | |
3721 | goto out; | |
761f6026 XL |
3722 | |
3723 | rc = rt6_remove_exception_rt(rt); | |
23fb93a4 DA |
3724 | out: |
3725 | return rc; | |
3726 | } | |
3727 | ||
0fa6efc5 DA |
3728 | static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, |
3729 | struct fib6_nh *nh) | |
3730 | { | |
3731 | struct fib6_result res = { | |
3732 | .f6i = rt, | |
3733 | .nh = nh, | |
3734 | }; | |
3735 | struct rt6_info *rt_cache; | |
3736 | ||
3737 | rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); | |
3738 | if (rt_cache) | |
3739 | return __ip6_del_cached_rt(rt_cache, cfg); | |
3740 | ||
3741 | return 0; | |
3742 | } | |
3743 | ||
333c4301 DA |
3744 | static int ip6_route_del(struct fib6_config *cfg, |
3745 | struct netlink_ext_ack *extack) | |
1da177e4 | 3746 | { |
c71099ac | 3747 | struct fib6_table *table; |
8d1c802b | 3748 | struct fib6_info *rt; |
1da177e4 | 3749 | struct fib6_node *fn; |
1da177e4 LT |
3750 | int err = -ESRCH; |
3751 | ||
5578689a | 3752 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
d5d531cb DA |
3753 | if (!table) { |
3754 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); | |
c71099ac | 3755 | return err; |
d5d531cb | 3756 | } |
c71099ac | 3757 | |
66f5d6ce | 3758 | rcu_read_lock(); |
1da177e4 | 3759 | |
c71099ac | 3760 | fn = fib6_locate(&table->tb6_root, |
86872cb5 | 3761 | &cfg->fc_dst, cfg->fc_dst_len, |
38fbeeee | 3762 | &cfg->fc_src, cfg->fc_src_len, |
2b760fcf | 3763 | !(cfg->fc_flags & RTF_CACHE)); |
1ab1457c | 3764 | |
1da177e4 | 3765 | if (fn) { |
66f5d6ce | 3766 | for_each_fib6_node_rt_rcu(fn) { |
ad1601ae DA |
3767 | struct fib6_nh *nh; |
3768 | ||
1cf844c7 | 3769 | nh = rt->fib6_nh; |
2b760fcf | 3770 | if (cfg->fc_flags & RTF_CACHE) { |
23fb93a4 DA |
3771 | int rc; |
3772 | ||
0fa6efc5 DA |
3773 | rc = ip6_del_cached_rt(cfg, rt, nh); |
3774 | if (rc != -ESRCH) { | |
3775 | rcu_read_unlock(); | |
3776 | return rc; | |
23fb93a4 DA |
3777 | } |
3778 | continue; | |
2b760fcf | 3779 | } |
ad1601ae | 3780 | |
86872cb5 | 3781 | if (cfg->fc_ifindex && |
ad1601ae DA |
3782 | (!nh->fib_nh_dev || |
3783 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) | |
1da177e4 | 3784 | continue; |
86872cb5 | 3785 | if (cfg->fc_flags & RTF_GATEWAY && |
ad1601ae | 3786 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
1da177e4 | 3787 | continue; |
93c2fb25 | 3788 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
1da177e4 | 3789 | continue; |
93c2fb25 | 3790 | if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) |
c2ed1880 | 3791 | continue; |
e873e4b9 WW |
3792 | if (!fib6_info_hold_safe(rt)) |
3793 | continue; | |
66f5d6ce | 3794 | rcu_read_unlock(); |
1da177e4 | 3795 | |
0ae81335 DA |
3796 | /* if gateway was specified only delete the one hop */ |
3797 | if (cfg->fc_flags & RTF_GATEWAY) | |
3798 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); | |
3799 | ||
3800 | return __ip6_del_rt_siblings(rt, cfg); | |
1da177e4 LT |
3801 | } |
3802 | } | |
66f5d6ce | 3803 | rcu_read_unlock(); |
1da177e4 LT |
3804 | |
3805 | return err; | |
3806 | } | |
3807 | ||
6700c270 | 3808 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) |
a6279458 | 3809 | { |
a6279458 | 3810 | struct netevent_redirect netevent; |
e8599ff4 | 3811 | struct rt6_info *rt, *nrt = NULL; |
85bd05de | 3812 | struct fib6_result res = {}; |
e8599ff4 DM |
3813 | struct ndisc_options ndopts; |
3814 | struct inet6_dev *in6_dev; | |
3815 | struct neighbour *neigh; | |
71bcdba0 | 3816 | struct rd_msg *msg; |
6e157b6a DM |
3817 | int optlen, on_link; |
3818 | u8 *lladdr; | |
e8599ff4 | 3819 | |
29a3cad5 | 3820 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
71bcdba0 | 3821 | optlen -= sizeof(*msg); |
e8599ff4 DM |
3822 | |
3823 | if (optlen < 0) { | |
6e157b6a | 3824 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
e8599ff4 DM |
3825 | return; |
3826 | } | |
3827 | ||
71bcdba0 | 3828 | msg = (struct rd_msg *)icmp6_hdr(skb); |
e8599ff4 | 3829 | |
71bcdba0 | 3830 | if (ipv6_addr_is_multicast(&msg->dest)) { |
6e157b6a | 3831 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
e8599ff4 DM |
3832 | return; |
3833 | } | |
3834 | ||
6e157b6a | 3835 | on_link = 0; |
71bcdba0 | 3836 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
e8599ff4 | 3837 | on_link = 1; |
71bcdba0 | 3838 | } else if (ipv6_addr_type(&msg->target) != |
e8599ff4 | 3839 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
6e157b6a | 3840 | net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); |
e8599ff4 DM |
3841 | return; |
3842 | } | |
3843 | ||
3844 | in6_dev = __in6_dev_get(skb->dev); | |
3845 | if (!in6_dev) | |
3846 | return; | |
3847 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) | |
3848 | return; | |
3849 | ||
3850 | /* RFC2461 8.1: | |
3851 | * The IP source address of the Redirect MUST be the same as the current | |
3852 | * first-hop router for the specified ICMP Destination Address. | |
3853 | */ | |
3854 | ||
f997c55c | 3855 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
e8599ff4 DM |
3856 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
3857 | return; | |
3858 | } | |
6e157b6a DM |
3859 | |
3860 | lladdr = NULL; | |
e8599ff4 DM |
3861 | if (ndopts.nd_opts_tgt_lladdr) { |
3862 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, | |
3863 | skb->dev); | |
3864 | if (!lladdr) { | |
3865 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); | |
3866 | return; | |
3867 | } | |
3868 | } | |
3869 | ||
6e157b6a | 3870 | rt = (struct rt6_info *) dst; |
ec13ad1d | 3871 | if (rt->rt6i_flags & RTF_REJECT) { |
6e157b6a | 3872 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
e8599ff4 | 3873 | return; |
6e157b6a | 3874 | } |
e8599ff4 | 3875 | |
6e157b6a DM |
3876 | /* Redirect received -> path was valid. |
3877 | * Look, redirects are sent only in response to data packets, | |
3878 | * so that this nexthop apparently is reachable. --ANK | |
3879 | */ | |
0dec879f | 3880 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
a6279458 | 3881 | |
71bcdba0 | 3882 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
6e157b6a DM |
3883 | if (!neigh) |
3884 | return; | |
a6279458 | 3885 | |
1da177e4 LT |
3886 | /* |
3887 | * We have finally decided to accept it. | |
3888 | */ | |
3889 | ||
f997c55c | 3890 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
1da177e4 LT |
3891 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
3892 | NEIGH_UPDATE_F_OVERRIDE| | |
3893 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| | |
f997c55c AA |
3894 | NEIGH_UPDATE_F_ISROUTER)), |
3895 | NDISC_REDIRECT, &ndopts); | |
1da177e4 | 3896 | |
4d85cd0c | 3897 | rcu_read_lock(); |
85bd05de | 3898 | res.f6i = rcu_dereference(rt->from); |
ff24e498 | 3899 | if (!res.f6i) |
886b7a50 | 3900 | goto out; |
8a14e46f | 3901 | |
1cf844c7 | 3902 | res.nh = res.f6i->fib6_nh; |
7d21fec9 DA |
3903 | res.fib6_flags = res.f6i->fib6_flags; |
3904 | res.fib6_type = res.f6i->fib6_type; | |
85bd05de | 3905 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
38308473 | 3906 | if (!nrt) |
1da177e4 LT |
3907 | goto out; |
3908 | ||
3909 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; | |
3910 | if (on_link) | |
3911 | nrt->rt6i_flags &= ~RTF_GATEWAY; | |
3912 | ||
4e3fd7a0 | 3913 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
1da177e4 | 3914 | |
886b7a50 | 3915 | /* rt6_insert_exception() will take care of duplicated exceptions */ |
5012f0a5 | 3916 | if (rt6_insert_exception(nrt, &res)) { |
2b760fcf WW |
3917 | dst_release_immediate(&nrt->dst); |
3918 | goto out; | |
3919 | } | |
1da177e4 | 3920 | |
d8d1f30b CG |
3921 | netevent.old = &rt->dst; |
3922 | netevent.new = &nrt->dst; | |
71bcdba0 | 3923 | netevent.daddr = &msg->dest; |
60592833 | 3924 | netevent.neigh = neigh; |
8d71740c TT |
3925 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
3926 | ||
1da177e4 | 3927 | out: |
886b7a50 | 3928 | rcu_read_unlock(); |
e8599ff4 | 3929 | neigh_release(neigh); |
6e157b6a DM |
3930 | } |
3931 | ||
70ceb4f5 | 3932 | #ifdef CONFIG_IPV6_ROUTE_INFO |
8d1c802b | 3933 | static struct fib6_info *rt6_get_route_info(struct net *net, |
b71d1d42 | 3934 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
3935 | const struct in6_addr *gwaddr, |
3936 | struct net_device *dev) | |
70ceb4f5 | 3937 | { |
830218c1 DA |
3938 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
3939 | int ifindex = dev->ifindex; | |
70ceb4f5 | 3940 | struct fib6_node *fn; |
8d1c802b | 3941 | struct fib6_info *rt = NULL; |
c71099ac TG |
3942 | struct fib6_table *table; |
3943 | ||
830218c1 | 3944 | table = fib6_get_table(net, tb_id); |
38308473 | 3945 | if (!table) |
c71099ac | 3946 | return NULL; |
70ceb4f5 | 3947 | |
66f5d6ce | 3948 | rcu_read_lock(); |
38fbeeee | 3949 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
70ceb4f5 YH |
3950 | if (!fn) |
3951 | goto out; | |
3952 | ||
66f5d6ce | 3953 | for_each_fib6_node_rt_rcu(fn) { |
f88d8ea6 DA |
3954 | /* these routes do not use nexthops */ |
3955 | if (rt->nh) | |
3956 | continue; | |
1cf844c7 | 3957 | if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) |
70ceb4f5 | 3958 | continue; |
2b2450ca | 3959 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
1cf844c7 | 3960 | !rt->fib6_nh->fib_nh_gw_family) |
70ceb4f5 | 3961 | continue; |
1cf844c7 | 3962 | if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) |
70ceb4f5 | 3963 | continue; |
e873e4b9 WW |
3964 | if (!fib6_info_hold_safe(rt)) |
3965 | continue; | |
70ceb4f5 YH |
3966 | break; |
3967 | } | |
3968 | out: | |
66f5d6ce | 3969 | rcu_read_unlock(); |
70ceb4f5 YH |
3970 | return rt; |
3971 | } | |
3972 | ||
8d1c802b | 3973 | static struct fib6_info *rt6_add_route_info(struct net *net, |
b71d1d42 | 3974 | const struct in6_addr *prefix, int prefixlen, |
830218c1 DA |
3975 | const struct in6_addr *gwaddr, |
3976 | struct net_device *dev, | |
95c96174 | 3977 | unsigned int pref) |
70ceb4f5 | 3978 | { |
86872cb5 | 3979 | struct fib6_config cfg = { |
238fc7ea | 3980 | .fc_metric = IP6_RT_PRIO_USER, |
830218c1 | 3981 | .fc_ifindex = dev->ifindex, |
86872cb5 TG |
3982 | .fc_dst_len = prefixlen, |
3983 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | | |
3984 | RTF_UP | RTF_PREF(pref), | |
b91d5329 | 3985 | .fc_protocol = RTPROT_RA, |
e8478e80 | 3986 | .fc_type = RTN_UNICAST, |
15e47304 | 3987 | .fc_nlinfo.portid = 0, |
efa2cea0 DL |
3988 | .fc_nlinfo.nlh = NULL, |
3989 | .fc_nlinfo.nl_net = net, | |
86872cb5 TG |
3990 | }; |
3991 | ||
830218c1 | 3992 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
4e3fd7a0 AD |
3993 | cfg.fc_dst = *prefix; |
3994 | cfg.fc_gateway = *gwaddr; | |
70ceb4f5 | 3995 | |
e317da96 YH |
3996 | /* We should treat it as a default route if prefix length is 0. */ |
3997 | if (!prefixlen) | |
86872cb5 | 3998 | cfg.fc_flags |= RTF_DEFAULT; |
70ceb4f5 | 3999 | |
acb54e3c | 4000 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
70ceb4f5 | 4001 | |
830218c1 | 4002 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
70ceb4f5 YH |
4003 | } |
4004 | #endif | |
4005 | ||
8d1c802b | 4006 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
afb1d4b5 DA |
4007 | const struct in6_addr *addr, |
4008 | struct net_device *dev) | |
1ab1457c | 4009 | { |
830218c1 | 4010 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
8d1c802b | 4011 | struct fib6_info *rt; |
c71099ac | 4012 | struct fib6_table *table; |
1da177e4 | 4013 | |
afb1d4b5 | 4014 | table = fib6_get_table(net, tb_id); |
38308473 | 4015 | if (!table) |
c71099ac | 4016 | return NULL; |
1da177e4 | 4017 | |
66f5d6ce WW |
4018 | rcu_read_lock(); |
4019 | for_each_fib6_node_rt_rcu(&table->tb6_root) { | |
f88d8ea6 | 4020 | struct fib6_nh *nh; |
ad1601ae | 4021 | |
f88d8ea6 DA |
4022 | /* RA routes do not use nexthops */ |
4023 | if (rt->nh) | |
4024 | continue; | |
4025 | ||
4026 | nh = rt->fib6_nh; | |
ad1601ae | 4027 | if (dev == nh->fib_nh_dev && |
93c2fb25 | 4028 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
ad1601ae | 4029 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
1da177e4 LT |
4030 | break; |
4031 | } | |
e873e4b9 WW |
4032 | if (rt && !fib6_info_hold_safe(rt)) |
4033 | rt = NULL; | |
66f5d6ce | 4034 | rcu_read_unlock(); |
1da177e4 LT |
4035 | return rt; |
4036 | } | |
4037 | ||
8d1c802b | 4038 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
afb1d4b5 | 4039 | const struct in6_addr *gwaddr, |
ebacaaa0 YH |
4040 | struct net_device *dev, |
4041 | unsigned int pref) | |
1da177e4 | 4042 | { |
86872cb5 | 4043 | struct fib6_config cfg = { |
ca254490 | 4044 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
238fc7ea | 4045 | .fc_metric = IP6_RT_PRIO_USER, |
86872cb5 TG |
4046 | .fc_ifindex = dev->ifindex, |
4047 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | | |
4048 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), | |
b91d5329 | 4049 | .fc_protocol = RTPROT_RA, |
e8478e80 | 4050 | .fc_type = RTN_UNICAST, |
15e47304 | 4051 | .fc_nlinfo.portid = 0, |
5578689a | 4052 | .fc_nlinfo.nlh = NULL, |
afb1d4b5 | 4053 | .fc_nlinfo.nl_net = net, |
86872cb5 | 4054 | }; |
1da177e4 | 4055 | |
4e3fd7a0 | 4056 | cfg.fc_gateway = *gwaddr; |
1da177e4 | 4057 | |
acb54e3c | 4058 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
830218c1 DA |
4059 | struct fib6_table *table; |
4060 | ||
4061 | table = fib6_get_table(dev_net(dev), cfg.fc_table); | |
4062 | if (table) | |
4063 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; | |
4064 | } | |
1da177e4 | 4065 | |
afb1d4b5 | 4066 | return rt6_get_dflt_router(net, gwaddr, dev); |
1da177e4 LT |
4067 | } |
4068 | ||
afb1d4b5 DA |
4069 | static void __rt6_purge_dflt_routers(struct net *net, |
4070 | struct fib6_table *table) | |
1da177e4 | 4071 | { |
8d1c802b | 4072 | struct fib6_info *rt; |
1da177e4 LT |
4073 | |
4074 | restart: | |
66f5d6ce WW |
4075 | rcu_read_lock(); |
4076 | for_each_fib6_node_rt_rcu(&table->tb6_root) { | |
dcd1f572 DA |
4077 | struct net_device *dev = fib6_info_nh_dev(rt); |
4078 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; | |
4079 | ||
93c2fb25 | 4080 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
e873e4b9 WW |
4081 | (!idev || idev->cnf.accept_ra != 2) && |
4082 | fib6_info_hold_safe(rt)) { | |
93531c67 DA |
4083 | rcu_read_unlock(); |
4084 | ip6_del_rt(net, rt); | |
1da177e4 LT |
4085 | goto restart; |
4086 | } | |
4087 | } | |
66f5d6ce | 4088 | rcu_read_unlock(); |
830218c1 DA |
4089 | |
4090 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; | |
4091 | } | |
4092 | ||
4093 | void rt6_purge_dflt_routers(struct net *net) | |
4094 | { | |
4095 | struct fib6_table *table; | |
4096 | struct hlist_head *head; | |
4097 | unsigned int h; | |
4098 | ||
4099 | rcu_read_lock(); | |
4100 | ||
4101 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { | |
4102 | head = &net->ipv6.fib_table_hash[h]; | |
4103 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { | |
4104 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) | |
afb1d4b5 | 4105 | __rt6_purge_dflt_routers(net, table); |
830218c1 DA |
4106 | } |
4107 | } | |
4108 | ||
4109 | rcu_read_unlock(); | |
1da177e4 LT |
4110 | } |
4111 | ||
5578689a DL |
4112 | static void rtmsg_to_fib6_config(struct net *net, |
4113 | struct in6_rtmsg *rtmsg, | |
86872cb5 TG |
4114 | struct fib6_config *cfg) |
4115 | { | |
8823a3ac MŻ |
4116 | *cfg = (struct fib6_config){ |
4117 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? | |
4118 | : RT6_TABLE_MAIN, | |
4119 | .fc_ifindex = rtmsg->rtmsg_ifindex, | |
67f69513 | 4120 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
8823a3ac MŻ |
4121 | .fc_expires = rtmsg->rtmsg_info, |
4122 | .fc_dst_len = rtmsg->rtmsg_dst_len, | |
4123 | .fc_src_len = rtmsg->rtmsg_src_len, | |
4124 | .fc_flags = rtmsg->rtmsg_flags, | |
4125 | .fc_type = rtmsg->rtmsg_type, | |
4126 | ||
4127 | .fc_nlinfo.nl_net = net, | |
4128 | ||
4129 | .fc_dst = rtmsg->rtmsg_dst, | |
4130 | .fc_src = rtmsg->rtmsg_src, | |
4131 | .fc_gateway = rtmsg->rtmsg_gateway, | |
4132 | }; | |
86872cb5 TG |
4133 | } |
4134 | ||
5578689a | 4135 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
1da177e4 | 4136 | { |
86872cb5 | 4137 | struct fib6_config cfg; |
1da177e4 LT |
4138 | struct in6_rtmsg rtmsg; |
4139 | int err; | |
4140 | ||
67ba4152 | 4141 | switch (cmd) { |
1da177e4 LT |
4142 | case SIOCADDRT: /* Add a route */ |
4143 | case SIOCDELRT: /* Delete a route */ | |
af31f412 | 4144 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
4145 | return -EPERM; |
4146 | err = copy_from_user(&rtmsg, arg, | |
4147 | sizeof(struct in6_rtmsg)); | |
4148 | if (err) | |
4149 | return -EFAULT; | |
86872cb5 | 4150 | |
5578689a | 4151 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
86872cb5 | 4152 | |
1da177e4 LT |
4153 | rtnl_lock(); |
4154 | switch (cmd) { | |
4155 | case SIOCADDRT: | |
acb54e3c | 4156 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
1da177e4 LT |
4157 | break; |
4158 | case SIOCDELRT: | |
333c4301 | 4159 | err = ip6_route_del(&cfg, NULL); |
1da177e4 LT |
4160 | break; |
4161 | default: | |
4162 | err = -EINVAL; | |
4163 | } | |
4164 | rtnl_unlock(); | |
4165 | ||
4166 | return err; | |
3ff50b79 | 4167 | } |
1da177e4 LT |
4168 | |
4169 | return -EINVAL; | |
4170 | } | |
4171 | ||
4172 | /* | |
4173 | * Drop the packet on the floor | |
4174 | */ | |
4175 | ||
d5fdd6ba | 4176 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) |
1da177e4 | 4177 | { |
adf30907 | 4178 | struct dst_entry *dst = skb_dst(skb); |
1d3fd8a1 SS |
4179 | struct net *net = dev_net(dst->dev); |
4180 | struct inet6_dev *idev; | |
4181 | int type; | |
4182 | ||
4183 | if (netif_is_l3_master(skb->dev) && | |
4184 | dst->dev == net->loopback_dev) | |
4185 | idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); | |
4186 | else | |
4187 | idev = ip6_dst_idev(dst); | |
4188 | ||
612f09e8 YH |
4189 | switch (ipstats_mib_noroutes) { |
4190 | case IPSTATS_MIB_INNOROUTES: | |
0660e03f | 4191 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
45bb0060 | 4192 | if (type == IPV6_ADDR_ANY) { |
1d3fd8a1 | 4193 | IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
612f09e8 YH |
4194 | break; |
4195 | } | |
4196 | /* FALLTHROUGH */ | |
4197 | case IPSTATS_MIB_OUTNOROUTES: | |
1d3fd8a1 | 4198 | IP6_INC_STATS(net, idev, ipstats_mib_noroutes); |
612f09e8 YH |
4199 | break; |
4200 | } | |
1d3fd8a1 SS |
4201 | |
4202 | /* Start over by dropping the dst for l3mdev case */ | |
4203 | if (netif_is_l3_master(skb->dev)) | |
4204 | skb_dst_drop(skb); | |
4205 | ||
3ffe533c | 4206 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
1da177e4 LT |
4207 | kfree_skb(skb); |
4208 | return 0; | |
4209 | } | |
4210 | ||
9ce8ade0 TG |
4211 | static int ip6_pkt_discard(struct sk_buff *skb) |
4212 | { | |
612f09e8 | 4213 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
4214 | } |
4215 | ||
ede2059d | 4216 | static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 4217 | { |
adf30907 | 4218 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 4219 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
1da177e4 LT |
4220 | } |
4221 | ||
9ce8ade0 TG |
4222 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
4223 | { | |
612f09e8 | 4224 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
4225 | } |
4226 | ||
ede2059d | 4227 | static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
9ce8ade0 | 4228 | { |
adf30907 | 4229 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 4230 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
9ce8ade0 TG |
4231 | } |
4232 | ||
1da177e4 LT |
4233 | /* |
4234 | * Allocate a dst for local (unicast / anycast) address. | |
4235 | */ | |
4236 | ||
360a9887 DA |
4237 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
4238 | struct inet6_dev *idev, | |
4239 | const struct in6_addr *addr, | |
4240 | bool anycast, gfp_t gfp_flags) | |
1da177e4 | 4241 | { |
c7a1ce39 DA |
4242 | struct fib6_config cfg = { |
4243 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, | |
4244 | .fc_ifindex = idev->dev->ifindex, | |
4245 | .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, | |
4246 | .fc_dst = *addr, | |
4247 | .fc_dst_len = 128, | |
4248 | .fc_protocol = RTPROT_KERNEL, | |
4249 | .fc_nlinfo.nl_net = net, | |
4250 | .fc_ignore_dev_down = true, | |
4251 | }; | |
1da177e4 | 4252 | |
e8478e80 | 4253 | if (anycast) { |
c7a1ce39 DA |
4254 | cfg.fc_type = RTN_ANYCAST; |
4255 | cfg.fc_flags |= RTF_ANYCAST; | |
e8478e80 | 4256 | } else { |
c7a1ce39 DA |
4257 | cfg.fc_type = RTN_LOCAL; |
4258 | cfg.fc_flags |= RTF_LOCAL; | |
e8478e80 | 4259 | } |
1da177e4 | 4260 | |
c7a1ce39 | 4261 | return ip6_route_info_create(&cfg, gfp_flags, NULL); |
1da177e4 LT |
4262 | } |
4263 | ||
c3968a85 DW |
4264 | /* remove deleted ip from prefsrc entries */ |
4265 | struct arg_dev_net_ip { | |
4266 | struct net_device *dev; | |
4267 | struct net *net; | |
4268 | struct in6_addr *addr; | |
4269 | }; | |
4270 | ||
8d1c802b | 4271 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
c3968a85 DW |
4272 | { |
4273 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; | |
4274 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; | |
4275 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; | |
4276 | ||
f88d8ea6 DA |
4277 | if (!rt->nh && |
4278 | ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && | |
421842ed | 4279 | rt != net->ipv6.fib6_null_entry && |
93c2fb25 | 4280 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
60006a48 | 4281 | spin_lock_bh(&rt6_exception_lock); |
c3968a85 | 4282 | /* remove prefsrc entry */ |
93c2fb25 | 4283 | rt->fib6_prefsrc.plen = 0; |
60006a48 | 4284 | spin_unlock_bh(&rt6_exception_lock); |
c3968a85 DW |
4285 | } |
4286 | return 0; | |
4287 | } | |
4288 | ||
4289 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) | |
4290 | { | |
4291 | struct net *net = dev_net(ifp->idev->dev); | |
4292 | struct arg_dev_net_ip adni = { | |
4293 | .dev = ifp->idev->dev, | |
4294 | .net = net, | |
4295 | .addr = &ifp->addr, | |
4296 | }; | |
0c3584d5 | 4297 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
c3968a85 DW |
4298 | } |
4299 | ||
2b2450ca | 4300 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
be7a010d DJ |
4301 | |
4302 | /* Remove routers and update dst entries when gateway turn into host. */ | |
8d1c802b | 4303 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
be7a010d DJ |
4304 | { |
4305 | struct in6_addr *gateway = (struct in6_addr *)arg; | |
f88d8ea6 DA |
4306 | struct fib6_nh *nh; |
4307 | ||
4308 | /* RA routes do not use nexthops */ | |
4309 | if (rt->nh) | |
4310 | return 0; | |
be7a010d | 4311 | |
f88d8ea6 | 4312 | nh = rt->fib6_nh; |
93c2fb25 | 4313 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
cc5c073a | 4314 | nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) |
be7a010d | 4315 | return -1; |
b16cb459 WW |
4316 | |
4317 | /* Further clean up cached routes in exception table. | |
4318 | * This is needed because cached route may have a different | |
4319 | * gateway than its 'parent' in the case of an ip redirect. | |
4320 | */ | |
cc5c073a | 4321 | fib6_nh_exceptions_clean_tohost(nh, gateway); |
b16cb459 | 4322 | |
be7a010d DJ |
4323 | return 0; |
4324 | } | |
4325 | ||
4326 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) | |
4327 | { | |
4328 | fib6_clean_all(net, fib6_clean_tohost, gateway); | |
4329 | } | |
4330 | ||
2127d95a IS |
4331 | struct arg_netdev_event { |
4332 | const struct net_device *dev; | |
4c981e28 | 4333 | union { |
ecc5663c | 4334 | unsigned char nh_flags; |
4c981e28 IS |
4335 | unsigned long event; |
4336 | }; | |
2127d95a IS |
4337 | }; |
4338 | ||
8d1c802b | 4339 | static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) |
d7dedee1 | 4340 | { |
8d1c802b | 4341 | struct fib6_info *iter; |
d7dedee1 IS |
4342 | struct fib6_node *fn; |
4343 | ||
93c2fb25 DA |
4344 | fn = rcu_dereference_protected(rt->fib6_node, |
4345 | lockdep_is_held(&rt->fib6_table->tb6_lock)); | |
d7dedee1 | 4346 | iter = rcu_dereference_protected(fn->leaf, |
93c2fb25 | 4347 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
d7dedee1 | 4348 | while (iter) { |
93c2fb25 | 4349 | if (iter->fib6_metric == rt->fib6_metric && |
33bd5ac5 | 4350 | rt6_qualify_for_ecmp(iter)) |
d7dedee1 | 4351 | return iter; |
8fb11a9a | 4352 | iter = rcu_dereference_protected(iter->fib6_next, |
93c2fb25 | 4353 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
d7dedee1 IS |
4354 | } |
4355 | ||
4356 | return NULL; | |
4357 | } | |
4358 | ||
f88d8ea6 | 4359 | /* only called for fib entries with builtin fib6_nh */ |
8d1c802b | 4360 | static bool rt6_is_dead(const struct fib6_info *rt) |
d7dedee1 | 4361 | { |
1cf844c7 DA |
4362 | if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || |
4363 | (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && | |
4364 | ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) | |
d7dedee1 IS |
4365 | return true; |
4366 | ||
4367 | return false; | |
4368 | } | |
4369 | ||
8d1c802b | 4370 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
d7dedee1 | 4371 | { |
8d1c802b | 4372 | struct fib6_info *iter; |
d7dedee1 IS |
4373 | int total = 0; |
4374 | ||
4375 | if (!rt6_is_dead(rt)) | |
1cf844c7 | 4376 | total += rt->fib6_nh->fib_nh_weight; |
d7dedee1 | 4377 | |
93c2fb25 | 4378 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
d7dedee1 | 4379 | if (!rt6_is_dead(iter)) |
1cf844c7 | 4380 | total += iter->fib6_nh->fib_nh_weight; |
d7dedee1 IS |
4381 | } |
4382 | ||
4383 | return total; | |
4384 | } | |
4385 | ||
8d1c802b | 4386 | static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) |
d7dedee1 IS |
4387 | { |
4388 | int upper_bound = -1; | |
4389 | ||
4390 | if (!rt6_is_dead(rt)) { | |
1cf844c7 | 4391 | *weight += rt->fib6_nh->fib_nh_weight; |
d7dedee1 IS |
4392 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
4393 | total) - 1; | |
4394 | } | |
1cf844c7 | 4395 | atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); |
d7dedee1 IS |
4396 | } |
4397 | ||
8d1c802b | 4398 | static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) |
d7dedee1 | 4399 | { |
8d1c802b | 4400 | struct fib6_info *iter; |
d7dedee1 IS |
4401 | int weight = 0; |
4402 | ||
4403 | rt6_upper_bound_set(rt, &weight, total); | |
4404 | ||
93c2fb25 | 4405 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
d7dedee1 IS |
4406 | rt6_upper_bound_set(iter, &weight, total); |
4407 | } | |
4408 | ||
8d1c802b | 4409 | void rt6_multipath_rebalance(struct fib6_info *rt) |
d7dedee1 | 4410 | { |
8d1c802b | 4411 | struct fib6_info *first; |
d7dedee1 IS |
4412 | int total; |
4413 | ||
4414 | /* In case the entire multipath route was marked for flushing, | |
4415 | * then there is no need to rebalance upon the removal of every | |
4416 | * sibling route. | |
4417 | */ | |
93c2fb25 | 4418 | if (!rt->fib6_nsiblings || rt->should_flush) |
d7dedee1 IS |
4419 | return; |
4420 | ||
4421 | /* During lookup routes are evaluated in order, so we need to | |
4422 | * make sure upper bounds are assigned from the first sibling | |
4423 | * onwards. | |
4424 | */ | |
4425 | first = rt6_multipath_first_sibling(rt); | |
4426 | if (WARN_ON_ONCE(!first)) | |
4427 | return; | |
4428 | ||
4429 | total = rt6_multipath_total_weight(first); | |
4430 | rt6_multipath_upper_bound_set(first, total); | |
4431 | } | |
4432 | ||
8d1c802b | 4433 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
2127d95a IS |
4434 | { |
4435 | const struct arg_netdev_event *arg = p_arg; | |
7aef6859 | 4436 | struct net *net = dev_net(arg->dev); |
2127d95a | 4437 | |
f88d8ea6 | 4438 | if (rt != net->ipv6.fib6_null_entry && !rt->nh && |
1cf844c7 DA |
4439 | rt->fib6_nh->fib_nh_dev == arg->dev) { |
4440 | rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; | |
7aef6859 | 4441 | fib6_update_sernum_upto_root(net, rt); |
d7dedee1 | 4442 | rt6_multipath_rebalance(rt); |
1de178ed | 4443 | } |
2127d95a IS |
4444 | |
4445 | return 0; | |
4446 | } | |
4447 | ||
ecc5663c | 4448 | void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) |
2127d95a IS |
4449 | { |
4450 | struct arg_netdev_event arg = { | |
4451 | .dev = dev, | |
6802f3ad IS |
4452 | { |
4453 | .nh_flags = nh_flags, | |
4454 | }, | |
2127d95a IS |
4455 | }; |
4456 | ||
4457 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) | |
4458 | arg.nh_flags |= RTNH_F_LINKDOWN; | |
4459 | ||
4460 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); | |
4461 | } | |
4462 | ||
f88d8ea6 | 4463 | /* only called for fib entries with inline fib6_nh */ |
8d1c802b | 4464 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
1de178ed IS |
4465 | const struct net_device *dev) |
4466 | { | |
8d1c802b | 4467 | struct fib6_info *iter; |
1de178ed | 4468 | |
1cf844c7 | 4469 | if (rt->fib6_nh->fib_nh_dev == dev) |
1de178ed | 4470 | return true; |
93c2fb25 | 4471 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 | 4472 | if (iter->fib6_nh->fib_nh_dev == dev) |
1de178ed IS |
4473 | return true; |
4474 | ||
4475 | return false; | |
4476 | } | |
4477 | ||
8d1c802b | 4478 | static void rt6_multipath_flush(struct fib6_info *rt) |
1de178ed | 4479 | { |
8d1c802b | 4480 | struct fib6_info *iter; |
1de178ed IS |
4481 | |
4482 | rt->should_flush = 1; | |
93c2fb25 | 4483 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1de178ed IS |
4484 | iter->should_flush = 1; |
4485 | } | |
4486 | ||
8d1c802b | 4487 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
1de178ed IS |
4488 | const struct net_device *down_dev) |
4489 | { | |
8d1c802b | 4490 | struct fib6_info *iter; |
1de178ed IS |
4491 | unsigned int dead = 0; |
4492 | ||
1cf844c7 DA |
4493 | if (rt->fib6_nh->fib_nh_dev == down_dev || |
4494 | rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) | |
1de178ed | 4495 | dead++; |
93c2fb25 | 4496 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 DA |
4497 | if (iter->fib6_nh->fib_nh_dev == down_dev || |
4498 | iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) | |
1de178ed IS |
4499 | dead++; |
4500 | ||
4501 | return dead; | |
4502 | } | |
4503 | ||
8d1c802b | 4504 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
1de178ed | 4505 | const struct net_device *dev, |
ecc5663c | 4506 | unsigned char nh_flags) |
1de178ed | 4507 | { |
8d1c802b | 4508 | struct fib6_info *iter; |
1de178ed | 4509 | |
1cf844c7 DA |
4510 | if (rt->fib6_nh->fib_nh_dev == dev) |
4511 | rt->fib6_nh->fib_nh_flags |= nh_flags; | |
93c2fb25 | 4512 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
1cf844c7 DA |
4513 | if (iter->fib6_nh->fib_nh_dev == dev) |
4514 | iter->fib6_nh->fib_nh_flags |= nh_flags; | |
1de178ed IS |
4515 | } |
4516 | ||
a1a22c12 | 4517 | /* called with write lock held for table with rt */ |
8d1c802b | 4518 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
1da177e4 | 4519 | { |
4c981e28 IS |
4520 | const struct arg_netdev_event *arg = p_arg; |
4521 | const struct net_device *dev = arg->dev; | |
7aef6859 | 4522 | struct net *net = dev_net(dev); |
8ed67789 | 4523 | |
f88d8ea6 | 4524 | if (rt == net->ipv6.fib6_null_entry || rt->nh) |
27c6fa73 IS |
4525 | return 0; |
4526 | ||
4527 | switch (arg->event) { | |
4528 | case NETDEV_UNREGISTER: | |
1cf844c7 | 4529 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
27c6fa73 | 4530 | case NETDEV_DOWN: |
1de178ed | 4531 | if (rt->should_flush) |
27c6fa73 | 4532 | return -1; |
93c2fb25 | 4533 | if (!rt->fib6_nsiblings) |
1cf844c7 | 4534 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
1de178ed IS |
4535 | if (rt6_multipath_uses_dev(rt, dev)) { |
4536 | unsigned int count; | |
4537 | ||
4538 | count = rt6_multipath_dead_count(rt, dev); | |
93c2fb25 | 4539 | if (rt->fib6_nsiblings + 1 == count) { |
1de178ed IS |
4540 | rt6_multipath_flush(rt); |
4541 | return -1; | |
4542 | } | |
4543 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | | |
4544 | RTNH_F_LINKDOWN); | |
7aef6859 | 4545 | fib6_update_sernum(net, rt); |
d7dedee1 | 4546 | rt6_multipath_rebalance(rt); |
1de178ed IS |
4547 | } |
4548 | return -2; | |
27c6fa73 | 4549 | case NETDEV_CHANGE: |
1cf844c7 | 4550 | if (rt->fib6_nh->fib_nh_dev != dev || |
93c2fb25 | 4551 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
27c6fa73 | 4552 | break; |
1cf844c7 | 4553 | rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
d7dedee1 | 4554 | rt6_multipath_rebalance(rt); |
27c6fa73 | 4555 | break; |
2b241361 | 4556 | } |
c159d30c | 4557 | |
1da177e4 LT |
4558 | return 0; |
4559 | } | |
4560 | ||
27c6fa73 | 4561 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
1da177e4 | 4562 | { |
4c981e28 | 4563 | struct arg_netdev_event arg = { |
8ed67789 | 4564 | .dev = dev, |
6802f3ad IS |
4565 | { |
4566 | .event = event, | |
4567 | }, | |
8ed67789 | 4568 | }; |
7c6bb7d2 | 4569 | struct net *net = dev_net(dev); |
8ed67789 | 4570 | |
7c6bb7d2 DA |
4571 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
4572 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); | |
4573 | else | |
4574 | fib6_clean_all(net, fib6_ifdown, &arg); | |
4c981e28 IS |
4575 | } |
4576 | ||
4577 | void rt6_disable_ip(struct net_device *dev, unsigned long event) | |
4578 | { | |
4579 | rt6_sync_down_dev(dev, event); | |
4580 | rt6_uncached_list_flush_dev(dev_net(dev), dev); | |
4581 | neigh_ifdown(&nd_tbl, dev); | |
1da177e4 LT |
4582 | } |
4583 | ||
95c96174 | 4584 | struct rt6_mtu_change_arg { |
1da177e4 | 4585 | struct net_device *dev; |
95c96174 | 4586 | unsigned int mtu; |
c0b220cf | 4587 | struct fib6_info *f6i; |
1da177e4 LT |
4588 | }; |
4589 | ||
cc5c073a | 4590 | static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) |
c0b220cf DA |
4591 | { |
4592 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; | |
cc5c073a | 4593 | struct fib6_info *f6i = arg->f6i; |
c0b220cf DA |
4594 | |
4595 | /* For administrative MTU increase, there is no way to discover | |
4596 | * IPv6 PMTU increase, so PMTU increase should be updated here. | |
4597 | * Since RFC 1981 doesn't include administrative MTU increase | |
4598 | * update PMTU increase is a MUST. (i.e. jumbo frame) | |
4599 | */ | |
4600 | if (nh->fib_nh_dev == arg->dev) { | |
4601 | struct inet6_dev *idev = __in6_dev_get(arg->dev); | |
4602 | u32 mtu = f6i->fib6_pmtu; | |
4603 | ||
4604 | if (mtu >= arg->mtu || | |
4605 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) | |
4606 | fib6_metric_set(f6i, RTAX_MTU, arg->mtu); | |
4607 | ||
4608 | spin_lock_bh(&rt6_exception_lock); | |
cc5c073a | 4609 | rt6_exceptions_update_pmtu(idev, nh, arg->mtu); |
c0b220cf DA |
4610 | spin_unlock_bh(&rt6_exception_lock); |
4611 | } | |
4612 | ||
4613 | return 0; | |
4614 | } | |
4615 | ||
4616 | static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) | |
1da177e4 LT |
4617 | { |
4618 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; | |
4619 | struct inet6_dev *idev; | |
4620 | ||
4621 | /* In IPv6 pmtu discovery is not optional, | |
4622 | so that RTAX_MTU lock cannot disable it. | |
4623 | We still use this lock to block changes | |
4624 | caused by addrconf/ndisc. | |
4625 | */ | |
4626 | ||
4627 | idev = __in6_dev_get(arg->dev); | |
38308473 | 4628 | if (!idev) |
1da177e4 LT |
4629 | return 0; |
4630 | ||
c0b220cf DA |
4631 | if (fib6_metric_locked(f6i, RTAX_MTU)) |
4632 | return 0; | |
d4ead6b3 | 4633 | |
c0b220cf | 4634 | arg->f6i = f6i; |
1cf844c7 | 4635 | return fib6_nh_mtu_change(f6i->fib6_nh, arg); |
1da177e4 LT |
4636 | } |
4637 | ||
95c96174 | 4638 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
1da177e4 | 4639 | { |
c71099ac TG |
4640 | struct rt6_mtu_change_arg arg = { |
4641 | .dev = dev, | |
4642 | .mtu = mtu, | |
4643 | }; | |
1da177e4 | 4644 | |
0c3584d5 | 4645 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
1da177e4 LT |
4646 | } |
4647 | ||
ef7c79ed | 4648 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
75425657 | 4649 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
5176f91e | 4650 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
aa8f8778 | 4651 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
86872cb5 | 4652 | [RTA_OIF] = { .type = NLA_U32 }, |
ab364a6f | 4653 | [RTA_IIF] = { .type = NLA_U32 }, |
86872cb5 TG |
4654 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
4655 | [RTA_METRICS] = { .type = NLA_NESTED }, | |
51ebd318 | 4656 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
c78ba6d6 | 4657 | [RTA_PREF] = { .type = NLA_U8 }, |
19e42e45 RP |
4658 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
4659 | [RTA_ENCAP] = { .type = NLA_NESTED }, | |
32bc201e | 4660 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
622ec2c9 | 4661 | [RTA_UID] = { .type = NLA_U32 }, |
3b45a410 | 4662 | [RTA_MARK] = { .type = NLA_U32 }, |
aa8f8778 | 4663 | [RTA_TABLE] = { .type = NLA_U32 }, |
eacb9384 RP |
4664 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
4665 | [RTA_SPORT] = { .type = NLA_U16 }, | |
4666 | [RTA_DPORT] = { .type = NLA_U16 }, | |
86872cb5 TG |
4667 | }; |
4668 | ||
4669 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, | |
333c4301 DA |
4670 | struct fib6_config *cfg, |
4671 | struct netlink_ext_ack *extack) | |
1da177e4 | 4672 | { |
86872cb5 TG |
4673 | struct rtmsg *rtm; |
4674 | struct nlattr *tb[RTA_MAX+1]; | |
c78ba6d6 | 4675 | unsigned int pref; |
86872cb5 | 4676 | int err; |
1da177e4 | 4677 | |
8cb08174 JB |
4678 | err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
4679 | rtm_ipv6_policy, extack); | |
86872cb5 TG |
4680 | if (err < 0) |
4681 | goto errout; | |
1da177e4 | 4682 | |
86872cb5 TG |
4683 | err = -EINVAL; |
4684 | rtm = nlmsg_data(nlh); | |
86872cb5 | 4685 | |
84db8407 MŻ |
4686 | *cfg = (struct fib6_config){ |
4687 | .fc_table = rtm->rtm_table, | |
4688 | .fc_dst_len = rtm->rtm_dst_len, | |
4689 | .fc_src_len = rtm->rtm_src_len, | |
4690 | .fc_flags = RTF_UP, | |
4691 | .fc_protocol = rtm->rtm_protocol, | |
4692 | .fc_type = rtm->rtm_type, | |
4693 | ||
4694 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, | |
4695 | .fc_nlinfo.nlh = nlh, | |
4696 | .fc_nlinfo.nl_net = sock_net(skb->sk), | |
4697 | }; | |
86872cb5 | 4698 | |
ef2c7d7b ND |
4699 | if (rtm->rtm_type == RTN_UNREACHABLE || |
4700 | rtm->rtm_type == RTN_BLACKHOLE || | |
b4949ab2 ND |
4701 | rtm->rtm_type == RTN_PROHIBIT || |
4702 | rtm->rtm_type == RTN_THROW) | |
86872cb5 TG |
4703 | cfg->fc_flags |= RTF_REJECT; |
4704 | ||
ab79ad14 MŻ |
4705 | if (rtm->rtm_type == RTN_LOCAL) |
4706 | cfg->fc_flags |= RTF_LOCAL; | |
4707 | ||
1f56a01f MKL |
4708 | if (rtm->rtm_flags & RTM_F_CLONED) |
4709 | cfg->fc_flags |= RTF_CACHE; | |
4710 | ||
fc1e64e1 DA |
4711 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
4712 | ||
86872cb5 | 4713 | if (tb[RTA_GATEWAY]) { |
67b61f6c | 4714 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
86872cb5 | 4715 | cfg->fc_flags |= RTF_GATEWAY; |
1da177e4 | 4716 | } |
e3818541 DA |
4717 | if (tb[RTA_VIA]) { |
4718 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); | |
4719 | goto errout; | |
4720 | } | |
86872cb5 TG |
4721 | |
4722 | if (tb[RTA_DST]) { | |
4723 | int plen = (rtm->rtm_dst_len + 7) >> 3; | |
4724 | ||
4725 | if (nla_len(tb[RTA_DST]) < plen) | |
4726 | goto errout; | |
4727 | ||
4728 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); | |
1da177e4 | 4729 | } |
86872cb5 TG |
4730 | |
4731 | if (tb[RTA_SRC]) { | |
4732 | int plen = (rtm->rtm_src_len + 7) >> 3; | |
4733 | ||
4734 | if (nla_len(tb[RTA_SRC]) < plen) | |
4735 | goto errout; | |
4736 | ||
4737 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); | |
1da177e4 | 4738 | } |
86872cb5 | 4739 | |
c3968a85 | 4740 | if (tb[RTA_PREFSRC]) |
67b61f6c | 4741 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
c3968a85 | 4742 | |
86872cb5 TG |
4743 | if (tb[RTA_OIF]) |
4744 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); | |
4745 | ||
4746 | if (tb[RTA_PRIORITY]) | |
4747 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); | |
4748 | ||
4749 | if (tb[RTA_METRICS]) { | |
4750 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); | |
4751 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); | |
1da177e4 | 4752 | } |
86872cb5 TG |
4753 | |
4754 | if (tb[RTA_TABLE]) | |
4755 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); | |
4756 | ||
51ebd318 ND |
4757 | if (tb[RTA_MULTIPATH]) { |
4758 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); | |
4759 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); | |
9ed59592 DA |
4760 | |
4761 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, | |
c255bd68 | 4762 | cfg->fc_mp_len, extack); |
9ed59592 DA |
4763 | if (err < 0) |
4764 | goto errout; | |
51ebd318 ND |
4765 | } |
4766 | ||
c78ba6d6 LR |
4767 | if (tb[RTA_PREF]) { |
4768 | pref = nla_get_u8(tb[RTA_PREF]); | |
4769 | if (pref != ICMPV6_ROUTER_PREF_LOW && | |
4770 | pref != ICMPV6_ROUTER_PREF_HIGH) | |
4771 | pref = ICMPV6_ROUTER_PREF_MEDIUM; | |
4772 | cfg->fc_flags |= RTF_PREF(pref); | |
4773 | } | |
4774 | ||
19e42e45 RP |
4775 | if (tb[RTA_ENCAP]) |
4776 | cfg->fc_encap = tb[RTA_ENCAP]; | |
4777 | ||
9ed59592 | 4778 | if (tb[RTA_ENCAP_TYPE]) { |
19e42e45 RP |
4779 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
4780 | ||
c255bd68 | 4781 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
9ed59592 DA |
4782 | if (err < 0) |
4783 | goto errout; | |
4784 | } | |
4785 | ||
32bc201e XL |
4786 | if (tb[RTA_EXPIRES]) { |
4787 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); | |
4788 | ||
4789 | if (addrconf_finite_timeout(timeout)) { | |
4790 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); | |
4791 | cfg->fc_flags |= RTF_EXPIRES; | |
4792 | } | |
4793 | } | |
4794 | ||
86872cb5 TG |
4795 | err = 0; |
4796 | errout: | |
4797 | return err; | |
1da177e4 LT |
4798 | } |
4799 | ||
6b9ea5a6 | 4800 | struct rt6_nh { |
8d1c802b | 4801 | struct fib6_info *fib6_info; |
6b9ea5a6 | 4802 | struct fib6_config r_cfg; |
6b9ea5a6 RP |
4803 | struct list_head next; |
4804 | }; | |
4805 | ||
d4ead6b3 DA |
4806 | static int ip6_route_info_append(struct net *net, |
4807 | struct list_head *rt6_nh_list, | |
8d1c802b DA |
4808 | struct fib6_info *rt, |
4809 | struct fib6_config *r_cfg) | |
6b9ea5a6 RP |
4810 | { |
4811 | struct rt6_nh *nh; | |
6b9ea5a6 RP |
4812 | int err = -EEXIST; |
4813 | ||
4814 | list_for_each_entry(nh, rt6_nh_list, next) { | |
8d1c802b DA |
4815 | /* check if fib6_info already exists */ |
4816 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) | |
6b9ea5a6 RP |
4817 | return err; |
4818 | } | |
4819 | ||
4820 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); | |
4821 | if (!nh) | |
4822 | return -ENOMEM; | |
8d1c802b | 4823 | nh->fib6_info = rt; |
6b9ea5a6 RP |
4824 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
4825 | list_add_tail(&nh->next, rt6_nh_list); | |
4826 | ||
4827 | return 0; | |
4828 | } | |
4829 | ||
8d1c802b DA |
4830 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
4831 | struct fib6_info *rt_last, | |
3b1137fe DA |
4832 | struct nl_info *info, |
4833 | __u16 nlflags) | |
4834 | { | |
4835 | /* if this is an APPEND route, then rt points to the first route | |
4836 | * inserted and rt_last points to last route inserted. Userspace | |
4837 | * wants a consistent dump of the route which starts at the first | |
4838 | * nexthop. Since sibling routes are always added at the end of | |
4839 | * the list, find the first sibling of the last route appended | |
4840 | */ | |
93c2fb25 DA |
4841 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
4842 | rt = list_first_entry(&rt_last->fib6_siblings, | |
8d1c802b | 4843 | struct fib6_info, |
93c2fb25 | 4844 | fib6_siblings); |
3b1137fe DA |
4845 | } |
4846 | ||
4847 | if (rt) | |
4848 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); | |
4849 | } | |
4850 | ||
333c4301 DA |
4851 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
4852 | struct netlink_ext_ack *extack) | |
51ebd318 | 4853 | { |
8d1c802b | 4854 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
3b1137fe | 4855 | struct nl_info *info = &cfg->fc_nlinfo; |
51ebd318 ND |
4856 | struct fib6_config r_cfg; |
4857 | struct rtnexthop *rtnh; | |
8d1c802b | 4858 | struct fib6_info *rt; |
6b9ea5a6 RP |
4859 | struct rt6_nh *err_nh; |
4860 | struct rt6_nh *nh, *nh_safe; | |
3b1137fe | 4861 | __u16 nlflags; |
51ebd318 ND |
4862 | int remaining; |
4863 | int attrlen; | |
6b9ea5a6 RP |
4864 | int err = 1; |
4865 | int nhn = 0; | |
4866 | int replace = (cfg->fc_nlinfo.nlh && | |
4867 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); | |
4868 | LIST_HEAD(rt6_nh_list); | |
51ebd318 | 4869 | |
3b1137fe DA |
4870 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
4871 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) | |
4872 | nlflags |= NLM_F_APPEND; | |
4873 | ||
35f1b4e9 | 4874 | remaining = cfg->fc_mp_len; |
51ebd318 | 4875 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
51ebd318 | 4876 | |
6b9ea5a6 | 4877 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
8d1c802b | 4878 | * fib6_info structs per nexthop |
6b9ea5a6 | 4879 | */ |
51ebd318 ND |
4880 | while (rtnh_ok(rtnh, remaining)) { |
4881 | memcpy(&r_cfg, cfg, sizeof(*cfg)); | |
4882 | if (rtnh->rtnh_ifindex) | |
4883 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; | |
4884 | ||
4885 | attrlen = rtnh_attrlen(rtnh); | |
4886 | if (attrlen > 0) { | |
4887 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
4888 | ||
4889 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
4890 | if (nla) { | |
67b61f6c | 4891 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
51ebd318 ND |
4892 | r_cfg.fc_flags |= RTF_GATEWAY; |
4893 | } | |
19e42e45 RP |
4894 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
4895 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); | |
4896 | if (nla) | |
4897 | r_cfg.fc_encap_type = nla_get_u16(nla); | |
51ebd318 | 4898 | } |
6b9ea5a6 | 4899 | |
68e2ffde | 4900 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
acb54e3c | 4901 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
8c5b83f0 RP |
4902 | if (IS_ERR(rt)) { |
4903 | err = PTR_ERR(rt); | |
4904 | rt = NULL; | |
6b9ea5a6 | 4905 | goto cleanup; |
8c5b83f0 | 4906 | } |
b5d2d75e DA |
4907 | if (!rt6_qualify_for_ecmp(rt)) { |
4908 | err = -EINVAL; | |
4909 | NL_SET_ERR_MSG(extack, | |
4910 | "Device only routes can not be added for IPv6 using the multipath API."); | |
4911 | fib6_info_release(rt); | |
4912 | goto cleanup; | |
4913 | } | |
6b9ea5a6 | 4914 | |
1cf844c7 | 4915 | rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; |
398958ae | 4916 | |
d4ead6b3 DA |
4917 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
4918 | rt, &r_cfg); | |
51ebd318 | 4919 | if (err) { |
93531c67 | 4920 | fib6_info_release(rt); |
6b9ea5a6 RP |
4921 | goto cleanup; |
4922 | } | |
4923 | ||
4924 | rtnh = rtnh_next(rtnh, &remaining); | |
4925 | } | |
4926 | ||
3b1137fe DA |
4927 | /* for add and replace send one notification with all nexthops. |
4928 | * Skip the notification in fib6_add_rt2node and send one with | |
4929 | * the full route when done | |
4930 | */ | |
4931 | info->skip_notify = 1; | |
4932 | ||
6b9ea5a6 RP |
4933 | err_nh = NULL; |
4934 | list_for_each_entry(nh, &rt6_nh_list, next) { | |
8d1c802b DA |
4935 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
4936 | fib6_info_release(nh->fib6_info); | |
93531c67 | 4937 | |
f7225172 DA |
4938 | if (!err) { |
4939 | /* save reference to last route successfully inserted */ | |
4940 | rt_last = nh->fib6_info; | |
4941 | ||
4942 | /* save reference to first route for notification */ | |
4943 | if (!rt_notif) | |
4944 | rt_notif = nh->fib6_info; | |
4945 | } | |
3b1137fe | 4946 | |
8d1c802b DA |
4947 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
4948 | nh->fib6_info = NULL; | |
6b9ea5a6 RP |
4949 | if (err) { |
4950 | if (replace && nhn) | |
a5a82d84 JK |
4951 | NL_SET_ERR_MSG_MOD(extack, |
4952 | "multipath route replace failed (check consistency of installed routes)"); | |
6b9ea5a6 RP |
4953 | err_nh = nh; |
4954 | goto add_errout; | |
51ebd318 | 4955 | } |
6b9ea5a6 | 4956 | |
1a72418b | 4957 | /* Because each route is added like a single route we remove |
27596472 MK |
4958 | * these flags after the first nexthop: if there is a collision, |
4959 | * we have already failed to add the first nexthop: | |
4960 | * fib6_add_rt2node() has rejected it; when replacing, old | |
4961 | * nexthops have been replaced by first new, the rest should | |
4962 | * be added to it. | |
1a72418b | 4963 | */ |
27596472 MK |
4964 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
4965 | NLM_F_REPLACE); | |
6b9ea5a6 RP |
4966 | nhn++; |
4967 | } | |
4968 | ||
3b1137fe DA |
4969 | /* success ... tell user about new route */ |
4970 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); | |
6b9ea5a6 RP |
4971 | goto cleanup; |
4972 | ||
4973 | add_errout: | |
3b1137fe DA |
4974 | /* send notification for routes that were added so that |
4975 | * the delete notifications sent by ip6_route_del are | |
4976 | * coherent | |
4977 | */ | |
4978 | if (rt_notif) | |
4979 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); | |
4980 | ||
6b9ea5a6 RP |
4981 | /* Delete routes that were already added */ |
4982 | list_for_each_entry(nh, &rt6_nh_list, next) { | |
4983 | if (err_nh == nh) | |
4984 | break; | |
333c4301 | 4985 | ip6_route_del(&nh->r_cfg, extack); |
6b9ea5a6 RP |
4986 | } |
4987 | ||
4988 | cleanup: | |
4989 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { | |
8d1c802b DA |
4990 | if (nh->fib6_info) |
4991 | fib6_info_release(nh->fib6_info); | |
6b9ea5a6 RP |
4992 | list_del(&nh->next); |
4993 | kfree(nh); | |
4994 | } | |
4995 | ||
4996 | return err; | |
4997 | } | |
4998 | ||
333c4301 DA |
4999 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
5000 | struct netlink_ext_ack *extack) | |
6b9ea5a6 RP |
5001 | { |
5002 | struct fib6_config r_cfg; | |
5003 | struct rtnexthop *rtnh; | |
5004 | int remaining; | |
5005 | int attrlen; | |
5006 | int err = 1, last_err = 0; | |
5007 | ||
5008 | remaining = cfg->fc_mp_len; | |
5009 | rtnh = (struct rtnexthop *)cfg->fc_mp; | |
5010 | ||
5011 | /* Parse a Multipath Entry */ | |
5012 | while (rtnh_ok(rtnh, remaining)) { | |
5013 | memcpy(&r_cfg, cfg, sizeof(*cfg)); | |
5014 | if (rtnh->rtnh_ifindex) | |
5015 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; | |
5016 | ||
5017 | attrlen = rtnh_attrlen(rtnh); | |
5018 | if (attrlen > 0) { | |
5019 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
5020 | ||
5021 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
5022 | if (nla) { | |
5023 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); | |
5024 | r_cfg.fc_flags |= RTF_GATEWAY; | |
5025 | } | |
5026 | } | |
333c4301 | 5027 | err = ip6_route_del(&r_cfg, extack); |
6b9ea5a6 RP |
5028 | if (err) |
5029 | last_err = err; | |
5030 | ||
51ebd318 ND |
5031 | rtnh = rtnh_next(rtnh, &remaining); |
5032 | } | |
5033 | ||
5034 | return last_err; | |
5035 | } | |
5036 | ||
c21ef3e3 DA |
5037 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
5038 | struct netlink_ext_ack *extack) | |
1da177e4 | 5039 | { |
86872cb5 TG |
5040 | struct fib6_config cfg; |
5041 | int err; | |
1da177e4 | 5042 | |
333c4301 | 5043 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
86872cb5 TG |
5044 | if (err < 0) |
5045 | return err; | |
5046 | ||
51ebd318 | 5047 | if (cfg.fc_mp) |
333c4301 | 5048 | return ip6_route_multipath_del(&cfg, extack); |
0ae81335 DA |
5049 | else { |
5050 | cfg.fc_delete_all_nh = 1; | |
333c4301 | 5051 | return ip6_route_del(&cfg, extack); |
0ae81335 | 5052 | } |
1da177e4 LT |
5053 | } |
5054 | ||
c21ef3e3 DA |
5055 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
5056 | struct netlink_ext_ack *extack) | |
1da177e4 | 5057 | { |
86872cb5 TG |
5058 | struct fib6_config cfg; |
5059 | int err; | |
1da177e4 | 5060 | |
333c4301 | 5061 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
86872cb5 TG |
5062 | if (err < 0) |
5063 | return err; | |
5064 | ||
67f69513 DA |
5065 | if (cfg.fc_metric == 0) |
5066 | cfg.fc_metric = IP6_RT_PRIO_USER; | |
5067 | ||
51ebd318 | 5068 | if (cfg.fc_mp) |
333c4301 | 5069 | return ip6_route_multipath_add(&cfg, extack); |
51ebd318 | 5070 | else |
acb54e3c | 5071 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
1da177e4 LT |
5072 | } |
5073 | ||
a1b7a1f0 DA |
5074 | /* add the overhead of this fib6_nh to nexthop_len */ |
5075 | static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) | |
339bf98f | 5076 | { |
a1b7a1f0 | 5077 | int *nexthop_len = arg; |
beb1afac | 5078 | |
a1b7a1f0 DA |
5079 | *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ |
5080 | + NLA_ALIGN(sizeof(struct rtnexthop)) | |
5081 | + nla_total_size(16); /* RTA_GATEWAY */ | |
5082 | ||
5083 | if (nh->fib_nh_lws) { | |
5084 | /* RTA_ENCAP_TYPE */ | |
5085 | *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); | |
5086 | /* RTA_ENCAP */ | |
5087 | *nexthop_len += nla_total_size(2); | |
5088 | } | |
f88d8ea6 | 5089 | |
a1b7a1f0 DA |
5090 | return 0; |
5091 | } | |
beb1afac | 5092 | |
a1b7a1f0 DA |
5093 | static size_t rt6_nlmsg_size(struct fib6_info *f6i) |
5094 | { | |
5095 | int nexthop_len; | |
5096 | ||
5097 | if (f6i->nh) { | |
5098 | nexthop_len = nla_total_size(4); /* RTA_NH_ID */ | |
5099 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, | |
5100 | &nexthop_len); | |
5101 | } else { | |
5102 | struct fib6_nh *nh = f6i->fib6_nh; | |
5103 | ||
5104 | nexthop_len = 0; | |
5105 | if (f6i->fib6_nsiblings) { | |
5106 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ | |
5107 | + NLA_ALIGN(sizeof(struct rtnexthop)) | |
5108 | + nla_total_size(16) /* RTA_GATEWAY */ | |
5109 | + lwtunnel_get_encap_size(nh->fib_nh_lws); | |
5110 | ||
5111 | nexthop_len *= f6i->fib6_nsiblings; | |
5112 | } | |
5113 | nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); | |
beb1afac DA |
5114 | } |
5115 | ||
339bf98f TG |
5116 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
5117 | + nla_total_size(16) /* RTA_SRC */ | |
5118 | + nla_total_size(16) /* RTA_DST */ | |
5119 | + nla_total_size(16) /* RTA_GATEWAY */ | |
5120 | + nla_total_size(16) /* RTA_PREFSRC */ | |
5121 | + nla_total_size(4) /* RTA_TABLE */ | |
5122 | + nla_total_size(4) /* RTA_IIF */ | |
5123 | + nla_total_size(4) /* RTA_OIF */ | |
5124 | + nla_total_size(4) /* RTA_PRIORITY */ | |
6a2b9ce0 | 5125 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
ea697639 | 5126 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
c78ba6d6 | 5127 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
19e42e45 | 5128 | + nla_total_size(1) /* RTA_PREF */ |
beb1afac DA |
5129 | + nexthop_len; |
5130 | } | |
5131 | ||
f88d8ea6 DA |
5132 | static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, |
5133 | unsigned char *flags) | |
5134 | { | |
5135 | if (nexthop_is_multipath(nh)) { | |
5136 | struct nlattr *mp; | |
5137 | ||
5138 | mp = nla_nest_start(skb, RTA_MULTIPATH); | |
5139 | if (!mp) | |
5140 | goto nla_put_failure; | |
5141 | ||
5142 | if (nexthop_mpath_fill_node(skb, nh)) | |
5143 | goto nla_put_failure; | |
5144 | ||
5145 | nla_nest_end(skb, mp); | |
5146 | } else { | |
5147 | struct fib6_nh *fib6_nh; | |
5148 | ||
5149 | fib6_nh = nexthop_fib6_nh(nh); | |
5150 | if (fib_nexthop_info(skb, &fib6_nh->nh_common, | |
5151 | flags, false) < 0) | |
5152 | goto nla_put_failure; | |
5153 | } | |
5154 | ||
5155 | return 0; | |
5156 | ||
5157 | nla_put_failure: | |
5158 | return -EMSGSIZE; | |
5159 | } | |
5160 | ||
d4ead6b3 | 5161 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
8d1c802b | 5162 | struct fib6_info *rt, struct dst_entry *dst, |
d4ead6b3 | 5163 | struct in6_addr *dest, struct in6_addr *src, |
15e47304 | 5164 | int iif, int type, u32 portid, u32 seq, |
f8cfe2ce | 5165 | unsigned int flags) |
1da177e4 | 5166 | { |
22d0bd82 XL |
5167 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
5168 | struct rt6key *rt6_dst, *rt6_src; | |
5169 | u32 *pmetrics, table, rt6_flags; | |
f88d8ea6 | 5170 | unsigned char nh_flags = 0; |
2d7202bf | 5171 | struct nlmsghdr *nlh; |
22d0bd82 | 5172 | struct rtmsg *rtm; |
d4ead6b3 | 5173 | long expires = 0; |
1da177e4 | 5174 | |
15e47304 | 5175 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
38308473 | 5176 | if (!nlh) |
26932566 | 5177 | return -EMSGSIZE; |
2d7202bf | 5178 | |
22d0bd82 XL |
5179 | if (rt6) { |
5180 | rt6_dst = &rt6->rt6i_dst; | |
5181 | rt6_src = &rt6->rt6i_src; | |
5182 | rt6_flags = rt6->rt6i_flags; | |
5183 | } else { | |
5184 | rt6_dst = &rt->fib6_dst; | |
5185 | rt6_src = &rt->fib6_src; | |
5186 | rt6_flags = rt->fib6_flags; | |
5187 | } | |
5188 | ||
2d7202bf | 5189 | rtm = nlmsg_data(nlh); |
1da177e4 | 5190 | rtm->rtm_family = AF_INET6; |
22d0bd82 XL |
5191 | rtm->rtm_dst_len = rt6_dst->plen; |
5192 | rtm->rtm_src_len = rt6_src->plen; | |
1da177e4 | 5193 | rtm->rtm_tos = 0; |
93c2fb25 DA |
5194 | if (rt->fib6_table) |
5195 | table = rt->fib6_table->tb6_id; | |
c71099ac | 5196 | else |
9e762a4a | 5197 | table = RT6_TABLE_UNSPEC; |
97f0082a | 5198 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
c78679e8 DM |
5199 | if (nla_put_u32(skb, RTA_TABLE, table)) |
5200 | goto nla_put_failure; | |
e8478e80 DA |
5201 | |
5202 | rtm->rtm_type = rt->fib6_type; | |
1da177e4 LT |
5203 | rtm->rtm_flags = 0; |
5204 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
93c2fb25 | 5205 | rtm->rtm_protocol = rt->fib6_protocol; |
1da177e4 | 5206 | |
22d0bd82 | 5207 | if (rt6_flags & RTF_CACHE) |
1da177e4 LT |
5208 | rtm->rtm_flags |= RTM_F_CLONED; |
5209 | ||
d4ead6b3 DA |
5210 | if (dest) { |
5211 | if (nla_put_in6_addr(skb, RTA_DST, dest)) | |
c78679e8 | 5212 | goto nla_put_failure; |
1ab1457c | 5213 | rtm->rtm_dst_len = 128; |
1da177e4 | 5214 | } else if (rtm->rtm_dst_len) |
22d0bd82 | 5215 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
c78679e8 | 5216 | goto nla_put_failure; |
1da177e4 LT |
5217 | #ifdef CONFIG_IPV6_SUBTREES |
5218 | if (src) { | |
930345ea | 5219 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
c78679e8 | 5220 | goto nla_put_failure; |
1ab1457c | 5221 | rtm->rtm_src_len = 128; |
c78679e8 | 5222 | } else if (rtm->rtm_src_len && |
22d0bd82 | 5223 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
c78679e8 | 5224 | goto nla_put_failure; |
1da177e4 | 5225 | #endif |
7bc570c8 YH |
5226 | if (iif) { |
5227 | #ifdef CONFIG_IPV6_MROUTE | |
22d0bd82 | 5228 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
fd61c6ba DA |
5229 | int err = ip6mr_get_route(net, skb, rtm, portid); |
5230 | ||
5231 | if (err == 0) | |
5232 | return 0; | |
5233 | if (err < 0) | |
5234 | goto nla_put_failure; | |
7bc570c8 YH |
5235 | } else |
5236 | #endif | |
c78679e8 DM |
5237 | if (nla_put_u32(skb, RTA_IIF, iif)) |
5238 | goto nla_put_failure; | |
d4ead6b3 | 5239 | } else if (dest) { |
1da177e4 | 5240 | struct in6_addr saddr_buf; |
d4ead6b3 | 5241 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
930345ea | 5242 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
c78679e8 | 5243 | goto nla_put_failure; |
1da177e4 | 5244 | } |
2d7202bf | 5245 | |
93c2fb25 | 5246 | if (rt->fib6_prefsrc.plen) { |
c3968a85 | 5247 | struct in6_addr saddr_buf; |
93c2fb25 | 5248 | saddr_buf = rt->fib6_prefsrc.addr; |
930345ea | 5249 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
c78679e8 | 5250 | goto nla_put_failure; |
c3968a85 DW |
5251 | } |
5252 | ||
d4ead6b3 DA |
5253 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
5254 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) | |
2d7202bf TG |
5255 | goto nla_put_failure; |
5256 | ||
93c2fb25 | 5257 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
c78679e8 | 5258 | goto nla_put_failure; |
8253947e | 5259 | |
beb1afac DA |
5260 | /* For multipath routes, walk the siblings list and add |
5261 | * each as a nexthop within RTA_MULTIPATH. | |
5262 | */ | |
22d0bd82 XL |
5263 | if (rt6) { |
5264 | if (rt6_flags & RTF_GATEWAY && | |
5265 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) | |
5266 | goto nla_put_failure; | |
5267 | ||
5268 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) | |
5269 | goto nla_put_failure; | |
5270 | } else if (rt->fib6_nsiblings) { | |
8d1c802b | 5271 | struct fib6_info *sibling, *next_sibling; |
beb1afac DA |
5272 | struct nlattr *mp; |
5273 | ||
ae0be8de | 5274 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
beb1afac DA |
5275 | if (!mp) |
5276 | goto nla_put_failure; | |
5277 | ||
1cf844c7 DA |
5278 | if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, |
5279 | rt->fib6_nh->fib_nh_weight) < 0) | |
beb1afac DA |
5280 | goto nla_put_failure; |
5281 | ||
5282 | list_for_each_entry_safe(sibling, next_sibling, | |
93c2fb25 | 5283 | &rt->fib6_siblings, fib6_siblings) { |
1cf844c7 DA |
5284 | if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, |
5285 | sibling->fib6_nh->fib_nh_weight) < 0) | |
beb1afac DA |
5286 | goto nla_put_failure; |
5287 | } | |
5288 | ||
5289 | nla_nest_end(skb, mp); | |
f88d8ea6 DA |
5290 | } else if (rt->nh) { |
5291 | if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) | |
5292 | goto nla_put_failure; | |
ecc5663c | 5293 | |
f88d8ea6 DA |
5294 | if (nexthop_is_blackhole(rt->nh)) |
5295 | rtm->rtm_type = RTN_BLACKHOLE; | |
5296 | ||
5297 | if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) | |
5298 | goto nla_put_failure; | |
5299 | ||
5300 | rtm->rtm_flags |= nh_flags; | |
5301 | } else { | |
1cf844c7 | 5302 | if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, |
ecc5663c | 5303 | &nh_flags, false) < 0) |
beb1afac | 5304 | goto nla_put_failure; |
ecc5663c DA |
5305 | |
5306 | rtm->rtm_flags |= nh_flags; | |
beb1afac DA |
5307 | } |
5308 | ||
22d0bd82 | 5309 | if (rt6_flags & RTF_EXPIRES) { |
14895687 DA |
5310 | expires = dst ? dst->expires : rt->expires; |
5311 | expires -= jiffies; | |
5312 | } | |
69cdf8f9 | 5313 | |
d4ead6b3 | 5314 | if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) |
e3703b3d | 5315 | goto nla_put_failure; |
2d7202bf | 5316 | |
22d0bd82 | 5317 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
c78ba6d6 LR |
5318 | goto nla_put_failure; |
5319 | ||
19e42e45 | 5320 | |
053c095a JB |
5321 | nlmsg_end(skb, nlh); |
5322 | return 0; | |
2d7202bf TG |
5323 | |
5324 | nla_put_failure: | |
26932566 PM |
5325 | nlmsg_cancel(skb, nlh); |
5326 | return -EMSGSIZE; | |
1da177e4 LT |
5327 | } |
5328 | ||
2c170e07 DA |
5329 | static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) |
5330 | { | |
5331 | const struct net_device *dev = arg; | |
5332 | ||
5333 | if (nh->fib_nh_dev == dev) | |
5334 | return 1; | |
5335 | ||
5336 | return 0; | |
5337 | } | |
5338 | ||
13e38901 DA |
5339 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
5340 | const struct net_device *dev) | |
5341 | { | |
2c170e07 DA |
5342 | if (f6i->nh) { |
5343 | struct net_device *_dev = (struct net_device *)dev; | |
5344 | ||
5345 | return !!nexthop_for_each_fib6_nh(f6i->nh, | |
5346 | fib6_info_nh_uses_dev, | |
5347 | _dev); | |
5348 | } | |
5349 | ||
1cf844c7 | 5350 | if (f6i->fib6_nh->fib_nh_dev == dev) |
13e38901 DA |
5351 | return true; |
5352 | ||
5353 | if (f6i->fib6_nsiblings) { | |
5354 | struct fib6_info *sibling, *next_sibling; | |
5355 | ||
5356 | list_for_each_entry_safe(sibling, next_sibling, | |
5357 | &f6i->fib6_siblings, fib6_siblings) { | |
1cf844c7 | 5358 | if (sibling->fib6_nh->fib_nh_dev == dev) |
13e38901 DA |
5359 | return true; |
5360 | } | |
5361 | } | |
5362 | ||
5363 | return false; | |
5364 | } | |
5365 | ||
8d1c802b | 5366 | int rt6_dump_route(struct fib6_info *rt, void *p_arg) |
1da177e4 LT |
5367 | { |
5368 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; | |
13e38901 DA |
5369 | struct fib_dump_filter *filter = &arg->filter; |
5370 | unsigned int flags = NLM_F_MULTI; | |
1f17e2f2 DA |
5371 | struct net *net = arg->net; |
5372 | ||
421842ed | 5373 | if (rt == net->ipv6.fib6_null_entry) |
1f17e2f2 | 5374 | return 0; |
1da177e4 | 5375 | |
13e38901 DA |
5376 | if ((filter->flags & RTM_F_PREFIX) && |
5377 | !(rt->fib6_flags & RTF_PREFIX_RT)) { | |
5378 | /* success since this is not a prefix route */ | |
5379 | return 1; | |
5380 | } | |
5381 | if (filter->filter_set) { | |
5382 | if ((filter->rt_type && rt->fib6_type != filter->rt_type) || | |
5383 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || | |
5384 | (filter->protocol && rt->fib6_protocol != filter->protocol)) { | |
f8cfe2ce DA |
5385 | return 1; |
5386 | } | |
13e38901 | 5387 | flags |= NLM_F_DUMP_FILTERED; |
f8cfe2ce | 5388 | } |
1da177e4 | 5389 | |
d4ead6b3 DA |
5390 | return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, |
5391 | RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, | |
13e38901 | 5392 | arg->cb->nlh->nlmsg_seq, flags); |
1da177e4 LT |
5393 | } |
5394 | ||
0eff0a27 JK |
5395 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
5396 | const struct nlmsghdr *nlh, | |
5397 | struct nlattr **tb, | |
5398 | struct netlink_ext_ack *extack) | |
5399 | { | |
5400 | struct rtmsg *rtm; | |
5401 | int i, err; | |
5402 | ||
5403 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { | |
5404 | NL_SET_ERR_MSG_MOD(extack, | |
5405 | "Invalid header for get route request"); | |
5406 | return -EINVAL; | |
5407 | } | |
5408 | ||
5409 | if (!netlink_strict_get_check(skb)) | |
8cb08174 JB |
5410 | return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
5411 | rtm_ipv6_policy, extack); | |
0eff0a27 JK |
5412 | |
5413 | rtm = nlmsg_data(nlh); | |
5414 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || | |
5415 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || | |
5416 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || | |
5417 | rtm->rtm_type) { | |
5418 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); | |
5419 | return -EINVAL; | |
5420 | } | |
5421 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { | |
5422 | NL_SET_ERR_MSG_MOD(extack, | |
5423 | "Invalid flags for get route request"); | |
5424 | return -EINVAL; | |
5425 | } | |
5426 | ||
8cb08174 JB |
5427 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
5428 | rtm_ipv6_policy, extack); | |
0eff0a27 JK |
5429 | if (err) |
5430 | return err; | |
5431 | ||
5432 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || | |
5433 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { | |
5434 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); | |
5435 | return -EINVAL; | |
5436 | } | |
5437 | ||
5438 | for (i = 0; i <= RTA_MAX; i++) { | |
5439 | if (!tb[i]) | |
5440 | continue; | |
5441 | ||
5442 | switch (i) { | |
5443 | case RTA_SRC: | |
5444 | case RTA_DST: | |
5445 | case RTA_IIF: | |
5446 | case RTA_OIF: | |
5447 | case RTA_MARK: | |
5448 | case RTA_UID: | |
5449 | case RTA_SPORT: | |
5450 | case RTA_DPORT: | |
5451 | case RTA_IP_PROTO: | |
5452 | break; | |
5453 | default: | |
5454 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); | |
5455 | return -EINVAL; | |
5456 | } | |
5457 | } | |
5458 | ||
5459 | return 0; | |
5460 | } | |
5461 | ||
c21ef3e3 DA |
5462 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
5463 | struct netlink_ext_ack *extack) | |
1da177e4 | 5464 | { |
3b1e0a65 | 5465 | struct net *net = sock_net(in_skb->sk); |
ab364a6f | 5466 | struct nlattr *tb[RTA_MAX+1]; |
18c3a61c | 5467 | int err, iif = 0, oif = 0; |
a68886a6 | 5468 | struct fib6_info *from; |
18c3a61c | 5469 | struct dst_entry *dst; |
ab364a6f | 5470 | struct rt6_info *rt; |
1da177e4 | 5471 | struct sk_buff *skb; |
ab364a6f | 5472 | struct rtmsg *rtm; |
744486d4 | 5473 | struct flowi6 fl6 = {}; |
18c3a61c | 5474 | bool fibmatch; |
1da177e4 | 5475 | |
0eff0a27 | 5476 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
ab364a6f TG |
5477 | if (err < 0) |
5478 | goto errout; | |
1da177e4 | 5479 | |
ab364a6f | 5480 | err = -EINVAL; |
38b7097b HFS |
5481 | rtm = nlmsg_data(nlh); |
5482 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); | |
18c3a61c | 5483 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
1da177e4 | 5484 | |
ab364a6f TG |
5485 | if (tb[RTA_SRC]) { |
5486 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) | |
5487 | goto errout; | |
5488 | ||
4e3fd7a0 | 5489 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
ab364a6f TG |
5490 | } |
5491 | ||
5492 | if (tb[RTA_DST]) { | |
5493 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) | |
5494 | goto errout; | |
5495 | ||
4e3fd7a0 | 5496 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
ab364a6f TG |
5497 | } |
5498 | ||
5499 | if (tb[RTA_IIF]) | |
5500 | iif = nla_get_u32(tb[RTA_IIF]); | |
5501 | ||
5502 | if (tb[RTA_OIF]) | |
72331bc0 | 5503 | oif = nla_get_u32(tb[RTA_OIF]); |
1da177e4 | 5504 | |
2e47b291 LC |
5505 | if (tb[RTA_MARK]) |
5506 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); | |
5507 | ||
622ec2c9 LC |
5508 | if (tb[RTA_UID]) |
5509 | fl6.flowi6_uid = make_kuid(current_user_ns(), | |
5510 | nla_get_u32(tb[RTA_UID])); | |
5511 | else | |
5512 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); | |
5513 | ||
eacb9384 RP |
5514 | if (tb[RTA_SPORT]) |
5515 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); | |
5516 | ||
5517 | if (tb[RTA_DPORT]) | |
5518 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); | |
5519 | ||
5520 | if (tb[RTA_IP_PROTO]) { | |
5521 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], | |
5e1a99ea HL |
5522 | &fl6.flowi6_proto, AF_INET6, |
5523 | extack); | |
eacb9384 RP |
5524 | if (err) |
5525 | goto errout; | |
5526 | } | |
5527 | ||
1da177e4 LT |
5528 | if (iif) { |
5529 | struct net_device *dev; | |
72331bc0 SL |
5530 | int flags = 0; |
5531 | ||
121622db FW |
5532 | rcu_read_lock(); |
5533 | ||
5534 | dev = dev_get_by_index_rcu(net, iif); | |
1da177e4 | 5535 | if (!dev) { |
121622db | 5536 | rcu_read_unlock(); |
1da177e4 | 5537 | err = -ENODEV; |
ab364a6f | 5538 | goto errout; |
1da177e4 | 5539 | } |
72331bc0 SL |
5540 | |
5541 | fl6.flowi6_iif = iif; | |
5542 | ||
5543 | if (!ipv6_addr_any(&fl6.saddr)) | |
5544 | flags |= RT6_LOOKUP_F_HAS_SADDR; | |
5545 | ||
b75cc8f9 | 5546 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
121622db FW |
5547 | |
5548 | rcu_read_unlock(); | |
72331bc0 SL |
5549 | } else { |
5550 | fl6.flowi6_oif = oif; | |
5551 | ||
58acfd71 | 5552 | dst = ip6_route_output(net, NULL, &fl6); |
18c3a61c RP |
5553 | } |
5554 | ||
18c3a61c RP |
5555 | |
5556 | rt = container_of(dst, struct rt6_info, dst); | |
5557 | if (rt->dst.error) { | |
5558 | err = rt->dst.error; | |
5559 | ip6_rt_put(rt); | |
5560 | goto errout; | |
1da177e4 LT |
5561 | } |
5562 | ||
9d6acb3b WC |
5563 | if (rt == net->ipv6.ip6_null_entry) { |
5564 | err = rt->dst.error; | |
5565 | ip6_rt_put(rt); | |
5566 | goto errout; | |
5567 | } | |
5568 | ||
ab364a6f | 5569 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
38308473 | 5570 | if (!skb) { |
94e187c0 | 5571 | ip6_rt_put(rt); |
ab364a6f TG |
5572 | err = -ENOBUFS; |
5573 | goto errout; | |
5574 | } | |
1da177e4 | 5575 | |
d8d1f30b | 5576 | skb_dst_set(skb, &rt->dst); |
a68886a6 DA |
5577 | |
5578 | rcu_read_lock(); | |
5579 | from = rcu_dereference(rt->from); | |
886b7a50 MKL |
5580 | if (from) { |
5581 | if (fibmatch) | |
5582 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, | |
5583 | iif, RTM_NEWROUTE, | |
5584 | NETLINK_CB(in_skb).portid, | |
5585 | nlh->nlmsg_seq, 0); | |
5586 | else | |
5587 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, | |
5588 | &fl6.saddr, iif, RTM_NEWROUTE, | |
5589 | NETLINK_CB(in_skb).portid, | |
5590 | nlh->nlmsg_seq, 0); | |
5591 | } else { | |
5592 | err = -ENETUNREACH; | |
5593 | } | |
a68886a6 DA |
5594 | rcu_read_unlock(); |
5595 | ||
1da177e4 | 5596 | if (err < 0) { |
ab364a6f TG |
5597 | kfree_skb(skb); |
5598 | goto errout; | |
1da177e4 LT |
5599 | } |
5600 | ||
15e47304 | 5601 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
ab364a6f | 5602 | errout: |
1da177e4 | 5603 | return err; |
1da177e4 LT |
5604 | } |
5605 | ||
8d1c802b | 5606 | void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, |
37a1d361 | 5607 | unsigned int nlm_flags) |
1da177e4 LT |
5608 | { |
5609 | struct sk_buff *skb; | |
5578689a | 5610 | struct net *net = info->nl_net; |
528c4ceb DL |
5611 | u32 seq; |
5612 | int err; | |
5613 | ||
5614 | err = -ENOBUFS; | |
38308473 | 5615 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
86872cb5 | 5616 | |
19e42e45 | 5617 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
38308473 | 5618 | if (!skb) |
21713ebc TG |
5619 | goto errout; |
5620 | ||
d4ead6b3 DA |
5621 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
5622 | event, info->portid, seq, nlm_flags); | |
26932566 PM |
5623 | if (err < 0) { |
5624 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | |
5625 | WARN_ON(err == -EMSGSIZE); | |
5626 | kfree_skb(skb); | |
5627 | goto errout; | |
5628 | } | |
15e47304 | 5629 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
1ce85fe4 PNA |
5630 | info->nlh, gfp_any()); |
5631 | return; | |
21713ebc TG |
5632 | errout: |
5633 | if (err < 0) | |
5578689a | 5634 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
1da177e4 LT |
5635 | } |
5636 | ||
19a3b7ee DA |
5637 | void fib6_rt_update(struct net *net, struct fib6_info *rt, |
5638 | struct nl_info *info) | |
5639 | { | |
5640 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; | |
5641 | struct sk_buff *skb; | |
5642 | int err = -ENOBUFS; | |
5643 | ||
5644 | /* call_fib6_entry_notifiers will be removed when in-kernel notifier | |
5645 | * is implemented and supported for nexthop objects | |
5646 | */ | |
5647 | call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); | |
5648 | ||
5649 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); | |
5650 | if (!skb) | |
5651 | goto errout; | |
5652 | ||
5653 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, | |
5654 | RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); | |
5655 | if (err < 0) { | |
5656 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | |
5657 | WARN_ON(err == -EMSGSIZE); | |
5658 | kfree_skb(skb); | |
5659 | goto errout; | |
5660 | } | |
5661 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, | |
5662 | info->nlh, gfp_any()); | |
5663 | return; | |
5664 | errout: | |
5665 | if (err < 0) | |
5666 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); | |
5667 | } | |
5668 | ||
8ed67789 | 5669 | static int ip6_route_dev_notify(struct notifier_block *this, |
351638e7 | 5670 | unsigned long event, void *ptr) |
8ed67789 | 5671 | { |
351638e7 | 5672 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
c346dca1 | 5673 | struct net *net = dev_net(dev); |
8ed67789 | 5674 | |
242d3a49 WC |
5675 | if (!(dev->flags & IFF_LOOPBACK)) |
5676 | return NOTIFY_OK; | |
5677 | ||
5678 | if (event == NETDEV_REGISTER) { | |
1cf844c7 | 5679 | net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; |
d8d1f30b | 5680 | net->ipv6.ip6_null_entry->dst.dev = dev; |
8ed67789 DL |
5681 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
5682 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
d8d1f30b | 5683 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
8ed67789 | 5684 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
d8d1f30b | 5685 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
8ed67789 | 5686 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
242d3a49 | 5687 | #endif |
76da0704 WC |
5688 | } else if (event == NETDEV_UNREGISTER && |
5689 | dev->reg_state != NETREG_UNREGISTERED) { | |
5690 | /* NETDEV_UNREGISTER could be fired for multiple times by | |
5691 | * netdev_wait_allrefs(). Make sure we only call this once. | |
5692 | */ | |
12d94a80 | 5693 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
242d3a49 | 5694 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
12d94a80 ED |
5695 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
5696 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); | |
8ed67789 DL |
5697 | #endif |
5698 | } | |
5699 | ||
5700 | return NOTIFY_OK; | |
5701 | } | |
5702 | ||
1da177e4 LT |
5703 | /* |
5704 | * /proc | |
5705 | */ | |
5706 | ||
5707 | #ifdef CONFIG_PROC_FS | |
1da177e4 LT |
5708 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
5709 | { | |
69ddb805 | 5710 | struct net *net = (struct net *)seq->private; |
1da177e4 | 5711 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
69ddb805 DL |
5712 | net->ipv6.rt6_stats->fib_nodes, |
5713 | net->ipv6.rt6_stats->fib_route_nodes, | |
81eb8447 | 5714 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
69ddb805 DL |
5715 | net->ipv6.rt6_stats->fib_rt_entries, |
5716 | net->ipv6.rt6_stats->fib_rt_cache, | |
fc66f95c | 5717 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
69ddb805 | 5718 | net->ipv6.rt6_stats->fib_discarded_routes); |
1da177e4 LT |
5719 | |
5720 | return 0; | |
5721 | } | |
1da177e4 LT |
5722 | #endif /* CONFIG_PROC_FS */ |
5723 | ||
5724 | #ifdef CONFIG_SYSCTL | |
5725 | ||
1da177e4 | 5726 | static |
fe2c6338 | 5727 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
1da177e4 LT |
5728 | void __user *buffer, size_t *lenp, loff_t *ppos) |
5729 | { | |
c486da34 LAG |
5730 | struct net *net; |
5731 | int delay; | |
f0fb9b28 | 5732 | int ret; |
c486da34 | 5733 | if (!write) |
1da177e4 | 5734 | return -EINVAL; |
c486da34 LAG |
5735 | |
5736 | net = (struct net *)ctl->extra1; | |
5737 | delay = net->ipv6.sysctl.flush_delay; | |
f0fb9b28 AP |
5738 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
5739 | if (ret) | |
5740 | return ret; | |
5741 | ||
2ac3ac8f | 5742 | fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); |
c486da34 | 5743 | return 0; |
1da177e4 LT |
5744 | } |
5745 | ||
7c6bb7d2 DA |
5746 | static int zero; |
5747 | static int one = 1; | |
5748 | ||
ed792e28 | 5749 | static struct ctl_table ipv6_route_table_template[] = { |
1ab1457c | 5750 | { |
1da177e4 | 5751 | .procname = "flush", |
4990509f | 5752 | .data = &init_net.ipv6.sysctl.flush_delay, |
1da177e4 | 5753 | .maxlen = sizeof(int), |
89c8b3a1 | 5754 | .mode = 0200, |
6d9f239a | 5755 | .proc_handler = ipv6_sysctl_rtcache_flush |
1da177e4 LT |
5756 | }, |
5757 | { | |
1da177e4 | 5758 | .procname = "gc_thresh", |
9a7ec3a9 | 5759 | .data = &ip6_dst_ops_template.gc_thresh, |
1da177e4 LT |
5760 | .maxlen = sizeof(int), |
5761 | .mode = 0644, | |
6d9f239a | 5762 | .proc_handler = proc_dointvec, |
1da177e4 LT |
5763 | }, |
5764 | { | |
1da177e4 | 5765 | .procname = "max_size", |
4990509f | 5766 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
1da177e4 LT |
5767 | .maxlen = sizeof(int), |
5768 | .mode = 0644, | |
6d9f239a | 5769 | .proc_handler = proc_dointvec, |
1da177e4 LT |
5770 | }, |
5771 | { | |
1da177e4 | 5772 | .procname = "gc_min_interval", |
4990509f | 5773 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
5774 | .maxlen = sizeof(int), |
5775 | .mode = 0644, | |
6d9f239a | 5776 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
5777 | }, |
5778 | { | |
1da177e4 | 5779 | .procname = "gc_timeout", |
4990509f | 5780 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
1da177e4 LT |
5781 | .maxlen = sizeof(int), |
5782 | .mode = 0644, | |
6d9f239a | 5783 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
5784 | }, |
5785 | { | |
1da177e4 | 5786 | .procname = "gc_interval", |
4990509f | 5787 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
1da177e4 LT |
5788 | .maxlen = sizeof(int), |
5789 | .mode = 0644, | |
6d9f239a | 5790 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
5791 | }, |
5792 | { | |
1da177e4 | 5793 | .procname = "gc_elasticity", |
4990509f | 5794 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
1da177e4 LT |
5795 | .maxlen = sizeof(int), |
5796 | .mode = 0644, | |
f3d3f616 | 5797 | .proc_handler = proc_dointvec, |
1da177e4 LT |
5798 | }, |
5799 | { | |
1da177e4 | 5800 | .procname = "mtu_expires", |
4990509f | 5801 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
1da177e4 LT |
5802 | .maxlen = sizeof(int), |
5803 | .mode = 0644, | |
6d9f239a | 5804 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
5805 | }, |
5806 | { | |
1da177e4 | 5807 | .procname = "min_adv_mss", |
4990509f | 5808 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
1da177e4 LT |
5809 | .maxlen = sizeof(int), |
5810 | .mode = 0644, | |
f3d3f616 | 5811 | .proc_handler = proc_dointvec, |
1da177e4 LT |
5812 | }, |
5813 | { | |
1da177e4 | 5814 | .procname = "gc_min_interval_ms", |
4990509f | 5815 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
5816 | .maxlen = sizeof(int), |
5817 | .mode = 0644, | |
6d9f239a | 5818 | .proc_handler = proc_dointvec_ms_jiffies, |
1da177e4 | 5819 | }, |
7c6bb7d2 DA |
5820 | { |
5821 | .procname = "skip_notify_on_dev_down", | |
5822 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, | |
5823 | .maxlen = sizeof(int), | |
5824 | .mode = 0644, | |
5825 | .proc_handler = proc_dointvec, | |
5826 | .extra1 = &zero, | |
5827 | .extra2 = &one, | |
5828 | }, | |
f8572d8f | 5829 | { } |
1da177e4 LT |
5830 | }; |
5831 | ||
2c8c1e72 | 5832 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
760f2d01 DL |
5833 | { |
5834 | struct ctl_table *table; | |
5835 | ||
5836 | table = kmemdup(ipv6_route_table_template, | |
5837 | sizeof(ipv6_route_table_template), | |
5838 | GFP_KERNEL); | |
5ee09105 YH |
5839 | |
5840 | if (table) { | |
5841 | table[0].data = &net->ipv6.sysctl.flush_delay; | |
c486da34 | 5842 | table[0].extra1 = net; |
86393e52 | 5843 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
5ee09105 YH |
5844 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
5845 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; | |
5846 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; | |
5847 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; | |
5848 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
5849 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; | |
5850 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; | |
9c69fabe | 5851 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
7c6bb7d2 | 5852 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
464dc801 EB |
5853 | |
5854 | /* Don't export sysctls to unprivileged users */ | |
5855 | if (net->user_ns != &init_user_ns) | |
5856 | table[0].procname = NULL; | |
5ee09105 YH |
5857 | } |
5858 | ||
760f2d01 DL |
5859 | return table; |
5860 | } | |
1da177e4 LT |
5861 | #endif |
5862 | ||
2c8c1e72 | 5863 | static int __net_init ip6_route_net_init(struct net *net) |
cdb18761 | 5864 | { |
633d424b | 5865 | int ret = -ENOMEM; |
8ed67789 | 5866 | |
86393e52 AD |
5867 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
5868 | sizeof(net->ipv6.ip6_dst_ops)); | |
f2fc6a54 | 5869 | |
fc66f95c ED |
5870 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
5871 | goto out_ip6_dst_ops; | |
5872 | ||
1cf844c7 | 5873 | net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); |
421842ed DA |
5874 | if (!net->ipv6.fib6_null_entry) |
5875 | goto out_ip6_dst_entries; | |
1cf844c7 DA |
5876 | memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, |
5877 | sizeof(*net->ipv6.fib6_null_entry)); | |
421842ed | 5878 | |
8ed67789 DL |
5879 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
5880 | sizeof(*net->ipv6.ip6_null_entry), | |
5881 | GFP_KERNEL); | |
5882 | if (!net->ipv6.ip6_null_entry) | |
421842ed | 5883 | goto out_fib6_null_entry; |
d8d1f30b | 5884 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
5885 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
5886 | ip6_template_metrics, true); | |
8ed67789 DL |
5887 | |
5888 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
feca7d8c | 5889 | net->ipv6.fib6_has_custom_rules = false; |
8ed67789 DL |
5890 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
5891 | sizeof(*net->ipv6.ip6_prohibit_entry), | |
5892 | GFP_KERNEL); | |
68fffc67 PZ |
5893 | if (!net->ipv6.ip6_prohibit_entry) |
5894 | goto out_ip6_null_entry; | |
d8d1f30b | 5895 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
5896 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
5897 | ip6_template_metrics, true); | |
8ed67789 DL |
5898 | |
5899 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, | |
5900 | sizeof(*net->ipv6.ip6_blk_hole_entry), | |
5901 | GFP_KERNEL); | |
68fffc67 PZ |
5902 | if (!net->ipv6.ip6_blk_hole_entry) |
5903 | goto out_ip6_prohibit_entry; | |
d8d1f30b | 5904 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
5905 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
5906 | ip6_template_metrics, true); | |
8ed67789 DL |
5907 | #endif |
5908 | ||
b339a47c PZ |
5909 | net->ipv6.sysctl.flush_delay = 0; |
5910 | net->ipv6.sysctl.ip6_rt_max_size = 4096; | |
5911 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; | |
5912 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; | |
5913 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; | |
5914 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; | |
5915 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; | |
5916 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; | |
7c6bb7d2 | 5917 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
b339a47c | 5918 | |
6891a346 BT |
5919 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
5920 | ||
8ed67789 DL |
5921 | ret = 0; |
5922 | out: | |
5923 | return ret; | |
f2fc6a54 | 5924 | |
68fffc67 PZ |
5925 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
5926 | out_ip6_prohibit_entry: | |
5927 | kfree(net->ipv6.ip6_prohibit_entry); | |
5928 | out_ip6_null_entry: | |
5929 | kfree(net->ipv6.ip6_null_entry); | |
5930 | #endif | |
421842ed DA |
5931 | out_fib6_null_entry: |
5932 | kfree(net->ipv6.fib6_null_entry); | |
fc66f95c ED |
5933 | out_ip6_dst_entries: |
5934 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); | |
f2fc6a54 | 5935 | out_ip6_dst_ops: |
f2fc6a54 | 5936 | goto out; |
cdb18761 DL |
5937 | } |
5938 | ||
2c8c1e72 | 5939 | static void __net_exit ip6_route_net_exit(struct net *net) |
cdb18761 | 5940 | { |
421842ed | 5941 | kfree(net->ipv6.fib6_null_entry); |
8ed67789 DL |
5942 | kfree(net->ipv6.ip6_null_entry); |
5943 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
5944 | kfree(net->ipv6.ip6_prohibit_entry); | |
5945 | kfree(net->ipv6.ip6_blk_hole_entry); | |
5946 | #endif | |
41bb78b4 | 5947 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
cdb18761 DL |
5948 | } |
5949 | ||
d189634e TG |
5950 | static int __net_init ip6_route_net_init_late(struct net *net) |
5951 | { | |
5952 | #ifdef CONFIG_PROC_FS | |
c3506372 CH |
5953 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
5954 | sizeof(struct ipv6_route_iter)); | |
3617d949 CH |
5955 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
5956 | rt6_stats_seq_show, NULL); | |
d189634e TG |
5957 | #endif |
5958 | return 0; | |
5959 | } | |
5960 | ||
5961 | static void __net_exit ip6_route_net_exit_late(struct net *net) | |
5962 | { | |
5963 | #ifdef CONFIG_PROC_FS | |
ece31ffd G |
5964 | remove_proc_entry("ipv6_route", net->proc_net); |
5965 | remove_proc_entry("rt6_stats", net->proc_net); | |
d189634e TG |
5966 | #endif |
5967 | } | |
5968 | ||
cdb18761 DL |
5969 | static struct pernet_operations ip6_route_net_ops = { |
5970 | .init = ip6_route_net_init, | |
5971 | .exit = ip6_route_net_exit, | |
5972 | }; | |
5973 | ||
c3426b47 DM |
5974 | static int __net_init ipv6_inetpeer_init(struct net *net) |
5975 | { | |
5976 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); | |
5977 | ||
5978 | if (!bp) | |
5979 | return -ENOMEM; | |
5980 | inet_peer_base_init(bp); | |
5981 | net->ipv6.peers = bp; | |
5982 | return 0; | |
5983 | } | |
5984 | ||
5985 | static void __net_exit ipv6_inetpeer_exit(struct net *net) | |
5986 | { | |
5987 | struct inet_peer_base *bp = net->ipv6.peers; | |
5988 | ||
5989 | net->ipv6.peers = NULL; | |
56a6b248 | 5990 | inetpeer_invalidate_tree(bp); |
c3426b47 DM |
5991 | kfree(bp); |
5992 | } | |
5993 | ||
2b823f72 | 5994 | static struct pernet_operations ipv6_inetpeer_ops = { |
c3426b47 DM |
5995 | .init = ipv6_inetpeer_init, |
5996 | .exit = ipv6_inetpeer_exit, | |
5997 | }; | |
5998 | ||
d189634e TG |
5999 | static struct pernet_operations ip6_route_net_late_ops = { |
6000 | .init = ip6_route_net_init_late, | |
6001 | .exit = ip6_route_net_exit_late, | |
6002 | }; | |
6003 | ||
8ed67789 DL |
6004 | static struct notifier_block ip6_route_dev_notifier = { |
6005 | .notifier_call = ip6_route_dev_notify, | |
242d3a49 | 6006 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
8ed67789 DL |
6007 | }; |
6008 | ||
2f460933 WC |
6009 | void __init ip6_route_init_special_entries(void) |
6010 | { | |
6011 | /* Registering of the loopback is done before this portion of code, | |
6012 | * the loopback reference in rt6_info will not be taken, do it | |
6013 | * manually for init_net */ | |
1cf844c7 | 6014 | init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; |
2f460933 WC |
6015 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
6016 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6017 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
6018 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; | |
6019 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6020 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; | |
6021 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | |
6022 | #endif | |
6023 | } | |
6024 | ||
433d49c3 | 6025 | int __init ip6_route_init(void) |
1da177e4 | 6026 | { |
433d49c3 | 6027 | int ret; |
8d0b94af | 6028 | int cpu; |
433d49c3 | 6029 | |
9a7ec3a9 DL |
6030 | ret = -ENOMEM; |
6031 | ip6_dst_ops_template.kmem_cachep = | |
e5d679f3 | 6032 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
f845ab6b | 6033 | SLAB_HWCACHE_ALIGN, NULL); |
9a7ec3a9 | 6034 | if (!ip6_dst_ops_template.kmem_cachep) |
c19a28e1 | 6035 | goto out; |
14e50e57 | 6036 | |
fc66f95c | 6037 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
8ed67789 | 6038 | if (ret) |
bdb3289f | 6039 | goto out_kmem_cache; |
bdb3289f | 6040 | |
c3426b47 DM |
6041 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
6042 | if (ret) | |
e8803b6c | 6043 | goto out_dst_entries; |
2a0c451a | 6044 | |
7e52b33b DM |
6045 | ret = register_pernet_subsys(&ip6_route_net_ops); |
6046 | if (ret) | |
6047 | goto out_register_inetpeer; | |
c3426b47 | 6048 | |
5dc121e9 AE |
6049 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
6050 | ||
e8803b6c | 6051 | ret = fib6_init(); |
433d49c3 | 6052 | if (ret) |
8ed67789 | 6053 | goto out_register_subsys; |
433d49c3 | 6054 | |
433d49c3 DL |
6055 | ret = xfrm6_init(); |
6056 | if (ret) | |
e8803b6c | 6057 | goto out_fib6_init; |
c35b7e72 | 6058 | |
433d49c3 DL |
6059 | ret = fib6_rules_init(); |
6060 | if (ret) | |
6061 | goto xfrm6_init; | |
7e5449c2 | 6062 | |
d189634e TG |
6063 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
6064 | if (ret) | |
6065 | goto fib6_rules_init; | |
6066 | ||
16feebcf FW |
6067 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
6068 | inet6_rtm_newroute, NULL, 0); | |
6069 | if (ret < 0) | |
6070 | goto out_register_late_subsys; | |
6071 | ||
6072 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, | |
6073 | inet6_rtm_delroute, NULL, 0); | |
6074 | if (ret < 0) | |
6075 | goto out_register_late_subsys; | |
6076 | ||
6077 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, | |
6078 | inet6_rtm_getroute, NULL, | |
6079 | RTNL_FLAG_DOIT_UNLOCKED); | |
6080 | if (ret < 0) | |
d189634e | 6081 | goto out_register_late_subsys; |
c127ea2c | 6082 | |
8ed67789 | 6083 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
cdb18761 | 6084 | if (ret) |
d189634e | 6085 | goto out_register_late_subsys; |
8ed67789 | 6086 | |
8d0b94af MKL |
6087 | for_each_possible_cpu(cpu) { |
6088 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); | |
6089 | ||
6090 | INIT_LIST_HEAD(&ul->head); | |
6091 | spin_lock_init(&ul->lock); | |
6092 | } | |
6093 | ||
433d49c3 DL |
6094 | out: |
6095 | return ret; | |
6096 | ||
d189634e | 6097 | out_register_late_subsys: |
16feebcf | 6098 | rtnl_unregister_all(PF_INET6); |
d189634e | 6099 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
433d49c3 | 6100 | fib6_rules_init: |
433d49c3 DL |
6101 | fib6_rules_cleanup(); |
6102 | xfrm6_init: | |
433d49c3 | 6103 | xfrm6_fini(); |
2a0c451a TG |
6104 | out_fib6_init: |
6105 | fib6_gc_cleanup(); | |
8ed67789 DL |
6106 | out_register_subsys: |
6107 | unregister_pernet_subsys(&ip6_route_net_ops); | |
7e52b33b DM |
6108 | out_register_inetpeer: |
6109 | unregister_pernet_subsys(&ipv6_inetpeer_ops); | |
fc66f95c ED |
6110 | out_dst_entries: |
6111 | dst_entries_destroy(&ip6_dst_blackhole_ops); | |
433d49c3 | 6112 | out_kmem_cache: |
f2fc6a54 | 6113 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
433d49c3 | 6114 | goto out; |
1da177e4 LT |
6115 | } |
6116 | ||
6117 | void ip6_route_cleanup(void) | |
6118 | { | |
8ed67789 | 6119 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
d189634e | 6120 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
101367c2 | 6121 | fib6_rules_cleanup(); |
1da177e4 | 6122 | xfrm6_fini(); |
1da177e4 | 6123 | fib6_gc_cleanup(); |
c3426b47 | 6124 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
8ed67789 | 6125 | unregister_pernet_subsys(&ip6_route_net_ops); |
41bb78b4 | 6126 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
f2fc6a54 | 6127 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
1da177e4 | 6128 | } |