blob: 90c8eaa24565398f909421811163878e9a0bdfde [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>
57#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070058#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070059#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000060#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/uaccess.h>
63
64#ifdef CONFIG_SYSCTL
65#include <linux/sysctl.h>
66#endif
67
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020068enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010069 RT6_NUD_FAIL_HARD = -3,
70 RT6_NUD_FAIL_PROBE = -2,
71 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020072 RT6_NUD_SUCCEED = 1
73};
74
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -070075static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080077static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000078static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static struct dst_entry *ip6_negative_advice(struct dst_entry *);
80static void ip6_dst_destroy(struct dst_entry *);
81static void ip6_dst_ifdown(struct dst_entry *,
82 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080083static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int ip6_pkt_discard(struct sk_buff *skb);
Eric Dumazetaad88722014-04-15 13:47:15 -040086static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053087static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric Dumazetaad88722014-04-15 13:47:15 -040088static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070090static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
91 struct sk_buff *skb, u32 mtu);
92static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
93 struct sk_buff *skb);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -070094static void rt6_dst_from_metrics_check(struct rt6_info *rt);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020095static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080097#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080098static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000099 const struct in6_addr *prefix, int prefixlen,
100 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +0000101 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800102static struct rt6_info *rt6_get_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);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800105#endif
106
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700107struct uncached_list {
108 spinlock_t lock;
109 struct list_head head;
110};
111
112static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
113
114static void rt6_uncached_list_add(struct rt6_info *rt)
115{
116 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
117
118 rt->dst.flags |= DST_NOCACHE;
119 rt->rt6i_uncached_list = ul;
120
121 spin_lock_bh(&ul->lock);
122 list_add_tail(&rt->rt6i_uncached, &ul->head);
123 spin_unlock_bh(&ul->lock);
124}
125
126static void rt6_uncached_list_del(struct rt6_info *rt)
127{
128 if (!list_empty(&rt->rt6i_uncached)) {
129 struct uncached_list *ul = rt->rt6i_uncached_list;
130
131 spin_lock_bh(&ul->lock);
132 list_del(&rt->rt6i_uncached);
133 spin_unlock_bh(&ul->lock);
134 }
135}
136
137static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
138{
139 struct net_device *loopback_dev = net->loopback_dev;
140 int cpu;
141
142 for_each_possible_cpu(cpu) {
143 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
144 struct rt6_info *rt;
145
146 spin_lock_bh(&ul->lock);
147 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
148 struct inet6_dev *rt_idev = rt->rt6i_idev;
149 struct net_device *rt_dev = rt->dst.dev;
150
151 if (rt_idev && (rt_idev->dev == dev || !dev) &&
152 rt_idev->dev != loopback_dev) {
153 rt->rt6i_idev = in6_dev_get(loopback_dev);
154 in6_dev_put(rt_idev);
155 }
156
157 if (rt_dev && (rt_dev == dev || !dev) &&
158 rt_dev != loopback_dev) {
159 rt->dst.dev = loopback_dev;
160 dev_hold(rt->dst.dev);
161 dev_put(rt_dev);
162 }
163 }
164 spin_unlock_bh(&ul->lock);
165 }
166}
167
David S. Miller06582542011-01-27 14:58:42 -0800168static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
169{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700170 struct rt6_info *rt = (struct rt6_info *)dst;
David S. Miller06582542011-01-27 14:58:42 -0800171
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700172 if (rt->rt6i_flags & RTF_CACHE)
173 return NULL;
174 else
Martin KaFai Lau3b471172015-02-12 16:14:08 -0800175 return dst_cow_metrics_generic(dst, old);
David S. Miller06582542011-01-27 14:58:42 -0800176}
177
David S. Millerf894cbf2012-07-02 21:52:24 -0700178static inline const void *choose_neigh_daddr(struct rt6_info *rt,
179 struct sk_buff *skb,
180 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500181{
182 struct in6_addr *p = &rt->rt6i_gateway;
183
David S. Millera7563f32012-01-26 16:29:16 -0500184 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500185 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700186 else if (skb)
187 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500188 return daddr;
189}
190
David S. Millerf894cbf2012-07-02 21:52:24 -0700191static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
192 struct sk_buff *skb,
193 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700194{
David S. Miller39232972012-01-26 15:22:32 -0500195 struct rt6_info *rt = (struct rt6_info *) dst;
196 struct neighbour *n;
197
David S. Millerf894cbf2012-07-02 21:52:24 -0700198 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000199 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500200 if (n)
201 return n;
202 return neigh_create(&nd_tbl, daddr, dst->dev);
203}
204
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800205static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .gc = ip6_dst_gc,
208 .gc_thresh = 1024,
209 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800210 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000211 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800212 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .destroy = ip6_dst_destroy,
214 .ifdown = ip6_dst_ifdown,
215 .negative_advice = ip6_negative_advice,
216 .link_failure = ip6_link_failure,
217 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700218 .redirect = rt6_do_redirect,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700219 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700220 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Steffen Klassertebb762f2011-11-23 02:12:51 +0000223static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800224{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000225 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
226
227 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800228}
229
David S. Miller6700c272012-07-17 03:29:28 -0700230static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
231 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700232{
233}
234
David S. Miller6700c272012-07-17 03:29:28 -0700235static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
236 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700237{
238}
239
Held Bernhard0972ddb2011-04-24 22:07:32 +0000240static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
241 unsigned long old)
242{
243 return NULL;
244}
245
David S. Miller14e50e52007-05-24 18:17:54 -0700246static struct dst_ops ip6_dst_blackhole_ops = {
247 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700248 .destroy = ip6_dst_destroy,
249 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000250 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800251 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700252 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700253 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000254 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700255 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700256};
257
David S. Miller62fa8a82011-01-26 20:51:05 -0800258static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800259 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800260};
261
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000262static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700263 .dst = {
264 .__refcnt = ATOMIC_INIT(1),
265 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000266 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700267 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700268 .input = ip6_pkt_discard,
269 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 },
271 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700272 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .rt6i_metric = ~(u32) 0,
274 .rt6i_ref = ATOMIC_INIT(1),
275};
276
Thomas Graf101367c2006-08-04 03:39:02 -0700277#ifdef CONFIG_IPV6_MULTIPLE_TABLES
278
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000279static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700280 .dst = {
281 .__refcnt = ATOMIC_INIT(1),
282 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000283 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700284 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700285 .input = ip6_pkt_prohibit,
286 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700287 },
288 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700289 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700290 .rt6i_metric = ~(u32) 0,
291 .rt6i_ref = ATOMIC_INIT(1),
292};
293
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000294static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700295 .dst = {
296 .__refcnt = ATOMIC_INIT(1),
297 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000298 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700299 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700300 .input = dst_discard,
Eric Dumazetaad88722014-04-15 13:47:15 -0400301 .output = dst_discard_sk,
Thomas Graf101367c2006-08-04 03:39:02 -0700302 },
303 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700304 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700305 .rt6i_metric = ~(u32) 0,
306 .rt6i_ref = ATOMIC_INIT(1),
307};
308
309#endif
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* allocate dst with ip6_dst_ops */
David S. Miller97bab732012-06-09 22:36:36 -0700312static inline struct rt6_info *ip6_dst_alloc(struct net *net,
David S. Miller957c6652011-06-24 15:25:00 -0700313 struct net_device *dev,
David S. Miller8b96d222012-06-11 02:01:56 -0700314 int flags,
315 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
David S. Miller97bab732012-06-09 22:36:36 -0700317 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000318 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700319
David S. Miller97bab732012-06-09 22:36:36 -0700320 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000321 struct dst_entry *dst = &rt->dst;
322
323 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000324 INIT_LIST_HEAD(&rt->rt6i_siblings);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700325 INIT_LIST_HEAD(&rt->rt6i_uncached);
David S. Miller97bab732012-06-09 22:36:36 -0700326 }
David S. Millercf911662011-04-28 14:31:47 -0700327 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330static void ip6_dst_destroy(struct dst_entry *dst)
331{
332 struct rt6_info *rt = (struct rt6_info *)dst;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000333 struct dst_entry *from = dst->from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700334 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700336 dst_destroy_metrics_generic(dst);
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000337
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700338 rt6_uncached_list_del(rt);
339
340 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500341 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 rt->rt6i_idev = NULL;
343 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900344 }
Gao feng1716a962012-04-06 00:13:10 +0000345
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000346 dst->from = NULL;
347 dst_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
351 int how)
352{
353 struct rt6_info *rt = (struct rt6_info *)dst;
354 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800355 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900356 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David S. Miller97cac082012-07-02 22:43:47 -0700358 if (dev != loopback_dev) {
359 if (idev && idev->dev == dev) {
360 struct inet6_dev *loopback_idev =
361 in6_dev_get(loopback_dev);
362 if (loopback_idev) {
363 rt->rt6i_idev = loopback_idev;
364 in6_dev_put(idev);
365 }
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368}
369
Eric Dumazeta50feda2012-05-18 18:57:34 +0000370static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Gao feng1716a962012-04-06 00:13:10 +0000372 if (rt->rt6i_flags & RTF_EXPIRES) {
373 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000374 return true;
Gao feng1716a962012-04-06 00:13:10 +0000375 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000376 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000377 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000378 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000381/* Multipath route selection:
382 * Hash based function using packet header and flowlabel.
383 * Adapted from fib_info_hashfn()
384 */
385static int rt6_info_hash_nhsfn(unsigned int candidate_count,
386 const struct flowi6 *fl6)
387{
388 unsigned int val = fl6->flowi6_proto;
389
YOSHIFUJI Hideaki / 吉藤英明c08977b2013-01-13 05:02:29 +0000390 val ^= ipv6_addr_hash(&fl6->daddr);
391 val ^= ipv6_addr_hash(&fl6->saddr);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000392
393 /* Work only if this not encapsulated */
394 switch (fl6->flowi6_proto) {
395 case IPPROTO_UDP:
396 case IPPROTO_TCP:
397 case IPPROTO_SCTP:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000398 val ^= (__force u16)fl6->fl6_sport;
399 val ^= (__force u16)fl6->fl6_dport;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000400 break;
401
402 case IPPROTO_ICMPV6:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000403 val ^= (__force u16)fl6->fl6_icmp_type;
404 val ^= (__force u16)fl6->fl6_icmp_code;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000405 break;
406 }
407 /* RFC6438 recommands to use flowlabel */
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000408 val ^= (__force u32)fl6->flowlabel;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000409
410 /* Perhaps, we need to tune, this function? */
411 val = val ^ (val >> 7) ^ (val >> 12);
412 return val % candidate_count;
413}
414
415static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200416 struct flowi6 *fl6, int oif,
417 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000418{
419 struct rt6_info *sibling, *next_sibling;
420 int route_choosen;
421
422 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
423 /* Don't change the route, if route_choosen == 0
424 * (siblings does not include ourself)
425 */
426 if (route_choosen)
427 list_for_each_entry_safe(sibling, next_sibling,
428 &match->rt6i_siblings, rt6i_siblings) {
429 route_choosen--;
430 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200431 if (rt6_score_route(sibling, oif, strict) < 0)
432 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000433 match = sibling;
434 break;
435 }
436 }
437 return match;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700441 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
443
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800444static inline struct rt6_info *rt6_device_match(struct net *net,
445 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000446 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700448 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct rt6_info *local = NULL;
451 struct rt6_info *sprt;
452
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900453 if (!oif && ipv6_addr_any(saddr))
454 goto out;
455
Changli Gaod8d1f302010-06-10 23:31:35 -0700456 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500457 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900458
459 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (dev->ifindex == oif)
461 return sprt;
462 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500463 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700465 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900467 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 local->rt6i_idev->dev->ifindex == oif))
469 continue;
470 }
471 local = sprt;
472 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900473 } else {
474 if (ipv6_chk_addr(net, saddr, dev,
475 flags & RT6_LOOKUP_F_IFACE))
476 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900480 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (local)
482 return local;
483
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700484 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800485 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900487out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return rt;
489}
490
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800491#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200492struct __rt6_probe_work {
493 struct work_struct work;
494 struct in6_addr target;
495 struct net_device *dev;
496};
497
498static void rt6_probe_deferred(struct work_struct *w)
499{
500 struct in6_addr mcaddr;
501 struct __rt6_probe_work *work =
502 container_of(w, struct __rt6_probe_work, work);
503
504 addrconf_addr_solict_mult(&work->target, &mcaddr);
505 ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
506 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100507 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200508}
509
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800510static void rt6_probe(struct rt6_info *rt)
511{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000512 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800513 /*
514 * Okay, this does not seem to be appropriate
515 * for now, however, we need to check if it
516 * is really so; aka Router Reachability Probing.
517 *
518 * Router Reachability Probe MUST be rate-limited
519 * to no more than one per minute.
520 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000521 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000522 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000523 rcu_read_lock_bh();
524 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
525 if (neigh) {
526 write_lock(&neigh->lock);
527 if (neigh->nud_state & NUD_VALID)
528 goto out;
YOSHIFUJI Hideaki / 吉藤英明7ff74a52013-01-17 12:53:02 +0000529 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000530
531 if (!neigh ||
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800532 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200533 struct __rt6_probe_work *work;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800534
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200535 work = kmalloc(sizeof(*work), GFP_ATOMIC);
536
537 if (neigh && work)
Jiri Benc7e980562013-12-11 13:48:20 +0100538 __neigh_set_probe_once(neigh);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000539
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200540 if (neigh)
541 write_unlock(&neigh->lock);
542
543 if (work) {
544 INIT_WORK(&work->work, rt6_probe_deferred);
545 work->target = rt->rt6i_gateway;
546 dev_hold(rt->dst.dev);
547 work->dev = rt->dst.dev;
548 schedule_work(&work->work);
549 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000550 } else {
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000551out:
552 write_unlock(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000553 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000554 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800555}
556#else
557static inline void rt6_probe(struct rt6_info *rt)
558{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800559}
560#endif
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800563 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700565static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
David S. Millerd1918542011-12-28 20:19:20 -0500567 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700568 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800569 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700570 if ((dev->flags & IFF_LOOPBACK) &&
571 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
572 return 1;
573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200576static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000578 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200579 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000580
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700581 if (rt->rt6i_flags & RTF_NONEXTHOP ||
582 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200583 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000584
585 rcu_read_lock_bh();
586 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
587 if (neigh) {
588 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800589 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200590 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800591#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000592 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200593 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100594 else
595 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800596#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000597 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200598 } else {
599 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100600 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000601 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000602 rcu_read_unlock_bh();
603
Paul Marksa5a81f02012-12-03 10:26:54 +0000604 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800607static int rt6_score_route(struct rt6_info *rt, int oif,
608 int strict)
609{
Paul Marksa5a81f02012-12-03 10:26:54 +0000610 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900611
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700612 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700613 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200614 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800615#ifdef CONFIG_IPV6_ROUTER_PREF
616 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
617#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200618 if (strict & RT6_LOOKUP_F_REACHABLE) {
619 int n = rt6_check_neigh(rt);
620 if (n < 0)
621 return n;
622 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800623 return m;
624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David S. Millerf11e6652007-03-24 20:36:25 -0700626static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200627 int *mpri, struct rt6_info *match,
628 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800629{
David S. Millerf11e6652007-03-24 20:36:25 -0700630 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200631 bool match_do_rr = false;
David S. Millerf11e6652007-03-24 20:36:25 -0700632
633 if (rt6_check_expired(rt))
634 goto out;
635
636 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100637 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200638 match_do_rr = true;
639 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100640 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700641 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700642 }
643
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200644 if (strict & RT6_LOOKUP_F_REACHABLE)
645 rt6_probe(rt);
646
Jiri Benc7e980562013-12-11 13:48:20 +0100647 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200648 if (m > *mpri) {
649 *do_rr = match_do_rr;
650 *mpri = m;
651 match = rt;
652 }
David S. Millerf11e6652007-03-24 20:36:25 -0700653out:
654 return match;
655}
656
657static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
658 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200659 u32 metric, int oif, int strict,
660 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700661{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700662 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800663 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David S. Millerf11e6652007-03-24 20:36:25 -0700665 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700666 cont = NULL;
667 for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
668 if (rt->rt6i_metric != metric) {
669 cont = rt;
670 break;
671 }
672
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200673 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700674 }
675
676 for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
677 if (rt->rt6i_metric != metric) {
678 cont = rt;
679 break;
680 }
681
682 match = find_match(rt, oif, strict, &mpri, match, do_rr);
683 }
684
685 if (match || !cont)
686 return match;
687
688 for (rt = cont; rt; rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200689 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800690
David S. Millerf11e6652007-03-24 20:36:25 -0700691 return match;
692}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800693
David S. Millerf11e6652007-03-24 20:36:25 -0700694static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
695{
696 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800697 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200698 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Millerf11e6652007-03-24 20:36:25 -0700700 rt0 = fn->rr_ptr;
701 if (!rt0)
702 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200704 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
705 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200707 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700708 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700709
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800710 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700711 if (!next || next->rt6i_metric != rt0->rt6i_metric)
712 next = fn->leaf;
713
714 if (next != rt0)
715 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
David S. Millerd1918542011-12-28 20:19:20 -0500718 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000719 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700722static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
723{
724 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
725}
726
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800727#ifdef CONFIG_IPV6_ROUTE_INFO
728int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000729 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800730{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900731 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800732 struct route_info *rinfo = (struct route_info *) opt;
733 struct in6_addr prefix_buf, *prefix;
734 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900735 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800736 struct rt6_info *rt;
737
738 if (len < sizeof(struct route_info)) {
739 return -EINVAL;
740 }
741
742 /* Sanity check for prefix_len and length */
743 if (rinfo->length > 3) {
744 return -EINVAL;
745 } else if (rinfo->prefix_len > 128) {
746 return -EINVAL;
747 } else if (rinfo->prefix_len > 64) {
748 if (rinfo->length < 2) {
749 return -EINVAL;
750 }
751 } else if (rinfo->prefix_len > 0) {
752 if (rinfo->length < 1) {
753 return -EINVAL;
754 }
755 }
756
757 pref = rinfo->route_pref;
758 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000759 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800760
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900761 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800762
763 if (rinfo->length == 3)
764 prefix = (struct in6_addr *)rinfo->prefix;
765 else {
766 /* this function is safe */
767 ipv6_addr_prefix(&prefix_buf,
768 (struct in6_addr *)rinfo->prefix,
769 rinfo->prefix_len);
770 prefix = &prefix_buf;
771 }
772
Duan Jiongf104a562013-11-08 09:56:53 +0800773 if (rinfo->prefix_len == 0)
774 rt = rt6_get_dflt_router(gwaddr, dev);
775 else
776 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
777 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800778
779 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700780 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800781 rt = NULL;
782 }
783
784 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800785 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800786 pref);
787 else if (rt)
788 rt->rt6i_flags = RTF_ROUTEINFO |
789 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
790
791 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000792 if (!addrconf_finite_timeout(lifetime))
793 rt6_clean_expires(rt);
794 else
795 rt6_set_expires(rt, jiffies + HZ * lifetime);
796
Amerigo Wang94e187c2012-10-29 00:13:19 +0000797 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800798 }
799 return 0;
800}
801#endif
802
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700803static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
804 struct in6_addr *saddr)
805{
806 struct fib6_node *pn;
807 while (1) {
808 if (fn->fn_flags & RTN_TL_ROOT)
809 return NULL;
810 pn = fn->parent;
811 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
812 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
813 else
814 fn = pn;
815 if (fn->fn_flags & RTN_RTINFO)
816 return fn;
817 }
818}
Thomas Grafc71099a2006-08-04 23:20:06 -0700819
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800820static struct rt6_info *ip6_pol_route_lookup(struct net *net,
821 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500822 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct fib6_node *fn;
825 struct rt6_info *rt;
826
Thomas Grafc71099a2006-08-04 23:20:06 -0700827 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500828 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700829restart:
830 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500831 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000832 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200833 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700834 if (rt == net->ipv6.ip6_null_entry) {
835 fn = fib6_backtrack(fn, &fl6->saddr);
836 if (fn)
837 goto restart;
838 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700839 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700840 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700841 return rt;
842
843}
844
Ian Morris67ba4152014-08-24 21:53:10 +0100845struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
Florian Westphalea6e5742011-09-05 16:05:44 +0200846 int flags)
847{
848 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
849}
850EXPORT_SYMBOL_GPL(ip6_route_lookup);
851
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900852struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
853 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700854{
David S. Miller4c9483b2011-03-12 16:22:43 -0500855 struct flowi6 fl6 = {
856 .flowi6_oif = oif,
857 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700858 };
859 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700860 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700861
Thomas Grafadaa70b2006-10-13 15:01:03 -0700862 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500863 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700864 flags |= RT6_LOOKUP_F_HAS_SADDR;
865 }
866
David S. Miller4c9483b2011-03-12 16:22:43 -0500867 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700868 if (dst->error == 0)
869 return (struct rt6_info *) dst;
870
871 dst_release(dst);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return NULL;
874}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900875EXPORT_SYMBOL(rt6_lookup);
876
Thomas Grafc71099a2006-08-04 23:20:06 -0700877/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 It takes new route entry, the addition fails by any reason the
879 route is freed. In any case, if caller does not hold it, it may
880 be destroyed.
881 */
882
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100883static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
Florian Westphale715b6d2015-01-05 23:57:44 +0100884 struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700887 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Thomas Grafc71099a2006-08-04 23:20:06 -0700889 table = rt->rt6i_table;
890 write_lock_bh(&table->tb6_lock);
Florian Westphale715b6d2015-01-05 23:57:44 +0100891 err = fib6_add(&table->tb6_root, rt, info, mxc);
Thomas Grafc71099a2006-08-04 23:20:06 -0700892 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return err;
895}
896
Thomas Graf40e22e82006-08-22 00:00:45 -0700897int ip6_ins_rt(struct rt6_info *rt)
898{
Florian Westphale715b6d2015-01-05 23:57:44 +0100899 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
900 struct mx6_config mxc = { .mx = NULL, };
901
902 return __ip6_ins_rt(rt, &info, &mxc);
Thomas Graf40e22e82006-08-22 00:00:45 -0700903}
904
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700905static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
906 const struct in6_addr *daddr,
907 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct rt6_info *rt;
910
911 /*
912 * Clone the route.
913 */
914
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700915 if (ort->rt6i_flags & RTF_CACHE)
916 ort = (struct rt6_info *)ort->dst.from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700918 rt = ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev,
919 0, ort->rt6i_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700921 if (!rt)
922 return NULL;
923
924 ip6_rt_copy_init(rt, ort);
925 rt->rt6i_flags |= RTF_CACHE;
926 rt->rt6i_metric = 0;
927 rt->dst.flags |= DST_HOST;
928 rt->rt6i_dst.addr = *daddr;
929 rt->rt6i_dst.plen = 128;
930
931 if (!rt6_is_gw_or_nonexthop(ort)) {
932 if (ort->rt6i_dst.plen != 128 &&
933 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
934 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700936 if (rt->rt6i_src.plen && saddr) {
937 rt->rt6i_src.addr = *saddr;
938 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700939 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700940#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800943 return rt;
944}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800946static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500947 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700949 struct fib6_node *fn, *saved_fn;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700950 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700951 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700953 strict |= flags & RT6_LOOKUP_F_IFACE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700954 if (net->ipv6.devconf_all->forwarding == 0)
955 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Thomas Grafc71099a2006-08-04 23:20:06 -0700957 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
David S. Miller4c9483b2011-03-12 16:22:43 -0500959 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700960 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700962redo_rt6_select:
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700963 rt = rt6_select(fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200964 if (rt->rt6i_nsiblings)
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700965 rt = rt6_multipath_select(rt, fl6, oif, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700966 if (rt == net->ipv6.ip6_null_entry) {
967 fn = fib6_backtrack(fn, &fl6->saddr);
968 if (fn)
969 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700970 else if (strict & RT6_LOOKUP_F_REACHABLE) {
971 /* also consider unreachable route */
972 strict &= ~RT6_LOOKUP_F_REACHABLE;
973 fn = saved_fn;
974 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700975 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700976 }
977
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700978 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700979 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800980
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700981 if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
982 goto done;
983 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
984 !(rt->rt6i_flags & RTF_GATEWAY))) {
985 /* Create a RTF_CACHE clone which will not be
986 * owned by the fib6 tree. It is for the special case where
987 * the daddr in the skb during the neighbor look-up is different
988 * from the fl6->daddr used to look-up route here.
989 */
Thomas Grafc71099a2006-08-04 23:20:06 -0700990
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700991 struct rt6_info *uncached_rt;
992
993 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
994 dst_release(&rt->dst);
995
996 if (uncached_rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700997 rt6_uncached_list_add(uncached_rt);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700998 else
999 uncached_rt = net->ipv6.ip6_null_entry;
1000 dst_hold(&uncached_rt->dst);
1001 return uncached_rt;
1002 }
1003
1004done:
1005 rt6_dst_from_metrics_check(rt);
Thomas Grafc71099a2006-08-04 23:20:06 -07001006 return rt;
1007}
1008
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001009static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001010 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001011{
David S. Miller4c9483b2011-03-12 16:22:43 -05001012 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001013}
1014
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001015static struct dst_entry *ip6_route_input_lookup(struct net *net,
1016 struct net_device *dev,
1017 struct flowi6 *fl6, int flags)
1018{
1019 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1020 flags |= RT6_LOOKUP_F_IFACE;
1021
1022 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1023}
1024
Thomas Grafc71099a2006-08-04 23:20:06 -07001025void ip6_route_input(struct sk_buff *skb)
1026{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001027 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001028 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001029 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -05001030 struct flowi6 fl6 = {
1031 .flowi6_iif = skb->dev->ifindex,
1032 .daddr = iph->daddr,
1033 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001034 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05001035 .flowi6_mark = skb->mark,
1036 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001037 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001038
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001039 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001040}
1041
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001042static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001043 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001044{
David S. Miller4c9483b2011-03-12 16:22:43 -05001045 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001046}
1047
Ian Morris67ba4152014-08-24 21:53:10 +01001048struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001049 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001050{
1051 int flags = 0;
1052
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001053 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001054
David S. Miller4c9483b2011-03-12 16:22:43 -05001055 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001056 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001057
David S. Miller4c9483b2011-03-12 16:22:43 -05001058 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001059 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001060 else if (sk)
1061 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001062
David S. Miller4c9483b2011-03-12 16:22:43 -05001063 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001065EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
David S. Miller2774c132011-03-01 14:59:04 -08001067struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001068{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001069 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001070 struct dst_entry *new = NULL;
1071
David S. Millerf5b0a872012-07-19 12:31:33 -07001072 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001073 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001074 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001075
Steffen Klassert81048912012-07-05 23:37:09 +00001076 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
Steffen Klassert81048912012-07-05 23:37:09 +00001077
David S. Miller14e50e52007-05-24 18:17:54 -07001078 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001079 new->input = dst_discard;
Eric Dumazetaad88722014-04-15 13:47:15 -04001080 new->output = dst_discard_sk;
David S. Miller14e50e52007-05-24 18:17:54 -07001081
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001082 if (dst_metrics_read_only(&ort->dst))
1083 new->_metrics = ort->dst._metrics;
1084 else
1085 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001086 rt->rt6i_idev = ort->rt6i_idev;
1087 if (rt->rt6i_idev)
1088 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001089
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001090 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001091 rt->rt6i_flags = ort->rt6i_flags;
David S. Miller14e50e52007-05-24 18:17:54 -07001092 rt->rt6i_metric = 0;
1093
1094 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1095#ifdef CONFIG_IPV6_SUBTREES
1096 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1097#endif
1098
1099 dst_free(new);
1100 }
1101
David S. Miller69ead7a2011-03-01 14:45:33 -08001102 dst_release(dst_orig);
1103 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001104}
David S. Miller14e50e52007-05-24 18:17:54 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
1107 * Destination cache support functions
1108 */
1109
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001110static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1111{
1112 if (rt->dst.from &&
1113 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1114 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1115}
1116
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001117static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
1118{
1119 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1120 return NULL;
1121
1122 if (rt6_check_expired(rt))
1123 return NULL;
1124
1125 return &rt->dst;
1126}
1127
1128static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
1129{
1130 if (rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
1131 rt6_check((struct rt6_info *)(rt->dst.from), cookie))
1132 return &rt->dst;
1133 else
1134 return NULL;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1138{
1139 struct rt6_info *rt;
1140
1141 rt = (struct rt6_info *) dst;
1142
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001143 /* All IPV6 dsts are created with ->obsolete set to the value
1144 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1145 * into this function always.
1146 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001147
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001148 rt6_dst_from_metrics_check(rt);
1149
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001150 if (unlikely(dst->flags & DST_NOCACHE))
1151 return rt6_dst_from_check(rt, cookie);
1152 else
1153 return rt6_check(rt, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1157{
1158 struct rt6_info *rt = (struct rt6_info *) dst;
1159
1160 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001161 if (rt->rt6i_flags & RTF_CACHE) {
1162 if (rt6_check_expired(rt)) {
1163 ip6_del_rt(rt);
1164 dst = NULL;
1165 }
1166 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001168 dst = NULL;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001171 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static void ip6_link_failure(struct sk_buff *skb)
1175{
1176 struct rt6_info *rt;
1177
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001178 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Eric Dumazetadf30902009-06-02 05:19:30 +00001180 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001182 if (rt->rt6i_flags & RTF_CACHE) {
1183 dst_hold(&rt->dst);
1184 if (ip6_del_rt(rt))
1185 dst_free(&rt->dst);
1186 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 rt->rt6i_node->fn_sernum = -1;
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190}
1191
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001192static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
1193{
1194 struct net *net = dev_net(rt->dst.dev);
1195
1196 rt->rt6i_flags |= RTF_MODIFIED;
1197 rt->rt6i_pmtu = mtu;
1198 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
1199}
1200
1201static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
1202 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Ian Morris67ba4152014-08-24 21:53:10 +01001204 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001206 if (rt6->rt6i_flags & RTF_LOCAL)
1207 return;
1208
David S. Miller81aded22012-06-15 14:54:11 -07001209 dst_confirm(dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001210 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
1211 if (mtu >= dst_mtu(dst))
1212 return;
David S. Miller81aded22012-06-15 14:54:11 -07001213
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001214 if (rt6->rt6i_flags & RTF_CACHE) {
1215 rt6_do_update_pmtu(rt6, mtu);
1216 } else {
1217 const struct in6_addr *daddr, *saddr;
1218 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001219
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001220 if (iph) {
1221 daddr = &iph->daddr;
1222 saddr = &iph->saddr;
1223 } else if (sk) {
1224 daddr = &sk->sk_v6_daddr;
1225 saddr = &inet6_sk(sk)->saddr;
1226 } else {
1227 return;
1228 }
1229 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
1230 if (nrt6) {
1231 rt6_do_update_pmtu(nrt6, mtu);
1232
1233 /* ip6_ins_rt(nrt6) will bump the
1234 * rt6->rt6i_node->fn_sernum
1235 * which will fail the next rt6_check() and
1236 * invalidate the sk->sk_dst_cache.
1237 */
1238 ip6_ins_rt(nrt6);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241}
1242
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001243static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1244 struct sk_buff *skb, u32 mtu)
1245{
1246 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
1247}
1248
David S. Miller42ae66c2012-06-15 20:01:57 -07001249void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1250 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001251{
1252 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1253 struct dst_entry *dst;
1254 struct flowi6 fl6;
1255
1256 memset(&fl6, 0, sizeof(fl6));
1257 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001258 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001259 fl6.daddr = iph->daddr;
1260 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001261 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001262
1263 dst = ip6_route_output(net, NULL, &fl6);
1264 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001265 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001266 dst_release(dst);
1267}
1268EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1269
1270void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1271{
1272 ip6_update_pmtu(skb, sock_net(sk), mtu,
1273 sk->sk_bound_dev_if, sk->sk_mark);
1274}
1275EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1276
Duan Jiongb55b76b2013-09-04 19:44:21 +08001277/* Handle redirects */
1278struct ip6rd_flowi {
1279 struct flowi6 fl6;
1280 struct in6_addr gateway;
1281};
1282
1283static struct rt6_info *__ip6_route_redirect(struct net *net,
1284 struct fib6_table *table,
1285 struct flowi6 *fl6,
1286 int flags)
1287{
1288 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1289 struct rt6_info *rt;
1290 struct fib6_node *fn;
1291
1292 /* Get the "current" route for this destination and
1293 * check if the redirect has come from approriate router.
1294 *
1295 * RFC 4861 specifies that redirects should only be
1296 * accepted if they come from the nexthop to the target.
1297 * Due to the way the routes are chosen, this notion
1298 * is a bit fuzzy and one might need to check all possible
1299 * routes.
1300 */
1301
1302 read_lock_bh(&table->tb6_lock);
1303 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1304restart:
1305 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1306 if (rt6_check_expired(rt))
1307 continue;
1308 if (rt->dst.error)
1309 break;
1310 if (!(rt->rt6i_flags & RTF_GATEWAY))
1311 continue;
1312 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1313 continue;
1314 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1315 continue;
1316 break;
1317 }
1318
1319 if (!rt)
1320 rt = net->ipv6.ip6_null_entry;
1321 else if (rt->dst.error) {
1322 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001323 goto out;
1324 }
1325
1326 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001327 fn = fib6_backtrack(fn, &fl6->saddr);
1328 if (fn)
1329 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001330 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001331
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001332out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001333 dst_hold(&rt->dst);
1334
1335 read_unlock_bh(&table->tb6_lock);
1336
1337 return rt;
1338};
1339
1340static struct dst_entry *ip6_route_redirect(struct net *net,
1341 const struct flowi6 *fl6,
1342 const struct in6_addr *gateway)
1343{
1344 int flags = RT6_LOOKUP_F_HAS_SADDR;
1345 struct ip6rd_flowi rdfl;
1346
1347 rdfl.fl6 = *fl6;
1348 rdfl.gateway = *gateway;
1349
1350 return fib6_rule_lookup(net, &rdfl.fl6,
1351 flags, __ip6_route_redirect);
1352}
1353
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001354void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1355{
1356 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1357 struct dst_entry *dst;
1358 struct flowi6 fl6;
1359
1360 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001361 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001362 fl6.flowi6_oif = oif;
1363 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001364 fl6.daddr = iph->daddr;
1365 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001366 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001367
Duan Jiongb55b76b2013-09-04 19:44:21 +08001368 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1369 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001370 dst_release(dst);
1371}
1372EXPORT_SYMBOL_GPL(ip6_redirect);
1373
Duan Jiongc92a59e2013-08-22 12:07:35 +08001374void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1375 u32 mark)
1376{
1377 const struct ipv6hdr *iph = ipv6_hdr(skb);
1378 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1379 struct dst_entry *dst;
1380 struct flowi6 fl6;
1381
1382 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001383 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001384 fl6.flowi6_oif = oif;
1385 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001386 fl6.daddr = msg->dest;
1387 fl6.saddr = iph->daddr;
1388
Duan Jiongb55b76b2013-09-04 19:44:21 +08001389 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1390 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001391 dst_release(dst);
1392}
1393
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001394void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1395{
1396 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1397}
1398EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1399
David S. Miller0dbaee32010-12-13 12:52:14 -08001400static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
David S. Miller0dbaee32010-12-13 12:52:14 -08001402 struct net_device *dev = dst->dev;
1403 unsigned int mtu = dst_mtu(dst);
1404 struct net *net = dev_net(dev);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1407
Daniel Lezcano55786892008-03-04 13:47:47 -08001408 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1409 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001412 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1413 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1414 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * rely only on pmtu discovery"
1416 */
1417 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1418 mtu = IPV6_MAXPLEN;
1419 return mtu;
1420}
1421
Steffen Klassertebb762f2011-11-23 02:12:51 +00001422static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001423{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001424 const struct rt6_info *rt = (const struct rt6_info *)dst;
1425 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001426 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001427
1428 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001429 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001430
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001431 mtu = dst_metric_raw(dst, RTAX_MTU);
1432 if (mtu)
1433 goto out;
1434
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001435 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001436
1437 rcu_read_lock();
1438 idev = __in6_dev_get(dst->dev);
1439 if (idev)
1440 mtu = idev->cnf.mtu6;
1441 rcu_read_unlock();
1442
Eric Dumazet30f78d82014-04-10 21:23:36 -07001443out:
1444 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001445}
1446
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001447static struct dst_entry *icmp6_dst_gc_list;
1448static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001449
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001450struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001451 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
David S. Miller87a11572011-12-06 17:04:13 -05001453 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 struct rt6_info *rt;
1455 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001456 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
David S. Miller38308472011-12-03 18:02:47 -05001458 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001459 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
David S. Miller8b96d222012-06-11 02:01:56 -07001461 rt = ip6_dst_alloc(net, dev, 0, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001462 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001464 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto out;
1466 }
1467
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001468 rt->dst.flags |= DST_HOST;
1469 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001470 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001471 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001472 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001473 rt->rt6i_dst.plen = 128;
1474 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001475 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001477 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001478 rt->dst.next = icmp6_dst_gc_list;
1479 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001480 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Daniel Lezcano55786892008-03-04 13:47:47 -08001482 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
David S. Miller87a11572011-12-06 17:04:13 -05001484 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486out:
David S. Miller87a11572011-12-06 17:04:13 -05001487 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001490int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001492 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001493 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001495 spin_lock_bh(&icmp6_dst_lock);
1496 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 while ((dst = *pprev) != NULL) {
1499 if (!atomic_read(&dst->__refcnt)) {
1500 *pprev = dst->next;
1501 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 } else {
1503 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001504 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 }
1507
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001508 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001509
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001510 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
David S. Miller1e493d12008-09-10 17:27:15 -07001513static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1514 void *arg)
1515{
1516 struct dst_entry *dst, **pprev;
1517
1518 spin_lock_bh(&icmp6_dst_lock);
1519 pprev = &icmp6_dst_gc_list;
1520 while ((dst = *pprev) != NULL) {
1521 struct rt6_info *rt = (struct rt6_info *) dst;
1522 if (func(rt, arg)) {
1523 *pprev = dst->next;
1524 dst_free(dst);
1525 } else {
1526 pprev = &dst->next;
1527 }
1528 }
1529 spin_unlock_bh(&icmp6_dst_lock);
1530}
1531
Daniel Lezcano569d3642008-01-18 03:56:57 -08001532static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001534 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001535 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1536 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1537 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1538 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1539 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001540 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric Dumazetfc66f952010-10-08 06:37:34 +00001542 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001543 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001544 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto out;
1546
Benjamin Thery6891a342008-03-04 13:49:47 -08001547 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001548 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001549 entries = dst_entries_get_slow(ops);
1550 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001551 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001553 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001554 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Florian Westphale715b6d2015-01-05 23:57:44 +01001557static int ip6_convert_metrics(struct mx6_config *mxc,
1558 const struct fib6_config *cfg)
1559{
1560 struct nlattr *nla;
1561 int remaining;
1562 u32 *mp;
1563
Ian Morris63159f22015-03-29 14:00:04 +01001564 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001565 return 0;
1566
1567 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1568 if (unlikely(!mp))
1569 return -ENOMEM;
1570
1571 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1572 int type = nla_type(nla);
1573
1574 if (type) {
Daniel Borkmannea697632015-01-05 23:57:47 +01001575 u32 val;
1576
Florian Westphale715b6d2015-01-05 23:57:44 +01001577 if (unlikely(type > RTAX_MAX))
1578 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001579 if (type == RTAX_CC_ALGO) {
1580 char tmp[TCP_CA_NAME_MAX];
Florian Westphale715b6d2015-01-05 23:57:44 +01001581
Daniel Borkmannea697632015-01-05 23:57:47 +01001582 nla_strlcpy(tmp, nla, sizeof(tmp));
1583 val = tcp_ca_get_key_by_name(tmp);
1584 if (val == TCP_CA_UNSPEC)
1585 goto err;
1586 } else {
1587 val = nla_get_u32(nla);
1588 }
1589
1590 mp[type - 1] = val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001591 __set_bit(type - 1, mxc->mx_valid);
1592 }
1593 }
1594
1595 mxc->mx = mp;
1596
1597 return 0;
1598 err:
1599 kfree(mp);
1600 return -EINVAL;
1601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Thomas Graf86872cb2006-08-22 00:01:08 -07001603int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001606 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct rt6_info *rt = NULL;
1608 struct net_device *dev = NULL;
1609 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001610 struct fib6_table *table;
Florian Westphale715b6d2015-01-05 23:57:44 +01001611 struct mx6_config mxc = { .mx = NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int addr_type;
1613
Thomas Graf86872cb2006-08-22 00:01:08 -07001614 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return -EINVAL;
1616#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001617 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -EINVAL;
1619#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001620 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001622 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!dev)
1624 goto out;
1625 idev = in6_dev_get(dev);
1626 if (!idev)
1627 goto out;
1628 }
1629
Thomas Graf86872cb2006-08-22 00:01:08 -07001630 if (cfg->fc_metric == 0)
1631 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Matti Vaittinend71314b2011-11-14 00:14:49 +00001633 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001634 if (cfg->fc_nlinfo.nlh &&
1635 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001636 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001637 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001638 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001639 table = fib6_new_table(net, cfg->fc_table);
1640 }
1641 } else {
1642 table = fib6_new_table(net, cfg->fc_table);
1643 }
David S. Miller38308472011-12-03 18:02:47 -05001644
1645 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001646 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001647
Sabrina Dubrocac88507f2014-03-06 17:51:57 +01001648 rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
David S. Miller38308472011-12-03 18:02:47 -05001650 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 err = -ENOMEM;
1652 goto out;
1653 }
1654
Gao feng1716a962012-04-06 00:13:10 +00001655 if (cfg->fc_flags & RTF_EXPIRES)
1656 rt6_set_expires(rt, jiffies +
1657 clock_t_to_jiffies(cfg->fc_expires));
1658 else
1659 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Thomas Graf86872cb2006-08-22 00:01:08 -07001661 if (cfg->fc_protocol == RTPROT_UNSPEC)
1662 cfg->fc_protocol = RTPROT_BOOT;
1663 rt->rt6i_protocol = cfg->fc_protocol;
1664
1665 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001668 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001669 else if (cfg->fc_flags & RTF_LOCAL)
1670 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001672 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Changli Gaod8d1f302010-06-10 23:31:35 -07001674 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Thomas Graf86872cb2006-08-22 00:01:08 -07001676 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1677 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07001678 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001679 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001682 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1683 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684#endif
1685
Thomas Graf86872cb2006-08-22 00:01:08 -07001686 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /* We cannot add true routes via loopback here,
1689 they would result in kernel looping; promote them to reject routes
1690 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001691 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001692 (dev && (dev->flags & IFF_LOOPBACK) &&
1693 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1694 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001696 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (dev) {
1698 dev_put(dev);
1699 in6_dev_put(idev);
1700 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001701 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 dev_hold(dev);
1703 idev = in6_dev_get(dev);
1704 if (!idev) {
1705 err = -ENODEV;
1706 goto out;
1707 }
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001710 switch (cfg->fc_type) {
1711 case RTN_BLACKHOLE:
1712 rt->dst.error = -EINVAL;
Eric Dumazetaad88722014-04-15 13:47:15 -04001713 rt->dst.output = dst_discard_sk;
Kamala R7150aed2013-12-02 19:55:21 +05301714 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001715 break;
1716 case RTN_PROHIBIT:
1717 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301718 rt->dst.output = ip6_pkt_prohibit_out;
1719 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001720 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001721 case RTN_THROW:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001722 default:
Kamala R7150aed2013-12-02 19:55:21 +05301723 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
1724 : -ENETUNREACH;
1725 rt->dst.output = ip6_pkt_discard_out;
1726 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001727 break;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto install_route;
1730 }
1731
Thomas Graf86872cb2006-08-22 00:01:08 -07001732 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001733 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int gwa_type;
1735
Thomas Graf86872cb2006-08-22 00:01:08 -07001736 gw_addr = &cfg->fc_gateway;
Florian Westphal48ed7b22015-05-21 00:25:41 +02001737
1738 /* if gw_addr is local we will fail to detect this in case
1739 * address is still TENTATIVE (DAD in progress). rt6_lookup()
1740 * will return already-added prefix route via interface that
1741 * prefix route was assigned to, which might be non-loopback.
1742 */
1743 err = -EINVAL;
1744 if (ipv6_chk_addr_and_flags(net, gw_addr, NULL, 0, 0))
1745 goto out;
1746
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001747 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 gwa_type = ipv6_addr_type(gw_addr);
1749
1750 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1751 struct rt6_info *grt;
1752
1753 /* IPv6 strictly inhibits using not link-local
1754 addresses as nexthop address.
1755 Otherwise, router will not able to send redirects.
1756 It is very good, but in some (rare!) circumstances
1757 (SIT, PtP, NBMA NOARP links) it is handy to allow
1758 some exceptions. --ANK
1759 */
David S. Miller38308472011-12-03 18:02:47 -05001760 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto out;
1762
Daniel Lezcano55786892008-03-04 13:47:47 -08001763 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001766 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto out;
1768 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001769 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001770 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto out;
1772 }
1773 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001774 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 idev = grt->rt6i_idev;
1776 dev_hold(dev);
1777 in6_dev_hold(grt->rt6i_idev);
1778 }
David S. Miller38308472011-12-03 18:02:47 -05001779 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001781 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (err)
1784 goto out;
1785 }
1786 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001787 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out;
1789 }
1790
1791 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001792 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto out;
1794
Daniel Walterc3968a82011-04-13 21:10:57 +00001795 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1796 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1797 err = -EINVAL;
1798 goto out;
1799 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001800 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001801 rt->rt6i_prefsrc.plen = 128;
1802 } else
1803 rt->rt6i_prefsrc.plen = 0;
1804
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001808 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001810 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001811
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001812 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001813
Florian Westphale715b6d2015-01-05 23:57:44 +01001814 err = ip6_convert_metrics(&mxc, cfg);
1815 if (err)
1816 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Florian Westphale715b6d2015-01-05 23:57:44 +01001818 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1819
1820 kfree(mxc.mx);
1821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822out:
1823 if (dev)
1824 dev_put(dev);
1825 if (idev)
1826 in6_dev_put(idev);
1827 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001828 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830}
1831
Thomas Graf86872cb2006-08-22 00:01:08 -07001832static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001835 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001836 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Gao feng6825a262012-09-19 19:25:34 +00001838 if (rt == net->ipv6.ip6_null_entry) {
1839 err = -ENOENT;
1840 goto out;
1841 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07001842
Thomas Grafc71099a2006-08-04 23:20:06 -07001843 table = rt->rt6i_table;
1844 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07001845 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07001846 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Gao feng6825a262012-09-19 19:25:34 +00001848out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001849 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return err;
1851}
1852
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001853int ip6_del_rt(struct rt6_info *rt)
1854{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001855 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001856 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001857 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001858 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001859}
1860
Thomas Graf86872cb2006-08-22 00:01:08 -07001861static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Thomas Grafc71099a2006-08-04 23:20:06 -07001863 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct fib6_node *fn;
1865 struct rt6_info *rt;
1866 int err = -ESRCH;
1867
Daniel Lezcano55786892008-03-04 13:47:47 -08001868 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001869 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001870 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Thomas Grafc71099a2006-08-04 23:20:06 -07001872 read_lock_bh(&table->tb6_lock);
1873
1874 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001875 &cfg->fc_dst, cfg->fc_dst_len,
1876 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001879 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07001880 if ((rt->rt6i_flags & RTF_CACHE) &&
1881 !(cfg->fc_flags & RTF_CACHE))
1882 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001883 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001884 (!rt->dst.dev ||
1885 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001887 if (cfg->fc_flags & RTF_GATEWAY &&
1888 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001890 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001892 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001893 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Thomas Graf86872cb2006-08-22 00:01:08 -07001895 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001898 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 return err;
1901}
1902
David S. Miller6700c272012-07-17 03:29:28 -07001903static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001904{
David S. Millere8599ff2012-07-11 23:43:53 -07001905 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001906 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07001907 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001908 struct ndisc_options ndopts;
1909 struct inet6_dev *in6_dev;
1910 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001911 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07001912 int optlen, on_link;
1913 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07001914
Simon Horman29a3cad2013-05-28 20:34:26 +00001915 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001916 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07001917
1918 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07001919 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001920 return;
1921 }
1922
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001923 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07001924
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001925 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001926 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001927 return;
1928 }
1929
David S. Miller6e157b62012-07-12 00:05:02 -07001930 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001931 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001932 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001933 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07001934 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001935 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001936 return;
1937 }
1938
1939 in6_dev = __in6_dev_get(skb->dev);
1940 if (!in6_dev)
1941 return;
1942 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1943 return;
1944
1945 /* RFC2461 8.1:
1946 * The IP source address of the Redirect MUST be the same as the current
1947 * first-hop router for the specified ICMP Destination Address.
1948 */
1949
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001950 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001951 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1952 return;
1953 }
David S. Miller6e157b62012-07-12 00:05:02 -07001954
1955 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001956 if (ndopts.nd_opts_tgt_lladdr) {
1957 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1958 skb->dev);
1959 if (!lladdr) {
1960 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1961 return;
1962 }
1963 }
1964
David S. Miller6e157b62012-07-12 00:05:02 -07001965 rt = (struct rt6_info *) dst;
1966 if (rt == net->ipv6.ip6_null_entry) {
1967 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1968 return;
1969 }
1970
1971 /* Redirect received -> path was valid.
1972 * Look, redirects are sent only in response to data packets,
1973 * so that this nexthop apparently is reachable. --ANK
1974 */
1975 dst_confirm(&rt->dst);
1976
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001977 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07001978 if (!neigh)
1979 return;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * We have finally decided to accept it.
1983 */
1984
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001985 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1987 NEIGH_UPDATE_F_OVERRIDE|
1988 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1989 NEIGH_UPDATE_F_ISROUTER))
1990 );
1991
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001992 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001993 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out;
1995
1996 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1997 if (on_link)
1998 nrt->rt6i_flags &= ~RTF_GATEWAY;
1999
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002000 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Thomas Graf40e22e82006-08-22 00:00:45 -07002002 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 goto out;
2004
Changli Gaod8d1f302010-06-10 23:31:35 -07002005 netevent.old = &rt->dst;
2006 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002007 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00002008 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07002009 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
2010
David S. Miller38308472011-12-03 18:02:47 -05002011 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07002012 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002013 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
2016out:
David S. Millere8599ff2012-07-11 23:43:53 -07002017 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07002018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * Misc support functions
2022 */
2023
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002024static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
2025{
2026 BUG_ON(from->dst.from);
2027
2028 rt->rt6i_flags &= ~RTF_EXPIRES;
2029 dst_hold(&from->dst);
2030 rt->dst.from = &from->dst;
2031 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
2032}
2033
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002034static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002036 rt->dst.input = ort->dst.input;
2037 rt->dst.output = ort->dst.output;
2038 rt->rt6i_dst = ort->rt6i_dst;
2039 rt->dst.error = ort->dst.error;
2040 rt->rt6i_idev = ort->rt6i_idev;
2041 if (rt->rt6i_idev)
2042 in6_dev_hold(rt->rt6i_idev);
2043 rt->dst.lastuse = jiffies;
2044 rt->rt6i_gateway = ort->rt6i_gateway;
2045 rt->rt6i_flags = ort->rt6i_flags;
2046 rt6_set_from(rt, ort);
2047 rt->rt6i_metric = ort->rt6i_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002049 rt->rt6i_src = ort->rt6i_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050#endif
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002051 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
2052 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002055#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002056static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002057 const struct in6_addr *prefix, int prefixlen,
2058 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002059{
2060 struct fib6_node *fn;
2061 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002062 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002063
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002064 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05002065 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002066 return NULL;
2067
Li RongQing5744dd92012-09-11 21:59:01 +00002068 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002069 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002070 if (!fn)
2071 goto out;
2072
Changli Gaod8d1f302010-06-10 23:31:35 -07002073 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002074 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002075 continue;
2076 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
2077 continue;
2078 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
2079 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002080 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002081 break;
2082 }
2083out:
Li RongQing5744dd92012-09-11 21:59:01 +00002084 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002085 return rt;
2086}
2087
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002088static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002089 const struct in6_addr *prefix, int prefixlen,
2090 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00002091 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002092{
Thomas Graf86872cb2006-08-22 00:01:08 -07002093 struct fib6_config cfg = {
2094 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002095 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002096 .fc_ifindex = ifindex,
2097 .fc_dst_len = prefixlen,
2098 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2099 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002100 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002101 .fc_nlinfo.nlh = NULL,
2102 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002103 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002104
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002105 cfg.fc_dst = *prefix;
2106 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002107
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002108 /* We should treat it as a default route if prefix length is 0. */
2109 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002110 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002111
Thomas Graf86872cb2006-08-22 00:01:08 -07002112 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002113
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002114 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002115}
2116#endif
2117
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002118struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002121 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002123 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002124 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002125 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Li RongQing5744dd92012-09-11 21:59:01 +00002127 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002128 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002129 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002130 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2132 break;
2133 }
2134 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002135 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002136 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return rt;
2138}
2139
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002140struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002141 struct net_device *dev,
2142 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Thomas Graf86872cb2006-08-22 00:01:08 -07002144 struct fib6_config cfg = {
2145 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002146 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002147 .fc_ifindex = dev->ifindex,
2148 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2149 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002150 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002151 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002152 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002155 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Thomas Graf86872cb2006-08-22 00:01:08 -07002157 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return rt6_get_dflt_router(gwaddr, dev);
2160}
2161
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002162void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002165 struct fib6_table *table;
2166
2167 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002168 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002169 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002173 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002174 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002175 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2176 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002177 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002178 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002179 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto restart;
2181 }
2182 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002183 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Daniel Lezcano55786892008-03-04 13:47:47 -08002186static void rtmsg_to_fib6_config(struct net *net,
2187 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002188 struct fib6_config *cfg)
2189{
2190 memset(cfg, 0, sizeof(*cfg));
2191
2192 cfg->fc_table = RT6_TABLE_MAIN;
2193 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2194 cfg->fc_metric = rtmsg->rtmsg_metric;
2195 cfg->fc_expires = rtmsg->rtmsg_info;
2196 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2197 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2198 cfg->fc_flags = rtmsg->rtmsg_flags;
2199
Daniel Lezcano55786892008-03-04 13:47:47 -08002200 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002201
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002202 cfg->fc_dst = rtmsg->rtmsg_dst;
2203 cfg->fc_src = rtmsg->rtmsg_src;
2204 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002205}
2206
Daniel Lezcano55786892008-03-04 13:47:47 -08002207int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Thomas Graf86872cb2006-08-22 00:01:08 -07002209 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct in6_rtmsg rtmsg;
2211 int err;
2212
Ian Morris67ba4152014-08-24 21:53:10 +01002213 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 case SIOCADDRT: /* Add a route */
2215 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002216 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return -EPERM;
2218 err = copy_from_user(&rtmsg, arg,
2219 sizeof(struct in6_rtmsg));
2220 if (err)
2221 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002222
Daniel Lezcano55786892008-03-04 13:47:47 -08002223 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rtnl_lock();
2226 switch (cmd) {
2227 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002228 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002231 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 break;
2233 default:
2234 err = -EINVAL;
2235 }
2236 rtnl_unlock();
2237
2238 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 return -EINVAL;
2242}
2243
2244/*
2245 * Drop the packet on the floor
2246 */
2247
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002248static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002250 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002251 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002252 switch (ipstats_mib_noroutes) {
2253 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002254 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002255 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002256 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2257 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002258 break;
2259 }
2260 /* FALLTHROUGH */
2261 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002262 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2263 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002264 break;
2265 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002266 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 kfree_skb(skb);
2268 return 0;
2269}
2270
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002271static int ip6_pkt_discard(struct sk_buff *skb)
2272{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002273 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002274}
2275
Eric Dumazetaad88722014-04-15 13:47:15 -04002276static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Eric Dumazetadf30902009-06-02 05:19:30 +00002278 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002279 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002282static int ip6_pkt_prohibit(struct sk_buff *skb)
2283{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002284 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002285}
2286
Eric Dumazetaad88722014-04-15 13:47:15 -04002287static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002288{
Eric Dumazetadf30902009-06-02 05:19:30 +00002289 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002290 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293/*
2294 * Allocate a dst for local (unicast / anycast) address.
2295 */
2296
2297struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2298 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002299 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002301 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002302 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2303 DST_NOCOUNT, NULL);
2304 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return ERR_PTR(-ENOMEM);
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 in6_dev_hold(idev);
2308
David S. Miller11d53b42011-06-24 15:23:34 -07002309 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002310 rt->dst.input = ip6_input;
2311 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002315 if (anycast)
2316 rt->rt6i_flags |= RTF_ANYCAST;
2317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Julian Anastasov550bab42013-10-20 15:43:04 +03002320 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002321 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002323 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Changli Gaod8d1f302010-06-10 23:31:35 -07002325 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 return rt;
2328}
2329
Daniel Walterc3968a82011-04-13 21:10:57 +00002330int ip6_route_get_saddr(struct net *net,
2331 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002332 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002333 unsigned int prefs,
2334 struct in6_addr *saddr)
2335{
Markus Stenberge16e8882015-05-05 13:36:59 +03002336 struct inet6_dev *idev =
2337 rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
Daniel Walterc3968a82011-04-13 21:10:57 +00002338 int err = 0;
Markus Stenberge16e8882015-05-05 13:36:59 +03002339 if (rt && rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002340 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002341 else
2342 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2343 daddr, prefs, saddr);
2344 return err;
2345}
2346
2347/* remove deleted ip from prefsrc entries */
2348struct arg_dev_net_ip {
2349 struct net_device *dev;
2350 struct net *net;
2351 struct in6_addr *addr;
2352};
2353
2354static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2355{
2356 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2357 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2358 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2359
David S. Millerd1918542011-12-28 20:19:20 -05002360 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002361 rt != net->ipv6.ip6_null_entry &&
2362 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2363 /* remove prefsrc entry */
2364 rt->rt6i_prefsrc.plen = 0;
2365 }
2366 return 0;
2367}
2368
2369void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2370{
2371 struct net *net = dev_net(ifp->idev->dev);
2372 struct arg_dev_net_ip adni = {
2373 .dev = ifp->idev->dev,
2374 .net = net,
2375 .addr = &ifp->addr,
2376 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002377 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002378}
2379
Duan Jiongbe7a0102014-05-15 15:56:14 +08002380#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2381#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2382
2383/* Remove routers and update dst entries when gateway turn into host. */
2384static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2385{
2386 struct in6_addr *gateway = (struct in6_addr *)arg;
2387
2388 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2389 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2390 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2391 return -1;
2392 }
2393 return 0;
2394}
2395
2396void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2397{
2398 fib6_clean_all(net, fib6_clean_tohost, gateway);
2399}
2400
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002401struct arg_dev_net {
2402 struct net_device *dev;
2403 struct net *net;
2404};
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406static int fib6_ifdown(struct rt6_info *rt, void *arg)
2407{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002408 const struct arg_dev_net *adn = arg;
2409 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002410
David S. Millerd1918542011-12-28 20:19:20 -05002411 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002412 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return 0;
2416}
2417
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002418void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002420 struct arg_dev_net adn = {
2421 .dev = dev,
2422 .net = net,
2423 };
2424
Li RongQing0c3584d2013-12-27 16:32:38 +08002425 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002426 icmp6_clean_all(fib6_ifdown, &adn);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07002427 rt6_uncached_list_flush_dev(net, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Eric Dumazet95c96172012-04-15 05:58:06 +00002430struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002432 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433};
2434
2435static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2436{
2437 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2438 struct inet6_dev *idev;
2439
2440 /* In IPv6 pmtu discovery is not optional,
2441 so that RTAX_MTU lock cannot disable it.
2442 We still use this lock to block changes
2443 caused by addrconf/ndisc.
2444 */
2445
2446 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002447 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return 0;
2449
2450 /* For administrative MTU increase, there is no way to discover
2451 IPv6 PMTU increase, so PMTU increase should be updated here.
2452 Since RFC 1981 doesn't include administrative MTU increase
2453 update PMTU increase is a MUST. (i.e. jumbo frame)
2454 */
2455 /*
2456 If new MTU is less than route PMTU, this new MTU will be the
2457 lowest MTU in the path, update the route PMTU to reflect PMTU
2458 decreases; if new MTU is greater than route PMTU, and the
2459 old MTU is the lowest MTU in the path, update the route PMTU
2460 to reflect the increase. In this case if the other nodes' MTU
2461 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2462 PMTU discouvery.
2463 */
David S. Millerd1918542011-12-28 20:19:20 -05002464 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002465 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2466 if (rt->rt6i_flags & RTF_CACHE) {
2467 /* For RTF_CACHE with rt6i_pmtu == 0
2468 * (i.e. a redirected route),
2469 * the metrics of its rt->dst.from has already
2470 * been updated.
2471 */
2472 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2473 rt->rt6i_pmtu = arg->mtu;
2474 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2475 (dst_mtu(&rt->dst) < arg->mtu &&
2476 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2477 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2478 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return 0;
2481}
2482
Eric Dumazet95c96172012-04-15 05:58:06 +00002483void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Thomas Grafc71099a2006-08-04 23:20:06 -07002485 struct rt6_mtu_change_arg arg = {
2486 .dev = dev,
2487 .mtu = mtu,
2488 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Li RongQing0c3584d2013-12-27 16:32:38 +08002490 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002493static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002494 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002495 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002496 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002497 [RTA_PRIORITY] = { .type = NLA_U32 },
2498 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002499 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002500 [RTA_PREF] = { .type = NLA_U8 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002501};
2502
2503static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2504 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Thomas Graf86872cb2006-08-22 00:01:08 -07002506 struct rtmsg *rtm;
2507 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002508 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Thomas Graf86872cb2006-08-22 00:01:08 -07002511 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2512 if (err < 0)
2513 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Thomas Graf86872cb2006-08-22 00:01:08 -07002515 err = -EINVAL;
2516 rtm = nlmsg_data(nlh);
2517 memset(cfg, 0, sizeof(*cfg));
2518
2519 cfg->fc_table = rtm->rtm_table;
2520 cfg->fc_dst_len = rtm->rtm_dst_len;
2521 cfg->fc_src_len = rtm->rtm_src_len;
2522 cfg->fc_flags = RTF_UP;
2523 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002524 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002525
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002526 if (rtm->rtm_type == RTN_UNREACHABLE ||
2527 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002528 rtm->rtm_type == RTN_PROHIBIT ||
2529 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002530 cfg->fc_flags |= RTF_REJECT;
2531
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002532 if (rtm->rtm_type == RTN_LOCAL)
2533 cfg->fc_flags |= RTF_LOCAL;
2534
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002535 if (rtm->rtm_flags & RTM_F_CLONED)
2536 cfg->fc_flags |= RTF_CACHE;
2537
Eric W. Biederman15e47302012-09-07 20:12:54 +00002538 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002539 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002540 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002541
2542 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002543 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002544 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002546
2547 if (tb[RTA_DST]) {
2548 int plen = (rtm->rtm_dst_len + 7) >> 3;
2549
2550 if (nla_len(tb[RTA_DST]) < plen)
2551 goto errout;
2552
2553 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002555
2556 if (tb[RTA_SRC]) {
2557 int plen = (rtm->rtm_src_len + 7) >> 3;
2558
2559 if (nla_len(tb[RTA_SRC]) < plen)
2560 goto errout;
2561
2562 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002564
Daniel Walterc3968a82011-04-13 21:10:57 +00002565 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002566 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002567
Thomas Graf86872cb2006-08-22 00:01:08 -07002568 if (tb[RTA_OIF])
2569 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2570
2571 if (tb[RTA_PRIORITY])
2572 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2573
2574 if (tb[RTA_METRICS]) {
2575 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2576 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002578
2579 if (tb[RTA_TABLE])
2580 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2581
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002582 if (tb[RTA_MULTIPATH]) {
2583 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2584 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2585 }
2586
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002587 if (tb[RTA_PREF]) {
2588 pref = nla_get_u8(tb[RTA_PREF]);
2589 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2590 pref != ICMPV6_ROUTER_PREF_HIGH)
2591 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2592 cfg->fc_flags |= RTF_PREF(pref);
2593 }
2594
Thomas Graf86872cb2006-08-22 00:01:08 -07002595 err = 0;
2596errout:
2597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002600static int ip6_route_multipath(struct fib6_config *cfg, int add)
2601{
2602 struct fib6_config r_cfg;
2603 struct rtnexthop *rtnh;
2604 int remaining;
2605 int attrlen;
2606 int err = 0, last_err = 0;
2607
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02002608 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002609beginning:
2610 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002611
2612 /* Parse a Multipath Entry */
2613 while (rtnh_ok(rtnh, remaining)) {
2614 memcpy(&r_cfg, cfg, sizeof(*cfg));
2615 if (rtnh->rtnh_ifindex)
2616 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2617
2618 attrlen = rtnh_attrlen(rtnh);
2619 if (attrlen > 0) {
2620 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2621
2622 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2623 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002624 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002625 r_cfg.fc_flags |= RTF_GATEWAY;
2626 }
2627 }
2628 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2629 if (err) {
2630 last_err = err;
2631 /* If we are trying to remove a route, do not stop the
2632 * loop when ip6_route_del() fails (because next hop is
2633 * already gone), we should try to remove all next hops.
2634 */
2635 if (add) {
2636 /* If add fails, we should try to delete all
2637 * next hops that have been already added.
2638 */
2639 add = 0;
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02002640 remaining = cfg->fc_mp_len - remaining;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002641 goto beginning;
2642 }
2643 }
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002644 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02002645 * these flags after the first nexthop: if there is a collision,
2646 * we have already failed to add the first nexthop:
2647 * fib6_add_rt2node() has rejected it; when replacing, old
2648 * nexthops have been replaced by first new, the rest should
2649 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002650 */
Michal Kubeček27596472015-05-18 20:54:00 +02002651 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
2652 NLM_F_REPLACE);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002653 rtnh = rtnh_next(rtnh, &remaining);
2654 }
2655
2656 return last_err;
2657}
2658
Ian Morris67ba4152014-08-24 21:53:10 +01002659static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Thomas Graf86872cb2006-08-22 00:01:08 -07002661 struct fib6_config cfg;
2662 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Thomas Graf86872cb2006-08-22 00:01:08 -07002664 err = rtm_to_fib6_config(skb, nlh, &cfg);
2665 if (err < 0)
2666 return err;
2667
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002668 if (cfg.fc_mp)
2669 return ip6_route_multipath(&cfg, 0);
2670 else
2671 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Ian Morris67ba4152014-08-24 21:53:10 +01002674static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Thomas Graf86872cb2006-08-22 00:01:08 -07002676 struct fib6_config cfg;
2677 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Thomas Graf86872cb2006-08-22 00:01:08 -07002679 err = rtm_to_fib6_config(skb, nlh, &cfg);
2680 if (err < 0)
2681 return err;
2682
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002683 if (cfg.fc_mp)
2684 return ip6_route_multipath(&cfg, 1);
2685 else
2686 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Thomas Graf339bf982006-11-10 14:10:15 -08002689static inline size_t rt6_nlmsg_size(void)
2690{
2691 return NLMSG_ALIGN(sizeof(struct rtmsg))
2692 + nla_total_size(16) /* RTA_SRC */
2693 + nla_total_size(16) /* RTA_DST */
2694 + nla_total_size(16) /* RTA_GATEWAY */
2695 + nla_total_size(16) /* RTA_PREFSRC */
2696 + nla_total_size(4) /* RTA_TABLE */
2697 + nla_total_size(4) /* RTA_IIF */
2698 + nla_total_size(4) /* RTA_OIF */
2699 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002700 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01002701 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002702 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2703 + nla_total_size(1); /* RTA_PREF */
Thomas Graf339bf982006-11-10 14:10:15 -08002704}
2705
Brian Haley191cd582008-08-14 15:33:21 -07002706static int rt6_fill_node(struct net *net,
2707 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002708 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002709 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002710 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002712 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002714 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002715 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002716 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if (prefix) { /* user wants prefix routes only */
2719 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2720 /* success since this is not a prefix route */
2721 return 1;
2722 }
2723 }
2724
Eric W. Biederman15e47302012-09-07 20:12:54 +00002725 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002726 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002727 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002728
2729 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 rtm->rtm_family = AF_INET6;
2731 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2732 rtm->rtm_src_len = rt->rt6i_src.plen;
2733 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002734 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002735 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002736 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002737 table = RT6_TABLE_UNSPEC;
2738 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04002739 if (nla_put_u32(skb, RTA_TABLE, table))
2740 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002741 if (rt->rt6i_flags & RTF_REJECT) {
2742 switch (rt->dst.error) {
2743 case -EINVAL:
2744 rtm->rtm_type = RTN_BLACKHOLE;
2745 break;
2746 case -EACCES:
2747 rtm->rtm_type = RTN_PROHIBIT;
2748 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002749 case -EAGAIN:
2750 rtm->rtm_type = RTN_THROW;
2751 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002752 default:
2753 rtm->rtm_type = RTN_UNREACHABLE;
2754 break;
2755 }
2756 }
David S. Miller38308472011-12-03 18:02:47 -05002757 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002758 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002759 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 rtm->rtm_type = RTN_LOCAL;
2761 else
2762 rtm->rtm_type = RTN_UNICAST;
2763 rtm->rtm_flags = 0;
2764 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2765 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002766 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00002768 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2769 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2770 rtm->rtm_protocol = RTPROT_RA;
2771 else
2772 rtm->rtm_protocol = RTPROT_KERNEL;
2773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
David S. Miller38308472011-12-03 18:02:47 -05002775 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 rtm->rtm_flags |= RTM_F_CLONED;
2777
2778 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02002779 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04002780 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002781 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02002783 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002784 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785#ifdef CONFIG_IPV6_SUBTREES
2786 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02002787 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04002788 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002789 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04002790 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02002791 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002792 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002794 if (iif) {
2795#ifdef CONFIG_IPV6_MROUTE
2796 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002797 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002798 if (err <= 0) {
2799 if (!nowait) {
2800 if (err == 0)
2801 return 0;
2802 goto nla_put_failure;
2803 } else {
2804 if (err == -EMSGSIZE)
2805 goto nla_put_failure;
2806 }
2807 }
2808 } else
2809#endif
David S. Millerc78679e2012-04-01 20:27:33 -04002810 if (nla_put_u32(skb, RTA_IIF, iif))
2811 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002812 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04002814 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02002815 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002816 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002818
Daniel Walterc3968a82011-04-13 21:10:57 +00002819 if (rt->rt6i_prefsrc.plen) {
2820 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002821 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02002822 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002823 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00002824 }
2825
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002826 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
2827 if (rt->rt6i_pmtu)
2828 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
2829 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002830 goto nla_put_failure;
2831
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00002832 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02002833 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00002834 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002835 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002836
David S. Millerc78679e2012-04-01 20:27:33 -04002837 if (rt->dst.dev &&
2838 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2839 goto nla_put_failure;
2840 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2841 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00002842
2843 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002844
David S. Miller87a50692012-07-10 05:06:14 -07002845 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002846 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002848 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2849 goto nla_put_failure;
2850
Johannes Berg053c0952015-01-16 22:09:00 +01002851 nlmsg_end(skb, nlh);
2852 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002853
2854nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002855 nlmsg_cancel(skb, nlh);
2856 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
Patrick McHardy1b43af52006-08-10 23:11:17 -07002859int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2862 int prefix;
2863
Thomas Graf2d7202b2006-08-22 00:01:27 -07002864 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2865 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2867 } else
2868 prefix = 0;
2869
Brian Haley191cd582008-08-14 15:33:21 -07002870 return rt6_fill_node(arg->net,
2871 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002872 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002873 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
Ian Morris67ba4152014-08-24 21:53:10 +01002876static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002878 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002879 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002881 struct sk_buff *skb;
2882 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002883 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002884 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07002885
2886 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2887 if (err < 0)
2888 goto errout;
2889
2890 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002891 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002892
2893 if (tb[RTA_SRC]) {
2894 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2895 goto errout;
2896
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002897 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002898 }
2899
2900 if (tb[RTA_DST]) {
2901 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2902 goto errout;
2903
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002904 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002905 }
2906
2907 if (tb[RTA_IIF])
2908 iif = nla_get_u32(tb[RTA_IIF]);
2909
2910 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002911 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002912
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07002913 if (tb[RTA_MARK])
2914 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
2915
Thomas Grafab364a62006-08-22 00:01:47 -07002916 if (iif) {
2917 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002918 int flags = 0;
2919
Daniel Lezcano55786892008-03-04 13:47:47 -08002920 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002921 if (!dev) {
2922 err = -ENODEV;
2923 goto errout;
2924 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002925
2926 fl6.flowi6_iif = iif;
2927
2928 if (!ipv6_addr_any(&fl6.saddr))
2929 flags |= RT6_LOOKUP_F_HAS_SADDR;
2930
2931 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2932 flags);
2933 } else {
2934 fl6.flowi6_oif = oif;
2935
2936 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07002937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002940 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00002941 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07002942 err = -ENOBUFS;
2943 goto errout;
2944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 /* Reserve room for dummy headers, this skb can pass
2947 through good chunk of routing engine.
2948 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002949 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2951
Changli Gaod8d1f302010-06-10 23:31:35 -07002952 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
David S. Miller4c9483b2011-03-12 16:22:43 -05002954 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002955 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002956 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002958 kfree_skb(skb);
2959 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961
Eric W. Biederman15e47302012-09-07 20:12:54 +00002962 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07002963errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Thomas Graf86872cb2006-08-22 00:01:08 -07002967void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002970 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002971 u32 seq;
2972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002974 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002975 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002976
Thomas Graf339bf982006-11-10 14:10:15 -08002977 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002978 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002979 goto errout;
2980
Brian Haley191cd582008-08-14 15:33:21 -07002981 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002982 event, info->portid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002983 if (err < 0) {
2984 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2985 WARN_ON(err == -EMSGSIZE);
2986 kfree_skb(skb);
2987 goto errout;
2988 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002989 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002990 info->nlh, gfp_any());
2991 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002992errout:
2993 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002994 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002997static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00002998 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002999{
Jiri Pirko351638e2013-05-28 01:30:21 +00003000 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003001 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003002
3003 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003004 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003005 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
3006#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003007 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003008 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003009 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003010 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
3011#endif
3012 }
3013
3014 return NOTIFY_OK;
3015}
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017/*
3018 * /proc
3019 */
3020
3021#ifdef CONFIG_PROC_FS
3022
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003023static const struct file_operations ipv6_route_proc_fops = {
3024 .owner = THIS_MODULE,
3025 .open = ipv6_route_open,
3026 .read = seq_read,
3027 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02003028 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003029};
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031static int rt6_stats_seq_show(struct seq_file *seq, void *v)
3032{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003033 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003035 net->ipv6.rt6_stats->fib_nodes,
3036 net->ipv6.rt6_stats->fib_route_nodes,
3037 net->ipv6.rt6_stats->fib_rt_alloc,
3038 net->ipv6.rt6_stats->fib_rt_entries,
3039 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00003040 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003041 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 return 0;
3044}
3045
3046static int rt6_stats_seq_open(struct inode *inode, struct file *file)
3047{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07003048 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003049}
3050
Arjan van de Ven9a321442007-02-12 00:55:35 -08003051static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 .owner = THIS_MODULE,
3053 .open = rt6_stats_seq_open,
3054 .read = seq_read,
3055 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07003056 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057};
3058#endif /* CONFIG_PROC_FS */
3059
3060#ifdef CONFIG_SYSCTL
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062static
Joe Perchesfe2c6332013-06-11 23:04:25 -07003063int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 void __user *buffer, size_t *lenp, loff_t *ppos)
3065{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003066 struct net *net;
3067 int delay;
3068 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003070
3071 net = (struct net *)ctl->extra1;
3072 delay = net->ipv6.sysctl.flush_delay;
3073 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02003074 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Joe Perchesfe2c6332013-06-11 23:04:25 -07003078struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003079 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08003081 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07003083 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003084 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 },
3086 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003088 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 .maxlen = sizeof(int),
3090 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003091 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 },
3093 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08003095 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 .maxlen = sizeof(int),
3097 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003098 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 },
3100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003102 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 .maxlen = sizeof(int),
3104 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003105 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 },
3107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003109 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 .maxlen = sizeof(int),
3111 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003112 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 },
3114 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003116 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 .maxlen = sizeof(int),
3118 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003119 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 },
3121 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003123 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .maxlen = sizeof(int),
3125 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003126 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 },
3128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003130 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 .maxlen = sizeof(int),
3132 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003133 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 },
3135 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003137 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 .maxlen = sizeof(int),
3139 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003140 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 },
3142 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003144 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 .maxlen = sizeof(int),
3146 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003147 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003149 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150};
3151
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003152struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003153{
3154 struct ctl_table *table;
3155
3156 table = kmemdup(ipv6_route_table_template,
3157 sizeof(ipv6_route_table_template),
3158 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003159
3160 if (table) {
3161 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003162 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003163 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003164 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3165 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3166 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3167 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3168 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3169 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3170 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003171 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003172
3173 /* Don't export sysctls to unprivileged users */
3174 if (net->user_ns != &init_user_ns)
3175 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003176 }
3177
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003178 return table;
3179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180#endif
3181
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003182static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003183{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003184 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003185
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003186 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3187 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003188
Eric Dumazetfc66f952010-10-08 06:37:34 +00003189 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3190 goto out_ip6_dst_ops;
3191
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003192 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3193 sizeof(*net->ipv6.ip6_null_entry),
3194 GFP_KERNEL);
3195 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003196 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003197 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003198 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003199 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003200 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3201 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003202
3203#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3204 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3205 sizeof(*net->ipv6.ip6_prohibit_entry),
3206 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003207 if (!net->ipv6.ip6_prohibit_entry)
3208 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003209 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003210 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003211 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003212 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3213 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003214
3215 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3216 sizeof(*net->ipv6.ip6_blk_hole_entry),
3217 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003218 if (!net->ipv6.ip6_blk_hole_entry)
3219 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003220 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003221 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003222 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003223 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3224 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003225#endif
3226
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003227 net->ipv6.sysctl.flush_delay = 0;
3228 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3229 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3230 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3231 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3232 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3233 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3234 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3235
Benjamin Thery6891a342008-03-04 13:49:47 -08003236 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3237
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003238 ret = 0;
3239out:
3240 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003241
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003242#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3243out_ip6_prohibit_entry:
3244 kfree(net->ipv6.ip6_prohibit_entry);
3245out_ip6_null_entry:
3246 kfree(net->ipv6.ip6_null_entry);
3247#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003248out_ip6_dst_entries:
3249 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003250out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003251 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003252}
3253
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003254static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003255{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003256 kfree(net->ipv6.ip6_null_entry);
3257#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3258 kfree(net->ipv6.ip6_prohibit_entry);
3259 kfree(net->ipv6.ip6_blk_hole_entry);
3260#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003261 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003262}
3263
Thomas Grafd1896342012-06-18 12:08:33 +00003264static int __net_init ip6_route_net_init_late(struct net *net)
3265{
3266#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003267 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3268 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003269#endif
3270 return 0;
3271}
3272
3273static void __net_exit ip6_route_net_exit_late(struct net *net)
3274{
3275#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003276 remove_proc_entry("ipv6_route", net->proc_net);
3277 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003278#endif
3279}
3280
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003281static struct pernet_operations ip6_route_net_ops = {
3282 .init = ip6_route_net_init,
3283 .exit = ip6_route_net_exit,
3284};
3285
David S. Millerc3426b42012-06-09 16:27:05 -07003286static int __net_init ipv6_inetpeer_init(struct net *net)
3287{
3288 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3289
3290 if (!bp)
3291 return -ENOMEM;
3292 inet_peer_base_init(bp);
3293 net->ipv6.peers = bp;
3294 return 0;
3295}
3296
3297static void __net_exit ipv6_inetpeer_exit(struct net *net)
3298{
3299 struct inet_peer_base *bp = net->ipv6.peers;
3300
3301 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003302 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003303 kfree(bp);
3304}
3305
David S. Miller2b823f72012-06-09 19:00:16 -07003306static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003307 .init = ipv6_inetpeer_init,
3308 .exit = ipv6_inetpeer_exit,
3309};
3310
Thomas Grafd1896342012-06-18 12:08:33 +00003311static struct pernet_operations ip6_route_net_late_ops = {
3312 .init = ip6_route_net_init_late,
3313 .exit = ip6_route_net_exit_late,
3314};
3315
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003316static struct notifier_block ip6_route_dev_notifier = {
3317 .notifier_call = ip6_route_dev_notify,
3318 .priority = 0,
3319};
3320
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003321int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003323 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003324 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003325
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003326 ret = -ENOMEM;
3327 ip6_dst_ops_template.kmem_cachep =
3328 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3329 SLAB_HWCACHE_ALIGN, NULL);
3330 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003331 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003332
Eric Dumazetfc66f952010-10-08 06:37:34 +00003333 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003334 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003335 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003336
David S. Millerc3426b42012-06-09 16:27:05 -07003337 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3338 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003339 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003340
David S. Miller7e52b332012-06-15 15:51:55 -07003341 ret = register_pernet_subsys(&ip6_route_net_ops);
3342 if (ret)
3343 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003344
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003345 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3346
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003347 /* Registering of the loopback is done before this portion of code,
3348 * the loopback reference in rt6_info will not be taken, do it
3349 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003350 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003351 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3352 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003353 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003354 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003355 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003356 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3357 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003358 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003359 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003360 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003361
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003362 ret = xfrm6_init();
3363 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003364 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003365
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003366 ret = fib6_rules_init();
3367 if (ret)
3368 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003369
Thomas Grafd1896342012-06-18 12:08:33 +00003370 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3371 if (ret)
3372 goto fib6_rules_init;
3373
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003374 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003375 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3376 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3377 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003378 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003379
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003380 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003381 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003382 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003383
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003384 for_each_possible_cpu(cpu) {
3385 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
3386
3387 INIT_LIST_HEAD(&ul->head);
3388 spin_lock_init(&ul->lock);
3389 }
3390
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003391out:
3392 return ret;
3393
Thomas Grafd1896342012-06-18 12:08:33 +00003394out_register_late_subsys:
3395 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003396fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003397 fib6_rules_cleanup();
3398xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003399 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003400out_fib6_init:
3401 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003402out_register_subsys:
3403 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003404out_register_inetpeer:
3405 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003406out_dst_entries:
3407 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003408out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003409 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
3413void ip6_route_cleanup(void)
3414{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003415 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003416 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003417 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003420 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003421 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003422 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003423 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}