blob: c8bc9b4ac32835e7328025a8912f3627b365e38a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Joe Perchesf3213832012-05-15 14:11:53 +000027#define pr_fmt(fmt) "IPv6: " fmt
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080045#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020047#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/snmp.h>
49#include <net/ipv6.h>
50#include <net/ip6_fib.h>
51#include <net/ip6_route.h>
52#include <net/ndisc.h>
53#include <net/addrconf.h>
54#include <net/tcp.h>
55#include <linux/rtnetlink.h>
56#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020057#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070059#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070060#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000061#include <net/nexthop.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020062#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020063#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070064#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
68#ifdef CONFIG_SYSCTL
69#include <linux/sysctl.h>
70#endif
71
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020072enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010073 RT6_NUD_FAIL_HARD = -3,
74 RT6_NUD_FAIL_PROBE = -2,
75 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020076 RT6_NUD_SUCCEED = 1
77};
78
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -070079static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080081static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000082static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct dst_entry *ip6_negative_advice(struct dst_entry *);
84static void ip6_dst_destroy(struct dst_entry *);
85static void ip6_dst_ifdown(struct dst_entry *,
86 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080087static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050090static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053091static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050092static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070094static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
95 struct sk_buff *skb, u32 mtu);
96static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
97 struct sk_buff *skb);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -070098static void rt6_dst_from_metrics_check(struct rt6_info *rt);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020099static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800101#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800102static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000103 const struct in6_addr *prefix, int prefixlen,
104 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +0000105 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800106static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000107 const struct in6_addr *prefix, int prefixlen,
108 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800109#endif
110
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700111struct uncached_list {
112 spinlock_t lock;
113 struct list_head head;
114};
115
116static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
117
118static void rt6_uncached_list_add(struct rt6_info *rt)
119{
120 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
121
122 rt->dst.flags |= DST_NOCACHE;
123 rt->rt6i_uncached_list = ul;
124
125 spin_lock_bh(&ul->lock);
126 list_add_tail(&rt->rt6i_uncached, &ul->head);
127 spin_unlock_bh(&ul->lock);
128}
129
130static void rt6_uncached_list_del(struct rt6_info *rt)
131{
132 if (!list_empty(&rt->rt6i_uncached)) {
133 struct uncached_list *ul = rt->rt6i_uncached_list;
134
135 spin_lock_bh(&ul->lock);
136 list_del(&rt->rt6i_uncached);
137 spin_unlock_bh(&ul->lock);
138 }
139}
140
141static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
142{
143 struct net_device *loopback_dev = net->loopback_dev;
144 int cpu;
145
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500146 if (dev == loopback_dev)
147 return;
148
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700149 for_each_possible_cpu(cpu) {
150 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
151 struct rt6_info *rt;
152
153 spin_lock_bh(&ul->lock);
154 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
155 struct inet6_dev *rt_idev = rt->rt6i_idev;
156 struct net_device *rt_dev = rt->dst.dev;
157
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500158 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700159 rt->rt6i_idev = in6_dev_get(loopback_dev);
160 in6_dev_put(rt_idev);
161 }
162
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500163 if (rt_dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700164 rt->dst.dev = loopback_dev;
165 dev_hold(rt->dst.dev);
166 dev_put(rt_dev);
167 }
168 }
169 spin_unlock_bh(&ul->lock);
170 }
171}
172
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700173static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
174{
175 return dst_metrics_write_ptr(rt->dst.from);
176}
177
David S. Miller06582542011-01-27 14:58:42 -0800178static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
179{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700180 struct rt6_info *rt = (struct rt6_info *)dst;
David S. Miller06582542011-01-27 14:58:42 -0800181
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700182 if (rt->rt6i_flags & RTF_PCPU)
183 return rt6_pcpu_cow_metrics(rt);
184 else if (rt->rt6i_flags & RTF_CACHE)
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700185 return NULL;
186 else
Martin KaFai Lau3b471172015-02-12 16:14:08 -0800187 return dst_cow_metrics_generic(dst, old);
David S. Miller06582542011-01-27 14:58:42 -0800188}
189
David S. Millerf894cbf2012-07-02 21:52:24 -0700190static inline const void *choose_neigh_daddr(struct rt6_info *rt,
191 struct sk_buff *skb,
192 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500193{
194 struct in6_addr *p = &rt->rt6i_gateway;
195
David S. Millera7563f32012-01-26 16:29:16 -0500196 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500197 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700198 else if (skb)
199 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500200 return daddr;
201}
202
David S. Millerf894cbf2012-07-02 21:52:24 -0700203static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
204 struct sk_buff *skb,
205 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700206{
David S. Miller39232972012-01-26 15:22:32 -0500207 struct rt6_info *rt = (struct rt6_info *) dst;
208 struct neighbour *n;
209
David S. Millerf894cbf2012-07-02 21:52:24 -0700210 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000211 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500212 if (n)
213 return n;
214 return neigh_create(&nd_tbl, daddr, dst->dev);
215}
216
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800217static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .gc = ip6_dst_gc,
220 .gc_thresh = 1024,
221 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800222 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000223 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800224 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .destroy = ip6_dst_destroy,
226 .ifdown = ip6_dst_ifdown,
227 .negative_advice = ip6_negative_advice,
228 .link_failure = ip6_link_failure,
229 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700230 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500231 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700232 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Steffen Klassertebb762f2011-11-23 02:12:51 +0000235static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800236{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000237 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
238
239 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800240}
241
David S. Miller6700c272012-07-17 03:29:28 -0700242static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
243 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700244{
245}
246
David S. Miller6700c272012-07-17 03:29:28 -0700247static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
248 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700249{
250}
251
David S. Miller14e50e52007-05-24 18:17:54 -0700252static struct dst_ops ip6_dst_blackhole_ops = {
253 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700254 .destroy = ip6_dst_destroy,
255 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000256 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800257 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700258 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700259 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700260 .cow_metrics = dst_cow_metrics_generic,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700261 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700262};
263
David S. Miller62fa8a82011-01-26 20:51:05 -0800264static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800265 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800266};
267
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000268static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700269 .dst = {
270 .__refcnt = ATOMIC_INIT(1),
271 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000272 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700273 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700274 .input = ip6_pkt_discard,
275 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 },
277 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700278 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 .rt6i_metric = ~(u32) 0,
280 .rt6i_ref = ATOMIC_INIT(1),
281};
282
Thomas Graf101367c2006-08-04 03:39:02 -0700283#ifdef CONFIG_IPV6_MULTIPLE_TABLES
284
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000285static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700286 .dst = {
287 .__refcnt = ATOMIC_INIT(1),
288 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000289 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700290 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700291 .input = ip6_pkt_prohibit,
292 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700293 },
294 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700295 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700296 .rt6i_metric = ~(u32) 0,
297 .rt6i_ref = ATOMIC_INIT(1),
298};
299
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000300static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700301 .dst = {
302 .__refcnt = ATOMIC_INIT(1),
303 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000304 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700305 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500307 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700308 },
309 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700310 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700311 .rt6i_metric = ~(u32) 0,
312 .rt6i_ref = ATOMIC_INIT(1),
313};
314
315#endif
316
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700317static void rt6_info_init(struct rt6_info *rt)
318{
319 struct dst_entry *dst = &rt->dst;
320
321 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
322 INIT_LIST_HEAD(&rt->rt6i_siblings);
323 INIT_LIST_HEAD(&rt->rt6i_uncached);
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/* allocate dst with ip6_dst_ops */
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700327static struct rt6_info *__ip6_dst_alloc(struct net *net,
328 struct net_device *dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -0700329 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
David S. Miller97bab732012-06-09 22:36:36 -0700331 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000332 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700333
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700334 if (rt)
335 rt6_info_init(rt);
Steffen Klassert81048912012-07-05 23:37:09 +0000336
David S. Millercf911662011-04-28 14:31:47 -0700337 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700340static struct rt6_info *ip6_dst_alloc(struct net *net,
341 struct net_device *dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -0700342 int flags)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700343{
Martin KaFai Lauad706862015-08-14 11:05:52 -0700344 struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700345
346 if (rt) {
347 rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
348 if (rt->rt6i_pcpu) {
349 int cpu;
350
351 for_each_possible_cpu(cpu) {
352 struct rt6_info **p;
353
354 p = per_cpu_ptr(rt->rt6i_pcpu, cpu);
355 /* no one shares rt */
356 *p = NULL;
357 }
358 } else {
359 dst_destroy((struct dst_entry *)rt);
360 return NULL;
361 }
362 }
363
364 return rt;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ip6_dst_destroy(struct dst_entry *dst)
368{
369 struct rt6_info *rt = (struct rt6_info *)dst;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000370 struct dst_entry *from = dst->from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700373 dst_destroy_metrics_generic(dst);
Markus Elfring87775312015-07-02 16:30:24 +0200374 free_percpu(rt->rt6i_pcpu);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700375 rt6_uncached_list_del(rt);
376
377 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500378 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rt->rt6i_idev = NULL;
380 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900381 }
Gao feng1716a962012-04-06 00:13:10 +0000382
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000383 dst->from = NULL;
384 dst_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
388 int how)
389{
390 struct rt6_info *rt = (struct rt6_info *)dst;
391 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800392 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900393 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David S. Miller97cac082012-07-02 22:43:47 -0700395 if (dev != loopback_dev) {
396 if (idev && idev->dev == dev) {
397 struct inet6_dev *loopback_idev =
398 in6_dev_get(loopback_dev);
399 if (loopback_idev) {
400 rt->rt6i_idev = loopback_idev;
401 in6_dev_put(idev);
402 }
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
406
Eric Dumazeta50feda2012-05-18 18:57:34 +0000407static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Gao feng1716a962012-04-06 00:13:10 +0000409 if (rt->rt6i_flags & RTF_EXPIRES) {
410 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000411 return true;
Gao feng1716a962012-04-06 00:13:10 +0000412 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000413 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000414 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000415 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000418/* Multipath route selection:
419 * Hash based function using packet header and flowlabel.
420 * Adapted from fib_info_hashfn()
421 */
422static int rt6_info_hash_nhsfn(unsigned int candidate_count,
423 const struct flowi6 *fl6)
424{
Tom Herbert644d0e62015-09-23 14:13:35 -0700425 return get_hash_from_flowi6(fl6) % candidate_count;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000426}
427
428static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200429 struct flowi6 *fl6, int oif,
430 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000431{
432 struct rt6_info *sibling, *next_sibling;
433 int route_choosen;
434
435 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
436 /* Don't change the route, if route_choosen == 0
437 * (siblings does not include ourself)
438 */
439 if (route_choosen)
440 list_for_each_entry_safe(sibling, next_sibling,
441 &match->rt6i_siblings, rt6i_siblings) {
442 route_choosen--;
443 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200444 if (rt6_score_route(sibling, oif, strict) < 0)
445 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000446 match = sibling;
447 break;
448 }
449 }
450 return match;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700454 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
456
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800457static inline struct rt6_info *rt6_device_match(struct net *net,
458 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000459 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700461 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct rt6_info *local = NULL;
464 struct rt6_info *sprt;
465
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900466 if (!oif && ipv6_addr_any(saddr))
467 goto out;
468
Changli Gaod8d1f302010-06-10 23:31:35 -0700469 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500470 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900471
472 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (dev->ifindex == oif)
474 return sprt;
475 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500476 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 sprt->rt6i_idev->dev->ifindex != oif) {
David Ahern17fb0b22015-09-25 15:22:54 -0600478 if (flags & RT6_LOOKUP_F_IFACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
David Ahern17fb0b22015-09-25 15:22:54 -0600480 if (local &&
481 local->rt6i_idev->dev->ifindex == oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 continue;
483 }
484 local = sprt;
485 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900486 } else {
487 if (ipv6_chk_addr(net, saddr, dev,
488 flags & RT6_LOOKUP_F_IFACE))
489 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900493 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (local)
495 return local;
496
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700497 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800498 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900500out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return rt;
502}
503
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800504#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200505struct __rt6_probe_work {
506 struct work_struct work;
507 struct in6_addr target;
508 struct net_device *dev;
509};
510
511static void rt6_probe_deferred(struct work_struct *w)
512{
513 struct in6_addr mcaddr;
514 struct __rt6_probe_work *work =
515 container_of(w, struct __rt6_probe_work, work);
516
517 addrconf_addr_solict_mult(&work->target, &mcaddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200518 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, NULL);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200519 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100520 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200521}
522
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800523static void rt6_probe(struct rt6_info *rt)
524{
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700525 struct __rt6_probe_work *work;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000526 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800527 /*
528 * Okay, this does not seem to be appropriate
529 * for now, however, we need to check if it
530 * is really so; aka Router Reachability Probing.
531 *
532 * Router Reachability Probe MUST be rate-limited
533 * to no more than one per minute.
534 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000535 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000536 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000537 rcu_read_lock_bh();
538 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
539 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700540 if (neigh->nud_state & NUD_VALID)
541 goto out;
542
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700543 work = NULL;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000544 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700545 if (!(neigh->nud_state & NUD_VALID) &&
546 time_after(jiffies,
547 neigh->updated +
548 rt->rt6i_idev->cnf.rtr_probe_interval)) {
549 work = kmalloc(sizeof(*work), GFP_ATOMIC);
550 if (work)
551 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200552 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000553 write_unlock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700554 } else {
555 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000556 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700557
558 if (work) {
559 INIT_WORK(&work->work, rt6_probe_deferred);
560 work->target = rt->rt6i_gateway;
561 dev_hold(rt->dst.dev);
562 work->dev = rt->dst.dev;
563 schedule_work(&work->work);
564 }
565
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700566out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000567 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800568}
569#else
570static inline void rt6_probe(struct rt6_info *rt)
571{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800572}
573#endif
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800576 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700578static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
David S. Millerd1918542011-12-28 20:19:20 -0500580 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700581 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800582 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700583 if ((dev->flags & IFF_LOOPBACK) &&
584 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
585 return 1;
586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200589static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000591 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200592 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000593
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700594 if (rt->rt6i_flags & RTF_NONEXTHOP ||
595 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200596 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000597
598 rcu_read_lock_bh();
599 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
600 if (neigh) {
601 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800602 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200603 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800604#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000605 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200606 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100607 else
608 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800609#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000610 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200611 } else {
612 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100613 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000614 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000615 rcu_read_unlock_bh();
616
Paul Marksa5a81f02012-12-03 10:26:54 +0000617 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800620static int rt6_score_route(struct rt6_info *rt, int oif,
621 int strict)
622{
Paul Marksa5a81f02012-12-03 10:26:54 +0000623 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900624
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700625 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700626 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200627 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800628#ifdef CONFIG_IPV6_ROUTER_PREF
629 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
630#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200631 if (strict & RT6_LOOKUP_F_REACHABLE) {
632 int n = rt6_check_neigh(rt);
633 if (n < 0)
634 return n;
635 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800636 return m;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David S. Millerf11e6652007-03-24 20:36:25 -0700639static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200640 int *mpri, struct rt6_info *match,
641 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800642{
David S. Millerf11e6652007-03-24 20:36:25 -0700643 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200644 bool match_do_rr = false;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400645 struct inet6_dev *idev = rt->rt6i_idev;
646 struct net_device *dev = rt->dst.dev;
647
648 if (dev && !netif_carrier_ok(dev) &&
649 idev->cnf.ignore_routes_with_linkdown)
650 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700651
652 if (rt6_check_expired(rt))
653 goto out;
654
655 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100656 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200657 match_do_rr = true;
658 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100659 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700660 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700661 }
662
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200663 if (strict & RT6_LOOKUP_F_REACHABLE)
664 rt6_probe(rt);
665
Jiri Benc7e980562013-12-11 13:48:20 +0100666 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200667 if (m > *mpri) {
668 *do_rr = match_do_rr;
669 *mpri = m;
670 match = rt;
671 }
David S. Millerf11e6652007-03-24 20:36:25 -0700672out:
673 return match;
674}
675
676static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
677 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200678 u32 metric, int oif, int strict,
679 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700680{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700681 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800682 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David S. Millerf11e6652007-03-24 20:36:25 -0700684 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700685 cont = NULL;
686 for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
687 if (rt->rt6i_metric != metric) {
688 cont = rt;
689 break;
690 }
691
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200692 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700693 }
694
695 for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
696 if (rt->rt6i_metric != metric) {
697 cont = rt;
698 break;
699 }
700
701 match = find_match(rt, oif, strict, &mpri, match, do_rr);
702 }
703
704 if (match || !cont)
705 return match;
706
707 for (rt = cont; rt; rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200708 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800709
David S. Millerf11e6652007-03-24 20:36:25 -0700710 return match;
711}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800712
David S. Millerf11e6652007-03-24 20:36:25 -0700713static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
714{
715 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800716 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200717 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
David S. Millerf11e6652007-03-24 20:36:25 -0700719 rt0 = fn->rr_ptr;
720 if (!rt0)
721 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200723 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
724 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700727 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700728
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800729 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700730 if (!next || next->rt6i_metric != rt0->rt6i_metric)
731 next = fn->leaf;
732
733 if (next != rt0)
734 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
David S. Millerd1918542011-12-28 20:19:20 -0500737 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000738 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700741static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
742{
743 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
744}
745
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800746#ifdef CONFIG_IPV6_ROUTE_INFO
747int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000748 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800749{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900750 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800751 struct route_info *rinfo = (struct route_info *) opt;
752 struct in6_addr prefix_buf, *prefix;
753 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900754 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800755 struct rt6_info *rt;
756
757 if (len < sizeof(struct route_info)) {
758 return -EINVAL;
759 }
760
761 /* Sanity check for prefix_len and length */
762 if (rinfo->length > 3) {
763 return -EINVAL;
764 } else if (rinfo->prefix_len > 128) {
765 return -EINVAL;
766 } else if (rinfo->prefix_len > 64) {
767 if (rinfo->length < 2) {
768 return -EINVAL;
769 }
770 } else if (rinfo->prefix_len > 0) {
771 if (rinfo->length < 1) {
772 return -EINVAL;
773 }
774 }
775
776 pref = rinfo->route_pref;
777 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000778 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800779
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900780 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800781
782 if (rinfo->length == 3)
783 prefix = (struct in6_addr *)rinfo->prefix;
784 else {
785 /* this function is safe */
786 ipv6_addr_prefix(&prefix_buf,
787 (struct in6_addr *)rinfo->prefix,
788 rinfo->prefix_len);
789 prefix = &prefix_buf;
790 }
791
Duan Jiongf104a562013-11-08 09:56:53 +0800792 if (rinfo->prefix_len == 0)
793 rt = rt6_get_dflt_router(gwaddr, dev);
794 else
795 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
796 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800797
798 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700799 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800800 rt = NULL;
801 }
802
803 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800804 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800805 pref);
806 else if (rt)
807 rt->rt6i_flags = RTF_ROUTEINFO |
808 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
809
810 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000811 if (!addrconf_finite_timeout(lifetime))
812 rt6_clean_expires(rt);
813 else
814 rt6_set_expires(rt, jiffies + HZ * lifetime);
815
Amerigo Wang94e187c2012-10-29 00:13:19 +0000816 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800817 }
818 return 0;
819}
820#endif
821
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700822static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
823 struct in6_addr *saddr)
824{
825 struct fib6_node *pn;
826 while (1) {
827 if (fn->fn_flags & RTN_TL_ROOT)
828 return NULL;
829 pn = fn->parent;
830 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
831 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
832 else
833 fn = pn;
834 if (fn->fn_flags & RTN_RTINFO)
835 return fn;
836 }
837}
Thomas Grafc71099a2006-08-04 23:20:06 -0700838
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800839static struct rt6_info *ip6_pol_route_lookup(struct net *net,
840 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500841 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct fib6_node *fn;
844 struct rt6_info *rt;
845
Thomas Grafc71099a2006-08-04 23:20:06 -0700846 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500847 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700848restart:
849 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500850 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000851 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200852 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700853 if (rt == net->ipv6.ip6_null_entry) {
854 fn = fib6_backtrack(fn, &fl6->saddr);
855 if (fn)
856 goto restart;
857 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700858 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700859 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700860 return rt;
861
862}
863
Ian Morris67ba4152014-08-24 21:53:10 +0100864struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
Florian Westphalea6e5742011-09-05 16:05:44 +0200865 int flags)
866{
867 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
868}
869EXPORT_SYMBOL_GPL(ip6_route_lookup);
870
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900871struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
872 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700873{
David S. Miller4c9483b2011-03-12 16:22:43 -0500874 struct flowi6 fl6 = {
875 .flowi6_oif = oif,
876 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700877 };
878 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700879 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700880
Thomas Grafadaa70b2006-10-13 15:01:03 -0700881 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500882 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700883 flags |= RT6_LOOKUP_F_HAS_SADDR;
884 }
885
David S. Miller4c9483b2011-03-12 16:22:43 -0500886 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700887 if (dst->error == 0)
888 return (struct rt6_info *) dst;
889
890 dst_release(dst);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return NULL;
893}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900894EXPORT_SYMBOL(rt6_lookup);
895
Thomas Grafc71099a2006-08-04 23:20:06 -0700896/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 It takes new route entry, the addition fails by any reason the
898 route is freed. In any case, if caller does not hold it, it may
899 be destroyed.
900 */
901
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100902static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
Florian Westphale715b6d2015-01-05 23:57:44 +0100903 struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700906 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Thomas Grafc71099a2006-08-04 23:20:06 -0700908 table = rt->rt6i_table;
909 write_lock_bh(&table->tb6_lock);
Florian Westphale715b6d2015-01-05 23:57:44 +0100910 err = fib6_add(&table->tb6_root, rt, info, mxc);
Thomas Grafc71099a2006-08-04 23:20:06 -0700911 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return err;
914}
915
Thomas Graf40e22e82006-08-22 00:00:45 -0700916int ip6_ins_rt(struct rt6_info *rt)
917{
Florian Westphale715b6d2015-01-05 23:57:44 +0100918 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
919 struct mx6_config mxc = { .mx = NULL, };
920
921 return __ip6_ins_rt(rt, &info, &mxc);
Thomas Graf40e22e82006-08-22 00:00:45 -0700922}
923
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700924static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
925 const struct in6_addr *daddr,
926 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct rt6_info *rt;
929
930 /*
931 * Clone the route.
932 */
933
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700934 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700935 ort = (struct rt6_info *)ort->dst.from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Martin KaFai Lauad706862015-08-14 11:05:52 -0700937 rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700939 if (!rt)
940 return NULL;
941
942 ip6_rt_copy_init(rt, ort);
943 rt->rt6i_flags |= RTF_CACHE;
944 rt->rt6i_metric = 0;
945 rt->dst.flags |= DST_HOST;
946 rt->rt6i_dst.addr = *daddr;
947 rt->rt6i_dst.plen = 128;
948
949 if (!rt6_is_gw_or_nonexthop(ort)) {
950 if (ort->rt6i_dst.plen != 128 &&
951 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
952 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700954 if (rt->rt6i_src.plen && saddr) {
955 rt->rt6i_src.addr = *saddr;
956 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700957 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700958#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800961 return rt;
962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700964static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
965{
966 struct rt6_info *pcpu_rt;
967
968 pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev),
Martin KaFai Lauad706862015-08-14 11:05:52 -0700969 rt->dst.dev, rt->dst.flags);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700970
971 if (!pcpu_rt)
972 return NULL;
973 ip6_rt_copy_init(pcpu_rt, rt);
974 pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
975 pcpu_rt->rt6i_flags |= RTF_PCPU;
976 return pcpu_rt;
977}
978
979/* It should be called with read_lock_bh(&tb6_lock) acquired */
980static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
981{
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700982 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700983
984 p = this_cpu_ptr(rt->rt6i_pcpu);
985 pcpu_rt = *p;
986
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700987 if (pcpu_rt) {
988 dst_hold(&pcpu_rt->dst);
989 rt6_dst_from_metrics_check(pcpu_rt);
990 }
991 return pcpu_rt;
992}
993
994static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
995{
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700996 struct fib6_table *table = rt->rt6i_table;
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700997 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700998
999 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1000 if (!pcpu_rt) {
1001 struct net *net = dev_net(rt->dst.dev);
1002
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001003 dst_hold(&net->ipv6.ip6_null_entry->dst);
1004 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001005 }
1006
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001007 read_lock_bh(&table->tb6_lock);
1008 if (rt->rt6i_pcpu) {
1009 p = this_cpu_ptr(rt->rt6i_pcpu);
1010 prev = cmpxchg(p, NULL, pcpu_rt);
1011 if (prev) {
1012 /* If someone did it before us, return prev instead */
1013 dst_destroy(&pcpu_rt->dst);
1014 pcpu_rt = prev;
1015 }
1016 } else {
1017 /* rt has been removed from the fib6 tree
1018 * before we have a chance to acquire the read_lock.
1019 * In this case, don't brother to create a pcpu rt
1020 * since rt is going away anyway. The next
1021 * dst_check() will trigger a re-lookup.
1022 */
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001023 dst_destroy(&pcpu_rt->dst);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001024 pcpu_rt = rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001025 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001026 dst_hold(&pcpu_rt->dst);
1027 rt6_dst_from_metrics_check(pcpu_rt);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001028 read_unlock_bh(&table->tb6_lock);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001029 return pcpu_rt;
1030}
1031
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001032static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -05001033 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001035 struct fib6_node *fn, *saved_fn;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001036 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001037 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001039 strict |= flags & RT6_LOOKUP_F_IFACE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001040 if (net->ipv6.devconf_all->forwarding == 0)
1041 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Grafc71099a2006-08-04 23:20:06 -07001043 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
David S. Miller4c9483b2011-03-12 16:22:43 -05001045 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001046 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
David Ahernca254492015-10-12 11:47:10 -07001048 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1049 oif = 0;
1050
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001051redo_rt6_select:
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001052 rt = rt6_select(fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +02001053 if (rt->rt6i_nsiblings)
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001054 rt = rt6_multipath_select(rt, fl6, oif, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001055 if (rt == net->ipv6.ip6_null_entry) {
1056 fn = fib6_backtrack(fn, &fl6->saddr);
1057 if (fn)
1058 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001059 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1060 /* also consider unreachable route */
1061 strict &= ~RT6_LOOKUP_F_REACHABLE;
1062 fn = saved_fn;
1063 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001064 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001065 }
1066
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001067
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001068 if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001069 dst_use(&rt->dst, jiffies);
1070 read_unlock_bh(&table->tb6_lock);
1071
1072 rt6_dst_from_metrics_check(rt);
1073 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001074 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1075 !(rt->rt6i_flags & RTF_GATEWAY))) {
1076 /* Create a RTF_CACHE clone which will not be
1077 * owned by the fib6 tree. It is for the special case where
1078 * the daddr in the skb during the neighbor look-up is different
1079 * from the fl6->daddr used to look-up route here.
1080 */
Thomas Grafc71099a2006-08-04 23:20:06 -07001081
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001082 struct rt6_info *uncached_rt;
1083
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001084 dst_use(&rt->dst, jiffies);
1085 read_unlock_bh(&table->tb6_lock);
1086
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001087 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
1088 dst_release(&rt->dst);
1089
1090 if (uncached_rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001091 rt6_uncached_list_add(uncached_rt);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001092 else
1093 uncached_rt = net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001094
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001095 dst_hold(&uncached_rt->dst);
1096 return uncached_rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001097
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001098 } else {
1099 /* Get a percpu copy */
1100
1101 struct rt6_info *pcpu_rt;
1102
1103 rt->dst.lastuse = jiffies;
1104 rt->dst.__use++;
1105 pcpu_rt = rt6_get_pcpu_route(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001106
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001107 if (pcpu_rt) {
1108 read_unlock_bh(&table->tb6_lock);
1109 } else {
1110 /* We have to do the read_unlock first
1111 * because rt6_make_pcpu_route() may trigger
1112 * ip6_dst_gc() which will take the write_lock.
1113 */
1114 dst_hold(&rt->dst);
1115 read_unlock_bh(&table->tb6_lock);
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001116 pcpu_rt = rt6_make_pcpu_route(rt);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001117 dst_release(&rt->dst);
1118 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001119
1120 return pcpu_rt;
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001121
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001122 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001123}
1124
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001125static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001126 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001127{
David S. Miller4c9483b2011-03-12 16:22:43 -05001128 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001129}
1130
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001131static struct dst_entry *ip6_route_input_lookup(struct net *net,
1132 struct net_device *dev,
1133 struct flowi6 *fl6, int flags)
1134{
1135 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1136 flags |= RT6_LOOKUP_F_IFACE;
1137
1138 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1139}
1140
Thomas Grafc71099a2006-08-04 23:20:06 -07001141void ip6_route_input(struct sk_buff *skb)
1142{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001143 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001144 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001145 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02001146 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05001147 struct flowi6 fl6 = {
David Ahernca254492015-10-12 11:47:10 -07001148 .flowi6_iif = l3mdev_fib_oif(skb->dev),
David S. Miller4c9483b2011-03-12 16:22:43 -05001149 .daddr = iph->daddr,
1150 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001151 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05001152 .flowi6_mark = skb->mark,
1153 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001154 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001155
Jiri Benc904af042015-08-20 13:56:31 +02001156 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02001157 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02001158 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Jiri Benc06e9d042015-08-20 13:56:26 +02001159 skb_dst_drop(skb);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001160 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001161}
1162
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001163static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001164 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001165{
David S. Miller4c9483b2011-03-12 16:22:43 -05001166 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001167}
1168
Ian Morris67ba4152014-08-24 21:53:10 +01001169struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001170 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001171{
David Ahernca254492015-10-12 11:47:10 -07001172 struct dst_entry *dst;
Thomas Grafc71099a2006-08-04 23:20:06 -07001173 int flags = 0;
David Ahernd46a9d62015-10-21 08:42:22 -07001174 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07001175
David Ahernca254492015-10-12 11:47:10 -07001176 dst = l3mdev_rt6_dst_by_oif(net, fl6);
1177 if (dst)
1178 return dst;
1179
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001180 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001181
David Ahernd46a9d62015-10-21 08:42:22 -07001182 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07001183 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07001184 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001185 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001186
David Ahernd46a9d62015-10-21 08:42:22 -07001187 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07001188 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001189 else if (sk)
1190 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001191
David S. Miller4c9483b2011-03-12 16:22:43 -05001192 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001194EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
David S. Miller2774c132011-03-01 14:59:04 -08001196struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001197{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001198 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001199 struct dst_entry *new = NULL;
1200
David S. Millerf5b0a872012-07-19 12:31:33 -07001201 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001202 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07001203 rt6_info_init(rt);
1204
Changli Gaod8d1f302010-06-10 23:31:35 -07001205 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001206 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001207 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05001208 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07001209
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07001210 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001211 rt->rt6i_idev = ort->rt6i_idev;
1212 if (rt->rt6i_idev)
1213 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001214
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001215 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07001216 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07001217 rt->rt6i_metric = 0;
1218
1219 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1220#ifdef CONFIG_IPV6_SUBTREES
1221 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1222#endif
1223
1224 dst_free(new);
1225 }
1226
David S. Miller69ead7a2011-03-01 14:45:33 -08001227 dst_release(dst_orig);
1228 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001229}
David S. Miller14e50e52007-05-24 18:17:54 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Destination cache support functions
1233 */
1234
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001235static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1236{
1237 if (rt->dst.from &&
1238 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1239 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1240}
1241
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001242static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
1243{
1244 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1245 return NULL;
1246
1247 if (rt6_check_expired(rt))
1248 return NULL;
1249
1250 return &rt->dst;
1251}
1252
1253static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
1254{
1255 if (rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
1256 rt6_check((struct rt6_info *)(rt->dst.from), cookie))
1257 return &rt->dst;
1258 else
1259 return NULL;
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1263{
1264 struct rt6_info *rt;
1265
1266 rt = (struct rt6_info *) dst;
1267
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001268 /* All IPV6 dsts are created with ->obsolete set to the value
1269 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1270 * into this function always.
1271 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001272
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001273 rt6_dst_from_metrics_check(rt);
1274
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001275 if ((rt->rt6i_flags & RTF_PCPU) || unlikely(dst->flags & DST_NOCACHE))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001276 return rt6_dst_from_check(rt, cookie);
1277 else
1278 return rt6_check(rt, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1282{
1283 struct rt6_info *rt = (struct rt6_info *) dst;
1284
1285 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001286 if (rt->rt6i_flags & RTF_CACHE) {
1287 if (rt6_check_expired(rt)) {
1288 ip6_del_rt(rt);
1289 dst = NULL;
1290 }
1291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001293 dst = NULL;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001296 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299static void ip6_link_failure(struct sk_buff *skb)
1300{
1301 struct rt6_info *rt;
1302
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001303 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Eric Dumazetadf30902009-06-02 05:19:30 +00001305 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001307 if (rt->rt6i_flags & RTF_CACHE) {
1308 dst_hold(&rt->dst);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001309 ip6_del_rt(rt);
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001310 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 rt->rt6i_node->fn_sernum = -1;
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314}
1315
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001316static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
1317{
1318 struct net *net = dev_net(rt->dst.dev);
1319
1320 rt->rt6i_flags |= RTF_MODIFIED;
1321 rt->rt6i_pmtu = mtu;
1322 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
1323}
1324
1325static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
1326 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Ian Morris67ba4152014-08-24 21:53:10 +01001328 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001330 if (rt6->rt6i_flags & RTF_LOCAL)
1331 return;
1332
David S. Miller81aded22012-06-15 14:54:11 -07001333 dst_confirm(dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001334 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
1335 if (mtu >= dst_mtu(dst))
1336 return;
David S. Miller81aded22012-06-15 14:54:11 -07001337
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001338 if (rt6->rt6i_flags & RTF_CACHE) {
1339 rt6_do_update_pmtu(rt6, mtu);
1340 } else {
1341 const struct in6_addr *daddr, *saddr;
1342 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001343
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001344 if (iph) {
1345 daddr = &iph->daddr;
1346 saddr = &iph->saddr;
1347 } else if (sk) {
1348 daddr = &sk->sk_v6_daddr;
1349 saddr = &inet6_sk(sk)->saddr;
1350 } else {
1351 return;
1352 }
1353 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
1354 if (nrt6) {
1355 rt6_do_update_pmtu(nrt6, mtu);
1356
1357 /* ip6_ins_rt(nrt6) will bump the
1358 * rt6->rt6i_node->fn_sernum
1359 * which will fail the next rt6_check() and
1360 * invalidate the sk->sk_dst_cache.
1361 */
1362 ip6_ins_rt(nrt6);
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365}
1366
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001367static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1368 struct sk_buff *skb, u32 mtu)
1369{
1370 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
1371}
1372
David S. Miller42ae66c2012-06-15 20:01:57 -07001373void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1374 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001375{
1376 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1377 struct dst_entry *dst;
1378 struct flowi6 fl6;
1379
1380 memset(&fl6, 0, sizeof(fl6));
1381 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001382 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001383 fl6.daddr = iph->daddr;
1384 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001385 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001386
1387 dst = ip6_route_output(net, NULL, &fl6);
1388 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001389 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001390 dst_release(dst);
1391}
1392EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1393
1394void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1395{
1396 ip6_update_pmtu(skb, sock_net(sk), mtu,
1397 sk->sk_bound_dev_if, sk->sk_mark);
1398}
1399EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1400
Duan Jiongb55b76b2013-09-04 19:44:21 +08001401/* Handle redirects */
1402struct ip6rd_flowi {
1403 struct flowi6 fl6;
1404 struct in6_addr gateway;
1405};
1406
1407static struct rt6_info *__ip6_route_redirect(struct net *net,
1408 struct fib6_table *table,
1409 struct flowi6 *fl6,
1410 int flags)
1411{
1412 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1413 struct rt6_info *rt;
1414 struct fib6_node *fn;
1415
1416 /* Get the "current" route for this destination and
1417 * check if the redirect has come from approriate router.
1418 *
1419 * RFC 4861 specifies that redirects should only be
1420 * accepted if they come from the nexthop to the target.
1421 * Due to the way the routes are chosen, this notion
1422 * is a bit fuzzy and one might need to check all possible
1423 * routes.
1424 */
1425
1426 read_lock_bh(&table->tb6_lock);
1427 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1428restart:
1429 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1430 if (rt6_check_expired(rt))
1431 continue;
1432 if (rt->dst.error)
1433 break;
1434 if (!(rt->rt6i_flags & RTF_GATEWAY))
1435 continue;
1436 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1437 continue;
1438 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1439 continue;
1440 break;
1441 }
1442
1443 if (!rt)
1444 rt = net->ipv6.ip6_null_entry;
1445 else if (rt->dst.error) {
1446 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001447 goto out;
1448 }
1449
1450 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001451 fn = fib6_backtrack(fn, &fl6->saddr);
1452 if (fn)
1453 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001454 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001455
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001456out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001457 dst_hold(&rt->dst);
1458
1459 read_unlock_bh(&table->tb6_lock);
1460
1461 return rt;
1462};
1463
1464static struct dst_entry *ip6_route_redirect(struct net *net,
1465 const struct flowi6 *fl6,
1466 const struct in6_addr *gateway)
1467{
1468 int flags = RT6_LOOKUP_F_HAS_SADDR;
1469 struct ip6rd_flowi rdfl;
1470
1471 rdfl.fl6 = *fl6;
1472 rdfl.gateway = *gateway;
1473
1474 return fib6_rule_lookup(net, &rdfl.fl6,
1475 flags, __ip6_route_redirect);
1476}
1477
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001478void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1479{
1480 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1481 struct dst_entry *dst;
1482 struct flowi6 fl6;
1483
1484 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001485 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001486 fl6.flowi6_oif = oif;
1487 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001488 fl6.daddr = iph->daddr;
1489 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001490 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001491
Duan Jiongb55b76b2013-09-04 19:44:21 +08001492 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1493 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001494 dst_release(dst);
1495}
1496EXPORT_SYMBOL_GPL(ip6_redirect);
1497
Duan Jiongc92a59e2013-08-22 12:07:35 +08001498void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1499 u32 mark)
1500{
1501 const struct ipv6hdr *iph = ipv6_hdr(skb);
1502 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1503 struct dst_entry *dst;
1504 struct flowi6 fl6;
1505
1506 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001507 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001508 fl6.flowi6_oif = oif;
1509 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001510 fl6.daddr = msg->dest;
1511 fl6.saddr = iph->daddr;
1512
Duan Jiongb55b76b2013-09-04 19:44:21 +08001513 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1514 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001515 dst_release(dst);
1516}
1517
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001518void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1519{
1520 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1521}
1522EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1523
David S. Miller0dbaee32010-12-13 12:52:14 -08001524static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
David S. Miller0dbaee32010-12-13 12:52:14 -08001526 struct net_device *dev = dst->dev;
1527 unsigned int mtu = dst_mtu(dst);
1528 struct net *net = dev_net(dev);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1531
Daniel Lezcano55786892008-03-04 13:47:47 -08001532 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1533 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001536 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1537 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1538 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * rely only on pmtu discovery"
1540 */
1541 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1542 mtu = IPV6_MAXPLEN;
1543 return mtu;
1544}
1545
Steffen Klassertebb762f2011-11-23 02:12:51 +00001546static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001547{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001548 const struct rt6_info *rt = (const struct rt6_info *)dst;
1549 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001550 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001551
1552 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001553 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001554
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001555 mtu = dst_metric_raw(dst, RTAX_MTU);
1556 if (mtu)
1557 goto out;
1558
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001559 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001560
1561 rcu_read_lock();
1562 idev = __in6_dev_get(dst->dev);
1563 if (idev)
1564 mtu = idev->cnf.mtu6;
1565 rcu_read_unlock();
1566
Eric Dumazet30f78d82014-04-10 21:23:36 -07001567out:
1568 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001569}
1570
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001571static struct dst_entry *icmp6_dst_gc_list;
1572static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001573
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001574struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001575 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
David S. Miller87a11572011-12-06 17:04:13 -05001577 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct rt6_info *rt;
1579 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001580 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
David S. Miller38308472011-12-03 18:02:47 -05001582 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001583 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Martin KaFai Lauad706862015-08-14 11:05:52 -07001585 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05001586 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001588 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto out;
1590 }
1591
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001592 rt->dst.flags |= DST_HOST;
1593 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001594 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001595 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001596 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001597 rt->rt6i_dst.plen = 128;
1598 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001599 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001601 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001602 rt->dst.next = icmp6_dst_gc_list;
1603 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001604 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Daniel Lezcano55786892008-03-04 13:47:47 -08001606 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David S. Miller87a11572011-12-06 17:04:13 -05001608 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610out:
David S. Miller87a11572011-12-06 17:04:13 -05001611 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001614int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001616 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001617 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001619 spin_lock_bh(&icmp6_dst_lock);
1620 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 while ((dst = *pprev) != NULL) {
1623 if (!atomic_read(&dst->__refcnt)) {
1624 *pprev = dst->next;
1625 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } else {
1627 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001628 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630 }
1631
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001632 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001633
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001634 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
David S. Miller1e493d12008-09-10 17:27:15 -07001637static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1638 void *arg)
1639{
1640 struct dst_entry *dst, **pprev;
1641
1642 spin_lock_bh(&icmp6_dst_lock);
1643 pprev = &icmp6_dst_gc_list;
1644 while ((dst = *pprev) != NULL) {
1645 struct rt6_info *rt = (struct rt6_info *) dst;
1646 if (func(rt, arg)) {
1647 *pprev = dst->next;
1648 dst_free(dst);
1649 } else {
1650 pprev = &dst->next;
1651 }
1652 }
1653 spin_unlock_bh(&icmp6_dst_lock);
1654}
1655
Daniel Lezcano569d3642008-01-18 03:56:57 -08001656static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001658 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001659 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1660 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1661 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1662 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1663 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001664 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric Dumazetfc66f952010-10-08 06:37:34 +00001666 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001667 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001668 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto out;
1670
Benjamin Thery6891a342008-03-04 13:49:47 -08001671 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001672 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001673 entries = dst_entries_get_slow(ops);
1674 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001675 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001677 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001678 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Florian Westphale715b6d2015-01-05 23:57:44 +01001681static int ip6_convert_metrics(struct mx6_config *mxc,
1682 const struct fib6_config *cfg)
1683{
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001684 bool ecn_ca = false;
Florian Westphale715b6d2015-01-05 23:57:44 +01001685 struct nlattr *nla;
1686 int remaining;
1687 u32 *mp;
1688
Ian Morris63159f22015-03-29 14:00:04 +01001689 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001690 return 0;
1691
1692 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1693 if (unlikely(!mp))
1694 return -ENOMEM;
1695
1696 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1697 int type = nla_type(nla);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001698 u32 val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001699
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001700 if (!type)
1701 continue;
1702 if (unlikely(type > RTAX_MAX))
1703 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001704
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001705 if (type == RTAX_CC_ALGO) {
1706 char tmp[TCP_CA_NAME_MAX];
1707
1708 nla_strlcpy(tmp, nla, sizeof(tmp));
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001709 val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001710 if (val == TCP_CA_UNSPEC)
Florian Westphale715b6d2015-01-05 23:57:44 +01001711 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001712 } else {
1713 val = nla_get_u32(nla);
Florian Westphale715b6d2015-01-05 23:57:44 +01001714 }
Daniel Borkmannb8d3e412015-08-31 15:58:46 +02001715 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
1716 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001717
1718 mp[type - 1] = val;
1719 __set_bit(type - 1, mxc->mx_valid);
Florian Westphale715b6d2015-01-05 23:57:44 +01001720 }
1721
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001722 if (ecn_ca) {
1723 __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
1724 mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
1725 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001726
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001727 mxc->mx = mp;
Florian Westphale715b6d2015-01-05 23:57:44 +01001728 return 0;
1729 err:
1730 kfree(mp);
1731 return -EINVAL;
1732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001734static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Daniel Lezcano55786892008-03-04 13:47:47 -08001736 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct rt6_info *rt = NULL;
1738 struct net_device *dev = NULL;
1739 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001740 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001742 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Thomas Graf86872cb2006-08-22 00:01:08 -07001744 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001747 if (cfg->fc_src_len)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001748 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001750 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001752 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!dev)
1754 goto out;
1755 idev = in6_dev_get(dev);
1756 if (!idev)
1757 goto out;
1758 }
1759
Thomas Graf86872cb2006-08-22 00:01:08 -07001760 if (cfg->fc_metric == 0)
1761 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Matti Vaittinend71314b2011-11-14 00:14:49 +00001763 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001764 if (cfg->fc_nlinfo.nlh &&
1765 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001766 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001767 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001768 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001769 table = fib6_new_table(net, cfg->fc_table);
1770 }
1771 } else {
1772 table = fib6_new_table(net, cfg->fc_table);
1773 }
David S. Miller38308472011-12-03 18:02:47 -05001774
1775 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001776 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001777
Martin KaFai Lauad706862015-08-14 11:05:52 -07001778 rt = ip6_dst_alloc(net, NULL,
1779 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
David S. Miller38308472011-12-03 18:02:47 -05001781 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 err = -ENOMEM;
1783 goto out;
1784 }
1785
Gao feng1716a962012-04-06 00:13:10 +00001786 if (cfg->fc_flags & RTF_EXPIRES)
1787 rt6_set_expires(rt, jiffies +
1788 clock_t_to_jiffies(cfg->fc_expires));
1789 else
1790 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Thomas Graf86872cb2006-08-22 00:01:08 -07001792 if (cfg->fc_protocol == RTPROT_UNSPEC)
1793 cfg->fc_protocol = RTPROT_BOOT;
1794 rt->rt6i_protocol = cfg->fc_protocol;
1795
1796 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001799 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001800 else if (cfg->fc_flags & RTF_LOCAL)
1801 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001803 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Changli Gaod8d1f302010-06-10 23:31:35 -07001805 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001807 if (cfg->fc_encap) {
1808 struct lwtunnel_state *lwtstate;
1809
1810 err = lwtunnel_build_state(dev, cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07001811 cfg->fc_encap, AF_INET6, cfg,
1812 &lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001813 if (err)
1814 goto out;
Jiri Benc61adedf2015-08-20 13:56:25 +02001815 rt->dst.lwtstate = lwtstate_get(lwtstate);
1816 if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
1817 rt->dst.lwtstate->orig_output = rt->dst.output;
1818 rt->dst.output = lwtunnel_output;
Tom Herbert25368622015-08-17 13:42:24 -07001819 }
Jiri Benc61adedf2015-08-20 13:56:25 +02001820 if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
1821 rt->dst.lwtstate->orig_input = rt->dst.input;
1822 rt->dst.input = lwtunnel_input;
Tom Herbert25368622015-08-17 13:42:24 -07001823 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001824 }
1825
Thomas Graf86872cb2006-08-22 00:01:08 -07001826 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1827 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07001828 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001829 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001832 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1833 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834#endif
1835
Thomas Graf86872cb2006-08-22 00:01:08 -07001836 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* We cannot add true routes via loopback here,
1839 they would result in kernel looping; promote them to reject routes
1840 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001841 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001842 (dev && (dev->flags & IFF_LOOPBACK) &&
1843 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1844 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001846 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (dev) {
1848 dev_put(dev);
1849 in6_dev_put(idev);
1850 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001851 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 dev_hold(dev);
1853 idev = in6_dev_get(dev);
1854 if (!idev) {
1855 err = -ENODEV;
1856 goto out;
1857 }
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001860 switch (cfg->fc_type) {
1861 case RTN_BLACKHOLE:
1862 rt->dst.error = -EINVAL;
Eric W. Biedermanede20592015-10-07 16:48:47 -05001863 rt->dst.output = dst_discard_out;
Kamala R7150aed2013-12-02 19:55:21 +05301864 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001865 break;
1866 case RTN_PROHIBIT:
1867 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301868 rt->dst.output = ip6_pkt_prohibit_out;
1869 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001870 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001871 case RTN_THROW:
Nikola Forró0315e382015-09-17 16:01:32 +02001872 case RTN_UNREACHABLE:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001873 default:
Kamala R7150aed2013-12-02 19:55:21 +05301874 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
Nikola Forró0315e382015-09-17 16:01:32 +02001875 : (cfg->fc_type == RTN_UNREACHABLE)
1876 ? -EHOSTUNREACH : -ENETUNREACH;
Kamala R7150aed2013-12-02 19:55:21 +05301877 rt->dst.output = ip6_pkt_discard_out;
1878 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001879 break;
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto install_route;
1882 }
1883
Thomas Graf86872cb2006-08-22 00:01:08 -07001884 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001885 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 int gwa_type;
1887
Thomas Graf86872cb2006-08-22 00:01:08 -07001888 gw_addr = &cfg->fc_gateway;
Florian Westphal330567b2015-08-07 10:54:28 +02001889 gwa_type = ipv6_addr_type(gw_addr);
Florian Westphal48ed7b22015-05-21 00:25:41 +02001890
1891 /* if gw_addr is local we will fail to detect this in case
1892 * address is still TENTATIVE (DAD in progress). rt6_lookup()
1893 * will return already-added prefix route via interface that
1894 * prefix route was assigned to, which might be non-loopback.
1895 */
1896 err = -EINVAL;
Florian Westphal330567b2015-08-07 10:54:28 +02001897 if (ipv6_chk_addr_and_flags(net, gw_addr,
1898 gwa_type & IPV6_ADDR_LINKLOCAL ?
1899 dev : NULL, 0, 0))
Florian Westphal48ed7b22015-05-21 00:25:41 +02001900 goto out;
1901
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001902 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1905 struct rt6_info *grt;
1906
1907 /* IPv6 strictly inhibits using not link-local
1908 addresses as nexthop address.
1909 Otherwise, router will not able to send redirects.
1910 It is very good, but in some (rare!) circumstances
1911 (SIT, PtP, NBMA NOARP links) it is handy to allow
1912 some exceptions. --ANK
1913 */
David S. Miller38308472011-12-03 18:02:47 -05001914 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto out;
1916
Daniel Lezcano55786892008-03-04 13:47:47 -08001917 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001920 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto out;
1922 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001923 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001924 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 goto out;
1926 }
1927 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001928 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 idev = grt->rt6i_idev;
1930 dev_hold(dev);
1931 in6_dev_hold(grt->rt6i_idev);
1932 }
David S. Miller38308472011-12-03 18:02:47 -05001933 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001935 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if (err)
1938 goto out;
1939 }
1940 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001941 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto out;
1943 }
1944
1945 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001946 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto out;
1948
Daniel Walterc3968a82011-04-13 21:10:57 +00001949 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1950 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1951 err = -EINVAL;
1952 goto out;
1953 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001954 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001955 rt->rt6i_prefsrc.plen = 128;
1956 } else
1957 rt->rt6i_prefsrc.plen = 0;
1958
Thomas Graf86872cb2006-08-22 00:01:08 -07001959 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001962 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001964 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001965
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001966 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001967
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001968 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969out:
1970 if (dev)
1971 dev_put(dev);
1972 if (idev)
1973 in6_dev_put(idev);
1974 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001975 dst_free(&rt->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001976
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001977 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001978}
1979
1980int ip6_route_add(struct fib6_config *cfg)
1981{
1982 struct mx6_config mxc = { .mx = NULL, };
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001983 struct rt6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001984 int err;
1985
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001986 rt = ip6_route_info_create(cfg);
1987 if (IS_ERR(rt)) {
1988 err = PTR_ERR(rt);
1989 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001990 goto out;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001991 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001992
1993 err = ip6_convert_metrics(&mxc, cfg);
1994 if (err)
1995 goto out;
1996
1997 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1998
1999 kfree(mxc.mx);
2000
2001 return err;
2002out:
2003 if (rt)
2004 dst_free(&rt->dst);
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return err;
2007}
2008
Thomas Graf86872cb2006-08-22 00:01:08 -07002009static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07002012 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05002013 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002015 if (rt == net->ipv6.ip6_null_entry ||
2016 rt->dst.flags & DST_NOCACHE) {
Gao feng6825a262012-09-19 19:25:34 +00002017 err = -ENOENT;
2018 goto out;
2019 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07002020
Thomas Grafc71099a2006-08-04 23:20:06 -07002021 table = rt->rt6i_table;
2022 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07002023 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07002024 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Gao feng6825a262012-09-19 19:25:34 +00002026out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00002027 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return err;
2029}
2030
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002031int ip6_del_rt(struct rt6_info *rt)
2032{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002033 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05002034 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002035 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002036 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002037}
2038
Thomas Graf86872cb2006-08-22 00:01:08 -07002039static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Thomas Grafc71099a2006-08-04 23:20:06 -07002041 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct fib6_node *fn;
2043 struct rt6_info *rt;
2044 int err = -ESRCH;
2045
Daniel Lezcano55786892008-03-04 13:47:47 -08002046 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002047 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Thomas Grafc71099a2006-08-04 23:20:06 -07002050 read_lock_bh(&table->tb6_lock);
2051
2052 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07002053 &cfg->fc_dst, cfg->fc_dst_len,
2054 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002057 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002058 if ((rt->rt6i_flags & RTF_CACHE) &&
2059 !(cfg->fc_flags & RTF_CACHE))
2060 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002061 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05002062 (!rt->dst.dev ||
2063 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002065 if (cfg->fc_flags & RTF_GATEWAY &&
2066 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002068 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002070 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002071 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Thomas Graf86872cb2006-08-22 00:01:08 -07002073 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002076 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 return err;
2079}
2080
David S. Miller6700c272012-07-17 03:29:28 -07002081static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002082{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002083 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07002084 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002085 struct ndisc_options ndopts;
2086 struct inet6_dev *in6_dev;
2087 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002088 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07002089 int optlen, on_link;
2090 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07002091
Simon Horman29a3cad2013-05-28 20:34:26 +00002092 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002093 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07002094
2095 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07002096 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002097 return;
2098 }
2099
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002100 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07002101
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002102 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002103 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002104 return;
2105 }
2106
David S. Miller6e157b62012-07-12 00:05:02 -07002107 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002108 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002109 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002110 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07002111 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002112 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002113 return;
2114 }
2115
2116 in6_dev = __in6_dev_get(skb->dev);
2117 if (!in6_dev)
2118 return;
2119 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2120 return;
2121
2122 /* RFC2461 8.1:
2123 * The IP source address of the Redirect MUST be the same as the current
2124 * first-hop router for the specified ICMP Destination Address.
2125 */
2126
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002127 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002128 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2129 return;
2130 }
David S. Miller6e157b62012-07-12 00:05:02 -07002131
2132 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002133 if (ndopts.nd_opts_tgt_lladdr) {
2134 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
2135 skb->dev);
2136 if (!lladdr) {
2137 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
2138 return;
2139 }
2140 }
2141
David S. Miller6e157b62012-07-12 00:05:02 -07002142 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01002143 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07002144 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
2145 return;
2146 }
2147
2148 /* Redirect received -> path was valid.
2149 * Look, redirects are sent only in response to data packets,
2150 * so that this nexthop apparently is reachable. --ANK
2151 */
2152 dst_confirm(&rt->dst);
2153
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002154 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07002155 if (!neigh)
2156 return;
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * We have finally decided to accept it.
2160 */
2161
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002162 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 NEIGH_UPDATE_F_WEAK_OVERRIDE|
2164 NEIGH_UPDATE_F_OVERRIDE|
2165 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
2166 NEIGH_UPDATE_F_ISROUTER))
2167 );
2168
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002169 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05002170 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto out;
2172
2173 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
2174 if (on_link)
2175 nrt->rt6i_flags &= ~RTF_GATEWAY;
2176
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002177 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Graf40e22e82006-08-22 00:00:45 -07002179 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto out;
2181
Changli Gaod8d1f302010-06-10 23:31:35 -07002182 netevent.old = &rt->dst;
2183 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002184 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00002185 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07002186 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
2187
David S. Miller38308472011-12-03 18:02:47 -05002188 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07002189 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002190 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
2193out:
David S. Millere8599ff2012-07-11 23:43:53 -07002194 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07002195}
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 * Misc support functions
2199 */
2200
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002201static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
2202{
2203 BUG_ON(from->dst.from);
2204
2205 rt->rt6i_flags &= ~RTF_EXPIRES;
2206 dst_hold(&from->dst);
2207 rt->dst.from = &from->dst;
2208 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
2209}
2210
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002211static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002213 rt->dst.input = ort->dst.input;
2214 rt->dst.output = ort->dst.output;
2215 rt->rt6i_dst = ort->rt6i_dst;
2216 rt->dst.error = ort->dst.error;
2217 rt->rt6i_idev = ort->rt6i_idev;
2218 if (rt->rt6i_idev)
2219 in6_dev_hold(rt->rt6i_idev);
2220 rt->dst.lastuse = jiffies;
2221 rt->rt6i_gateway = ort->rt6i_gateway;
2222 rt->rt6i_flags = ort->rt6i_flags;
2223 rt6_set_from(rt, ort);
2224 rt->rt6i_metric = ort->rt6i_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002226 rt->rt6i_src = ort->rt6i_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227#endif
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002228 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
2229 rt->rt6i_table = ort->rt6i_table;
Jiri Benc61adedf2015-08-20 13:56:25 +02002230 rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002233#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002234static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002235 const struct in6_addr *prefix, int prefixlen,
2236 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002237{
2238 struct fib6_node *fn;
2239 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002240 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002241
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002242 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05002243 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002244 return NULL;
2245
Li RongQing5744dd92012-09-11 21:59:01 +00002246 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002247 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002248 if (!fn)
2249 goto out;
2250
Changli Gaod8d1f302010-06-10 23:31:35 -07002251 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002252 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002253 continue;
2254 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
2255 continue;
2256 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
2257 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002258 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002259 break;
2260 }
2261out:
Li RongQing5744dd92012-09-11 21:59:01 +00002262 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002263 return rt;
2264}
2265
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002266static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002267 const struct in6_addr *prefix, int prefixlen,
2268 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00002269 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002270{
Thomas Graf86872cb2006-08-22 00:01:08 -07002271 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08002272 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002273 .fc_ifindex = ifindex,
2274 .fc_dst_len = prefixlen,
2275 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2276 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002277 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002278 .fc_nlinfo.nlh = NULL,
2279 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002280 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002281
David Ahernca254492015-10-12 11:47:10 -07002282 cfg.fc_table = l3mdev_fib_table_by_index(net, ifindex) ? : RT6_TABLE_INFO;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002283 cfg.fc_dst = *prefix;
2284 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002285
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002286 /* We should treat it as a default route if prefix length is 0. */
2287 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002288 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002289
Thomas Graf86872cb2006-08-22 00:01:08 -07002290 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002291
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002292 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002293}
2294#endif
2295
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002296struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002299 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002301 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002302 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002303 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Li RongQing5744dd92012-09-11 21:59:01 +00002305 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002306 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002307 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002308 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2310 break;
2311 }
2312 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002313 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002314 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return rt;
2316}
2317
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002318struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002319 struct net_device *dev,
2320 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Thomas Graf86872cb2006-08-22 00:01:08 -07002322 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002323 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002324 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002325 .fc_ifindex = dev->ifindex,
2326 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2327 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002328 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002329 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002330 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002333 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Thomas Graf86872cb2006-08-22 00:01:08 -07002335 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return rt6_get_dflt_router(gwaddr, dev);
2338}
2339
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002340void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002343 struct fib6_table *table;
2344
2345 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002346 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002347 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002351 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002352 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002353 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2354 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002355 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002356 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002357 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto restart;
2359 }
2360 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002361 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Daniel Lezcano55786892008-03-04 13:47:47 -08002364static void rtmsg_to_fib6_config(struct net *net,
2365 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002366 struct fib6_config *cfg)
2367{
2368 memset(cfg, 0, sizeof(*cfg));
2369
David Ahernca254492015-10-12 11:47:10 -07002370 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
2371 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07002372 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2373 cfg->fc_metric = rtmsg->rtmsg_metric;
2374 cfg->fc_expires = rtmsg->rtmsg_info;
2375 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2376 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2377 cfg->fc_flags = rtmsg->rtmsg_flags;
2378
Daniel Lezcano55786892008-03-04 13:47:47 -08002379 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002380
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002381 cfg->fc_dst = rtmsg->rtmsg_dst;
2382 cfg->fc_src = rtmsg->rtmsg_src;
2383 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002384}
2385
Daniel Lezcano55786892008-03-04 13:47:47 -08002386int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Thomas Graf86872cb2006-08-22 00:01:08 -07002388 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct in6_rtmsg rtmsg;
2390 int err;
2391
Ian Morris67ba4152014-08-24 21:53:10 +01002392 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 case SIOCADDRT: /* Add a route */
2394 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002395 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return -EPERM;
2397 err = copy_from_user(&rtmsg, arg,
2398 sizeof(struct in6_rtmsg));
2399 if (err)
2400 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002401
Daniel Lezcano55786892008-03-04 13:47:47 -08002402 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 rtnl_lock();
2405 switch (cmd) {
2406 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002407 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
2409 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002410 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 break;
2412 default:
2413 err = -EINVAL;
2414 }
2415 rtnl_unlock();
2416
2417 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 return -EINVAL;
2421}
2422
2423/*
2424 * Drop the packet on the floor
2425 */
2426
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002427static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002429 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002430 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002431 switch (ipstats_mib_noroutes) {
2432 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002433 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002434 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002435 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2436 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002437 break;
2438 }
2439 /* FALLTHROUGH */
2440 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002441 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2442 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002443 break;
2444 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002445 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 kfree_skb(skb);
2447 return 0;
2448}
2449
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002450static int ip6_pkt_discard(struct sk_buff *skb)
2451{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002452 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002453}
2454
Eric W. Biedermanede20592015-10-07 16:48:47 -05002455static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Eric Dumazetadf30902009-06-02 05:19:30 +00002457 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002458 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002461static int ip6_pkt_prohibit(struct sk_buff *skb)
2462{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002463 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002464}
2465
Eric W. Biedermanede20592015-10-07 16:48:47 -05002466static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002467{
Eric Dumazetadf30902009-06-02 05:19:30 +00002468 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002469 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/*
2473 * Allocate a dst for local (unicast / anycast) address.
2474 */
2475
2476struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2477 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002478 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
David Ahernca254492015-10-12 11:47:10 -07002480 u32 tb_id;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002481 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002482 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -07002483 DST_NOCOUNT);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002484 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return ERR_PTR(-ENOMEM);
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 in6_dev_hold(idev);
2488
David S. Miller11d53b42011-06-24 15:23:34 -07002489 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002490 rt->dst.input = ip6_input;
2491 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002495 if (anycast)
2496 rt->rt6i_flags |= RTF_ANYCAST;
2497 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Julian Anastasov550bab42013-10-20 15:43:04 +03002500 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002501 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 rt->rt6i_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07002503 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
2504 rt->rt6i_table = fib6_get_table(net, tb_id);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002505 rt->dst.flags |= DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Changli Gaod8d1f302010-06-10 23:31:35 -07002507 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 return rt;
2510}
2511
Daniel Walterc3968a82011-04-13 21:10:57 +00002512int ip6_route_get_saddr(struct net *net,
2513 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002514 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002515 unsigned int prefs,
2516 struct in6_addr *saddr)
2517{
Markus Stenberge16e8882015-05-05 13:36:59 +03002518 struct inet6_dev *idev =
2519 rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
Daniel Walterc3968a82011-04-13 21:10:57 +00002520 int err = 0;
Markus Stenberge16e8882015-05-05 13:36:59 +03002521 if (rt && rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002522 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002523 else
2524 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2525 daddr, prefs, saddr);
2526 return err;
2527}
2528
2529/* remove deleted ip from prefsrc entries */
2530struct arg_dev_net_ip {
2531 struct net_device *dev;
2532 struct net *net;
2533 struct in6_addr *addr;
2534};
2535
2536static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2537{
2538 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2539 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2540 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2541
David S. Millerd1918542011-12-28 20:19:20 -05002542 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002543 rt != net->ipv6.ip6_null_entry &&
2544 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2545 /* remove prefsrc entry */
2546 rt->rt6i_prefsrc.plen = 0;
2547 }
2548 return 0;
2549}
2550
2551void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2552{
2553 struct net *net = dev_net(ifp->idev->dev);
2554 struct arg_dev_net_ip adni = {
2555 .dev = ifp->idev->dev,
2556 .net = net,
2557 .addr = &ifp->addr,
2558 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002559 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002560}
2561
Duan Jiongbe7a0102014-05-15 15:56:14 +08002562#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2563#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2564
2565/* Remove routers and update dst entries when gateway turn into host. */
2566static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2567{
2568 struct in6_addr *gateway = (struct in6_addr *)arg;
2569
2570 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2571 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2572 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2573 return -1;
2574 }
2575 return 0;
2576}
2577
2578void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2579{
2580 fib6_clean_all(net, fib6_clean_tohost, gateway);
2581}
2582
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002583struct arg_dev_net {
2584 struct net_device *dev;
2585 struct net *net;
2586};
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588static int fib6_ifdown(struct rt6_info *rt, void *arg)
2589{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002590 const struct arg_dev_net *adn = arg;
2591 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002592
David S. Millerd1918542011-12-28 20:19:20 -05002593 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002594 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002600void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002602 struct arg_dev_net adn = {
2603 .dev = dev,
2604 .net = net,
2605 };
2606
Li RongQing0c3584d2013-12-27 16:32:38 +08002607 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002608 icmp6_clean_all(fib6_ifdown, &adn);
Eric W. Biedermane332bc62015-10-12 11:02:08 -05002609 if (dev)
2610 rt6_uncached_list_flush_dev(net, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Eric Dumazet95c96172012-04-15 05:58:06 +00002613struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002615 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616};
2617
2618static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2619{
2620 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2621 struct inet6_dev *idev;
2622
2623 /* In IPv6 pmtu discovery is not optional,
2624 so that RTAX_MTU lock cannot disable it.
2625 We still use this lock to block changes
2626 caused by addrconf/ndisc.
2627 */
2628
2629 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002630 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return 0;
2632
2633 /* For administrative MTU increase, there is no way to discover
2634 IPv6 PMTU increase, so PMTU increase should be updated here.
2635 Since RFC 1981 doesn't include administrative MTU increase
2636 update PMTU increase is a MUST. (i.e. jumbo frame)
2637 */
2638 /*
2639 If new MTU is less than route PMTU, this new MTU will be the
2640 lowest MTU in the path, update the route PMTU to reflect PMTU
2641 decreases; if new MTU is greater than route PMTU, and the
2642 old MTU is the lowest MTU in the path, update the route PMTU
2643 to reflect the increase. In this case if the other nodes' MTU
2644 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2645 PMTU discouvery.
2646 */
David S. Millerd1918542011-12-28 20:19:20 -05002647 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002648 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2649 if (rt->rt6i_flags & RTF_CACHE) {
2650 /* For RTF_CACHE with rt6i_pmtu == 0
2651 * (i.e. a redirected route),
2652 * the metrics of its rt->dst.from has already
2653 * been updated.
2654 */
2655 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2656 rt->rt6i_pmtu = arg->mtu;
2657 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2658 (dst_mtu(&rt->dst) < arg->mtu &&
2659 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2660 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2661 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return 0;
2664}
2665
Eric Dumazet95c96172012-04-15 05:58:06 +00002666void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Thomas Grafc71099a2006-08-04 23:20:06 -07002668 struct rt6_mtu_change_arg arg = {
2669 .dev = dev,
2670 .mtu = mtu,
2671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Li RongQing0c3584d2013-12-27 16:32:38 +08002673 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002676static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002677 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002678 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002679 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002680 [RTA_PRIORITY] = { .type = NLA_U32 },
2681 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002682 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002683 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002684 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
2685 [RTA_ENCAP] = { .type = NLA_NESTED },
Thomas Graf86872cb2006-08-22 00:01:08 -07002686};
2687
2688static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2689 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Thomas Graf86872cb2006-08-22 00:01:08 -07002691 struct rtmsg *rtm;
2692 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002693 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002694 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Thomas Graf86872cb2006-08-22 00:01:08 -07002696 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2697 if (err < 0)
2698 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Thomas Graf86872cb2006-08-22 00:01:08 -07002700 err = -EINVAL;
2701 rtm = nlmsg_data(nlh);
2702 memset(cfg, 0, sizeof(*cfg));
2703
2704 cfg->fc_table = rtm->rtm_table;
2705 cfg->fc_dst_len = rtm->rtm_dst_len;
2706 cfg->fc_src_len = rtm->rtm_src_len;
2707 cfg->fc_flags = RTF_UP;
2708 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002709 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002710
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002711 if (rtm->rtm_type == RTN_UNREACHABLE ||
2712 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002713 rtm->rtm_type == RTN_PROHIBIT ||
2714 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002715 cfg->fc_flags |= RTF_REJECT;
2716
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002717 if (rtm->rtm_type == RTN_LOCAL)
2718 cfg->fc_flags |= RTF_LOCAL;
2719
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002720 if (rtm->rtm_flags & RTM_F_CLONED)
2721 cfg->fc_flags |= RTF_CACHE;
2722
Eric W. Biederman15e47302012-09-07 20:12:54 +00002723 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002724 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002725 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002726
2727 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002728 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002729 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002731
2732 if (tb[RTA_DST]) {
2733 int plen = (rtm->rtm_dst_len + 7) >> 3;
2734
2735 if (nla_len(tb[RTA_DST]) < plen)
2736 goto errout;
2737
2738 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002740
2741 if (tb[RTA_SRC]) {
2742 int plen = (rtm->rtm_src_len + 7) >> 3;
2743
2744 if (nla_len(tb[RTA_SRC]) < plen)
2745 goto errout;
2746
2747 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002749
Daniel Walterc3968a82011-04-13 21:10:57 +00002750 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002751 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002752
Thomas Graf86872cb2006-08-22 00:01:08 -07002753 if (tb[RTA_OIF])
2754 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2755
2756 if (tb[RTA_PRIORITY])
2757 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2758
2759 if (tb[RTA_METRICS]) {
2760 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2761 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002763
2764 if (tb[RTA_TABLE])
2765 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2766
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002767 if (tb[RTA_MULTIPATH]) {
2768 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2769 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2770 }
2771
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002772 if (tb[RTA_PREF]) {
2773 pref = nla_get_u8(tb[RTA_PREF]);
2774 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2775 pref != ICMPV6_ROUTER_PREF_HIGH)
2776 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2777 cfg->fc_flags |= RTF_PREF(pref);
2778 }
2779
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002780 if (tb[RTA_ENCAP])
2781 cfg->fc_encap = tb[RTA_ENCAP];
2782
2783 if (tb[RTA_ENCAP_TYPE])
2784 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
2785
Thomas Graf86872cb2006-08-22 00:01:08 -07002786 err = 0;
2787errout:
2788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002791struct rt6_nh {
2792 struct rt6_info *rt6_info;
2793 struct fib6_config r_cfg;
2794 struct mx6_config mxc;
2795 struct list_head next;
2796};
2797
2798static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
2799{
2800 struct rt6_nh *nh;
2801
2802 list_for_each_entry(nh, rt6_nh_list, next) {
2803 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6 nexthop %pI6 ifi %d\n",
2804 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
2805 nh->r_cfg.fc_ifindex);
2806 }
2807}
2808
2809static int ip6_route_info_append(struct list_head *rt6_nh_list,
2810 struct rt6_info *rt, struct fib6_config *r_cfg)
2811{
2812 struct rt6_nh *nh;
2813 struct rt6_info *rtnh;
2814 int err = -EEXIST;
2815
2816 list_for_each_entry(nh, rt6_nh_list, next) {
2817 /* check if rt6_info already exists */
2818 rtnh = nh->rt6_info;
2819
2820 if (rtnh->dst.dev == rt->dst.dev &&
2821 rtnh->rt6i_idev == rt->rt6i_idev &&
2822 ipv6_addr_equal(&rtnh->rt6i_gateway,
2823 &rt->rt6i_gateway))
2824 return err;
2825 }
2826
2827 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
2828 if (!nh)
2829 return -ENOMEM;
2830 nh->rt6_info = rt;
2831 err = ip6_convert_metrics(&nh->mxc, r_cfg);
2832 if (err) {
2833 kfree(nh);
2834 return err;
2835 }
2836 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
2837 list_add_tail(&nh->next, rt6_nh_list);
2838
2839 return 0;
2840}
2841
2842static int ip6_route_multipath_add(struct fib6_config *cfg)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002843{
2844 struct fib6_config r_cfg;
2845 struct rtnexthop *rtnh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002846 struct rt6_info *rt;
2847 struct rt6_nh *err_nh;
2848 struct rt6_nh *nh, *nh_safe;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002849 int remaining;
2850 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002851 int err = 1;
2852 int nhn = 0;
2853 int replace = (cfg->fc_nlinfo.nlh &&
2854 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
2855 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002856
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02002857 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002858 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002859
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002860 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
2861 * rt6_info structs per nexthop
2862 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002863 while (rtnh_ok(rtnh, remaining)) {
2864 memcpy(&r_cfg, cfg, sizeof(*cfg));
2865 if (rtnh->rtnh_ifindex)
2866 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2867
2868 attrlen = rtnh_attrlen(rtnh);
2869 if (attrlen > 0) {
2870 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2871
2872 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2873 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002874 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002875 r_cfg.fc_flags |= RTF_GATEWAY;
2876 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002877 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
2878 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
2879 if (nla)
2880 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002881 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002882
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002883 rt = ip6_route_info_create(&r_cfg);
2884 if (IS_ERR(rt)) {
2885 err = PTR_ERR(rt);
2886 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002887 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002888 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002889
2890 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002891 if (err) {
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002892 dst_free(&rt->dst);
2893 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002894 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002895
2896 rtnh = rtnh_next(rtnh, &remaining);
2897 }
2898
2899 err_nh = NULL;
2900 list_for_each_entry(nh, &rt6_nh_list, next) {
2901 err = __ip6_ins_rt(nh->rt6_info, &cfg->fc_nlinfo, &nh->mxc);
2902 /* nh->rt6_info is used or freed at this point, reset to NULL*/
2903 nh->rt6_info = NULL;
2904 if (err) {
2905 if (replace && nhn)
2906 ip6_print_replace_route_err(&rt6_nh_list);
2907 err_nh = nh;
2908 goto add_errout;
2909 }
2910
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002911 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02002912 * these flags after the first nexthop: if there is a collision,
2913 * we have already failed to add the first nexthop:
2914 * fib6_add_rt2node() has rejected it; when replacing, old
2915 * nexthops have been replaced by first new, the rest should
2916 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002917 */
Michal Kubeček27596472015-05-18 20:54:00 +02002918 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
2919 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002920 nhn++;
2921 }
2922
2923 goto cleanup;
2924
2925add_errout:
2926 /* Delete routes that were already added */
2927 list_for_each_entry(nh, &rt6_nh_list, next) {
2928 if (err_nh == nh)
2929 break;
2930 ip6_route_del(&nh->r_cfg);
2931 }
2932
2933cleanup:
2934 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
2935 if (nh->rt6_info)
2936 dst_free(&nh->rt6_info->dst);
Wu Fengguang52fe51f2015-09-10 06:57:12 +08002937 kfree(nh->mxc.mx);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002938 list_del(&nh->next);
2939 kfree(nh);
2940 }
2941
2942 return err;
2943}
2944
2945static int ip6_route_multipath_del(struct fib6_config *cfg)
2946{
2947 struct fib6_config r_cfg;
2948 struct rtnexthop *rtnh;
2949 int remaining;
2950 int attrlen;
2951 int err = 1, last_err = 0;
2952
2953 remaining = cfg->fc_mp_len;
2954 rtnh = (struct rtnexthop *)cfg->fc_mp;
2955
2956 /* Parse a Multipath Entry */
2957 while (rtnh_ok(rtnh, remaining)) {
2958 memcpy(&r_cfg, cfg, sizeof(*cfg));
2959 if (rtnh->rtnh_ifindex)
2960 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2961
2962 attrlen = rtnh_attrlen(rtnh);
2963 if (attrlen > 0) {
2964 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2965
2966 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2967 if (nla) {
2968 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2969 r_cfg.fc_flags |= RTF_GATEWAY;
2970 }
2971 }
2972 err = ip6_route_del(&r_cfg);
2973 if (err)
2974 last_err = err;
2975
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002976 rtnh = rtnh_next(rtnh, &remaining);
2977 }
2978
2979 return last_err;
2980}
2981
Ian Morris67ba4152014-08-24 21:53:10 +01002982static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Thomas Graf86872cb2006-08-22 00:01:08 -07002984 struct fib6_config cfg;
2985 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Thomas Graf86872cb2006-08-22 00:01:08 -07002987 err = rtm_to_fib6_config(skb, nlh, &cfg);
2988 if (err < 0)
2989 return err;
2990
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002991 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002992 return ip6_route_multipath_del(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002993 else
2994 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Ian Morris67ba4152014-08-24 21:53:10 +01002997static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Thomas Graf86872cb2006-08-22 00:01:08 -07002999 struct fib6_config cfg;
3000 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Thomas Graf86872cb2006-08-22 00:01:08 -07003002 err = rtm_to_fib6_config(skb, nlh, &cfg);
3003 if (err < 0)
3004 return err;
3005
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003006 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003007 return ip6_route_multipath_add(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003008 else
3009 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003012static inline size_t rt6_nlmsg_size(struct rt6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08003013{
3014 return NLMSG_ALIGN(sizeof(struct rtmsg))
3015 + nla_total_size(16) /* RTA_SRC */
3016 + nla_total_size(16) /* RTA_DST */
3017 + nla_total_size(16) /* RTA_GATEWAY */
3018 + nla_total_size(16) /* RTA_PREFSRC */
3019 + nla_total_size(4) /* RTA_TABLE */
3020 + nla_total_size(4) /* RTA_IIF */
3021 + nla_total_size(4) /* RTA_OIF */
3022 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08003023 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01003024 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003025 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003026 + nla_total_size(1) /* RTA_PREF */
Jiri Benc61adedf2015-08-20 13:56:25 +02003027 + lwtunnel_get_encap_size(rt->dst.lwtstate);
Thomas Graf339bf982006-11-10 14:10:15 -08003028}
3029
Brian Haley191cd582008-08-14 15:33:21 -07003030static int rt6_fill_node(struct net *net,
3031 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07003032 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003033 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003034 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003036 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003038 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08003039 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07003040 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 if (prefix) { /* user wants prefix routes only */
3043 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
3044 /* success since this is not a prefix route */
3045 return 1;
3046 }
3047 }
3048
Eric W. Biederman15e47302012-09-07 20:12:54 +00003049 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05003050 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08003051 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003052
3053 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 rtm->rtm_family = AF_INET6;
3055 rtm->rtm_dst_len = rt->rt6i_dst.plen;
3056 rtm->rtm_src_len = rt->rt6i_src.plen;
3057 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07003058 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07003059 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07003060 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07003061 table = RT6_TABLE_UNSPEC;
3062 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04003063 if (nla_put_u32(skb, RTA_TABLE, table))
3064 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003065 if (rt->rt6i_flags & RTF_REJECT) {
3066 switch (rt->dst.error) {
3067 case -EINVAL:
3068 rtm->rtm_type = RTN_BLACKHOLE;
3069 break;
3070 case -EACCES:
3071 rtm->rtm_type = RTN_PROHIBIT;
3072 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00003073 case -EAGAIN:
3074 rtm->rtm_type = RTN_THROW;
3075 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003076 default:
3077 rtm->rtm_type = RTN_UNREACHABLE;
3078 break;
3079 }
3080 }
David S. Miller38308472011-12-03 18:02:47 -05003081 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00003082 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05003083 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 rtm->rtm_type = RTN_LOCAL;
3085 else
3086 rtm->rtm_type = RTN_UNICAST;
3087 rtm->rtm_flags = 0;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003088 if (!netif_carrier_ok(rt->dst.dev)) {
Andy Gospodarekcea45e22015-08-13 10:39:00 -04003089 rtm->rtm_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003090 if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
3091 rtm->rtm_flags |= RTNH_F_DEAD;
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
3094 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05003095 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00003097 else if (rt->rt6i_flags & RTF_ADDRCONF) {
3098 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
3099 rtm->rtm_protocol = RTPROT_RA;
3100 else
3101 rtm->rtm_protocol = RTPROT_KERNEL;
3102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
David S. Miller38308472011-12-03 18:02:47 -05003104 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 rtm->rtm_flags |= RTM_F_CLONED;
3106
3107 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02003108 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04003109 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003110 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02003112 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003113 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114#ifdef CONFIG_IPV6_SUBTREES
3115 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02003116 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04003117 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003118 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04003119 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02003120 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003121 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003123 if (iif) {
3124#ifdef CONFIG_IPV6_MROUTE
3125 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08003126 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003127 if (err <= 0) {
3128 if (!nowait) {
3129 if (err == 0)
3130 return 0;
3131 goto nla_put_failure;
3132 } else {
3133 if (err == -EMSGSIZE)
3134 goto nla_put_failure;
3135 }
3136 }
3137 } else
3138#endif
David S. Millerc78679e2012-04-01 20:27:33 -04003139 if (nla_put_u32(skb, RTA_IIF, iif))
3140 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003141 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04003143 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02003144 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003145 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003147
Daniel Walterc3968a82011-04-13 21:10:57 +00003148 if (rt->rt6i_prefsrc.plen) {
3149 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003150 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02003151 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003152 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00003153 }
3154
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003155 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
3156 if (rt->rt6i_pmtu)
3157 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
3158 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07003159 goto nla_put_failure;
3160
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00003161 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02003162 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00003163 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00003164 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003165
David S. Millerc78679e2012-04-01 20:27:33 -04003166 if (rt->dst.dev &&
3167 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
3168 goto nla_put_failure;
3169 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
3170 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00003171
3172 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07003173
David S. Miller87a50692012-07-10 05:06:14 -07003174 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08003175 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003177 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
3178 goto nla_put_failure;
3179
Jiri Benc61adedf2015-08-20 13:56:25 +02003180 lwtunnel_fill_encap(skb, rt->dst.lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003181
Johannes Berg053c0952015-01-16 22:09:00 +01003182 nlmsg_end(skb, nlh);
3183 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003184
3185nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003186 nlmsg_cancel(skb, nlh);
3187 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Patrick McHardy1b43af52006-08-10 23:11:17 -07003190int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
3193 int prefix;
3194
Thomas Graf2d7202b2006-08-22 00:01:27 -07003195 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
3196 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
3198 } else
3199 prefix = 0;
3200
Brian Haley191cd582008-08-14 15:33:21 -07003201 return rt6_fill_node(arg->net,
3202 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003203 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003204 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Ian Morris67ba4152014-08-24 21:53:10 +01003207static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003209 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07003210 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07003212 struct sk_buff *skb;
3213 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05003214 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003215 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07003216
3217 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
3218 if (err < 0)
3219 goto errout;
3220
3221 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05003222 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07003223
3224 if (tb[RTA_SRC]) {
3225 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
3226 goto errout;
3227
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003228 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07003229 }
3230
3231 if (tb[RTA_DST]) {
3232 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
3233 goto errout;
3234
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003235 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07003236 }
3237
3238 if (tb[RTA_IIF])
3239 iif = nla_get_u32(tb[RTA_IIF]);
3240
3241 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003242 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07003243
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07003244 if (tb[RTA_MARK])
3245 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
3246
Thomas Grafab364a62006-08-22 00:01:47 -07003247 if (iif) {
3248 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003249 int flags = 0;
3250
Daniel Lezcano55786892008-03-04 13:47:47 -08003251 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07003252 if (!dev) {
3253 err = -ENODEV;
3254 goto errout;
3255 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003256
3257 fl6.flowi6_iif = iif;
3258
3259 if (!ipv6_addr_any(&fl6.saddr))
3260 flags |= RT6_LOOKUP_F_HAS_SADDR;
3261
3262 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
3263 flags);
3264 } else {
3265 fl6.flowi6_oif = oif;
3266
David Ahernca254492015-10-12 11:47:10 -07003267 if (netif_index_is_l3_master(net, oif)) {
3268 fl6.flowi6_flags = FLOWI_FLAG_L3MDEV_SRC |
3269 FLOWI_FLAG_SKIP_NH_OIF;
3270 }
3271
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003272 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05003276 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00003277 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07003278 err = -ENOBUFS;
3279 goto errout;
3280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 /* Reserve room for dummy headers, this skb can pass
3283 through good chunk of routing engine.
3284 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07003285 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
3287
Changli Gaod8d1f302010-06-10 23:31:35 -07003288 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
David S. Miller4c9483b2011-03-12 16:22:43 -05003290 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003291 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003292 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07003294 kfree_skb(skb);
3295 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
Eric W. Biederman15e47302012-09-07 20:12:54 +00003298 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07003299errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003303void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
3304 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08003307 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003308 u32 seq;
3309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003311 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003312 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07003313
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003314 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05003315 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07003316 goto errout;
3317
Brian Haley191cd582008-08-14 15:33:21 -07003318 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003319 event, info->portid, seq, 0, 0, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003320 if (err < 0) {
3321 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
3322 WARN_ON(err == -EMSGSIZE);
3323 kfree_skb(skb);
3324 goto errout;
3325 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00003326 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003327 info->nlh, gfp_any());
3328 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07003329errout:
3330 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08003331 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003334static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00003335 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003336{
Jiri Pirko351638e2013-05-28 01:30:21 +00003337 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003338 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003339
3340 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003341 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003342 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
3343#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003344 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003345 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003346 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003347 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
3348#endif
3349 }
3350
3351 return NOTIFY_OK;
3352}
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354/*
3355 * /proc
3356 */
3357
3358#ifdef CONFIG_PROC_FS
3359
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003360static const struct file_operations ipv6_route_proc_fops = {
3361 .owner = THIS_MODULE,
3362 .open = ipv6_route_open,
3363 .read = seq_read,
3364 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02003365 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003366};
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368static int rt6_stats_seq_show(struct seq_file *seq, void *v)
3369{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003370 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003372 net->ipv6.rt6_stats->fib_nodes,
3373 net->ipv6.rt6_stats->fib_route_nodes,
3374 net->ipv6.rt6_stats->fib_rt_alloc,
3375 net->ipv6.rt6_stats->fib_rt_entries,
3376 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00003377 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003378 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 return 0;
3381}
3382
3383static int rt6_stats_seq_open(struct inode *inode, struct file *file)
3384{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07003385 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003386}
3387
Arjan van de Ven9a321442007-02-12 00:55:35 -08003388static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 .owner = THIS_MODULE,
3390 .open = rt6_stats_seq_open,
3391 .read = seq_read,
3392 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07003393 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394};
3395#endif /* CONFIG_PROC_FS */
3396
3397#ifdef CONFIG_SYSCTL
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399static
Joe Perchesfe2c6332013-06-11 23:04:25 -07003400int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 void __user *buffer, size_t *lenp, loff_t *ppos)
3402{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003403 struct net *net;
3404 int delay;
3405 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003407
3408 net = (struct net *)ctl->extra1;
3409 delay = net->ipv6.sysctl.flush_delay;
3410 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02003411 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Joe Perchesfe2c6332013-06-11 23:04:25 -07003415struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003416 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08003418 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07003420 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003421 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 },
3423 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003425 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 .maxlen = sizeof(int),
3427 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003428 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 },
3430 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08003432 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 .maxlen = sizeof(int),
3434 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003435 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 },
3437 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003439 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 .maxlen = sizeof(int),
3441 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003442 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 },
3444 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003446 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 .maxlen = sizeof(int),
3448 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003449 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 },
3451 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003453 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 .maxlen = sizeof(int),
3455 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003456 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 },
3458 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003460 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 .maxlen = sizeof(int),
3462 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003463 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 },
3465 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003467 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 .maxlen = sizeof(int),
3469 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003470 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 },
3472 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003474 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 .maxlen = sizeof(int),
3476 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003477 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 },
3479 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003481 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 .maxlen = sizeof(int),
3483 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003484 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003486 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487};
3488
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003489struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003490{
3491 struct ctl_table *table;
3492
3493 table = kmemdup(ipv6_route_table_template,
3494 sizeof(ipv6_route_table_template),
3495 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003496
3497 if (table) {
3498 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003499 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003500 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003501 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3502 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3503 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3504 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3505 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3506 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3507 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003508 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003509
3510 /* Don't export sysctls to unprivileged users */
3511 if (net->user_ns != &init_user_ns)
3512 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003513 }
3514
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003515 return table;
3516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517#endif
3518
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003519static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003520{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003521 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003522
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003523 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3524 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003525
Eric Dumazetfc66f952010-10-08 06:37:34 +00003526 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3527 goto out_ip6_dst_ops;
3528
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003529 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3530 sizeof(*net->ipv6.ip6_null_entry),
3531 GFP_KERNEL);
3532 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003533 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003534 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003535 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003536 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003537 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3538 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003539
3540#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3541 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3542 sizeof(*net->ipv6.ip6_prohibit_entry),
3543 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003544 if (!net->ipv6.ip6_prohibit_entry)
3545 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003546 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003547 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003548 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003549 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3550 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003551
3552 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3553 sizeof(*net->ipv6.ip6_blk_hole_entry),
3554 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003555 if (!net->ipv6.ip6_blk_hole_entry)
3556 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003557 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003558 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003559 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003560 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3561 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003562#endif
3563
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003564 net->ipv6.sysctl.flush_delay = 0;
3565 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3566 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3567 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3568 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3569 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3570 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3571 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3572
Benjamin Thery6891a342008-03-04 13:49:47 -08003573 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3574
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003575 ret = 0;
3576out:
3577 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003578
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003579#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3580out_ip6_prohibit_entry:
3581 kfree(net->ipv6.ip6_prohibit_entry);
3582out_ip6_null_entry:
3583 kfree(net->ipv6.ip6_null_entry);
3584#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003585out_ip6_dst_entries:
3586 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003587out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003588 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003589}
3590
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003591static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003592{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003593 kfree(net->ipv6.ip6_null_entry);
3594#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3595 kfree(net->ipv6.ip6_prohibit_entry);
3596 kfree(net->ipv6.ip6_blk_hole_entry);
3597#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003598 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003599}
3600
Thomas Grafd1896342012-06-18 12:08:33 +00003601static int __net_init ip6_route_net_init_late(struct net *net)
3602{
3603#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003604 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3605 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003606#endif
3607 return 0;
3608}
3609
3610static void __net_exit ip6_route_net_exit_late(struct net *net)
3611{
3612#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003613 remove_proc_entry("ipv6_route", net->proc_net);
3614 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003615#endif
3616}
3617
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003618static struct pernet_operations ip6_route_net_ops = {
3619 .init = ip6_route_net_init,
3620 .exit = ip6_route_net_exit,
3621};
3622
David S. Millerc3426b42012-06-09 16:27:05 -07003623static int __net_init ipv6_inetpeer_init(struct net *net)
3624{
3625 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3626
3627 if (!bp)
3628 return -ENOMEM;
3629 inet_peer_base_init(bp);
3630 net->ipv6.peers = bp;
3631 return 0;
3632}
3633
3634static void __net_exit ipv6_inetpeer_exit(struct net *net)
3635{
3636 struct inet_peer_base *bp = net->ipv6.peers;
3637
3638 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003639 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003640 kfree(bp);
3641}
3642
David S. Miller2b823f72012-06-09 19:00:16 -07003643static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003644 .init = ipv6_inetpeer_init,
3645 .exit = ipv6_inetpeer_exit,
3646};
3647
Thomas Grafd1896342012-06-18 12:08:33 +00003648static struct pernet_operations ip6_route_net_late_ops = {
3649 .init = ip6_route_net_init_late,
3650 .exit = ip6_route_net_exit_late,
3651};
3652
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003653static struct notifier_block ip6_route_dev_notifier = {
3654 .notifier_call = ip6_route_dev_notify,
3655 .priority = 0,
3656};
3657
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003658int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003660 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003661 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003662
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003663 ret = -ENOMEM;
3664 ip6_dst_ops_template.kmem_cachep =
3665 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3666 SLAB_HWCACHE_ALIGN, NULL);
3667 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003668 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003669
Eric Dumazetfc66f952010-10-08 06:37:34 +00003670 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003671 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003672 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003673
David S. Millerc3426b42012-06-09 16:27:05 -07003674 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3675 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003676 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003677
David S. Miller7e52b332012-06-15 15:51:55 -07003678 ret = register_pernet_subsys(&ip6_route_net_ops);
3679 if (ret)
3680 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003681
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003682 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3683
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003684 /* Registering of the loopback is done before this portion of code,
3685 * the loopback reference in rt6_info will not be taken, do it
3686 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003687 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003688 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3689 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003690 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003691 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003692 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003693 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3694 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003695 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003696 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003697 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003698
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003699 ret = xfrm6_init();
3700 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003701 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003702
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003703 ret = fib6_rules_init();
3704 if (ret)
3705 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003706
Thomas Grafd1896342012-06-18 12:08:33 +00003707 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3708 if (ret)
3709 goto fib6_rules_init;
3710
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003711 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003712 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3713 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3714 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003715 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003716
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003717 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003718 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003719 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003720
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003721 for_each_possible_cpu(cpu) {
3722 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
3723
3724 INIT_LIST_HEAD(&ul->head);
3725 spin_lock_init(&ul->lock);
3726 }
3727
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003728out:
3729 return ret;
3730
Thomas Grafd1896342012-06-18 12:08:33 +00003731out_register_late_subsys:
3732 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003733fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003734 fib6_rules_cleanup();
3735xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003736 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003737out_fib6_init:
3738 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003739out_register_subsys:
3740 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003741out_register_inetpeer:
3742 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003743out_dst_entries:
3744 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003745out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003746 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750void ip6_route_cleanup(void)
3751{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003752 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003753 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003754 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003757 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003758 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003759 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003760 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}