blob: 74907c58a5c76f3c39c0d6f460533d0e1fb79fe5 [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
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08001325static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
1326{
1327 return !(rt->rt6i_flags & RTF_CACHE) &&
1328 (rt->rt6i_flags & RTF_PCPU || rt->rt6i_node);
1329}
1330
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001331static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
1332 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Ian Morris67ba4152014-08-24 21:53:10 +01001334 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001336 if (rt6->rt6i_flags & RTF_LOCAL)
1337 return;
1338
David S. Miller81aded22012-06-15 14:54:11 -07001339 dst_confirm(dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001340 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
1341 if (mtu >= dst_mtu(dst))
1342 return;
David S. Miller81aded22012-06-15 14:54:11 -07001343
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08001344 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001345 rt6_do_update_pmtu(rt6, mtu);
1346 } else {
1347 const struct in6_addr *daddr, *saddr;
1348 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001349
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001350 if (iph) {
1351 daddr = &iph->daddr;
1352 saddr = &iph->saddr;
1353 } else if (sk) {
1354 daddr = &sk->sk_v6_daddr;
1355 saddr = &inet6_sk(sk)->saddr;
1356 } else {
1357 return;
1358 }
1359 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
1360 if (nrt6) {
1361 rt6_do_update_pmtu(nrt6, mtu);
1362
1363 /* ip6_ins_rt(nrt6) will bump the
1364 * rt6->rt6i_node->fn_sernum
1365 * which will fail the next rt6_check() and
1366 * invalidate the sk->sk_dst_cache.
1367 */
1368 ip6_ins_rt(nrt6);
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371}
1372
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001373static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1374 struct sk_buff *skb, u32 mtu)
1375{
1376 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
1377}
1378
David S. Miller42ae66c2012-06-15 20:01:57 -07001379void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1380 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001381{
1382 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1383 struct dst_entry *dst;
1384 struct flowi6 fl6;
1385
1386 memset(&fl6, 0, sizeof(fl6));
1387 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001388 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001389 fl6.daddr = iph->daddr;
1390 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001391 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001392
1393 dst = ip6_route_output(net, NULL, &fl6);
1394 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001395 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001396 dst_release(dst);
1397}
1398EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1399
1400void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1401{
1402 ip6_update_pmtu(skb, sock_net(sk), mtu,
1403 sk->sk_bound_dev_if, sk->sk_mark);
1404}
1405EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1406
Duan Jiongb55b76b2013-09-04 19:44:21 +08001407/* Handle redirects */
1408struct ip6rd_flowi {
1409 struct flowi6 fl6;
1410 struct in6_addr gateway;
1411};
1412
1413static struct rt6_info *__ip6_route_redirect(struct net *net,
1414 struct fib6_table *table,
1415 struct flowi6 *fl6,
1416 int flags)
1417{
1418 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1419 struct rt6_info *rt;
1420 struct fib6_node *fn;
1421
1422 /* Get the "current" route for this destination and
1423 * check if the redirect has come from approriate router.
1424 *
1425 * RFC 4861 specifies that redirects should only be
1426 * accepted if they come from the nexthop to the target.
1427 * Due to the way the routes are chosen, this notion
1428 * is a bit fuzzy and one might need to check all possible
1429 * routes.
1430 */
1431
1432 read_lock_bh(&table->tb6_lock);
1433 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1434restart:
1435 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1436 if (rt6_check_expired(rt))
1437 continue;
1438 if (rt->dst.error)
1439 break;
1440 if (!(rt->rt6i_flags & RTF_GATEWAY))
1441 continue;
1442 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1443 continue;
1444 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1445 continue;
1446 break;
1447 }
1448
1449 if (!rt)
1450 rt = net->ipv6.ip6_null_entry;
1451 else if (rt->dst.error) {
1452 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001453 goto out;
1454 }
1455
1456 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001457 fn = fib6_backtrack(fn, &fl6->saddr);
1458 if (fn)
1459 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001460 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001461
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001462out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001463 dst_hold(&rt->dst);
1464
1465 read_unlock_bh(&table->tb6_lock);
1466
1467 return rt;
1468};
1469
1470static struct dst_entry *ip6_route_redirect(struct net *net,
1471 const struct flowi6 *fl6,
1472 const struct in6_addr *gateway)
1473{
1474 int flags = RT6_LOOKUP_F_HAS_SADDR;
1475 struct ip6rd_flowi rdfl;
1476
1477 rdfl.fl6 = *fl6;
1478 rdfl.gateway = *gateway;
1479
1480 return fib6_rule_lookup(net, &rdfl.fl6,
1481 flags, __ip6_route_redirect);
1482}
1483
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001484void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1485{
1486 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1487 struct dst_entry *dst;
1488 struct flowi6 fl6;
1489
1490 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001491 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001492 fl6.flowi6_oif = oif;
1493 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001494 fl6.daddr = iph->daddr;
1495 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001496 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001497
Duan Jiongb55b76b2013-09-04 19:44:21 +08001498 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1499 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001500 dst_release(dst);
1501}
1502EXPORT_SYMBOL_GPL(ip6_redirect);
1503
Duan Jiongc92a59e2013-08-22 12:07:35 +08001504void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1505 u32 mark)
1506{
1507 const struct ipv6hdr *iph = ipv6_hdr(skb);
1508 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1509 struct dst_entry *dst;
1510 struct flowi6 fl6;
1511
1512 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001513 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001514 fl6.flowi6_oif = oif;
1515 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001516 fl6.daddr = msg->dest;
1517 fl6.saddr = iph->daddr;
1518
Duan Jiongb55b76b2013-09-04 19:44:21 +08001519 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1520 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001521 dst_release(dst);
1522}
1523
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001524void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1525{
1526 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1527}
1528EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1529
David S. Miller0dbaee32010-12-13 12:52:14 -08001530static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
David S. Miller0dbaee32010-12-13 12:52:14 -08001532 struct net_device *dev = dst->dev;
1533 unsigned int mtu = dst_mtu(dst);
1534 struct net *net = dev_net(dev);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1537
Daniel Lezcano55786892008-03-04 13:47:47 -08001538 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1539 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001542 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1543 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1544 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * rely only on pmtu discovery"
1546 */
1547 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1548 mtu = IPV6_MAXPLEN;
1549 return mtu;
1550}
1551
Steffen Klassertebb762f2011-11-23 02:12:51 +00001552static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001553{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001554 const struct rt6_info *rt = (const struct rt6_info *)dst;
1555 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001556 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001557
1558 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001559 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001560
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001561 mtu = dst_metric_raw(dst, RTAX_MTU);
1562 if (mtu)
1563 goto out;
1564
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001565 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001566
1567 rcu_read_lock();
1568 idev = __in6_dev_get(dst->dev);
1569 if (idev)
1570 mtu = idev->cnf.mtu6;
1571 rcu_read_unlock();
1572
Eric Dumazet30f78d82014-04-10 21:23:36 -07001573out:
1574 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001575}
1576
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001577static struct dst_entry *icmp6_dst_gc_list;
1578static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001579
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001580struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001581 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
David S. Miller87a11572011-12-06 17:04:13 -05001583 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct rt6_info *rt;
1585 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001586 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
David S. Miller38308472011-12-03 18:02:47 -05001588 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001589 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Martin KaFai Lauad706862015-08-14 11:05:52 -07001591 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05001592 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001594 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto out;
1596 }
1597
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001598 rt->dst.flags |= DST_HOST;
1599 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001600 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001601 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001602 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001603 rt->rt6i_dst.plen = 128;
1604 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001605 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001607 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001608 rt->dst.next = icmp6_dst_gc_list;
1609 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001610 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Daniel Lezcano55786892008-03-04 13:47:47 -08001612 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
David S. Miller87a11572011-12-06 17:04:13 -05001614 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616out:
David S. Miller87a11572011-12-06 17:04:13 -05001617 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001620int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001622 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001623 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001625 spin_lock_bh(&icmp6_dst_lock);
1626 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 while ((dst = *pprev) != NULL) {
1629 if (!atomic_read(&dst->__refcnt)) {
1630 *pprev = dst->next;
1631 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 } else {
1633 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001634 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 }
1637
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001638 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001639
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001640 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
David S. Miller1e493d12008-09-10 17:27:15 -07001643static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1644 void *arg)
1645{
1646 struct dst_entry *dst, **pprev;
1647
1648 spin_lock_bh(&icmp6_dst_lock);
1649 pprev = &icmp6_dst_gc_list;
1650 while ((dst = *pprev) != NULL) {
1651 struct rt6_info *rt = (struct rt6_info *) dst;
1652 if (func(rt, arg)) {
1653 *pprev = dst->next;
1654 dst_free(dst);
1655 } else {
1656 pprev = &dst->next;
1657 }
1658 }
1659 spin_unlock_bh(&icmp6_dst_lock);
1660}
1661
Daniel Lezcano569d3642008-01-18 03:56:57 -08001662static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001664 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001665 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1666 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1667 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1668 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1669 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001670 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Eric Dumazetfc66f952010-10-08 06:37:34 +00001672 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001673 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001674 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto out;
1676
Benjamin Thery6891a342008-03-04 13:49:47 -08001677 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001678 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001679 entries = dst_entries_get_slow(ops);
1680 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001681 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001683 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001684 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Florian Westphale715b6d2015-01-05 23:57:44 +01001687static int ip6_convert_metrics(struct mx6_config *mxc,
1688 const struct fib6_config *cfg)
1689{
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001690 bool ecn_ca = false;
Florian Westphale715b6d2015-01-05 23:57:44 +01001691 struct nlattr *nla;
1692 int remaining;
1693 u32 *mp;
1694
Ian Morris63159f22015-03-29 14:00:04 +01001695 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001696 return 0;
1697
1698 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1699 if (unlikely(!mp))
1700 return -ENOMEM;
1701
1702 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1703 int type = nla_type(nla);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001704 u32 val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001705
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001706 if (!type)
1707 continue;
1708 if (unlikely(type > RTAX_MAX))
1709 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001710
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001711 if (type == RTAX_CC_ALGO) {
1712 char tmp[TCP_CA_NAME_MAX];
1713
1714 nla_strlcpy(tmp, nla, sizeof(tmp));
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001715 val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001716 if (val == TCP_CA_UNSPEC)
Florian Westphale715b6d2015-01-05 23:57:44 +01001717 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001718 } else {
1719 val = nla_get_u32(nla);
Florian Westphale715b6d2015-01-05 23:57:44 +01001720 }
Daniel Borkmannb8d3e412015-08-31 15:58:46 +02001721 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
1722 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001723
1724 mp[type - 1] = val;
1725 __set_bit(type - 1, mxc->mx_valid);
Florian Westphale715b6d2015-01-05 23:57:44 +01001726 }
1727
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001728 if (ecn_ca) {
1729 __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
1730 mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
1731 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001732
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001733 mxc->mx = mp;
Florian Westphale715b6d2015-01-05 23:57:44 +01001734 return 0;
1735 err:
1736 kfree(mp);
1737 return -EINVAL;
1738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001740static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Daniel Lezcano55786892008-03-04 13:47:47 -08001742 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct rt6_info *rt = NULL;
1744 struct net_device *dev = NULL;
1745 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001746 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001748 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Thomas Graf86872cb2006-08-22 00:01:08 -07001750 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001751 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001753 if (cfg->fc_src_len)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001756 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001758 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (!dev)
1760 goto out;
1761 idev = in6_dev_get(dev);
1762 if (!idev)
1763 goto out;
1764 }
1765
Thomas Graf86872cb2006-08-22 00:01:08 -07001766 if (cfg->fc_metric == 0)
1767 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Matti Vaittinend71314b2011-11-14 00:14:49 +00001769 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001770 if (cfg->fc_nlinfo.nlh &&
1771 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001772 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001773 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001774 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001775 table = fib6_new_table(net, cfg->fc_table);
1776 }
1777 } else {
1778 table = fib6_new_table(net, cfg->fc_table);
1779 }
David S. Miller38308472011-12-03 18:02:47 -05001780
1781 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001782 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001783
Martin KaFai Lauad706862015-08-14 11:05:52 -07001784 rt = ip6_dst_alloc(net, NULL,
1785 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
David S. Miller38308472011-12-03 18:02:47 -05001787 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 err = -ENOMEM;
1789 goto out;
1790 }
1791
Gao feng1716a962012-04-06 00:13:10 +00001792 if (cfg->fc_flags & RTF_EXPIRES)
1793 rt6_set_expires(rt, jiffies +
1794 clock_t_to_jiffies(cfg->fc_expires));
1795 else
1796 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 if (cfg->fc_protocol == RTPROT_UNSPEC)
1799 cfg->fc_protocol = RTPROT_BOOT;
1800 rt->rt6i_protocol = cfg->fc_protocol;
1801
1802 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001805 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001806 else if (cfg->fc_flags & RTF_LOCAL)
1807 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001809 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Changli Gaod8d1f302010-06-10 23:31:35 -07001811 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001813 if (cfg->fc_encap) {
1814 struct lwtunnel_state *lwtstate;
1815
1816 err = lwtunnel_build_state(dev, cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07001817 cfg->fc_encap, AF_INET6, cfg,
1818 &lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001819 if (err)
1820 goto out;
Jiri Benc61adedf2015-08-20 13:56:25 +02001821 rt->dst.lwtstate = lwtstate_get(lwtstate);
1822 if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
1823 rt->dst.lwtstate->orig_output = rt->dst.output;
1824 rt->dst.output = lwtunnel_output;
Tom Herbert25368622015-08-17 13:42:24 -07001825 }
Jiri Benc61adedf2015-08-20 13:56:25 +02001826 if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
1827 rt->dst.lwtstate->orig_input = rt->dst.input;
1828 rt->dst.input = lwtunnel_input;
Tom Herbert25368622015-08-17 13:42:24 -07001829 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001830 }
1831
Thomas Graf86872cb2006-08-22 00:01:08 -07001832 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1833 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07001834 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001835 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001838 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1839 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840#endif
1841
Thomas Graf86872cb2006-08-22 00:01:08 -07001842 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* We cannot add true routes via loopback here,
1845 they would result in kernel looping; promote them to reject routes
1846 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001847 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001848 (dev && (dev->flags & IFF_LOOPBACK) &&
1849 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1850 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001852 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (dev) {
1854 dev_put(dev);
1855 in6_dev_put(idev);
1856 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001857 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 dev_hold(dev);
1859 idev = in6_dev_get(dev);
1860 if (!idev) {
1861 err = -ENODEV;
1862 goto out;
1863 }
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001866 switch (cfg->fc_type) {
1867 case RTN_BLACKHOLE:
1868 rt->dst.error = -EINVAL;
Eric W. Biedermanede20592015-10-07 16:48:47 -05001869 rt->dst.output = dst_discard_out;
Kamala R7150aed2013-12-02 19:55:21 +05301870 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001871 break;
1872 case RTN_PROHIBIT:
1873 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301874 rt->dst.output = ip6_pkt_prohibit_out;
1875 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001876 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001877 case RTN_THROW:
Nikola Forró0315e382015-09-17 16:01:32 +02001878 case RTN_UNREACHABLE:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001879 default:
Kamala R7150aed2013-12-02 19:55:21 +05301880 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
Nikola Forró0315e382015-09-17 16:01:32 +02001881 : (cfg->fc_type == RTN_UNREACHABLE)
1882 ? -EHOSTUNREACH : -ENETUNREACH;
Kamala R7150aed2013-12-02 19:55:21 +05301883 rt->dst.output = ip6_pkt_discard_out;
1884 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001885 break;
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto install_route;
1888 }
1889
Thomas Graf86872cb2006-08-22 00:01:08 -07001890 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001891 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int gwa_type;
1893
Thomas Graf86872cb2006-08-22 00:01:08 -07001894 gw_addr = &cfg->fc_gateway;
Florian Westphal330567b2015-08-07 10:54:28 +02001895 gwa_type = ipv6_addr_type(gw_addr);
Florian Westphal48ed7b22015-05-21 00:25:41 +02001896
1897 /* if gw_addr is local we will fail to detect this in case
1898 * address is still TENTATIVE (DAD in progress). rt6_lookup()
1899 * will return already-added prefix route via interface that
1900 * prefix route was assigned to, which might be non-loopback.
1901 */
1902 err = -EINVAL;
Florian Westphal330567b2015-08-07 10:54:28 +02001903 if (ipv6_chk_addr_and_flags(net, gw_addr,
1904 gwa_type & IPV6_ADDR_LINKLOCAL ?
1905 dev : NULL, 0, 0))
Florian Westphal48ed7b22015-05-21 00:25:41 +02001906 goto out;
1907
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001908 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1911 struct rt6_info *grt;
1912
1913 /* IPv6 strictly inhibits using not link-local
1914 addresses as nexthop address.
1915 Otherwise, router will not able to send redirects.
1916 It is very good, but in some (rare!) circumstances
1917 (SIT, PtP, NBMA NOARP links) it is handy to allow
1918 some exceptions. --ANK
1919 */
David S. Miller38308472011-12-03 18:02:47 -05001920 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto out;
1922
Daniel Lezcano55786892008-03-04 13:47:47 -08001923 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001926 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto out;
1928 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001929 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001930 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 goto out;
1932 }
1933 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001934 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 idev = grt->rt6i_idev;
1936 dev_hold(dev);
1937 in6_dev_hold(grt->rt6i_idev);
1938 }
David S. Miller38308472011-12-03 18:02:47 -05001939 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001941 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 if (err)
1944 goto out;
1945 }
1946 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001947 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto out;
1949 }
1950
1951 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001952 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto out;
1954
Daniel Walterc3968a82011-04-13 21:10:57 +00001955 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1956 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1957 err = -EINVAL;
1958 goto out;
1959 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001960 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001961 rt->rt6i_prefsrc.plen = 128;
1962 } else
1963 rt->rt6i_prefsrc.plen = 0;
1964
Thomas Graf86872cb2006-08-22 00:01:08 -07001965 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001968 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001970 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001971
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001972 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001973
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001974 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975out:
1976 if (dev)
1977 dev_put(dev);
1978 if (idev)
1979 in6_dev_put(idev);
1980 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001981 dst_free(&rt->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001982
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001983 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001984}
1985
1986int ip6_route_add(struct fib6_config *cfg)
1987{
1988 struct mx6_config mxc = { .mx = NULL, };
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001989 struct rt6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001990 int err;
1991
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001992 rt = ip6_route_info_create(cfg);
1993 if (IS_ERR(rt)) {
1994 err = PTR_ERR(rt);
1995 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001996 goto out;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001997 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001998
1999 err = ip6_convert_metrics(&mxc, cfg);
2000 if (err)
2001 goto out;
2002
2003 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
2004
2005 kfree(mxc.mx);
2006
2007 return err;
2008out:
2009 if (rt)
2010 dst_free(&rt->dst);
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return err;
2013}
2014
Thomas Graf86872cb2006-08-22 00:01:08 -07002015static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07002018 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05002019 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002021 if (rt == net->ipv6.ip6_null_entry ||
2022 rt->dst.flags & DST_NOCACHE) {
Gao feng6825a262012-09-19 19:25:34 +00002023 err = -ENOENT;
2024 goto out;
2025 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07002026
Thomas Grafc71099a2006-08-04 23:20:06 -07002027 table = rt->rt6i_table;
2028 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07002029 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07002030 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Gao feng6825a262012-09-19 19:25:34 +00002032out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00002033 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return err;
2035}
2036
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002037int ip6_del_rt(struct rt6_info *rt)
2038{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002039 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05002040 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002041 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002042 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002043}
2044
Thomas Graf86872cb2006-08-22 00:01:08 -07002045static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Thomas Grafc71099a2006-08-04 23:20:06 -07002047 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct fib6_node *fn;
2049 struct rt6_info *rt;
2050 int err = -ESRCH;
2051
Daniel Lezcano55786892008-03-04 13:47:47 -08002052 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002053 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Thomas Grafc71099a2006-08-04 23:20:06 -07002056 read_lock_bh(&table->tb6_lock);
2057
2058 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07002059 &cfg->fc_dst, cfg->fc_dst_len,
2060 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002063 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002064 if ((rt->rt6i_flags & RTF_CACHE) &&
2065 !(cfg->fc_flags & RTF_CACHE))
2066 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002067 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05002068 (!rt->dst.dev ||
2069 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002071 if (cfg->fc_flags & RTF_GATEWAY &&
2072 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002074 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002076 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002077 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Thomas Graf86872cb2006-08-22 00:01:08 -07002079 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002082 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 return err;
2085}
2086
David S. Miller6700c272012-07-17 03:29:28 -07002087static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002088{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002089 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07002090 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002091 struct ndisc_options ndopts;
2092 struct inet6_dev *in6_dev;
2093 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002094 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07002095 int optlen, on_link;
2096 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07002097
Simon Horman29a3cad2013-05-28 20:34:26 +00002098 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002099 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07002100
2101 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07002102 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002103 return;
2104 }
2105
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002106 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07002107
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002108 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002109 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002110 return;
2111 }
2112
David S. Miller6e157b62012-07-12 00:05:02 -07002113 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002114 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002115 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002116 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07002117 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002118 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002119 return;
2120 }
2121
2122 in6_dev = __in6_dev_get(skb->dev);
2123 if (!in6_dev)
2124 return;
2125 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2126 return;
2127
2128 /* RFC2461 8.1:
2129 * The IP source address of the Redirect MUST be the same as the current
2130 * first-hop router for the specified ICMP Destination Address.
2131 */
2132
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002133 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002134 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2135 return;
2136 }
David S. Miller6e157b62012-07-12 00:05:02 -07002137
2138 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002139 if (ndopts.nd_opts_tgt_lladdr) {
2140 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
2141 skb->dev);
2142 if (!lladdr) {
2143 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
2144 return;
2145 }
2146 }
2147
David S. Miller6e157b62012-07-12 00:05:02 -07002148 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01002149 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07002150 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
2151 return;
2152 }
2153
2154 /* Redirect received -> path was valid.
2155 * Look, redirects are sent only in response to data packets,
2156 * so that this nexthop apparently is reachable. --ANK
2157 */
2158 dst_confirm(&rt->dst);
2159
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002160 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07002161 if (!neigh)
2162 return;
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 /*
2165 * We have finally decided to accept it.
2166 */
2167
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002168 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 NEIGH_UPDATE_F_WEAK_OVERRIDE|
2170 NEIGH_UPDATE_F_OVERRIDE|
2171 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
2172 NEIGH_UPDATE_F_ISROUTER))
2173 );
2174
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002175 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05002176 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto out;
2178
2179 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
2180 if (on_link)
2181 nrt->rt6i_flags &= ~RTF_GATEWAY;
2182
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002183 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Thomas Graf40e22e82006-08-22 00:00:45 -07002185 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto out;
2187
Changli Gaod8d1f302010-06-10 23:31:35 -07002188 netevent.old = &rt->dst;
2189 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002190 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00002191 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07002192 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
2193
David S. Miller38308472011-12-03 18:02:47 -05002194 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07002195 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002196 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
2199out:
David S. Millere8599ff2012-07-11 23:43:53 -07002200 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 * Misc support functions
2205 */
2206
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002207static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
2208{
2209 BUG_ON(from->dst.from);
2210
2211 rt->rt6i_flags &= ~RTF_EXPIRES;
2212 dst_hold(&from->dst);
2213 rt->dst.from = &from->dst;
2214 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
2215}
2216
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002217static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002219 rt->dst.input = ort->dst.input;
2220 rt->dst.output = ort->dst.output;
2221 rt->rt6i_dst = ort->rt6i_dst;
2222 rt->dst.error = ort->dst.error;
2223 rt->rt6i_idev = ort->rt6i_idev;
2224 if (rt->rt6i_idev)
2225 in6_dev_hold(rt->rt6i_idev);
2226 rt->dst.lastuse = jiffies;
2227 rt->rt6i_gateway = ort->rt6i_gateway;
2228 rt->rt6i_flags = ort->rt6i_flags;
2229 rt6_set_from(rt, ort);
2230 rt->rt6i_metric = ort->rt6i_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002232 rt->rt6i_src = ort->rt6i_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#endif
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002234 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
2235 rt->rt6i_table = ort->rt6i_table;
Jiri Benc61adedf2015-08-20 13:56:25 +02002236 rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002239#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002240static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002241 const struct in6_addr *prefix, int prefixlen,
2242 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002243{
2244 struct fib6_node *fn;
2245 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002246 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002247
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002248 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05002249 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002250 return NULL;
2251
Li RongQing5744dd92012-09-11 21:59:01 +00002252 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002253 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002254 if (!fn)
2255 goto out;
2256
Changli Gaod8d1f302010-06-10 23:31:35 -07002257 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002258 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002259 continue;
2260 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
2261 continue;
2262 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
2263 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002264 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002265 break;
2266 }
2267out:
Li RongQing5744dd92012-09-11 21:59:01 +00002268 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002269 return rt;
2270}
2271
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002272static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002273 const struct in6_addr *prefix, int prefixlen,
2274 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00002275 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002276{
Thomas Graf86872cb2006-08-22 00:01:08 -07002277 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08002278 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002279 .fc_ifindex = ifindex,
2280 .fc_dst_len = prefixlen,
2281 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2282 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002283 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002284 .fc_nlinfo.nlh = NULL,
2285 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002286 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002287
David Ahernca254492015-10-12 11:47:10 -07002288 cfg.fc_table = l3mdev_fib_table_by_index(net, ifindex) ? : RT6_TABLE_INFO;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002289 cfg.fc_dst = *prefix;
2290 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002291
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002292 /* We should treat it as a default route if prefix length is 0. */
2293 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002294 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002295
Thomas Graf86872cb2006-08-22 00:01:08 -07002296 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002297
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002298 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002299}
2300#endif
2301
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002302struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002305 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002307 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002308 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002309 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Li RongQing5744dd92012-09-11 21:59:01 +00002311 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002312 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002313 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002314 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2316 break;
2317 }
2318 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002319 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002320 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return rt;
2322}
2323
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002324struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002325 struct net_device *dev,
2326 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Thomas Graf86872cb2006-08-22 00:01:08 -07002328 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002329 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002330 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002331 .fc_ifindex = dev->ifindex,
2332 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2333 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002334 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002335 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002336 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002339 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Thomas Graf86872cb2006-08-22 00:01:08 -07002341 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return rt6_get_dflt_router(gwaddr, dev);
2344}
2345
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002346void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002349 struct fib6_table *table;
2350
2351 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002352 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002353 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002354 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002357 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002358 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002359 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2360 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002361 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002362 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002363 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto restart;
2365 }
2366 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002367 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Daniel Lezcano55786892008-03-04 13:47:47 -08002370static void rtmsg_to_fib6_config(struct net *net,
2371 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002372 struct fib6_config *cfg)
2373{
2374 memset(cfg, 0, sizeof(*cfg));
2375
David Ahernca254492015-10-12 11:47:10 -07002376 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
2377 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07002378 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2379 cfg->fc_metric = rtmsg->rtmsg_metric;
2380 cfg->fc_expires = rtmsg->rtmsg_info;
2381 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2382 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2383 cfg->fc_flags = rtmsg->rtmsg_flags;
2384
Daniel Lezcano55786892008-03-04 13:47:47 -08002385 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002386
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002387 cfg->fc_dst = rtmsg->rtmsg_dst;
2388 cfg->fc_src = rtmsg->rtmsg_src;
2389 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002390}
2391
Daniel Lezcano55786892008-03-04 13:47:47 -08002392int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Thomas Graf86872cb2006-08-22 00:01:08 -07002394 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct in6_rtmsg rtmsg;
2396 int err;
2397
Ian Morris67ba4152014-08-24 21:53:10 +01002398 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 case SIOCADDRT: /* Add a route */
2400 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002401 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return -EPERM;
2403 err = copy_from_user(&rtmsg, arg,
2404 sizeof(struct in6_rtmsg));
2405 if (err)
2406 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002407
Daniel Lezcano55786892008-03-04 13:47:47 -08002408 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 rtnl_lock();
2411 switch (cmd) {
2412 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002413 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 break;
2415 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002416 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 break;
2418 default:
2419 err = -EINVAL;
2420 }
2421 rtnl_unlock();
2422
2423 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 return -EINVAL;
2427}
2428
2429/*
2430 * Drop the packet on the floor
2431 */
2432
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002433static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002435 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002436 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002437 switch (ipstats_mib_noroutes) {
2438 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002439 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002440 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002441 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2442 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002443 break;
2444 }
2445 /* FALLTHROUGH */
2446 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002447 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2448 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002449 break;
2450 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002451 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 kfree_skb(skb);
2453 return 0;
2454}
2455
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002456static int ip6_pkt_discard(struct sk_buff *skb)
2457{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002458 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002459}
2460
Eric W. Biedermanede20592015-10-07 16:48:47 -05002461static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Eric Dumazetadf30902009-06-02 05:19:30 +00002463 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002464 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002467static int ip6_pkt_prohibit(struct sk_buff *skb)
2468{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002469 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002470}
2471
Eric W. Biedermanede20592015-10-07 16:48:47 -05002472static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002473{
Eric Dumazetadf30902009-06-02 05:19:30 +00002474 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002475 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
2479 * Allocate a dst for local (unicast / anycast) address.
2480 */
2481
2482struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2483 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002484 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
David Ahernca254492015-10-12 11:47:10 -07002486 u32 tb_id;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002487 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002488 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -07002489 DST_NOCOUNT);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002490 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return ERR_PTR(-ENOMEM);
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 in6_dev_hold(idev);
2494
David S. Miller11d53b42011-06-24 15:23:34 -07002495 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002496 rt->dst.input = ip6_input;
2497 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002501 if (anycast)
2502 rt->rt6i_flags |= RTF_ANYCAST;
2503 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Julian Anastasov550bab42013-10-20 15:43:04 +03002506 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002507 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 rt->rt6i_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07002509 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
2510 rt->rt6i_table = fib6_get_table(net, tb_id);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002511 rt->dst.flags |= DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Changli Gaod8d1f302010-06-10 23:31:35 -07002513 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 return rt;
2516}
2517
Daniel Walterc3968a82011-04-13 21:10:57 +00002518int ip6_route_get_saddr(struct net *net,
2519 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002520 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002521 unsigned int prefs,
2522 struct in6_addr *saddr)
2523{
Markus Stenberge16e8882015-05-05 13:36:59 +03002524 struct inet6_dev *idev =
2525 rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
Daniel Walterc3968a82011-04-13 21:10:57 +00002526 int err = 0;
Markus Stenberge16e8882015-05-05 13:36:59 +03002527 if (rt && rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002528 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002529 else
2530 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2531 daddr, prefs, saddr);
2532 return err;
2533}
2534
2535/* remove deleted ip from prefsrc entries */
2536struct arg_dev_net_ip {
2537 struct net_device *dev;
2538 struct net *net;
2539 struct in6_addr *addr;
2540};
2541
2542static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2543{
2544 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2545 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2546 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2547
David S. Millerd1918542011-12-28 20:19:20 -05002548 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002549 rt != net->ipv6.ip6_null_entry &&
2550 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2551 /* remove prefsrc entry */
2552 rt->rt6i_prefsrc.plen = 0;
2553 }
2554 return 0;
2555}
2556
2557void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2558{
2559 struct net *net = dev_net(ifp->idev->dev);
2560 struct arg_dev_net_ip adni = {
2561 .dev = ifp->idev->dev,
2562 .net = net,
2563 .addr = &ifp->addr,
2564 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002565 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002566}
2567
Duan Jiongbe7a0102014-05-15 15:56:14 +08002568#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2569#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2570
2571/* Remove routers and update dst entries when gateway turn into host. */
2572static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2573{
2574 struct in6_addr *gateway = (struct in6_addr *)arg;
2575
2576 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2577 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2578 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2579 return -1;
2580 }
2581 return 0;
2582}
2583
2584void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2585{
2586 fib6_clean_all(net, fib6_clean_tohost, gateway);
2587}
2588
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002589struct arg_dev_net {
2590 struct net_device *dev;
2591 struct net *net;
2592};
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594static int fib6_ifdown(struct rt6_info *rt, void *arg)
2595{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002596 const struct arg_dev_net *adn = arg;
2597 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002598
David S. Millerd1918542011-12-28 20:19:20 -05002599 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002600 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return 0;
2604}
2605
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002606void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002608 struct arg_dev_net adn = {
2609 .dev = dev,
2610 .net = net,
2611 };
2612
Li RongQing0c3584d2013-12-27 16:32:38 +08002613 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002614 icmp6_clean_all(fib6_ifdown, &adn);
Eric W. Biedermane332bc62015-10-12 11:02:08 -05002615 if (dev)
2616 rt6_uncached_list_flush_dev(net, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Eric Dumazet95c96172012-04-15 05:58:06 +00002619struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002621 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622};
2623
2624static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2625{
2626 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2627 struct inet6_dev *idev;
2628
2629 /* In IPv6 pmtu discovery is not optional,
2630 so that RTAX_MTU lock cannot disable it.
2631 We still use this lock to block changes
2632 caused by addrconf/ndisc.
2633 */
2634
2635 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002636 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return 0;
2638
2639 /* For administrative MTU increase, there is no way to discover
2640 IPv6 PMTU increase, so PMTU increase should be updated here.
2641 Since RFC 1981 doesn't include administrative MTU increase
2642 update PMTU increase is a MUST. (i.e. jumbo frame)
2643 */
2644 /*
2645 If new MTU is less than route PMTU, this new MTU will be the
2646 lowest MTU in the path, update the route PMTU to reflect PMTU
2647 decreases; if new MTU is greater than route PMTU, and the
2648 old MTU is the lowest MTU in the path, update the route PMTU
2649 to reflect the increase. In this case if the other nodes' MTU
2650 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2651 PMTU discouvery.
2652 */
David S. Millerd1918542011-12-28 20:19:20 -05002653 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002654 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2655 if (rt->rt6i_flags & RTF_CACHE) {
2656 /* For RTF_CACHE with rt6i_pmtu == 0
2657 * (i.e. a redirected route),
2658 * the metrics of its rt->dst.from has already
2659 * been updated.
2660 */
2661 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2662 rt->rt6i_pmtu = arg->mtu;
2663 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2664 (dst_mtu(&rt->dst) < arg->mtu &&
2665 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2666 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2667 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return 0;
2670}
2671
Eric Dumazet95c96172012-04-15 05:58:06 +00002672void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Thomas Grafc71099a2006-08-04 23:20:06 -07002674 struct rt6_mtu_change_arg arg = {
2675 .dev = dev,
2676 .mtu = mtu,
2677 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Li RongQing0c3584d2013-12-27 16:32:38 +08002679 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002682static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002683 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002684 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002685 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002686 [RTA_PRIORITY] = { .type = NLA_U32 },
2687 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002688 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002689 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002690 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
2691 [RTA_ENCAP] = { .type = NLA_NESTED },
Thomas Graf86872cb2006-08-22 00:01:08 -07002692};
2693
2694static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2695 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Thomas Graf86872cb2006-08-22 00:01:08 -07002697 struct rtmsg *rtm;
2698 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002699 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002700 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Thomas Graf86872cb2006-08-22 00:01:08 -07002702 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2703 if (err < 0)
2704 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Thomas Graf86872cb2006-08-22 00:01:08 -07002706 err = -EINVAL;
2707 rtm = nlmsg_data(nlh);
2708 memset(cfg, 0, sizeof(*cfg));
2709
2710 cfg->fc_table = rtm->rtm_table;
2711 cfg->fc_dst_len = rtm->rtm_dst_len;
2712 cfg->fc_src_len = rtm->rtm_src_len;
2713 cfg->fc_flags = RTF_UP;
2714 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002715 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002716
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002717 if (rtm->rtm_type == RTN_UNREACHABLE ||
2718 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002719 rtm->rtm_type == RTN_PROHIBIT ||
2720 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002721 cfg->fc_flags |= RTF_REJECT;
2722
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002723 if (rtm->rtm_type == RTN_LOCAL)
2724 cfg->fc_flags |= RTF_LOCAL;
2725
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002726 if (rtm->rtm_flags & RTM_F_CLONED)
2727 cfg->fc_flags |= RTF_CACHE;
2728
Eric W. Biederman15e47302012-09-07 20:12:54 +00002729 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002730 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002731 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002732
2733 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002734 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002735 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002737
2738 if (tb[RTA_DST]) {
2739 int plen = (rtm->rtm_dst_len + 7) >> 3;
2740
2741 if (nla_len(tb[RTA_DST]) < plen)
2742 goto errout;
2743
2744 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002746
2747 if (tb[RTA_SRC]) {
2748 int plen = (rtm->rtm_src_len + 7) >> 3;
2749
2750 if (nla_len(tb[RTA_SRC]) < plen)
2751 goto errout;
2752
2753 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002755
Daniel Walterc3968a82011-04-13 21:10:57 +00002756 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002757 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002758
Thomas Graf86872cb2006-08-22 00:01:08 -07002759 if (tb[RTA_OIF])
2760 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2761
2762 if (tb[RTA_PRIORITY])
2763 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2764
2765 if (tb[RTA_METRICS]) {
2766 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2767 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002769
2770 if (tb[RTA_TABLE])
2771 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2772
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002773 if (tb[RTA_MULTIPATH]) {
2774 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2775 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2776 }
2777
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002778 if (tb[RTA_PREF]) {
2779 pref = nla_get_u8(tb[RTA_PREF]);
2780 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2781 pref != ICMPV6_ROUTER_PREF_HIGH)
2782 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2783 cfg->fc_flags |= RTF_PREF(pref);
2784 }
2785
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002786 if (tb[RTA_ENCAP])
2787 cfg->fc_encap = tb[RTA_ENCAP];
2788
2789 if (tb[RTA_ENCAP_TYPE])
2790 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
2791
Thomas Graf86872cb2006-08-22 00:01:08 -07002792 err = 0;
2793errout:
2794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002797struct rt6_nh {
2798 struct rt6_info *rt6_info;
2799 struct fib6_config r_cfg;
2800 struct mx6_config mxc;
2801 struct list_head next;
2802};
2803
2804static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
2805{
2806 struct rt6_nh *nh;
2807
2808 list_for_each_entry(nh, rt6_nh_list, next) {
2809 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6 nexthop %pI6 ifi %d\n",
2810 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
2811 nh->r_cfg.fc_ifindex);
2812 }
2813}
2814
2815static int ip6_route_info_append(struct list_head *rt6_nh_list,
2816 struct rt6_info *rt, struct fib6_config *r_cfg)
2817{
2818 struct rt6_nh *nh;
2819 struct rt6_info *rtnh;
2820 int err = -EEXIST;
2821
2822 list_for_each_entry(nh, rt6_nh_list, next) {
2823 /* check if rt6_info already exists */
2824 rtnh = nh->rt6_info;
2825
2826 if (rtnh->dst.dev == rt->dst.dev &&
2827 rtnh->rt6i_idev == rt->rt6i_idev &&
2828 ipv6_addr_equal(&rtnh->rt6i_gateway,
2829 &rt->rt6i_gateway))
2830 return err;
2831 }
2832
2833 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
2834 if (!nh)
2835 return -ENOMEM;
2836 nh->rt6_info = rt;
2837 err = ip6_convert_metrics(&nh->mxc, r_cfg);
2838 if (err) {
2839 kfree(nh);
2840 return err;
2841 }
2842 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
2843 list_add_tail(&nh->next, rt6_nh_list);
2844
2845 return 0;
2846}
2847
2848static int ip6_route_multipath_add(struct fib6_config *cfg)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002849{
2850 struct fib6_config r_cfg;
2851 struct rtnexthop *rtnh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002852 struct rt6_info *rt;
2853 struct rt6_nh *err_nh;
2854 struct rt6_nh *nh, *nh_safe;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002855 int remaining;
2856 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002857 int err = 1;
2858 int nhn = 0;
2859 int replace = (cfg->fc_nlinfo.nlh &&
2860 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
2861 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002862
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02002863 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002864 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002865
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002866 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
2867 * rt6_info structs per nexthop
2868 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002869 while (rtnh_ok(rtnh, remaining)) {
2870 memcpy(&r_cfg, cfg, sizeof(*cfg));
2871 if (rtnh->rtnh_ifindex)
2872 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2873
2874 attrlen = rtnh_attrlen(rtnh);
2875 if (attrlen > 0) {
2876 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2877
2878 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2879 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002880 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002881 r_cfg.fc_flags |= RTF_GATEWAY;
2882 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002883 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
2884 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
2885 if (nla)
2886 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002887 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002888
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002889 rt = ip6_route_info_create(&r_cfg);
2890 if (IS_ERR(rt)) {
2891 err = PTR_ERR(rt);
2892 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002893 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002894 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002895
2896 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002897 if (err) {
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002898 dst_free(&rt->dst);
2899 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002900 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002901
2902 rtnh = rtnh_next(rtnh, &remaining);
2903 }
2904
2905 err_nh = NULL;
2906 list_for_each_entry(nh, &rt6_nh_list, next) {
2907 err = __ip6_ins_rt(nh->rt6_info, &cfg->fc_nlinfo, &nh->mxc);
2908 /* nh->rt6_info is used or freed at this point, reset to NULL*/
2909 nh->rt6_info = NULL;
2910 if (err) {
2911 if (replace && nhn)
2912 ip6_print_replace_route_err(&rt6_nh_list);
2913 err_nh = nh;
2914 goto add_errout;
2915 }
2916
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002917 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02002918 * these flags after the first nexthop: if there is a collision,
2919 * we have already failed to add the first nexthop:
2920 * fib6_add_rt2node() has rejected it; when replacing, old
2921 * nexthops have been replaced by first new, the rest should
2922 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002923 */
Michal Kubeček27596472015-05-18 20:54:00 +02002924 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
2925 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002926 nhn++;
2927 }
2928
2929 goto cleanup;
2930
2931add_errout:
2932 /* Delete routes that were already added */
2933 list_for_each_entry(nh, &rt6_nh_list, next) {
2934 if (err_nh == nh)
2935 break;
2936 ip6_route_del(&nh->r_cfg);
2937 }
2938
2939cleanup:
2940 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
2941 if (nh->rt6_info)
2942 dst_free(&nh->rt6_info->dst);
Wu Fengguang52fe51f2015-09-10 06:57:12 +08002943 kfree(nh->mxc.mx);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002944 list_del(&nh->next);
2945 kfree(nh);
2946 }
2947
2948 return err;
2949}
2950
2951static int ip6_route_multipath_del(struct fib6_config *cfg)
2952{
2953 struct fib6_config r_cfg;
2954 struct rtnexthop *rtnh;
2955 int remaining;
2956 int attrlen;
2957 int err = 1, last_err = 0;
2958
2959 remaining = cfg->fc_mp_len;
2960 rtnh = (struct rtnexthop *)cfg->fc_mp;
2961
2962 /* Parse a Multipath Entry */
2963 while (rtnh_ok(rtnh, remaining)) {
2964 memcpy(&r_cfg, cfg, sizeof(*cfg));
2965 if (rtnh->rtnh_ifindex)
2966 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2967
2968 attrlen = rtnh_attrlen(rtnh);
2969 if (attrlen > 0) {
2970 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2971
2972 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2973 if (nla) {
2974 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2975 r_cfg.fc_flags |= RTF_GATEWAY;
2976 }
2977 }
2978 err = ip6_route_del(&r_cfg);
2979 if (err)
2980 last_err = err;
2981
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002982 rtnh = rtnh_next(rtnh, &remaining);
2983 }
2984
2985 return last_err;
2986}
2987
Ian Morris67ba4152014-08-24 21:53:10 +01002988static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Thomas Graf86872cb2006-08-22 00:01:08 -07002990 struct fib6_config cfg;
2991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Thomas Graf86872cb2006-08-22 00:01:08 -07002993 err = rtm_to_fib6_config(skb, nlh, &cfg);
2994 if (err < 0)
2995 return err;
2996
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002997 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002998 return ip6_route_multipath_del(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002999 else
3000 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
Ian Morris67ba4152014-08-24 21:53:10 +01003003static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Thomas Graf86872cb2006-08-22 00:01:08 -07003005 struct fib6_config cfg;
3006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Thomas Graf86872cb2006-08-22 00:01:08 -07003008 err = rtm_to_fib6_config(skb, nlh, &cfg);
3009 if (err < 0)
3010 return err;
3011
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003012 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003013 return ip6_route_multipath_add(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003014 else
3015 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003018static inline size_t rt6_nlmsg_size(struct rt6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08003019{
3020 return NLMSG_ALIGN(sizeof(struct rtmsg))
3021 + nla_total_size(16) /* RTA_SRC */
3022 + nla_total_size(16) /* RTA_DST */
3023 + nla_total_size(16) /* RTA_GATEWAY */
3024 + nla_total_size(16) /* RTA_PREFSRC */
3025 + nla_total_size(4) /* RTA_TABLE */
3026 + nla_total_size(4) /* RTA_IIF */
3027 + nla_total_size(4) /* RTA_OIF */
3028 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08003029 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01003030 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003031 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003032 + nla_total_size(1) /* RTA_PREF */
Jiri Benc61adedf2015-08-20 13:56:25 +02003033 + lwtunnel_get_encap_size(rt->dst.lwtstate);
Thomas Graf339bf982006-11-10 14:10:15 -08003034}
3035
Brian Haley191cd582008-08-14 15:33:21 -07003036static int rt6_fill_node(struct net *net,
3037 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07003038 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003039 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003040 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003042 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003044 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08003045 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07003046 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 if (prefix) { /* user wants prefix routes only */
3049 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
3050 /* success since this is not a prefix route */
3051 return 1;
3052 }
3053 }
3054
Eric W. Biederman15e47302012-09-07 20:12:54 +00003055 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05003056 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08003057 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003058
3059 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 rtm->rtm_family = AF_INET6;
3061 rtm->rtm_dst_len = rt->rt6i_dst.plen;
3062 rtm->rtm_src_len = rt->rt6i_src.plen;
3063 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07003064 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07003065 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07003066 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07003067 table = RT6_TABLE_UNSPEC;
3068 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04003069 if (nla_put_u32(skb, RTA_TABLE, table))
3070 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003071 if (rt->rt6i_flags & RTF_REJECT) {
3072 switch (rt->dst.error) {
3073 case -EINVAL:
3074 rtm->rtm_type = RTN_BLACKHOLE;
3075 break;
3076 case -EACCES:
3077 rtm->rtm_type = RTN_PROHIBIT;
3078 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00003079 case -EAGAIN:
3080 rtm->rtm_type = RTN_THROW;
3081 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003082 default:
3083 rtm->rtm_type = RTN_UNREACHABLE;
3084 break;
3085 }
3086 }
David S. Miller38308472011-12-03 18:02:47 -05003087 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00003088 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05003089 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 rtm->rtm_type = RTN_LOCAL;
3091 else
3092 rtm->rtm_type = RTN_UNICAST;
3093 rtm->rtm_flags = 0;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003094 if (!netif_carrier_ok(rt->dst.dev)) {
Andy Gospodarekcea45e22015-08-13 10:39:00 -04003095 rtm->rtm_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003096 if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
3097 rtm->rtm_flags |= RTNH_F_DEAD;
3098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
3100 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05003101 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00003103 else if (rt->rt6i_flags & RTF_ADDRCONF) {
3104 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
3105 rtm->rtm_protocol = RTPROT_RA;
3106 else
3107 rtm->rtm_protocol = RTPROT_KERNEL;
3108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
David S. Miller38308472011-12-03 18:02:47 -05003110 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 rtm->rtm_flags |= RTM_F_CLONED;
3112
3113 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02003114 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04003115 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003116 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02003118 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003119 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120#ifdef CONFIG_IPV6_SUBTREES
3121 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02003122 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04003123 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003124 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04003125 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02003126 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003127 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003129 if (iif) {
3130#ifdef CONFIG_IPV6_MROUTE
3131 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08003132 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003133 if (err <= 0) {
3134 if (!nowait) {
3135 if (err == 0)
3136 return 0;
3137 goto nla_put_failure;
3138 } else {
3139 if (err == -EMSGSIZE)
3140 goto nla_put_failure;
3141 }
3142 }
3143 } else
3144#endif
David S. Millerc78679e2012-04-01 20:27:33 -04003145 if (nla_put_u32(skb, RTA_IIF, iif))
3146 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003147 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04003149 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02003150 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003151 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003153
Daniel Walterc3968a82011-04-13 21:10:57 +00003154 if (rt->rt6i_prefsrc.plen) {
3155 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003156 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02003157 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003158 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00003159 }
3160
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003161 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
3162 if (rt->rt6i_pmtu)
3163 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
3164 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07003165 goto nla_put_failure;
3166
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00003167 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02003168 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00003169 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00003170 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003171
David S. Millerc78679e2012-04-01 20:27:33 -04003172 if (rt->dst.dev &&
3173 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
3174 goto nla_put_failure;
3175 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
3176 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00003177
3178 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07003179
David S. Miller87a50692012-07-10 05:06:14 -07003180 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08003181 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003183 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
3184 goto nla_put_failure;
3185
Jiri Benc61adedf2015-08-20 13:56:25 +02003186 lwtunnel_fill_encap(skb, rt->dst.lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003187
Johannes Berg053c0952015-01-16 22:09:00 +01003188 nlmsg_end(skb, nlh);
3189 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003190
3191nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003192 nlmsg_cancel(skb, nlh);
3193 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Patrick McHardy1b43af52006-08-10 23:11:17 -07003196int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
3199 int prefix;
3200
Thomas Graf2d7202b2006-08-22 00:01:27 -07003201 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
3202 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
3204 } else
3205 prefix = 0;
3206
Brian Haley191cd582008-08-14 15:33:21 -07003207 return rt6_fill_node(arg->net,
3208 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003209 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003210 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Ian Morris67ba4152014-08-24 21:53:10 +01003213static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003215 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07003216 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07003218 struct sk_buff *skb;
3219 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05003220 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003221 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07003222
3223 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
3224 if (err < 0)
3225 goto errout;
3226
3227 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05003228 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07003229
3230 if (tb[RTA_SRC]) {
3231 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
3232 goto errout;
3233
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003234 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07003235 }
3236
3237 if (tb[RTA_DST]) {
3238 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
3239 goto errout;
3240
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003241 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07003242 }
3243
3244 if (tb[RTA_IIF])
3245 iif = nla_get_u32(tb[RTA_IIF]);
3246
3247 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003248 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07003249
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07003250 if (tb[RTA_MARK])
3251 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
3252
Thomas Grafab364a62006-08-22 00:01:47 -07003253 if (iif) {
3254 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003255 int flags = 0;
3256
Daniel Lezcano55786892008-03-04 13:47:47 -08003257 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07003258 if (!dev) {
3259 err = -ENODEV;
3260 goto errout;
3261 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003262
3263 fl6.flowi6_iif = iif;
3264
3265 if (!ipv6_addr_any(&fl6.saddr))
3266 flags |= RT6_LOOKUP_F_HAS_SADDR;
3267
3268 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
3269 flags);
3270 } else {
3271 fl6.flowi6_oif = oif;
3272
David Ahernca254492015-10-12 11:47:10 -07003273 if (netif_index_is_l3_master(net, oif)) {
3274 fl6.flowi6_flags = FLOWI_FLAG_L3MDEV_SRC |
3275 FLOWI_FLAG_SKIP_NH_OIF;
3276 }
3277
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003278 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05003282 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00003283 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07003284 err = -ENOBUFS;
3285 goto errout;
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* Reserve room for dummy headers, this skb can pass
3289 through good chunk of routing engine.
3290 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07003291 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
3293
Changli Gaod8d1f302010-06-10 23:31:35 -07003294 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
David S. Miller4c9483b2011-03-12 16:22:43 -05003296 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003297 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003298 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07003300 kfree_skb(skb);
3301 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303
Eric W. Biederman15e47302012-09-07 20:12:54 +00003304 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07003305errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003309void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
3310 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08003313 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003314 u32 seq;
3315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003317 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003318 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07003319
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003320 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05003321 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07003322 goto errout;
3323
Brian Haley191cd582008-08-14 15:33:21 -07003324 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003325 event, info->portid, seq, 0, 0, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003326 if (err < 0) {
3327 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
3328 WARN_ON(err == -EMSGSIZE);
3329 kfree_skb(skb);
3330 goto errout;
3331 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00003332 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003333 info->nlh, gfp_any());
3334 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07003335errout:
3336 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08003337 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003340static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00003341 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003342{
Jiri Pirko351638e2013-05-28 01:30:21 +00003343 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003344 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003345
3346 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003347 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003348 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
3349#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003350 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003351 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003352 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003353 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
3354#endif
3355 }
3356
3357 return NOTIFY_OK;
3358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/*
3361 * /proc
3362 */
3363
3364#ifdef CONFIG_PROC_FS
3365
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003366static const struct file_operations ipv6_route_proc_fops = {
3367 .owner = THIS_MODULE,
3368 .open = ipv6_route_open,
3369 .read = seq_read,
3370 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02003371 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003372};
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374static int rt6_stats_seq_show(struct seq_file *seq, void *v)
3375{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003376 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003378 net->ipv6.rt6_stats->fib_nodes,
3379 net->ipv6.rt6_stats->fib_route_nodes,
3380 net->ipv6.rt6_stats->fib_rt_alloc,
3381 net->ipv6.rt6_stats->fib_rt_entries,
3382 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00003383 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003384 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 return 0;
3387}
3388
3389static int rt6_stats_seq_open(struct inode *inode, struct file *file)
3390{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07003391 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003392}
3393
Arjan van de Ven9a321442007-02-12 00:55:35 -08003394static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 .owner = THIS_MODULE,
3396 .open = rt6_stats_seq_open,
3397 .read = seq_read,
3398 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07003399 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400};
3401#endif /* CONFIG_PROC_FS */
3402
3403#ifdef CONFIG_SYSCTL
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405static
Joe Perchesfe2c6332013-06-11 23:04:25 -07003406int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 void __user *buffer, size_t *lenp, loff_t *ppos)
3408{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003409 struct net *net;
3410 int delay;
3411 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003413
3414 net = (struct net *)ctl->extra1;
3415 delay = net->ipv6.sysctl.flush_delay;
3416 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02003417 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
Joe Perchesfe2c6332013-06-11 23:04:25 -07003421struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003422 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08003424 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07003426 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003427 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 },
3429 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003431 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 .maxlen = sizeof(int),
3433 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003434 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 },
3436 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08003438 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 .maxlen = sizeof(int),
3440 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003441 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 },
3443 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003445 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 .maxlen = sizeof(int),
3447 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003448 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 },
3450 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003452 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 .maxlen = sizeof(int),
3454 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003455 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 },
3457 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003459 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 .maxlen = sizeof(int),
3461 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003462 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 },
3464 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003466 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 .maxlen = sizeof(int),
3468 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003469 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 },
3471 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003473 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 .maxlen = sizeof(int),
3475 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003476 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 },
3478 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003480 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 .maxlen = sizeof(int),
3482 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003483 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 },
3485 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003487 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 .maxlen = sizeof(int),
3489 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003490 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003492 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493};
3494
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003495struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003496{
3497 struct ctl_table *table;
3498
3499 table = kmemdup(ipv6_route_table_template,
3500 sizeof(ipv6_route_table_template),
3501 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003502
3503 if (table) {
3504 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003505 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003506 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003507 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3508 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3509 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3510 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3511 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3512 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3513 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003514 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003515
3516 /* Don't export sysctls to unprivileged users */
3517 if (net->user_ns != &init_user_ns)
3518 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003519 }
3520
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003521 return table;
3522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523#endif
3524
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003525static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003526{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003527 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003528
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003529 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3530 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003531
Eric Dumazetfc66f952010-10-08 06:37:34 +00003532 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3533 goto out_ip6_dst_ops;
3534
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003535 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3536 sizeof(*net->ipv6.ip6_null_entry),
3537 GFP_KERNEL);
3538 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003539 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003540 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003541 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003542 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003543 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3544 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003545
3546#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3547 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3548 sizeof(*net->ipv6.ip6_prohibit_entry),
3549 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003550 if (!net->ipv6.ip6_prohibit_entry)
3551 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003552 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003553 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003554 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003555 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3556 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003557
3558 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3559 sizeof(*net->ipv6.ip6_blk_hole_entry),
3560 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003561 if (!net->ipv6.ip6_blk_hole_entry)
3562 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003563 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003564 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003565 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003566 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3567 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003568#endif
3569
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003570 net->ipv6.sysctl.flush_delay = 0;
3571 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3572 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3573 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3574 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3575 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3576 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3577 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3578
Benjamin Thery6891a342008-03-04 13:49:47 -08003579 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3580
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003581 ret = 0;
3582out:
3583 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003584
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003585#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3586out_ip6_prohibit_entry:
3587 kfree(net->ipv6.ip6_prohibit_entry);
3588out_ip6_null_entry:
3589 kfree(net->ipv6.ip6_null_entry);
3590#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003591out_ip6_dst_entries:
3592 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003593out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003594 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003595}
3596
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003597static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003598{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003599 kfree(net->ipv6.ip6_null_entry);
3600#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3601 kfree(net->ipv6.ip6_prohibit_entry);
3602 kfree(net->ipv6.ip6_blk_hole_entry);
3603#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003604 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003605}
3606
Thomas Grafd1896342012-06-18 12:08:33 +00003607static int __net_init ip6_route_net_init_late(struct net *net)
3608{
3609#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003610 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3611 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003612#endif
3613 return 0;
3614}
3615
3616static void __net_exit ip6_route_net_exit_late(struct net *net)
3617{
3618#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003619 remove_proc_entry("ipv6_route", net->proc_net);
3620 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003621#endif
3622}
3623
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003624static struct pernet_operations ip6_route_net_ops = {
3625 .init = ip6_route_net_init,
3626 .exit = ip6_route_net_exit,
3627};
3628
David S. Millerc3426b42012-06-09 16:27:05 -07003629static int __net_init ipv6_inetpeer_init(struct net *net)
3630{
3631 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3632
3633 if (!bp)
3634 return -ENOMEM;
3635 inet_peer_base_init(bp);
3636 net->ipv6.peers = bp;
3637 return 0;
3638}
3639
3640static void __net_exit ipv6_inetpeer_exit(struct net *net)
3641{
3642 struct inet_peer_base *bp = net->ipv6.peers;
3643
3644 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003645 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003646 kfree(bp);
3647}
3648
David S. Miller2b823f72012-06-09 19:00:16 -07003649static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003650 .init = ipv6_inetpeer_init,
3651 .exit = ipv6_inetpeer_exit,
3652};
3653
Thomas Grafd1896342012-06-18 12:08:33 +00003654static struct pernet_operations ip6_route_net_late_ops = {
3655 .init = ip6_route_net_init_late,
3656 .exit = ip6_route_net_exit_late,
3657};
3658
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003659static struct notifier_block ip6_route_dev_notifier = {
3660 .notifier_call = ip6_route_dev_notify,
3661 .priority = 0,
3662};
3663
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003664int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003666 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003667 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003668
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003669 ret = -ENOMEM;
3670 ip6_dst_ops_template.kmem_cachep =
3671 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3672 SLAB_HWCACHE_ALIGN, NULL);
3673 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003674 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003675
Eric Dumazetfc66f952010-10-08 06:37:34 +00003676 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003677 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003678 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003679
David S. Millerc3426b42012-06-09 16:27:05 -07003680 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3681 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003682 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003683
David S. Miller7e52b332012-06-15 15:51:55 -07003684 ret = register_pernet_subsys(&ip6_route_net_ops);
3685 if (ret)
3686 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003687
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003688 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3689
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003690 /* Registering of the loopback is done before this portion of code,
3691 * the loopback reference in rt6_info will not be taken, do it
3692 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003693 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003694 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3695 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003696 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003697 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003698 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003699 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3700 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003701 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003702 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003703 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003704
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003705 ret = xfrm6_init();
3706 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003707 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003708
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003709 ret = fib6_rules_init();
3710 if (ret)
3711 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003712
Thomas Grafd1896342012-06-18 12:08:33 +00003713 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3714 if (ret)
3715 goto fib6_rules_init;
3716
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003717 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003718 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3719 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3720 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003721 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003722
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003723 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003724 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003725 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003726
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003727 for_each_possible_cpu(cpu) {
3728 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
3729
3730 INIT_LIST_HEAD(&ul->head);
3731 spin_lock_init(&ul->lock);
3732 }
3733
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003734out:
3735 return ret;
3736
Thomas Grafd1896342012-06-18 12:08:33 +00003737out_register_late_subsys:
3738 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003739fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003740 fib6_rules_cleanup();
3741xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003742 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003743out_fib6_init:
3744 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003745out_register_subsys:
3746 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003747out_register_inetpeer:
3748 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003749out_dst_entries:
3750 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003751out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003752 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
3756void ip6_route_cleanup(void)
3757{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003758 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003759 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003760 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003763 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003764 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003765 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003766 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}