blob: 5fc1149fe91d85bb26c18ac5a74d1728146d8b78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Joe Perchesf3213832012-05-15 14:11:53 +000027#define pr_fmt(fmt) "IPv6: " fmt
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080045#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020047#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/snmp.h>
49#include <net/ipv6.h>
50#include <net/ip6_fib.h>
51#include <net/ip6_route.h>
52#include <net/ndisc.h>
53#include <net/addrconf.h>
54#include <net/tcp.h>
55#include <linux/rtnetlink.h>
56#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020057#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070059#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070060#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000061#include <net/nexthop.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020062#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020063#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070064#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/uaccess.h>
67
68#ifdef CONFIG_SYSCTL
69#include <linux/sysctl.h>
70#endif
71
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020072enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010073 RT6_NUD_FAIL_HARD = -3,
74 RT6_NUD_FAIL_PROBE = -2,
75 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020076 RT6_NUD_SUCCEED = 1
77};
78
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -070079static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080081static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000082static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct dst_entry *ip6_negative_advice(struct dst_entry *);
84static void ip6_dst_destroy(struct dst_entry *);
85static void ip6_dst_ifdown(struct dst_entry *,
86 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080087static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050090static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053091static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050092static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070094static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
95 struct sk_buff *skb, u32 mtu);
96static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
97 struct sk_buff *skb);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -070098static void rt6_dst_from_metrics_check(struct rt6_info *rt);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020099static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800101#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800102static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000103 const struct in6_addr *prefix, int prefixlen,
104 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +0000105 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800106static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000107 const struct in6_addr *prefix, int prefixlen,
108 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800109#endif
110
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700111struct uncached_list {
112 spinlock_t lock;
113 struct list_head head;
114};
115
116static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
117
118static void rt6_uncached_list_add(struct rt6_info *rt)
119{
120 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
121
122 rt->dst.flags |= DST_NOCACHE;
123 rt->rt6i_uncached_list = ul;
124
125 spin_lock_bh(&ul->lock);
126 list_add_tail(&rt->rt6i_uncached, &ul->head);
127 spin_unlock_bh(&ul->lock);
128}
129
130static void rt6_uncached_list_del(struct rt6_info *rt)
131{
132 if (!list_empty(&rt->rt6i_uncached)) {
133 struct uncached_list *ul = rt->rt6i_uncached_list;
134
135 spin_lock_bh(&ul->lock);
136 list_del(&rt->rt6i_uncached);
137 spin_unlock_bh(&ul->lock);
138 }
139}
140
141static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
142{
143 struct net_device *loopback_dev = net->loopback_dev;
144 int cpu;
145
146 for_each_possible_cpu(cpu) {
147 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
148 struct rt6_info *rt;
149
150 spin_lock_bh(&ul->lock);
151 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
152 struct inet6_dev *rt_idev = rt->rt6i_idev;
153 struct net_device *rt_dev = rt->dst.dev;
154
155 if (rt_idev && (rt_idev->dev == dev || !dev) &&
156 rt_idev->dev != loopback_dev) {
157 rt->rt6i_idev = in6_dev_get(loopback_dev);
158 in6_dev_put(rt_idev);
159 }
160
161 if (rt_dev && (rt_dev == dev || !dev) &&
162 rt_dev != loopback_dev) {
163 rt->dst.dev = loopback_dev;
164 dev_hold(rt->dst.dev);
165 dev_put(rt_dev);
166 }
167 }
168 spin_unlock_bh(&ul->lock);
169 }
170}
171
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700172static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
173{
174 return dst_metrics_write_ptr(rt->dst.from);
175}
176
David S. Miller06582542011-01-27 14:58:42 -0800177static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
178{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700179 struct rt6_info *rt = (struct rt6_info *)dst;
David S. Miller06582542011-01-27 14:58:42 -0800180
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700181 if (rt->rt6i_flags & RTF_PCPU)
182 return rt6_pcpu_cow_metrics(rt);
183 else if (rt->rt6i_flags & RTF_CACHE)
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700184 return NULL;
185 else
Martin KaFai Lau3b471172015-02-12 16:14:08 -0800186 return dst_cow_metrics_generic(dst, old);
David S. Miller06582542011-01-27 14:58:42 -0800187}
188
David S. Millerf894cbf2012-07-02 21:52:24 -0700189static inline const void *choose_neigh_daddr(struct rt6_info *rt,
190 struct sk_buff *skb,
191 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500192{
193 struct in6_addr *p = &rt->rt6i_gateway;
194
David S. Millera7563f32012-01-26 16:29:16 -0500195 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500196 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700197 else if (skb)
198 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500199 return daddr;
200}
201
David S. Millerf894cbf2012-07-02 21:52:24 -0700202static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
203 struct sk_buff *skb,
204 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700205{
David S. Miller39232972012-01-26 15:22:32 -0500206 struct rt6_info *rt = (struct rt6_info *) dst;
207 struct neighbour *n;
208
David S. Millerf894cbf2012-07-02 21:52:24 -0700209 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000210 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500211 if (n)
212 return n;
213 return neigh_create(&nd_tbl, daddr, dst->dev);
214}
215
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800216static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .gc = ip6_dst_gc,
219 .gc_thresh = 1024,
220 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800221 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000222 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800223 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .destroy = ip6_dst_destroy,
225 .ifdown = ip6_dst_ifdown,
226 .negative_advice = ip6_negative_advice,
227 .link_failure = ip6_link_failure,
228 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700229 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500230 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700231 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Steffen Klassertebb762f2011-11-23 02:12:51 +0000234static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800235{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000236 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
237
238 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800239}
240
David S. Miller6700c272012-07-17 03:29:28 -0700241static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
242 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700243{
244}
245
David S. Miller6700c272012-07-17 03:29:28 -0700246static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
247 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700248{
249}
250
Held Bernhard0972ddb2011-04-24 22:07:32 +0000251static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
252 unsigned long old)
253{
254 return NULL;
255}
256
David S. Miller14e50e52007-05-24 18:17:54 -0700257static struct dst_ops ip6_dst_blackhole_ops = {
258 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700259 .destroy = ip6_dst_destroy,
260 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000261 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800262 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700263 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700264 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000265 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700266 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700267};
268
David S. Miller62fa8a82011-01-26 20:51:05 -0800269static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800270 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800271};
272
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000273static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700274 .dst = {
275 .__refcnt = ATOMIC_INIT(1),
276 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000277 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700278 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700279 .input = ip6_pkt_discard,
280 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 },
282 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700283 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .rt6i_metric = ~(u32) 0,
285 .rt6i_ref = ATOMIC_INIT(1),
286};
287
Thomas Graf101367c2006-08-04 03:39:02 -0700288#ifdef CONFIG_IPV6_MULTIPLE_TABLES
289
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000290static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700291 .dst = {
292 .__refcnt = ATOMIC_INIT(1),
293 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000294 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700295 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700296 .input = ip6_pkt_prohibit,
297 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700298 },
299 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700300 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700301 .rt6i_metric = ~(u32) 0,
302 .rt6i_ref = ATOMIC_INIT(1),
303};
304
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000305static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .dst = {
307 .__refcnt = ATOMIC_INIT(1),
308 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000309 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700310 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700311 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500312 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700313 },
314 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700315 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700316 .rt6i_metric = ~(u32) 0,
317 .rt6i_ref = ATOMIC_INIT(1),
318};
319
320#endif
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/* allocate dst with ip6_dst_ops */
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700323static struct rt6_info *__ip6_dst_alloc(struct net *net,
324 struct net_device *dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -0700325 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
David S. Miller97bab732012-06-09 22:36:36 -0700327 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000328 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700329
David S. Miller97bab732012-06-09 22:36:36 -0700330 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000331 struct dst_entry *dst = &rt->dst;
332
333 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000334 INIT_LIST_HEAD(&rt->rt6i_siblings);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700335 INIT_LIST_HEAD(&rt->rt6i_uncached);
David S. Miller97bab732012-06-09 22:36:36 -0700336 }
David S. Millercf911662011-04-28 14:31:47 -0700337 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700340static struct rt6_info *ip6_dst_alloc(struct net *net,
341 struct net_device *dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -0700342 int flags)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700343{
Martin KaFai Lauad706862015-08-14 11:05:52 -0700344 struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700345
346 if (rt) {
347 rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
348 if (rt->rt6i_pcpu) {
349 int cpu;
350
351 for_each_possible_cpu(cpu) {
352 struct rt6_info **p;
353
354 p = per_cpu_ptr(rt->rt6i_pcpu, cpu);
355 /* no one shares rt */
356 *p = NULL;
357 }
358 } else {
359 dst_destroy((struct dst_entry *)rt);
360 return NULL;
361 }
362 }
363
364 return rt;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ip6_dst_destroy(struct dst_entry *dst)
368{
369 struct rt6_info *rt = (struct rt6_info *)dst;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000370 struct dst_entry *from = dst->from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700373 dst_destroy_metrics_generic(dst);
Markus Elfring87775312015-07-02 16:30:24 +0200374 free_percpu(rt->rt6i_pcpu);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700375 rt6_uncached_list_del(rt);
376
377 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500378 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rt->rt6i_idev = NULL;
380 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900381 }
Gao feng1716a962012-04-06 00:13:10 +0000382
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000383 dst->from = NULL;
384 dst_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
388 int how)
389{
390 struct rt6_info *rt = (struct rt6_info *)dst;
391 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800392 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900393 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David S. Miller97cac082012-07-02 22:43:47 -0700395 if (dev != loopback_dev) {
396 if (idev && idev->dev == dev) {
397 struct inet6_dev *loopback_idev =
398 in6_dev_get(loopback_dev);
399 if (loopback_idev) {
400 rt->rt6i_idev = loopback_idev;
401 in6_dev_put(idev);
402 }
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
406
Eric Dumazeta50feda2012-05-18 18:57:34 +0000407static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Gao feng1716a962012-04-06 00:13:10 +0000409 if (rt->rt6i_flags & RTF_EXPIRES) {
410 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000411 return true;
Gao feng1716a962012-04-06 00:13:10 +0000412 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000413 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000414 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000415 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000418/* Multipath route selection:
419 * Hash based function using packet header and flowlabel.
420 * Adapted from fib_info_hashfn()
421 */
422static int rt6_info_hash_nhsfn(unsigned int candidate_count,
423 const struct flowi6 *fl6)
424{
Tom Herbert644d0e62015-09-23 14:13:35 -0700425 return get_hash_from_flowi6(fl6) % candidate_count;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000426}
427
428static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200429 struct flowi6 *fl6, int oif,
430 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000431{
432 struct rt6_info *sibling, *next_sibling;
433 int route_choosen;
434
435 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
436 /* Don't change the route, if route_choosen == 0
437 * (siblings does not include ourself)
438 */
439 if (route_choosen)
440 list_for_each_entry_safe(sibling, next_sibling,
441 &match->rt6i_siblings, rt6i_siblings) {
442 route_choosen--;
443 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200444 if (rt6_score_route(sibling, oif, strict) < 0)
445 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000446 match = sibling;
447 break;
448 }
449 }
450 return match;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700454 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
456
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800457static inline struct rt6_info *rt6_device_match(struct net *net,
458 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000459 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700461 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct rt6_info *local = NULL;
464 struct rt6_info *sprt;
465
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900466 if (!oif && ipv6_addr_any(saddr))
467 goto out;
468
Changli Gaod8d1f302010-06-10 23:31:35 -0700469 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500470 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900471
472 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (dev->ifindex == oif)
474 return sprt;
475 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500476 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 sprt->rt6i_idev->dev->ifindex != oif) {
David Ahern17fb0b22015-09-25 15:22:54 -0600478 if (flags & RT6_LOOKUP_F_IFACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
David Ahern17fb0b22015-09-25 15:22:54 -0600480 if (local &&
481 local->rt6i_idev->dev->ifindex == oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 continue;
483 }
484 local = sprt;
485 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900486 } else {
487 if (ipv6_chk_addr(net, saddr, dev,
488 flags & RT6_LOOKUP_F_IFACE))
489 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900493 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (local)
495 return local;
496
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700497 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800498 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900500out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return rt;
502}
503
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800504#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200505struct __rt6_probe_work {
506 struct work_struct work;
507 struct in6_addr target;
508 struct net_device *dev;
509};
510
511static void rt6_probe_deferred(struct work_struct *w)
512{
513 struct in6_addr mcaddr;
514 struct __rt6_probe_work *work =
515 container_of(w, struct __rt6_probe_work, work);
516
517 addrconf_addr_solict_mult(&work->target, &mcaddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200518 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, NULL);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200519 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100520 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200521}
522
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800523static void rt6_probe(struct rt6_info *rt)
524{
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700525 struct __rt6_probe_work *work;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000526 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800527 /*
528 * Okay, this does not seem to be appropriate
529 * for now, however, we need to check if it
530 * is really so; aka Router Reachability Probing.
531 *
532 * Router Reachability Probe MUST be rate-limited
533 * to no more than one per minute.
534 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000535 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000536 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000537 rcu_read_lock_bh();
538 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
539 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700540 if (neigh->nud_state & NUD_VALID)
541 goto out;
542
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700543 work = NULL;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000544 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700545 if (!(neigh->nud_state & NUD_VALID) &&
546 time_after(jiffies,
547 neigh->updated +
548 rt->rt6i_idev->cnf.rtr_probe_interval)) {
549 work = kmalloc(sizeof(*work), GFP_ATOMIC);
550 if (work)
551 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200552 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000553 write_unlock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700554 } else {
555 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000556 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700557
558 if (work) {
559 INIT_WORK(&work->work, rt6_probe_deferred);
560 work->target = rt->rt6i_gateway;
561 dev_hold(rt->dst.dev);
562 work->dev = rt->dst.dev;
563 schedule_work(&work->work);
564 }
565
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700566out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000567 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800568}
569#else
570static inline void rt6_probe(struct rt6_info *rt)
571{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800572}
573#endif
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800576 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700578static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
David S. Millerd1918542011-12-28 20:19:20 -0500580 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700581 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800582 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700583 if ((dev->flags & IFF_LOOPBACK) &&
584 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
585 return 1;
586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200589static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000591 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200592 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000593
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700594 if (rt->rt6i_flags & RTF_NONEXTHOP ||
595 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200596 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000597
598 rcu_read_lock_bh();
599 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
600 if (neigh) {
601 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800602 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200603 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800604#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000605 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200606 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100607 else
608 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800609#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000610 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200611 } else {
612 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100613 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000614 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000615 rcu_read_unlock_bh();
616
Paul Marksa5a81f02012-12-03 10:26:54 +0000617 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800620static int rt6_score_route(struct rt6_info *rt, int oif,
621 int strict)
622{
Paul Marksa5a81f02012-12-03 10:26:54 +0000623 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900624
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700625 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700626 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200627 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800628#ifdef CONFIG_IPV6_ROUTER_PREF
629 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
630#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200631 if (strict & RT6_LOOKUP_F_REACHABLE) {
632 int n = rt6_check_neigh(rt);
633 if (n < 0)
634 return n;
635 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800636 return m;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David S. Millerf11e6652007-03-24 20:36:25 -0700639static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200640 int *mpri, struct rt6_info *match,
641 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800642{
David S. Millerf11e6652007-03-24 20:36:25 -0700643 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200644 bool match_do_rr = false;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400645 struct inet6_dev *idev = rt->rt6i_idev;
646 struct net_device *dev = rt->dst.dev;
647
648 if (dev && !netif_carrier_ok(dev) &&
649 idev->cnf.ignore_routes_with_linkdown)
650 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700651
652 if (rt6_check_expired(rt))
653 goto out;
654
655 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100656 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200657 match_do_rr = true;
658 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100659 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700660 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700661 }
662
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200663 if (strict & RT6_LOOKUP_F_REACHABLE)
664 rt6_probe(rt);
665
Jiri Benc7e980562013-12-11 13:48:20 +0100666 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200667 if (m > *mpri) {
668 *do_rr = match_do_rr;
669 *mpri = m;
670 match = rt;
671 }
David S. Millerf11e6652007-03-24 20:36:25 -0700672out:
673 return match;
674}
675
676static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
677 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200678 u32 metric, int oif, int strict,
679 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700680{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700681 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800682 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David S. Millerf11e6652007-03-24 20:36:25 -0700684 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700685 cont = NULL;
686 for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
687 if (rt->rt6i_metric != metric) {
688 cont = rt;
689 break;
690 }
691
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200692 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700693 }
694
695 for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
696 if (rt->rt6i_metric != metric) {
697 cont = rt;
698 break;
699 }
700
701 match = find_match(rt, oif, strict, &mpri, match, do_rr);
702 }
703
704 if (match || !cont)
705 return match;
706
707 for (rt = cont; rt; rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200708 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800709
David S. Millerf11e6652007-03-24 20:36:25 -0700710 return match;
711}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800712
David S. Millerf11e6652007-03-24 20:36:25 -0700713static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
714{
715 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800716 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200717 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
David S. Millerf11e6652007-03-24 20:36:25 -0700719 rt0 = fn->rr_ptr;
720 if (!rt0)
721 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200723 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
724 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700727 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700728
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800729 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700730 if (!next || next->rt6i_metric != rt0->rt6i_metric)
731 next = fn->leaf;
732
733 if (next != rt0)
734 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
David S. Millerd1918542011-12-28 20:19:20 -0500737 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000738 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700741static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
742{
743 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
744}
745
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800746#ifdef CONFIG_IPV6_ROUTE_INFO
747int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000748 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800749{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900750 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800751 struct route_info *rinfo = (struct route_info *) opt;
752 struct in6_addr prefix_buf, *prefix;
753 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900754 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800755 struct rt6_info *rt;
756
757 if (len < sizeof(struct route_info)) {
758 return -EINVAL;
759 }
760
761 /* Sanity check for prefix_len and length */
762 if (rinfo->length > 3) {
763 return -EINVAL;
764 } else if (rinfo->prefix_len > 128) {
765 return -EINVAL;
766 } else if (rinfo->prefix_len > 64) {
767 if (rinfo->length < 2) {
768 return -EINVAL;
769 }
770 } else if (rinfo->prefix_len > 0) {
771 if (rinfo->length < 1) {
772 return -EINVAL;
773 }
774 }
775
776 pref = rinfo->route_pref;
777 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000778 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800779
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900780 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800781
782 if (rinfo->length == 3)
783 prefix = (struct in6_addr *)rinfo->prefix;
784 else {
785 /* this function is safe */
786 ipv6_addr_prefix(&prefix_buf,
787 (struct in6_addr *)rinfo->prefix,
788 rinfo->prefix_len);
789 prefix = &prefix_buf;
790 }
791
Duan Jiongf104a562013-11-08 09:56:53 +0800792 if (rinfo->prefix_len == 0)
793 rt = rt6_get_dflt_router(gwaddr, dev);
794 else
795 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
796 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800797
798 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700799 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800800 rt = NULL;
801 }
802
803 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800804 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800805 pref);
806 else if (rt)
807 rt->rt6i_flags = RTF_ROUTEINFO |
808 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
809
810 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000811 if (!addrconf_finite_timeout(lifetime))
812 rt6_clean_expires(rt);
813 else
814 rt6_set_expires(rt, jiffies + HZ * lifetime);
815
Amerigo Wang94e187c2012-10-29 00:13:19 +0000816 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800817 }
818 return 0;
819}
820#endif
821
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700822static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
823 struct in6_addr *saddr)
824{
825 struct fib6_node *pn;
826 while (1) {
827 if (fn->fn_flags & RTN_TL_ROOT)
828 return NULL;
829 pn = fn->parent;
830 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
831 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
832 else
833 fn = pn;
834 if (fn->fn_flags & RTN_RTINFO)
835 return fn;
836 }
837}
Thomas Grafc71099a2006-08-04 23:20:06 -0700838
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800839static struct rt6_info *ip6_pol_route_lookup(struct net *net,
840 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500841 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct fib6_node *fn;
844 struct rt6_info *rt;
845
Thomas Grafc71099a2006-08-04 23:20:06 -0700846 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500847 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700848restart:
849 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500850 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000851 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200852 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700853 if (rt == net->ipv6.ip6_null_entry) {
854 fn = fib6_backtrack(fn, &fl6->saddr);
855 if (fn)
856 goto restart;
857 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700858 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700859 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700860 return rt;
861
862}
863
Ian Morris67ba4152014-08-24 21:53:10 +0100864struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
Florian Westphalea6e5742011-09-05 16:05:44 +0200865 int flags)
866{
867 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
868}
869EXPORT_SYMBOL_GPL(ip6_route_lookup);
870
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900871struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
872 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700873{
David S. Miller4c9483b2011-03-12 16:22:43 -0500874 struct flowi6 fl6 = {
875 .flowi6_oif = oif,
876 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700877 };
878 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700879 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700880
Thomas Grafadaa70b2006-10-13 15:01:03 -0700881 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500882 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700883 flags |= RT6_LOOKUP_F_HAS_SADDR;
884 }
885
David S. Miller4c9483b2011-03-12 16:22:43 -0500886 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700887 if (dst->error == 0)
888 return (struct rt6_info *) dst;
889
890 dst_release(dst);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return NULL;
893}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900894EXPORT_SYMBOL(rt6_lookup);
895
Thomas Grafc71099a2006-08-04 23:20:06 -0700896/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 It takes new route entry, the addition fails by any reason the
898 route is freed. In any case, if caller does not hold it, it may
899 be destroyed.
900 */
901
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100902static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
Florian Westphale715b6d2015-01-05 23:57:44 +0100903 struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700906 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Thomas Grafc71099a2006-08-04 23:20:06 -0700908 table = rt->rt6i_table;
909 write_lock_bh(&table->tb6_lock);
Florian Westphale715b6d2015-01-05 23:57:44 +0100910 err = fib6_add(&table->tb6_root, rt, info, mxc);
Thomas Grafc71099a2006-08-04 23:20:06 -0700911 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return err;
914}
915
Thomas Graf40e22e82006-08-22 00:00:45 -0700916int ip6_ins_rt(struct rt6_info *rt)
917{
Florian Westphale715b6d2015-01-05 23:57:44 +0100918 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
919 struct mx6_config mxc = { .mx = NULL, };
920
921 return __ip6_ins_rt(rt, &info, &mxc);
Thomas Graf40e22e82006-08-22 00:00:45 -0700922}
923
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700924static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
925 const struct in6_addr *daddr,
926 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct rt6_info *rt;
929
930 /*
931 * Clone the route.
932 */
933
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700934 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700935 ort = (struct rt6_info *)ort->dst.from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Martin KaFai Lauad706862015-08-14 11:05:52 -0700937 rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700939 if (!rt)
940 return NULL;
941
942 ip6_rt_copy_init(rt, ort);
943 rt->rt6i_flags |= RTF_CACHE;
944 rt->rt6i_metric = 0;
945 rt->dst.flags |= DST_HOST;
946 rt->rt6i_dst.addr = *daddr;
947 rt->rt6i_dst.plen = 128;
948
949 if (!rt6_is_gw_or_nonexthop(ort)) {
950 if (ort->rt6i_dst.plen != 128 &&
951 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
952 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700954 if (rt->rt6i_src.plen && saddr) {
955 rt->rt6i_src.addr = *saddr;
956 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700957 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -0700958#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800961 return rt;
962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700964static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
965{
966 struct rt6_info *pcpu_rt;
967
968 pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev),
Martin KaFai Lauad706862015-08-14 11:05:52 -0700969 rt->dst.dev, rt->dst.flags);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700970
971 if (!pcpu_rt)
972 return NULL;
973 ip6_rt_copy_init(pcpu_rt, rt);
974 pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
975 pcpu_rt->rt6i_flags |= RTF_PCPU;
976 return pcpu_rt;
977}
978
979/* It should be called with read_lock_bh(&tb6_lock) acquired */
980static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
981{
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700982 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700983
984 p = this_cpu_ptr(rt->rt6i_pcpu);
985 pcpu_rt = *p;
986
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700987 if (pcpu_rt) {
988 dst_hold(&pcpu_rt->dst);
989 rt6_dst_from_metrics_check(pcpu_rt);
990 }
991 return pcpu_rt;
992}
993
994static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
995{
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700996 struct fib6_table *table = rt->rt6i_table;
Martin KaFai Laua73e4192015-08-14 11:05:53 -0700997 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700998
999 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1000 if (!pcpu_rt) {
1001 struct net *net = dev_net(rt->dst.dev);
1002
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001003 dst_hold(&net->ipv6.ip6_null_entry->dst);
1004 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001005 }
1006
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001007 read_lock_bh(&table->tb6_lock);
1008 if (rt->rt6i_pcpu) {
1009 p = this_cpu_ptr(rt->rt6i_pcpu);
1010 prev = cmpxchg(p, NULL, pcpu_rt);
1011 if (prev) {
1012 /* If someone did it before us, return prev instead */
1013 dst_destroy(&pcpu_rt->dst);
1014 pcpu_rt = prev;
1015 }
1016 } else {
1017 /* rt has been removed from the fib6 tree
1018 * before we have a chance to acquire the read_lock.
1019 * In this case, don't brother to create a pcpu rt
1020 * since rt is going away anyway. The next
1021 * dst_check() will trigger a re-lookup.
1022 */
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001023 dst_destroy(&pcpu_rt->dst);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001024 pcpu_rt = rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001025 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001026 dst_hold(&pcpu_rt->dst);
1027 rt6_dst_from_metrics_check(pcpu_rt);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001028 read_unlock_bh(&table->tb6_lock);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001029 return pcpu_rt;
1030}
1031
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001032static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -05001033 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001035 struct fib6_node *fn, *saved_fn;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001036 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001037 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001039 strict |= flags & RT6_LOOKUP_F_IFACE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001040 if (net->ipv6.devconf_all->forwarding == 0)
1041 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Grafc71099a2006-08-04 23:20:06 -07001043 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
David S. Miller4c9483b2011-03-12 16:22:43 -05001045 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001046 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
David Ahernca254492015-10-12 11:47:10 -07001048 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1049 oif = 0;
1050
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001051redo_rt6_select:
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001052 rt = rt6_select(fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +02001053 if (rt->rt6i_nsiblings)
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001054 rt = rt6_multipath_select(rt, fl6, oif, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001055 if (rt == net->ipv6.ip6_null_entry) {
1056 fn = fib6_backtrack(fn, &fl6->saddr);
1057 if (fn)
1058 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001059 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1060 /* also consider unreachable route */
1061 strict &= ~RT6_LOOKUP_F_REACHABLE;
1062 fn = saved_fn;
1063 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001064 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001065 }
1066
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001067
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001068 if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001069 dst_use(&rt->dst, jiffies);
1070 read_unlock_bh(&table->tb6_lock);
1071
1072 rt6_dst_from_metrics_check(rt);
1073 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001074 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1075 !(rt->rt6i_flags & RTF_GATEWAY))) {
1076 /* Create a RTF_CACHE clone which will not be
1077 * owned by the fib6 tree. It is for the special case where
1078 * the daddr in the skb during the neighbor look-up is different
1079 * from the fl6->daddr used to look-up route here.
1080 */
Thomas Grafc71099a2006-08-04 23:20:06 -07001081
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001082 struct rt6_info *uncached_rt;
1083
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001084 dst_use(&rt->dst, jiffies);
1085 read_unlock_bh(&table->tb6_lock);
1086
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001087 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
1088 dst_release(&rt->dst);
1089
1090 if (uncached_rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001091 rt6_uncached_list_add(uncached_rt);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001092 else
1093 uncached_rt = net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001094
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001095 dst_hold(&uncached_rt->dst);
1096 return uncached_rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001097
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001098 } else {
1099 /* Get a percpu copy */
1100
1101 struct rt6_info *pcpu_rt;
1102
1103 rt->dst.lastuse = jiffies;
1104 rt->dst.__use++;
1105 pcpu_rt = rt6_get_pcpu_route(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001106
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001107 if (pcpu_rt) {
1108 read_unlock_bh(&table->tb6_lock);
1109 } else {
1110 /* We have to do the read_unlock first
1111 * because rt6_make_pcpu_route() may trigger
1112 * ip6_dst_gc() which will take the write_lock.
1113 */
1114 dst_hold(&rt->dst);
1115 read_unlock_bh(&table->tb6_lock);
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001116 pcpu_rt = rt6_make_pcpu_route(rt);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001117 dst_release(&rt->dst);
1118 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001119
1120 return pcpu_rt;
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001121
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001122 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001123}
1124
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001125static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001126 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001127{
David S. Miller4c9483b2011-03-12 16:22:43 -05001128 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001129}
1130
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001131static struct dst_entry *ip6_route_input_lookup(struct net *net,
1132 struct net_device *dev,
1133 struct flowi6 *fl6, int flags)
1134{
1135 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1136 flags |= RT6_LOOKUP_F_IFACE;
1137
1138 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1139}
1140
Thomas Grafc71099a2006-08-04 23:20:06 -07001141void ip6_route_input(struct sk_buff *skb)
1142{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001143 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001144 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001145 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02001146 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05001147 struct flowi6 fl6 = {
David Ahernca254492015-10-12 11:47:10 -07001148 .flowi6_iif = l3mdev_fib_oif(skb->dev),
David S. Miller4c9483b2011-03-12 16:22:43 -05001149 .daddr = iph->daddr,
1150 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001151 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05001152 .flowi6_mark = skb->mark,
1153 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001154 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001155
Jiri Benc904af042015-08-20 13:56:31 +02001156 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02001157 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02001158 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Jiri Benc06e9d042015-08-20 13:56:26 +02001159 skb_dst_drop(skb);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001160 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001161}
1162
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001163static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001164 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001165{
David S. Miller4c9483b2011-03-12 16:22:43 -05001166 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001167}
1168
Ian Morris67ba4152014-08-24 21:53:10 +01001169struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001170 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001171{
David Ahernca254492015-10-12 11:47:10 -07001172 struct dst_entry *dst;
Thomas Grafc71099a2006-08-04 23:20:06 -07001173 int flags = 0;
1174
David Ahernca254492015-10-12 11:47:10 -07001175 dst = l3mdev_rt6_dst_by_oif(net, fl6);
1176 if (dst)
1177 return dst;
1178
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001179 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001180
David Ahern741a11d2015-09-28 10:12:13 -07001181 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
1182 fl6->flowi6_oif)
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001183 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001184
David S. Miller4c9483b2011-03-12 16:22:43 -05001185 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001186 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001187 else if (sk)
1188 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001189
David S. Miller4c9483b2011-03-12 16:22:43 -05001190 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001192EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
David S. Miller2774c132011-03-01 14:59:04 -08001194struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001195{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001196 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001197 struct dst_entry *new = NULL;
1198
David S. Millerf5b0a872012-07-19 12:31:33 -07001199 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001200 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001201 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001202
Steffen Klassert81048912012-07-05 23:37:09 +00001203 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
Steffen Klassert81048912012-07-05 23:37:09 +00001204
David S. Miller14e50e52007-05-24 18:17:54 -07001205 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001206 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05001207 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07001208
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001209 if (dst_metrics_read_only(&ort->dst))
1210 new->_metrics = ort->dst._metrics;
1211 else
1212 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001213 rt->rt6i_idev = ort->rt6i_idev;
1214 if (rt->rt6i_idev)
1215 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001216
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001217 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001218 rt->rt6i_flags = ort->rt6i_flags;
David S. Miller14e50e52007-05-24 18:17:54 -07001219 rt->rt6i_metric = 0;
1220
1221 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1222#ifdef CONFIG_IPV6_SUBTREES
1223 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1224#endif
1225
1226 dst_free(new);
1227 }
1228
David S. Miller69ead7a2011-03-01 14:45:33 -08001229 dst_release(dst_orig);
1230 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001231}
David S. Miller14e50e52007-05-24 18:17:54 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233/*
1234 * Destination cache support functions
1235 */
1236
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001237static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1238{
1239 if (rt->dst.from &&
1240 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1241 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1242}
1243
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001244static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
1245{
1246 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1247 return NULL;
1248
1249 if (rt6_check_expired(rt))
1250 return NULL;
1251
1252 return &rt->dst;
1253}
1254
1255static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
1256{
1257 if (rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
1258 rt6_check((struct rt6_info *)(rt->dst.from), cookie))
1259 return &rt->dst;
1260 else
1261 return NULL;
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1265{
1266 struct rt6_info *rt;
1267
1268 rt = (struct rt6_info *) dst;
1269
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001270 /* All IPV6 dsts are created with ->obsolete set to the value
1271 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1272 * into this function always.
1273 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001274
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001275 rt6_dst_from_metrics_check(rt);
1276
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001277 if ((rt->rt6i_flags & RTF_PCPU) || unlikely(dst->flags & DST_NOCACHE))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001278 return rt6_dst_from_check(rt, cookie);
1279 else
1280 return rt6_check(rt, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1284{
1285 struct rt6_info *rt = (struct rt6_info *) dst;
1286
1287 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001288 if (rt->rt6i_flags & RTF_CACHE) {
1289 if (rt6_check_expired(rt)) {
1290 ip6_del_rt(rt);
1291 dst = NULL;
1292 }
1293 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001295 dst = NULL;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001298 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301static void ip6_link_failure(struct sk_buff *skb)
1302{
1303 struct rt6_info *rt;
1304
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001305 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Eric Dumazetadf30902009-06-02 05:19:30 +00001307 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001309 if (rt->rt6i_flags & RTF_CACHE) {
1310 dst_hold(&rt->dst);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001311 ip6_del_rt(rt);
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001312 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 rt->rt6i_node->fn_sernum = -1;
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316}
1317
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001318static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
1319{
1320 struct net *net = dev_net(rt->dst.dev);
1321
1322 rt->rt6i_flags |= RTF_MODIFIED;
1323 rt->rt6i_pmtu = mtu;
1324 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
1325}
1326
1327static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
1328 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Ian Morris67ba4152014-08-24 21:53:10 +01001330 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001332 if (rt6->rt6i_flags & RTF_LOCAL)
1333 return;
1334
David S. Miller81aded22012-06-15 14:54:11 -07001335 dst_confirm(dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001336 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
1337 if (mtu >= dst_mtu(dst))
1338 return;
David S. Miller81aded22012-06-15 14:54:11 -07001339
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001340 if (rt6->rt6i_flags & RTF_CACHE) {
1341 rt6_do_update_pmtu(rt6, mtu);
1342 } else {
1343 const struct in6_addr *daddr, *saddr;
1344 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001345
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001346 if (iph) {
1347 daddr = &iph->daddr;
1348 saddr = &iph->saddr;
1349 } else if (sk) {
1350 daddr = &sk->sk_v6_daddr;
1351 saddr = &inet6_sk(sk)->saddr;
1352 } else {
1353 return;
1354 }
1355 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
1356 if (nrt6) {
1357 rt6_do_update_pmtu(nrt6, mtu);
1358
1359 /* ip6_ins_rt(nrt6) will bump the
1360 * rt6->rt6i_node->fn_sernum
1361 * which will fail the next rt6_check() and
1362 * invalidate the sk->sk_dst_cache.
1363 */
1364 ip6_ins_rt(nrt6);
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367}
1368
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001369static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1370 struct sk_buff *skb, u32 mtu)
1371{
1372 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
1373}
1374
David S. Miller42ae66c2012-06-15 20:01:57 -07001375void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1376 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001377{
1378 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1379 struct dst_entry *dst;
1380 struct flowi6 fl6;
1381
1382 memset(&fl6, 0, sizeof(fl6));
1383 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001384 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001385 fl6.daddr = iph->daddr;
1386 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001387 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001388
1389 dst = ip6_route_output(net, NULL, &fl6);
1390 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07001391 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001392 dst_release(dst);
1393}
1394EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1395
1396void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1397{
1398 ip6_update_pmtu(skb, sock_net(sk), mtu,
1399 sk->sk_bound_dev_if, sk->sk_mark);
1400}
1401EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1402
Duan Jiongb55b76b2013-09-04 19:44:21 +08001403/* Handle redirects */
1404struct ip6rd_flowi {
1405 struct flowi6 fl6;
1406 struct in6_addr gateway;
1407};
1408
1409static struct rt6_info *__ip6_route_redirect(struct net *net,
1410 struct fib6_table *table,
1411 struct flowi6 *fl6,
1412 int flags)
1413{
1414 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1415 struct rt6_info *rt;
1416 struct fib6_node *fn;
1417
1418 /* Get the "current" route for this destination and
1419 * check if the redirect has come from approriate router.
1420 *
1421 * RFC 4861 specifies that redirects should only be
1422 * accepted if they come from the nexthop to the target.
1423 * Due to the way the routes are chosen, this notion
1424 * is a bit fuzzy and one might need to check all possible
1425 * routes.
1426 */
1427
1428 read_lock_bh(&table->tb6_lock);
1429 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1430restart:
1431 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1432 if (rt6_check_expired(rt))
1433 continue;
1434 if (rt->dst.error)
1435 break;
1436 if (!(rt->rt6i_flags & RTF_GATEWAY))
1437 continue;
1438 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1439 continue;
1440 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1441 continue;
1442 break;
1443 }
1444
1445 if (!rt)
1446 rt = net->ipv6.ip6_null_entry;
1447 else if (rt->dst.error) {
1448 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001449 goto out;
1450 }
1451
1452 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001453 fn = fib6_backtrack(fn, &fl6->saddr);
1454 if (fn)
1455 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001456 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001457
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001458out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001459 dst_hold(&rt->dst);
1460
1461 read_unlock_bh(&table->tb6_lock);
1462
1463 return rt;
1464};
1465
1466static struct dst_entry *ip6_route_redirect(struct net *net,
1467 const struct flowi6 *fl6,
1468 const struct in6_addr *gateway)
1469{
1470 int flags = RT6_LOOKUP_F_HAS_SADDR;
1471 struct ip6rd_flowi rdfl;
1472
1473 rdfl.fl6 = *fl6;
1474 rdfl.gateway = *gateway;
1475
1476 return fib6_rule_lookup(net, &rdfl.fl6,
1477 flags, __ip6_route_redirect);
1478}
1479
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001480void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1481{
1482 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1483 struct dst_entry *dst;
1484 struct flowi6 fl6;
1485
1486 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001487 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001488 fl6.flowi6_oif = oif;
1489 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001490 fl6.daddr = iph->daddr;
1491 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001492 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001493
Duan Jiongb55b76b2013-09-04 19:44:21 +08001494 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1495 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001496 dst_release(dst);
1497}
1498EXPORT_SYMBOL_GPL(ip6_redirect);
1499
Duan Jiongc92a59e2013-08-22 12:07:35 +08001500void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1501 u32 mark)
1502{
1503 const struct ipv6hdr *iph = ipv6_hdr(skb);
1504 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1505 struct dst_entry *dst;
1506 struct flowi6 fl6;
1507
1508 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001509 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001510 fl6.flowi6_oif = oif;
1511 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001512 fl6.daddr = msg->dest;
1513 fl6.saddr = iph->daddr;
1514
Duan Jiongb55b76b2013-09-04 19:44:21 +08001515 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1516 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001517 dst_release(dst);
1518}
1519
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001520void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1521{
1522 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1523}
1524EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1525
David S. Miller0dbaee32010-12-13 12:52:14 -08001526static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
David S. Miller0dbaee32010-12-13 12:52:14 -08001528 struct net_device *dev = dst->dev;
1529 unsigned int mtu = dst_mtu(dst);
1530 struct net *net = dev_net(dev);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1533
Daniel Lezcano55786892008-03-04 13:47:47 -08001534 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1535 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001538 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1539 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1540 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * rely only on pmtu discovery"
1542 */
1543 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1544 mtu = IPV6_MAXPLEN;
1545 return mtu;
1546}
1547
Steffen Klassertebb762f2011-11-23 02:12:51 +00001548static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001549{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001550 const struct rt6_info *rt = (const struct rt6_info *)dst;
1551 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001552 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001553
1554 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001555 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001556
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001557 mtu = dst_metric_raw(dst, RTAX_MTU);
1558 if (mtu)
1559 goto out;
1560
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001561 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001562
1563 rcu_read_lock();
1564 idev = __in6_dev_get(dst->dev);
1565 if (idev)
1566 mtu = idev->cnf.mtu6;
1567 rcu_read_unlock();
1568
Eric Dumazet30f78d82014-04-10 21:23:36 -07001569out:
1570 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001571}
1572
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001573static struct dst_entry *icmp6_dst_gc_list;
1574static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001575
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001576struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001577 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
David S. Miller87a11572011-12-06 17:04:13 -05001579 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 struct rt6_info *rt;
1581 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001582 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
David S. Miller38308472011-12-03 18:02:47 -05001584 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001585 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Martin KaFai Lauad706862015-08-14 11:05:52 -07001587 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05001588 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001590 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto out;
1592 }
1593
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001594 rt->dst.flags |= DST_HOST;
1595 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001596 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001597 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001598 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001599 rt->rt6i_dst.plen = 128;
1600 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001601 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001603 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001604 rt->dst.next = icmp6_dst_gc_list;
1605 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001606 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Daniel Lezcano55786892008-03-04 13:47:47 -08001608 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David S. Miller87a11572011-12-06 17:04:13 -05001610 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612out:
David S. Miller87a11572011-12-06 17:04:13 -05001613 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001616int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001618 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001619 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001621 spin_lock_bh(&icmp6_dst_lock);
1622 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 while ((dst = *pprev) != NULL) {
1625 if (!atomic_read(&dst->__refcnt)) {
1626 *pprev = dst->next;
1627 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 } else {
1629 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001630 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 }
1633
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001634 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001635
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001636 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
David S. Miller1e493d12008-09-10 17:27:15 -07001639static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1640 void *arg)
1641{
1642 struct dst_entry *dst, **pprev;
1643
1644 spin_lock_bh(&icmp6_dst_lock);
1645 pprev = &icmp6_dst_gc_list;
1646 while ((dst = *pprev) != NULL) {
1647 struct rt6_info *rt = (struct rt6_info *) dst;
1648 if (func(rt, arg)) {
1649 *pprev = dst->next;
1650 dst_free(dst);
1651 } else {
1652 pprev = &dst->next;
1653 }
1654 }
1655 spin_unlock_bh(&icmp6_dst_lock);
1656}
1657
Daniel Lezcano569d3642008-01-18 03:56:57 -08001658static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001660 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001661 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1662 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1663 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1664 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1665 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001666 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Eric Dumazetfc66f952010-10-08 06:37:34 +00001668 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001669 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001670 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto out;
1672
Benjamin Thery6891a342008-03-04 13:49:47 -08001673 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001674 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001675 entries = dst_entries_get_slow(ops);
1676 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001677 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001679 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001680 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Florian Westphale715b6d2015-01-05 23:57:44 +01001683static int ip6_convert_metrics(struct mx6_config *mxc,
1684 const struct fib6_config *cfg)
1685{
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001686 bool ecn_ca = false;
Florian Westphale715b6d2015-01-05 23:57:44 +01001687 struct nlattr *nla;
1688 int remaining;
1689 u32 *mp;
1690
Ian Morris63159f22015-03-29 14:00:04 +01001691 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001692 return 0;
1693
1694 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1695 if (unlikely(!mp))
1696 return -ENOMEM;
1697
1698 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1699 int type = nla_type(nla);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001700 u32 val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001701
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001702 if (!type)
1703 continue;
1704 if (unlikely(type > RTAX_MAX))
1705 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001706
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001707 if (type == RTAX_CC_ALGO) {
1708 char tmp[TCP_CA_NAME_MAX];
1709
1710 nla_strlcpy(tmp, nla, sizeof(tmp));
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001711 val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001712 if (val == TCP_CA_UNSPEC)
Florian Westphale715b6d2015-01-05 23:57:44 +01001713 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001714 } else {
1715 val = nla_get_u32(nla);
Florian Westphale715b6d2015-01-05 23:57:44 +01001716 }
Daniel Borkmannb8d3e412015-08-31 15:58:46 +02001717 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
1718 goto err;
Daniel Borkmann1bb14802015-08-31 15:58:45 +02001719
1720 mp[type - 1] = val;
1721 __set_bit(type - 1, mxc->mx_valid);
Florian Westphale715b6d2015-01-05 23:57:44 +01001722 }
1723
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001724 if (ecn_ca) {
1725 __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
1726 mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
1727 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001728
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001729 mxc->mx = mp;
Florian Westphale715b6d2015-01-05 23:57:44 +01001730 return 0;
1731 err:
1732 kfree(mp);
1733 return -EINVAL;
1734}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001736static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Daniel Lezcano55786892008-03-04 13:47:47 -08001738 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct rt6_info *rt = NULL;
1740 struct net_device *dev = NULL;
1741 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001742 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001744 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Thomas Graf86872cb2006-08-22 00:01:08 -07001746 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001749 if (cfg->fc_src_len)
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001752 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001754 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (!dev)
1756 goto out;
1757 idev = in6_dev_get(dev);
1758 if (!idev)
1759 goto out;
1760 }
1761
Thomas Graf86872cb2006-08-22 00:01:08 -07001762 if (cfg->fc_metric == 0)
1763 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Matti Vaittinend71314b2011-11-14 00:14:49 +00001765 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001766 if (cfg->fc_nlinfo.nlh &&
1767 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001768 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001769 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001770 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001771 table = fib6_new_table(net, cfg->fc_table);
1772 }
1773 } else {
1774 table = fib6_new_table(net, cfg->fc_table);
1775 }
David S. Miller38308472011-12-03 18:02:47 -05001776
1777 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001778 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001779
Martin KaFai Lauad706862015-08-14 11:05:52 -07001780 rt = ip6_dst_alloc(net, NULL,
1781 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
David S. Miller38308472011-12-03 18:02:47 -05001783 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 err = -ENOMEM;
1785 goto out;
1786 }
1787
Gao feng1716a962012-04-06 00:13:10 +00001788 if (cfg->fc_flags & RTF_EXPIRES)
1789 rt6_set_expires(rt, jiffies +
1790 clock_t_to_jiffies(cfg->fc_expires));
1791 else
1792 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Thomas Graf86872cb2006-08-22 00:01:08 -07001794 if (cfg->fc_protocol == RTPROT_UNSPEC)
1795 cfg->fc_protocol = RTPROT_BOOT;
1796 rt->rt6i_protocol = cfg->fc_protocol;
1797
1798 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001801 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001802 else if (cfg->fc_flags & RTF_LOCAL)
1803 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001805 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Changli Gaod8d1f302010-06-10 23:31:35 -07001807 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001809 if (cfg->fc_encap) {
1810 struct lwtunnel_state *lwtstate;
1811
1812 err = lwtunnel_build_state(dev, cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07001813 cfg->fc_encap, AF_INET6, cfg,
1814 &lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001815 if (err)
1816 goto out;
Jiri Benc61adedf2015-08-20 13:56:25 +02001817 rt->dst.lwtstate = lwtstate_get(lwtstate);
1818 if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
1819 rt->dst.lwtstate->orig_output = rt->dst.output;
1820 rt->dst.output = lwtunnel_output;
Tom Herbert25368622015-08-17 13:42:24 -07001821 }
Jiri Benc61adedf2015-08-20 13:56:25 +02001822 if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
1823 rt->dst.lwtstate->orig_input = rt->dst.input;
1824 rt->dst.input = lwtunnel_input;
Tom Herbert25368622015-08-17 13:42:24 -07001825 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02001826 }
1827
Thomas Graf86872cb2006-08-22 00:01:08 -07001828 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1829 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07001830 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001831 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001834 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1835 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836#endif
1837
Thomas Graf86872cb2006-08-22 00:01:08 -07001838 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* We cannot add true routes via loopback here,
1841 they would result in kernel looping; promote them to reject routes
1842 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001843 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001844 (dev && (dev->flags & IFF_LOOPBACK) &&
1845 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1846 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001848 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (dev) {
1850 dev_put(dev);
1851 in6_dev_put(idev);
1852 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001853 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 dev_hold(dev);
1855 idev = in6_dev_get(dev);
1856 if (!idev) {
1857 err = -ENODEV;
1858 goto out;
1859 }
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001862 switch (cfg->fc_type) {
1863 case RTN_BLACKHOLE:
1864 rt->dst.error = -EINVAL;
Eric W. Biedermanede20592015-10-07 16:48:47 -05001865 rt->dst.output = dst_discard_out;
Kamala R7150aed2013-12-02 19:55:21 +05301866 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001867 break;
1868 case RTN_PROHIBIT:
1869 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301870 rt->dst.output = ip6_pkt_prohibit_out;
1871 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001872 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001873 case RTN_THROW:
Nikola Forró0315e382015-09-17 16:01:32 +02001874 case RTN_UNREACHABLE:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001875 default:
Kamala R7150aed2013-12-02 19:55:21 +05301876 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
Nikola Forró0315e382015-09-17 16:01:32 +02001877 : (cfg->fc_type == RTN_UNREACHABLE)
1878 ? -EHOSTUNREACH : -ENETUNREACH;
Kamala R7150aed2013-12-02 19:55:21 +05301879 rt->dst.output = ip6_pkt_discard_out;
1880 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001881 break;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto install_route;
1884 }
1885
Thomas Graf86872cb2006-08-22 00:01:08 -07001886 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001887 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int gwa_type;
1889
Thomas Graf86872cb2006-08-22 00:01:08 -07001890 gw_addr = &cfg->fc_gateway;
Florian Westphal330567b2015-08-07 10:54:28 +02001891 gwa_type = ipv6_addr_type(gw_addr);
Florian Westphal48ed7b22015-05-21 00:25:41 +02001892
1893 /* if gw_addr is local we will fail to detect this in case
1894 * address is still TENTATIVE (DAD in progress). rt6_lookup()
1895 * will return already-added prefix route via interface that
1896 * prefix route was assigned to, which might be non-loopback.
1897 */
1898 err = -EINVAL;
Florian Westphal330567b2015-08-07 10:54:28 +02001899 if (ipv6_chk_addr_and_flags(net, gw_addr,
1900 gwa_type & IPV6_ADDR_LINKLOCAL ?
1901 dev : NULL, 0, 0))
Florian Westphal48ed7b22015-05-21 00:25:41 +02001902 goto out;
1903
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001904 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1907 struct rt6_info *grt;
1908
1909 /* IPv6 strictly inhibits using not link-local
1910 addresses as nexthop address.
1911 Otherwise, router will not able to send redirects.
1912 It is very good, but in some (rare!) circumstances
1913 (SIT, PtP, NBMA NOARP links) it is handy to allow
1914 some exceptions. --ANK
1915 */
David S. Miller38308472011-12-03 18:02:47 -05001916 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto out;
1918
Daniel Lezcano55786892008-03-04 13:47:47 -08001919 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001922 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto out;
1924 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001925 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001926 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto out;
1928 }
1929 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001930 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 idev = grt->rt6i_idev;
1932 dev_hold(dev);
1933 in6_dev_hold(grt->rt6i_idev);
1934 }
David S. Miller38308472011-12-03 18:02:47 -05001935 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001937 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (err)
1940 goto out;
1941 }
1942 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001943 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 goto out;
1945 }
1946
1947 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001948 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto out;
1950
Daniel Walterc3968a82011-04-13 21:10:57 +00001951 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1952 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1953 err = -EINVAL;
1954 goto out;
1955 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001956 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001957 rt->rt6i_prefsrc.plen = 128;
1958 } else
1959 rt->rt6i_prefsrc.plen = 0;
1960
Thomas Graf86872cb2006-08-22 00:01:08 -07001961 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001964 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001966 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001967
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001968 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001969
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001970 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971out:
1972 if (dev)
1973 dev_put(dev);
1974 if (idev)
1975 in6_dev_put(idev);
1976 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001977 dst_free(&rt->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001978
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001979 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001980}
1981
1982int ip6_route_add(struct fib6_config *cfg)
1983{
1984 struct mx6_config mxc = { .mx = NULL, };
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001985 struct rt6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001986 int err;
1987
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001988 rt = ip6_route_info_create(cfg);
1989 if (IS_ERR(rt)) {
1990 err = PTR_ERR(rt);
1991 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001992 goto out;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07001993 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07001994
1995 err = ip6_convert_metrics(&mxc, cfg);
1996 if (err)
1997 goto out;
1998
1999 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
2000
2001 kfree(mxc.mx);
2002
2003 return err;
2004out:
2005 if (rt)
2006 dst_free(&rt->dst);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return err;
2009}
2010
Thomas Graf86872cb2006-08-22 00:01:08 -07002011static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07002014 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05002015 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002017 if (rt == net->ipv6.ip6_null_entry ||
2018 rt->dst.flags & DST_NOCACHE) {
Gao feng6825a262012-09-19 19:25:34 +00002019 err = -ENOENT;
2020 goto out;
2021 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07002022
Thomas Grafc71099a2006-08-04 23:20:06 -07002023 table = rt->rt6i_table;
2024 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07002025 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07002026 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Gao feng6825a262012-09-19 19:25:34 +00002028out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00002029 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return err;
2031}
2032
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002033int ip6_del_rt(struct rt6_info *rt)
2034{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002035 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05002036 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08002037 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002038 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002039}
2040
Thomas Graf86872cb2006-08-22 00:01:08 -07002041static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Thomas Grafc71099a2006-08-04 23:20:06 -07002043 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct fib6_node *fn;
2045 struct rt6_info *rt;
2046 int err = -ESRCH;
2047
Daniel Lezcano55786892008-03-04 13:47:47 -08002048 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002049 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Thomas Grafc71099a2006-08-04 23:20:06 -07002052 read_lock_bh(&table->tb6_lock);
2053
2054 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07002055 &cfg->fc_dst, cfg->fc_dst_len,
2056 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002059 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002060 if ((rt->rt6i_flags & RTF_CACHE) &&
2061 !(cfg->fc_flags & RTF_CACHE))
2062 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002063 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05002064 (!rt->dst.dev ||
2065 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002067 if (cfg->fc_flags & RTF_GATEWAY &&
2068 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07002070 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002072 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002073 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Thomas Graf86872cb2006-08-22 00:01:08 -07002075 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002078 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 return err;
2081}
2082
David S. Miller6700c272012-07-17 03:29:28 -07002083static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002084{
David S. Millere8599ff2012-07-11 23:43:53 -07002085 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07002086 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07002087 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002088 struct ndisc_options ndopts;
2089 struct inet6_dev *in6_dev;
2090 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002091 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07002092 int optlen, on_link;
2093 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07002094
Simon Horman29a3cad2013-05-28 20:34:26 +00002095 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002096 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07002097
2098 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07002099 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002100 return;
2101 }
2102
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002103 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07002104
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002105 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002106 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002107 return;
2108 }
2109
David S. Miller6e157b62012-07-12 00:05:02 -07002110 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002111 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002112 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002113 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07002114 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07002115 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07002116 return;
2117 }
2118
2119 in6_dev = __in6_dev_get(skb->dev);
2120 if (!in6_dev)
2121 return;
2122 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2123 return;
2124
2125 /* RFC2461 8.1:
2126 * The IP source address of the Redirect MUST be the same as the current
2127 * first-hop router for the specified ICMP Destination Address.
2128 */
2129
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002130 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07002131 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2132 return;
2133 }
David S. Miller6e157b62012-07-12 00:05:02 -07002134
2135 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07002136 if (ndopts.nd_opts_tgt_lladdr) {
2137 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
2138 skb->dev);
2139 if (!lladdr) {
2140 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
2141 return;
2142 }
2143 }
2144
David S. Miller6e157b62012-07-12 00:05:02 -07002145 rt = (struct rt6_info *) dst;
2146 if (rt == net->ipv6.ip6_null_entry) {
2147 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
2148 return;
2149 }
2150
2151 /* Redirect received -> path was valid.
2152 * Look, redirects are sent only in response to data packets,
2153 * so that this nexthop apparently is reachable. --ANK
2154 */
2155 dst_confirm(&rt->dst);
2156
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002157 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07002158 if (!neigh)
2159 return;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /*
2162 * We have finally decided to accept it.
2163 */
2164
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002165 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 NEIGH_UPDATE_F_WEAK_OVERRIDE|
2167 NEIGH_UPDATE_F_OVERRIDE|
2168 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
2169 NEIGH_UPDATE_F_ISROUTER))
2170 );
2171
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002172 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05002173 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto out;
2175
2176 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
2177 if (on_link)
2178 nrt->rt6i_flags &= ~RTF_GATEWAY;
2179
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002180 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Thomas Graf40e22e82006-08-22 00:00:45 -07002182 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto out;
2184
Changli Gaod8d1f302010-06-10 23:31:35 -07002185 netevent.old = &rt->dst;
2186 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00002187 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00002188 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07002189 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
2190
David S. Miller38308472011-12-03 18:02:47 -05002191 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07002192 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002193 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
2196out:
David S. Millere8599ff2012-07-11 23:43:53 -07002197 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07002198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * Misc support functions
2202 */
2203
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002204static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
2205{
2206 BUG_ON(from->dst.from);
2207
2208 rt->rt6i_flags &= ~RTF_EXPIRES;
2209 dst_hold(&from->dst);
2210 rt->dst.from = &from->dst;
2211 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
2212}
2213
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002214static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002216 rt->dst.input = ort->dst.input;
2217 rt->dst.output = ort->dst.output;
2218 rt->rt6i_dst = ort->rt6i_dst;
2219 rt->dst.error = ort->dst.error;
2220 rt->rt6i_idev = ort->rt6i_idev;
2221 if (rt->rt6i_idev)
2222 in6_dev_hold(rt->rt6i_idev);
2223 rt->dst.lastuse = jiffies;
2224 rt->rt6i_gateway = ort->rt6i_gateway;
2225 rt->rt6i_flags = ort->rt6i_flags;
2226 rt6_set_from(rt, ort);
2227 rt->rt6i_metric = ort->rt6i_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002229 rt->rt6i_src = ort->rt6i_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230#endif
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07002231 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
2232 rt->rt6i_table = ort->rt6i_table;
Jiri Benc61adedf2015-08-20 13:56:25 +02002233 rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002236#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002237static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002238 const struct in6_addr *prefix, int prefixlen,
2239 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002240{
2241 struct fib6_node *fn;
2242 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002243 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002244
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002245 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05002246 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002247 return NULL;
2248
Li RongQing5744dd92012-09-11 21:59:01 +00002249 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002250 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002251 if (!fn)
2252 goto out;
2253
Changli Gaod8d1f302010-06-10 23:31:35 -07002254 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002255 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002256 continue;
2257 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
2258 continue;
2259 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
2260 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002261 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002262 break;
2263 }
2264out:
Li RongQing5744dd92012-09-11 21:59:01 +00002265 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002266 return rt;
2267}
2268
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002269static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002270 const struct in6_addr *prefix, int prefixlen,
2271 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00002272 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002273{
Thomas Graf86872cb2006-08-22 00:01:08 -07002274 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08002275 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002276 .fc_ifindex = ifindex,
2277 .fc_dst_len = prefixlen,
2278 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2279 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002280 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002281 .fc_nlinfo.nlh = NULL,
2282 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002283 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002284
David Ahernca254492015-10-12 11:47:10 -07002285 cfg.fc_table = l3mdev_fib_table_by_index(net, ifindex) ? : RT6_TABLE_INFO;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002286 cfg.fc_dst = *prefix;
2287 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002288
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002289 /* We should treat it as a default route if prefix length is 0. */
2290 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002291 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002292
Thomas Graf86872cb2006-08-22 00:01:08 -07002293 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002294
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002295 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002296}
2297#endif
2298
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002299struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002302 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002304 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002305 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002306 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Li RongQing5744dd92012-09-11 21:59:01 +00002308 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002309 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002310 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002311 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2313 break;
2314 }
2315 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002316 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002317 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return rt;
2319}
2320
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002321struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002322 struct net_device *dev,
2323 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Thomas Graf86872cb2006-08-22 00:01:08 -07002325 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002326 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002327 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002328 .fc_ifindex = dev->ifindex,
2329 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2330 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002331 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002332 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002333 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002336 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Thomas Graf86872cb2006-08-22 00:01:08 -07002338 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return rt6_get_dflt_router(gwaddr, dev);
2341}
2342
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002343void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002346 struct fib6_table *table;
2347
2348 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002349 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002350 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002351 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002354 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002355 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002356 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2357 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002358 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002359 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002360 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto restart;
2362 }
2363 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002364 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Daniel Lezcano55786892008-03-04 13:47:47 -08002367static void rtmsg_to_fib6_config(struct net *net,
2368 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002369 struct fib6_config *cfg)
2370{
2371 memset(cfg, 0, sizeof(*cfg));
2372
David Ahernca254492015-10-12 11:47:10 -07002373 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
2374 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07002375 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2376 cfg->fc_metric = rtmsg->rtmsg_metric;
2377 cfg->fc_expires = rtmsg->rtmsg_info;
2378 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2379 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2380 cfg->fc_flags = rtmsg->rtmsg_flags;
2381
Daniel Lezcano55786892008-03-04 13:47:47 -08002382 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002383
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002384 cfg->fc_dst = rtmsg->rtmsg_dst;
2385 cfg->fc_src = rtmsg->rtmsg_src;
2386 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002387}
2388
Daniel Lezcano55786892008-03-04 13:47:47 -08002389int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Thomas Graf86872cb2006-08-22 00:01:08 -07002391 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct in6_rtmsg rtmsg;
2393 int err;
2394
Ian Morris67ba4152014-08-24 21:53:10 +01002395 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 case SIOCADDRT: /* Add a route */
2397 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002398 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return -EPERM;
2400 err = copy_from_user(&rtmsg, arg,
2401 sizeof(struct in6_rtmsg));
2402 if (err)
2403 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002404
Daniel Lezcano55786892008-03-04 13:47:47 -08002405 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 rtnl_lock();
2408 switch (cmd) {
2409 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002410 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 break;
2412 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002413 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 break;
2415 default:
2416 err = -EINVAL;
2417 }
2418 rtnl_unlock();
2419
2420 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 return -EINVAL;
2424}
2425
2426/*
2427 * Drop the packet on the floor
2428 */
2429
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002430static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002432 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002433 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002434 switch (ipstats_mib_noroutes) {
2435 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002436 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002437 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002438 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2439 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002440 break;
2441 }
2442 /* FALLTHROUGH */
2443 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002444 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2445 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002446 break;
2447 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002448 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 kfree_skb(skb);
2450 return 0;
2451}
2452
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002453static int ip6_pkt_discard(struct sk_buff *skb)
2454{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002455 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002456}
2457
Eric W. Biedermanede20592015-10-07 16:48:47 -05002458static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Eric Dumazetadf30902009-06-02 05:19:30 +00002460 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002461 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002464static int ip6_pkt_prohibit(struct sk_buff *skb)
2465{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002466 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002467}
2468
Eric W. Biedermanede20592015-10-07 16:48:47 -05002469static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002470{
Eric Dumazetadf30902009-06-02 05:19:30 +00002471 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002472 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475/*
2476 * Allocate a dst for local (unicast / anycast) address.
2477 */
2478
2479struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2480 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002481 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
David Ahernca254492015-10-12 11:47:10 -07002483 u32 tb_id;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002484 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002485 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -07002486 DST_NOCOUNT);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002487 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return ERR_PTR(-ENOMEM);
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 in6_dev_hold(idev);
2491
David S. Miller11d53b42011-06-24 15:23:34 -07002492 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002493 rt->dst.input = ip6_input;
2494 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002498 if (anycast)
2499 rt->rt6i_flags |= RTF_ANYCAST;
2500 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Julian Anastasov550bab42013-10-20 15:43:04 +03002503 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002504 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 rt->rt6i_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07002506 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
2507 rt->rt6i_table = fib6_get_table(net, tb_id);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07002508 rt->dst.flags |= DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Changli Gaod8d1f302010-06-10 23:31:35 -07002510 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 return rt;
2513}
2514
Daniel Walterc3968a82011-04-13 21:10:57 +00002515int ip6_route_get_saddr(struct net *net,
2516 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002517 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002518 unsigned int prefs,
2519 struct in6_addr *saddr)
2520{
Markus Stenberge16e8882015-05-05 13:36:59 +03002521 struct inet6_dev *idev =
2522 rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
Daniel Walterc3968a82011-04-13 21:10:57 +00002523 int err = 0;
Markus Stenberge16e8882015-05-05 13:36:59 +03002524 if (rt && rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002525 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002526 else
2527 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2528 daddr, prefs, saddr);
2529 return err;
2530}
2531
2532/* remove deleted ip from prefsrc entries */
2533struct arg_dev_net_ip {
2534 struct net_device *dev;
2535 struct net *net;
2536 struct in6_addr *addr;
2537};
2538
2539static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2540{
2541 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2542 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2543 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2544
David S. Millerd1918542011-12-28 20:19:20 -05002545 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002546 rt != net->ipv6.ip6_null_entry &&
2547 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2548 /* remove prefsrc entry */
2549 rt->rt6i_prefsrc.plen = 0;
2550 }
2551 return 0;
2552}
2553
2554void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2555{
2556 struct net *net = dev_net(ifp->idev->dev);
2557 struct arg_dev_net_ip adni = {
2558 .dev = ifp->idev->dev,
2559 .net = net,
2560 .addr = &ifp->addr,
2561 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002562 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002563}
2564
Duan Jiongbe7a0102014-05-15 15:56:14 +08002565#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2566#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2567
2568/* Remove routers and update dst entries when gateway turn into host. */
2569static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2570{
2571 struct in6_addr *gateway = (struct in6_addr *)arg;
2572
2573 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2574 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2575 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2576 return -1;
2577 }
2578 return 0;
2579}
2580
2581void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2582{
2583 fib6_clean_all(net, fib6_clean_tohost, gateway);
2584}
2585
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002586struct arg_dev_net {
2587 struct net_device *dev;
2588 struct net *net;
2589};
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591static int fib6_ifdown(struct rt6_info *rt, void *arg)
2592{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002593 const struct arg_dev_net *adn = arg;
2594 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002595
David S. Millerd1918542011-12-28 20:19:20 -05002596 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002597 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601}
2602
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002603void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002605 struct arg_dev_net adn = {
2606 .dev = dev,
2607 .net = net,
2608 };
2609
Li RongQing0c3584d2013-12-27 16:32:38 +08002610 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002611 icmp6_clean_all(fib6_ifdown, &adn);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07002612 rt6_uncached_list_flush_dev(net, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
Eric Dumazet95c96172012-04-15 05:58:06 +00002615struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002617 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618};
2619
2620static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2621{
2622 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2623 struct inet6_dev *idev;
2624
2625 /* In IPv6 pmtu discovery is not optional,
2626 so that RTAX_MTU lock cannot disable it.
2627 We still use this lock to block changes
2628 caused by addrconf/ndisc.
2629 */
2630
2631 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002632 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return 0;
2634
2635 /* For administrative MTU increase, there is no way to discover
2636 IPv6 PMTU increase, so PMTU increase should be updated here.
2637 Since RFC 1981 doesn't include administrative MTU increase
2638 update PMTU increase is a MUST. (i.e. jumbo frame)
2639 */
2640 /*
2641 If new MTU is less than route PMTU, this new MTU will be the
2642 lowest MTU in the path, update the route PMTU to reflect PMTU
2643 decreases; if new MTU is greater than route PMTU, and the
2644 old MTU is the lowest MTU in the path, update the route PMTU
2645 to reflect the increase. In this case if the other nodes' MTU
2646 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2647 PMTU discouvery.
2648 */
David S. Millerd1918542011-12-28 20:19:20 -05002649 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002650 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2651 if (rt->rt6i_flags & RTF_CACHE) {
2652 /* For RTF_CACHE with rt6i_pmtu == 0
2653 * (i.e. a redirected route),
2654 * the metrics of its rt->dst.from has already
2655 * been updated.
2656 */
2657 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2658 rt->rt6i_pmtu = arg->mtu;
2659 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2660 (dst_mtu(&rt->dst) < arg->mtu &&
2661 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2662 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2663 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return 0;
2666}
2667
Eric Dumazet95c96172012-04-15 05:58:06 +00002668void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Thomas Grafc71099a2006-08-04 23:20:06 -07002670 struct rt6_mtu_change_arg arg = {
2671 .dev = dev,
2672 .mtu = mtu,
2673 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Li RongQing0c3584d2013-12-27 16:32:38 +08002675 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002678static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002679 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002680 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002681 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002682 [RTA_PRIORITY] = { .type = NLA_U32 },
2683 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002684 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002685 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002686 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
2687 [RTA_ENCAP] = { .type = NLA_NESTED },
Thomas Graf86872cb2006-08-22 00:01:08 -07002688};
2689
2690static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2691 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Thomas Graf86872cb2006-08-22 00:01:08 -07002693 struct rtmsg *rtm;
2694 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002695 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002696 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Thomas Graf86872cb2006-08-22 00:01:08 -07002698 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2699 if (err < 0)
2700 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Thomas Graf86872cb2006-08-22 00:01:08 -07002702 err = -EINVAL;
2703 rtm = nlmsg_data(nlh);
2704 memset(cfg, 0, sizeof(*cfg));
2705
2706 cfg->fc_table = rtm->rtm_table;
2707 cfg->fc_dst_len = rtm->rtm_dst_len;
2708 cfg->fc_src_len = rtm->rtm_src_len;
2709 cfg->fc_flags = RTF_UP;
2710 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002711 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002712
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002713 if (rtm->rtm_type == RTN_UNREACHABLE ||
2714 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002715 rtm->rtm_type == RTN_PROHIBIT ||
2716 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002717 cfg->fc_flags |= RTF_REJECT;
2718
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002719 if (rtm->rtm_type == RTN_LOCAL)
2720 cfg->fc_flags |= RTF_LOCAL;
2721
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002722 if (rtm->rtm_flags & RTM_F_CLONED)
2723 cfg->fc_flags |= RTF_CACHE;
2724
Eric W. Biederman15e47302012-09-07 20:12:54 +00002725 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002726 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002727 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002728
2729 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002730 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002731 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002733
2734 if (tb[RTA_DST]) {
2735 int plen = (rtm->rtm_dst_len + 7) >> 3;
2736
2737 if (nla_len(tb[RTA_DST]) < plen)
2738 goto errout;
2739
2740 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002742
2743 if (tb[RTA_SRC]) {
2744 int plen = (rtm->rtm_src_len + 7) >> 3;
2745
2746 if (nla_len(tb[RTA_SRC]) < plen)
2747 goto errout;
2748
2749 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002751
Daniel Walterc3968a82011-04-13 21:10:57 +00002752 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002753 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002754
Thomas Graf86872cb2006-08-22 00:01:08 -07002755 if (tb[RTA_OIF])
2756 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2757
2758 if (tb[RTA_PRIORITY])
2759 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2760
2761 if (tb[RTA_METRICS]) {
2762 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2763 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002765
2766 if (tb[RTA_TABLE])
2767 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2768
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002769 if (tb[RTA_MULTIPATH]) {
2770 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2771 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2772 }
2773
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002774 if (tb[RTA_PREF]) {
2775 pref = nla_get_u8(tb[RTA_PREF]);
2776 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2777 pref != ICMPV6_ROUTER_PREF_HIGH)
2778 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2779 cfg->fc_flags |= RTF_PREF(pref);
2780 }
2781
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002782 if (tb[RTA_ENCAP])
2783 cfg->fc_encap = tb[RTA_ENCAP];
2784
2785 if (tb[RTA_ENCAP_TYPE])
2786 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
2787
Thomas Graf86872cb2006-08-22 00:01:08 -07002788 err = 0;
2789errout:
2790 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002793struct rt6_nh {
2794 struct rt6_info *rt6_info;
2795 struct fib6_config r_cfg;
2796 struct mx6_config mxc;
2797 struct list_head next;
2798};
2799
2800static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
2801{
2802 struct rt6_nh *nh;
2803
2804 list_for_each_entry(nh, rt6_nh_list, next) {
2805 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6 nexthop %pI6 ifi %d\n",
2806 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
2807 nh->r_cfg.fc_ifindex);
2808 }
2809}
2810
2811static int ip6_route_info_append(struct list_head *rt6_nh_list,
2812 struct rt6_info *rt, struct fib6_config *r_cfg)
2813{
2814 struct rt6_nh *nh;
2815 struct rt6_info *rtnh;
2816 int err = -EEXIST;
2817
2818 list_for_each_entry(nh, rt6_nh_list, next) {
2819 /* check if rt6_info already exists */
2820 rtnh = nh->rt6_info;
2821
2822 if (rtnh->dst.dev == rt->dst.dev &&
2823 rtnh->rt6i_idev == rt->rt6i_idev &&
2824 ipv6_addr_equal(&rtnh->rt6i_gateway,
2825 &rt->rt6i_gateway))
2826 return err;
2827 }
2828
2829 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
2830 if (!nh)
2831 return -ENOMEM;
2832 nh->rt6_info = rt;
2833 err = ip6_convert_metrics(&nh->mxc, r_cfg);
2834 if (err) {
2835 kfree(nh);
2836 return err;
2837 }
2838 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
2839 list_add_tail(&nh->next, rt6_nh_list);
2840
2841 return 0;
2842}
2843
2844static int ip6_route_multipath_add(struct fib6_config *cfg)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002845{
2846 struct fib6_config r_cfg;
2847 struct rtnexthop *rtnh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002848 struct rt6_info *rt;
2849 struct rt6_nh *err_nh;
2850 struct rt6_nh *nh, *nh_safe;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002851 int remaining;
2852 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002853 int err = 1;
2854 int nhn = 0;
2855 int replace = (cfg->fc_nlinfo.nlh &&
2856 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
2857 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002858
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02002859 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002860 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002861
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002862 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
2863 * rt6_info structs per nexthop
2864 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002865 while (rtnh_ok(rtnh, remaining)) {
2866 memcpy(&r_cfg, cfg, sizeof(*cfg));
2867 if (rtnh->rtnh_ifindex)
2868 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2869
2870 attrlen = rtnh_attrlen(rtnh);
2871 if (attrlen > 0) {
2872 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2873
2874 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2875 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002876 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002877 r_cfg.fc_flags |= RTF_GATEWAY;
2878 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002879 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
2880 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
2881 if (nla)
2882 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002883 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002884
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002885 rt = ip6_route_info_create(&r_cfg);
2886 if (IS_ERR(rt)) {
2887 err = PTR_ERR(rt);
2888 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002889 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002890 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002891
2892 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002893 if (err) {
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002894 dst_free(&rt->dst);
2895 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002896 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002897
2898 rtnh = rtnh_next(rtnh, &remaining);
2899 }
2900
2901 err_nh = NULL;
2902 list_for_each_entry(nh, &rt6_nh_list, next) {
2903 err = __ip6_ins_rt(nh->rt6_info, &cfg->fc_nlinfo, &nh->mxc);
2904 /* nh->rt6_info is used or freed at this point, reset to NULL*/
2905 nh->rt6_info = NULL;
2906 if (err) {
2907 if (replace && nhn)
2908 ip6_print_replace_route_err(&rt6_nh_list);
2909 err_nh = nh;
2910 goto add_errout;
2911 }
2912
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002913 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02002914 * these flags after the first nexthop: if there is a collision,
2915 * we have already failed to add the first nexthop:
2916 * fib6_add_rt2node() has rejected it; when replacing, old
2917 * nexthops have been replaced by first new, the rest should
2918 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002919 */
Michal Kubeček27596472015-05-18 20:54:00 +02002920 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
2921 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002922 nhn++;
2923 }
2924
2925 goto cleanup;
2926
2927add_errout:
2928 /* Delete routes that were already added */
2929 list_for_each_entry(nh, &rt6_nh_list, next) {
2930 if (err_nh == nh)
2931 break;
2932 ip6_route_del(&nh->r_cfg);
2933 }
2934
2935cleanup:
2936 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
2937 if (nh->rt6_info)
2938 dst_free(&nh->rt6_info->dst);
Wu Fengguang52fe51f2015-09-10 06:57:12 +08002939 kfree(nh->mxc.mx);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002940 list_del(&nh->next);
2941 kfree(nh);
2942 }
2943
2944 return err;
2945}
2946
2947static int ip6_route_multipath_del(struct fib6_config *cfg)
2948{
2949 struct fib6_config r_cfg;
2950 struct rtnexthop *rtnh;
2951 int remaining;
2952 int attrlen;
2953 int err = 1, last_err = 0;
2954
2955 remaining = cfg->fc_mp_len;
2956 rtnh = (struct rtnexthop *)cfg->fc_mp;
2957
2958 /* Parse a Multipath Entry */
2959 while (rtnh_ok(rtnh, remaining)) {
2960 memcpy(&r_cfg, cfg, sizeof(*cfg));
2961 if (rtnh->rtnh_ifindex)
2962 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2963
2964 attrlen = rtnh_attrlen(rtnh);
2965 if (attrlen > 0) {
2966 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2967
2968 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2969 if (nla) {
2970 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2971 r_cfg.fc_flags |= RTF_GATEWAY;
2972 }
2973 }
2974 err = ip6_route_del(&r_cfg);
2975 if (err)
2976 last_err = err;
2977
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002978 rtnh = rtnh_next(rtnh, &remaining);
2979 }
2980
2981 return last_err;
2982}
2983
Ian Morris67ba4152014-08-24 21:53:10 +01002984static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Thomas Graf86872cb2006-08-22 00:01:08 -07002986 struct fib6_config cfg;
2987 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Thomas Graf86872cb2006-08-22 00:01:08 -07002989 err = rtm_to_fib6_config(skb, nlh, &cfg);
2990 if (err < 0)
2991 return err;
2992
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002993 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07002994 return ip6_route_multipath_del(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002995 else
2996 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Ian Morris67ba4152014-08-24 21:53:10 +01002999static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Thomas Graf86872cb2006-08-22 00:01:08 -07003001 struct fib6_config cfg;
3002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Thomas Graf86872cb2006-08-22 00:01:08 -07003004 err = rtm_to_fib6_config(skb, nlh, &cfg);
3005 if (err < 0)
3006 return err;
3007
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003008 if (cfg.fc_mp)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003009 return ip6_route_multipath_add(&cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003010 else
3011 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003014static inline size_t rt6_nlmsg_size(struct rt6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08003015{
3016 return NLMSG_ALIGN(sizeof(struct rtmsg))
3017 + nla_total_size(16) /* RTA_SRC */
3018 + nla_total_size(16) /* RTA_DST */
3019 + nla_total_size(16) /* RTA_GATEWAY */
3020 + nla_total_size(16) /* RTA_PREFSRC */
3021 + nla_total_size(4) /* RTA_TABLE */
3022 + nla_total_size(4) /* RTA_IIF */
3023 + nla_total_size(4) /* RTA_OIF */
3024 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08003025 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01003026 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003027 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003028 + nla_total_size(1) /* RTA_PREF */
Jiri Benc61adedf2015-08-20 13:56:25 +02003029 + lwtunnel_get_encap_size(rt->dst.lwtstate);
Thomas Graf339bf982006-11-10 14:10:15 -08003030}
3031
Brian Haley191cd582008-08-14 15:33:21 -07003032static int rt6_fill_node(struct net *net,
3033 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07003034 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003035 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003036 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003038 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003040 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08003041 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07003042 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (prefix) { /* user wants prefix routes only */
3045 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
3046 /* success since this is not a prefix route */
3047 return 1;
3048 }
3049 }
3050
Eric W. Biederman15e47302012-09-07 20:12:54 +00003051 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05003052 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08003053 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003054
3055 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 rtm->rtm_family = AF_INET6;
3057 rtm->rtm_dst_len = rt->rt6i_dst.plen;
3058 rtm->rtm_src_len = rt->rt6i_src.plen;
3059 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07003060 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07003061 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07003062 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07003063 table = RT6_TABLE_UNSPEC;
3064 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04003065 if (nla_put_u32(skb, RTA_TABLE, table))
3066 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003067 if (rt->rt6i_flags & RTF_REJECT) {
3068 switch (rt->dst.error) {
3069 case -EINVAL:
3070 rtm->rtm_type = RTN_BLACKHOLE;
3071 break;
3072 case -EACCES:
3073 rtm->rtm_type = RTN_PROHIBIT;
3074 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00003075 case -EAGAIN:
3076 rtm->rtm_type = RTN_THROW;
3077 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00003078 default:
3079 rtm->rtm_type = RTN_UNREACHABLE;
3080 break;
3081 }
3082 }
David S. Miller38308472011-12-03 18:02:47 -05003083 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00003084 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05003085 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 rtm->rtm_type = RTN_LOCAL;
3087 else
3088 rtm->rtm_type = RTN_UNICAST;
3089 rtm->rtm_flags = 0;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003090 if (!netif_carrier_ok(rt->dst.dev)) {
Andy Gospodarekcea45e22015-08-13 10:39:00 -04003091 rtm->rtm_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek35103d12015-08-13 10:39:01 -04003092 if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
3093 rtm->rtm_flags |= RTNH_F_DEAD;
3094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
3096 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05003097 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00003099 else if (rt->rt6i_flags & RTF_ADDRCONF) {
3100 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
3101 rtm->rtm_protocol = RTPROT_RA;
3102 else
3103 rtm->rtm_protocol = RTPROT_KERNEL;
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
David S. Miller38308472011-12-03 18:02:47 -05003106 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 rtm->rtm_flags |= RTM_F_CLONED;
3108
3109 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02003110 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04003111 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003112 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02003114 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003115 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116#ifdef CONFIG_IPV6_SUBTREES
3117 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02003118 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04003119 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003120 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04003121 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02003122 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04003123 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003125 if (iif) {
3126#ifdef CONFIG_IPV6_MROUTE
3127 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08003128 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003129 if (err <= 0) {
3130 if (!nowait) {
3131 if (err == 0)
3132 return 0;
3133 goto nla_put_failure;
3134 } else {
3135 if (err == -EMSGSIZE)
3136 goto nla_put_failure;
3137 }
3138 }
3139 } else
3140#endif
David S. Millerc78679e2012-04-01 20:27:33 -04003141 if (nla_put_u32(skb, RTA_IIF, iif))
3142 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003143 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04003145 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02003146 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003147 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003149
Daniel Walterc3968a82011-04-13 21:10:57 +00003150 if (rt->rt6i_prefsrc.plen) {
3151 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003152 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02003153 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04003154 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00003155 }
3156
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003157 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
3158 if (rt->rt6i_pmtu)
3159 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
3160 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07003161 goto nla_put_failure;
3162
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00003163 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02003164 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00003165 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00003166 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07003167
David S. Millerc78679e2012-04-01 20:27:33 -04003168 if (rt->dst.dev &&
3169 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
3170 goto nla_put_failure;
3171 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
3172 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00003173
3174 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07003175
David S. Miller87a50692012-07-10 05:06:14 -07003176 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08003177 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003179 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
3180 goto nla_put_failure;
3181
Jiri Benc61adedf2015-08-20 13:56:25 +02003182 lwtunnel_fill_encap(skb, rt->dst.lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003183
Johannes Berg053c0952015-01-16 22:09:00 +01003184 nlmsg_end(skb, nlh);
3185 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07003186
3187nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003188 nlmsg_cancel(skb, nlh);
3189 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Patrick McHardy1b43af52006-08-10 23:11:17 -07003192int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
3195 int prefix;
3196
Thomas Graf2d7202b2006-08-22 00:01:27 -07003197 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
3198 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
3200 } else
3201 prefix = 0;
3202
Brian Haley191cd582008-08-14 15:33:21 -07003203 return rt6_fill_node(arg->net,
3204 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003205 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003206 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Ian Morris67ba4152014-08-24 21:53:10 +01003209static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003211 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07003212 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07003214 struct sk_buff *skb;
3215 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05003216 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003217 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07003218
3219 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
3220 if (err < 0)
3221 goto errout;
3222
3223 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05003224 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07003225
3226 if (tb[RTA_SRC]) {
3227 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
3228 goto errout;
3229
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003230 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07003231 }
3232
3233 if (tb[RTA_DST]) {
3234 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
3235 goto errout;
3236
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003237 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07003238 }
3239
3240 if (tb[RTA_IIF])
3241 iif = nla_get_u32(tb[RTA_IIF]);
3242
3243 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003244 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07003245
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07003246 if (tb[RTA_MARK])
3247 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
3248
Thomas Grafab364a62006-08-22 00:01:47 -07003249 if (iif) {
3250 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003251 int flags = 0;
3252
Daniel Lezcano55786892008-03-04 13:47:47 -08003253 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07003254 if (!dev) {
3255 err = -ENODEV;
3256 goto errout;
3257 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003258
3259 fl6.flowi6_iif = iif;
3260
3261 if (!ipv6_addr_any(&fl6.saddr))
3262 flags |= RT6_LOOKUP_F_HAS_SADDR;
3263
3264 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
3265 flags);
3266 } else {
3267 fl6.flowi6_oif = oif;
3268
David Ahernca254492015-10-12 11:47:10 -07003269 if (netif_index_is_l3_master(net, oif)) {
3270 fl6.flowi6_flags = FLOWI_FLAG_L3MDEV_SRC |
3271 FLOWI_FLAG_SKIP_NH_OIF;
3272 }
3273
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00003274 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07003275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05003278 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00003279 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07003280 err = -ENOBUFS;
3281 goto errout;
3282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 /* Reserve room for dummy headers, this skb can pass
3285 through good chunk of routing engine.
3286 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07003287 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
3289
Changli Gaod8d1f302010-06-10 23:31:35 -07003290 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
David S. Miller4c9483b2011-03-12 16:22:43 -05003292 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003293 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003294 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07003296 kfree_skb(skb);
3297 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
Eric W. Biederman15e47302012-09-07 20:12:54 +00003300 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07003301errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003305void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
3306 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08003309 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003310 u32 seq;
3311 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003313 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003314 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07003315
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003316 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05003317 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07003318 goto errout;
3319
Brian Haley191cd582008-08-14 15:33:21 -07003320 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07003321 event, info->portid, seq, 0, 0, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003322 if (err < 0) {
3323 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
3324 WARN_ON(err == -EMSGSIZE);
3325 kfree_skb(skb);
3326 goto errout;
3327 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00003328 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003329 info->nlh, gfp_any());
3330 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07003331errout:
3332 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08003333 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003336static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00003337 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003338{
Jiri Pirko351638e2013-05-28 01:30:21 +00003339 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003340 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003341
3342 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003343 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003344 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
3345#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003346 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003347 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003348 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003349 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
3350#endif
3351 }
3352
3353 return NOTIFY_OK;
3354}
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356/*
3357 * /proc
3358 */
3359
3360#ifdef CONFIG_PROC_FS
3361
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003362static const struct file_operations ipv6_route_proc_fops = {
3363 .owner = THIS_MODULE,
3364 .open = ipv6_route_open,
3365 .read = seq_read,
3366 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02003367 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08003368};
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370static int rt6_stats_seq_show(struct seq_file *seq, void *v)
3371{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003372 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003374 net->ipv6.rt6_stats->fib_nodes,
3375 net->ipv6.rt6_stats->fib_route_nodes,
3376 net->ipv6.rt6_stats->fib_rt_alloc,
3377 net->ipv6.rt6_stats->fib_rt_entries,
3378 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00003379 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003380 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 return 0;
3383}
3384
3385static int rt6_stats_seq_open(struct inode *inode, struct file *file)
3386{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07003387 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08003388}
3389
Arjan van de Ven9a321442007-02-12 00:55:35 -08003390static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 .owner = THIS_MODULE,
3392 .open = rt6_stats_seq_open,
3393 .read = seq_read,
3394 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07003395 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396};
3397#endif /* CONFIG_PROC_FS */
3398
3399#ifdef CONFIG_SYSCTL
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401static
Joe Perchesfe2c6332013-06-11 23:04:25 -07003402int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 void __user *buffer, size_t *lenp, loff_t *ppos)
3404{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003405 struct net *net;
3406 int delay;
3407 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003409
3410 net = (struct net *)ctl->extra1;
3411 delay = net->ipv6.sysctl.flush_delay;
3412 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02003413 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Joe Perchesfe2c6332013-06-11 23:04:25 -07003417struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003418 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08003420 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07003422 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003423 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 },
3425 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003427 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 .maxlen = sizeof(int),
3429 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003430 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 },
3432 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08003434 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 .maxlen = sizeof(int),
3436 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003437 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 },
3439 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003441 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 .maxlen = sizeof(int),
3443 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003444 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 },
3446 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003448 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 .maxlen = sizeof(int),
3450 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003451 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 },
3453 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003455 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 .maxlen = sizeof(int),
3457 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003458 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 },
3460 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003462 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 .maxlen = sizeof(int),
3464 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003465 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 },
3467 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003469 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 .maxlen = sizeof(int),
3471 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003472 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 },
3474 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003476 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 .maxlen = sizeof(int),
3478 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003479 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 },
3481 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003483 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 .maxlen = sizeof(int),
3485 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003486 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003488 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489};
3490
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003491struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003492{
3493 struct ctl_table *table;
3494
3495 table = kmemdup(ipv6_route_table_template,
3496 sizeof(ipv6_route_table_template),
3497 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003498
3499 if (table) {
3500 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003501 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003502 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003503 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3504 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3505 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3506 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3507 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3508 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3509 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003510 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003511
3512 /* Don't export sysctls to unprivileged users */
3513 if (net->user_ns != &init_user_ns)
3514 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003515 }
3516
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003517 return table;
3518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519#endif
3520
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003521static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003522{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003523 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003524
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003525 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3526 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003527
Eric Dumazetfc66f952010-10-08 06:37:34 +00003528 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3529 goto out_ip6_dst_ops;
3530
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003531 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3532 sizeof(*net->ipv6.ip6_null_entry),
3533 GFP_KERNEL);
3534 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003535 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003536 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003537 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003538 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003539 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3540 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003541
3542#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3543 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3544 sizeof(*net->ipv6.ip6_prohibit_entry),
3545 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003546 if (!net->ipv6.ip6_prohibit_entry)
3547 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003548 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003549 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003550 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003551 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3552 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003553
3554 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3555 sizeof(*net->ipv6.ip6_blk_hole_entry),
3556 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003557 if (!net->ipv6.ip6_blk_hole_entry)
3558 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003559 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003560 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003561 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003562 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3563 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003564#endif
3565
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003566 net->ipv6.sysctl.flush_delay = 0;
3567 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3568 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3569 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3570 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3571 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3572 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3573 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3574
Benjamin Thery6891a342008-03-04 13:49:47 -08003575 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3576
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003577 ret = 0;
3578out:
3579 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003580
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003581#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3582out_ip6_prohibit_entry:
3583 kfree(net->ipv6.ip6_prohibit_entry);
3584out_ip6_null_entry:
3585 kfree(net->ipv6.ip6_null_entry);
3586#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003587out_ip6_dst_entries:
3588 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003589out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003590 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003591}
3592
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003593static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003594{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003595 kfree(net->ipv6.ip6_null_entry);
3596#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3597 kfree(net->ipv6.ip6_prohibit_entry);
3598 kfree(net->ipv6.ip6_blk_hole_entry);
3599#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003600 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003601}
3602
Thomas Grafd1896342012-06-18 12:08:33 +00003603static int __net_init ip6_route_net_init_late(struct net *net)
3604{
3605#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003606 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3607 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003608#endif
3609 return 0;
3610}
3611
3612static void __net_exit ip6_route_net_exit_late(struct net *net)
3613{
3614#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003615 remove_proc_entry("ipv6_route", net->proc_net);
3616 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003617#endif
3618}
3619
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003620static struct pernet_operations ip6_route_net_ops = {
3621 .init = ip6_route_net_init,
3622 .exit = ip6_route_net_exit,
3623};
3624
David S. Millerc3426b42012-06-09 16:27:05 -07003625static int __net_init ipv6_inetpeer_init(struct net *net)
3626{
3627 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3628
3629 if (!bp)
3630 return -ENOMEM;
3631 inet_peer_base_init(bp);
3632 net->ipv6.peers = bp;
3633 return 0;
3634}
3635
3636static void __net_exit ipv6_inetpeer_exit(struct net *net)
3637{
3638 struct inet_peer_base *bp = net->ipv6.peers;
3639
3640 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003641 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003642 kfree(bp);
3643}
3644
David S. Miller2b823f72012-06-09 19:00:16 -07003645static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003646 .init = ipv6_inetpeer_init,
3647 .exit = ipv6_inetpeer_exit,
3648};
3649
Thomas Grafd1896342012-06-18 12:08:33 +00003650static struct pernet_operations ip6_route_net_late_ops = {
3651 .init = ip6_route_net_init_late,
3652 .exit = ip6_route_net_exit_late,
3653};
3654
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003655static struct notifier_block ip6_route_dev_notifier = {
3656 .notifier_call = ip6_route_dev_notify,
3657 .priority = 0,
3658};
3659
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003660int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003662 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003663 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003664
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003665 ret = -ENOMEM;
3666 ip6_dst_ops_template.kmem_cachep =
3667 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3668 SLAB_HWCACHE_ALIGN, NULL);
3669 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003670 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003671
Eric Dumazetfc66f952010-10-08 06:37:34 +00003672 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003673 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003674 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003675
David S. Millerc3426b42012-06-09 16:27:05 -07003676 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3677 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003678 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003679
David S. Miller7e52b332012-06-15 15:51:55 -07003680 ret = register_pernet_subsys(&ip6_route_net_ops);
3681 if (ret)
3682 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003683
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003684 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3685
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003686 /* Registering of the loopback is done before this portion of code,
3687 * the loopback reference in rt6_info will not be taken, do it
3688 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003689 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003690 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3691 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003692 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003693 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003694 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003695 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3696 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003697 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003698 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003699 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003700
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003701 ret = xfrm6_init();
3702 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003703 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003704
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003705 ret = fib6_rules_init();
3706 if (ret)
3707 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003708
Thomas Grafd1896342012-06-18 12:08:33 +00003709 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3710 if (ret)
3711 goto fib6_rules_init;
3712
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003713 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003714 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3715 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3716 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003717 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003718
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003719 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003720 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003721 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003722
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07003723 for_each_possible_cpu(cpu) {
3724 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
3725
3726 INIT_LIST_HEAD(&ul->head);
3727 spin_lock_init(&ul->lock);
3728 }
3729
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003730out:
3731 return ret;
3732
Thomas Grafd1896342012-06-18 12:08:33 +00003733out_register_late_subsys:
3734 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003735fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003736 fib6_rules_cleanup();
3737xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003738 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003739out_fib6_init:
3740 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003741out_register_subsys:
3742 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003743out_register_inetpeer:
3744 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003745out_dst_entries:
3746 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003747out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003748 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
3752void ip6_route_cleanup(void)
3753{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003754 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003755 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003756 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003759 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003760 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003761 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003762 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}