blob: 352271154ddb079d17c2021334525772bb9de266 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Joe Perchesf3213832012-05-15 14:11:53 +000027#define pr_fmt(fmt) "IPv6: " fmt
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080045#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020047#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/snmp.h>
49#include <net/ipv6.h>
50#include <net/ip6_fib.h>
51#include <net/ip6_route.h>
52#include <net/ndisc.h>
53#include <net/addrconf.h>
54#include <net/tcp.h>
55#include <linux/rtnetlink.h>
56#include <net/dst.h>
57#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070058#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070059#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000060#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/uaccess.h>
63
64#ifdef CONFIG_SYSCTL
65#include <linux/sysctl.h>
66#endif
67
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020068enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010069 RT6_NUD_FAIL_HARD = -3,
70 RT6_NUD_FAIL_PROBE = -2,
71 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020072 RT6_NUD_SUCCEED = 1
73};
74
Gao feng1716a962012-04-06 00:13:10 +000075static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +000076 const struct in6_addr *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080078static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000079static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static struct dst_entry *ip6_negative_advice(struct dst_entry *);
81static void ip6_dst_destroy(struct dst_entry *);
82static void ip6_dst_ifdown(struct dst_entry *,
83 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080084static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static int ip6_pkt_discard(struct sk_buff *skb);
Eric Dumazetaad88722014-04-15 13:47:15 -040087static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053088static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric Dumazetaad88722014-04-15 13:47:15 -040089static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070091static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
92 struct sk_buff *skb, u32 mtu);
93static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
94 struct sk_buff *skb);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -070095static void rt6_dst_from_metrics_check(struct rt6_info *rt);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020096static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080098#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080099static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000100 const struct in6_addr *prefix, int prefixlen,
101 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +0000102 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800103static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000104 const struct in6_addr *prefix, int prefixlen,
105 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800106#endif
107
David S. Miller06582542011-01-27 14:58:42 -0800108static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
109{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700110 struct rt6_info *rt = (struct rt6_info *)dst;
David S. Miller06582542011-01-27 14:58:42 -0800111
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700112 if (rt->rt6i_flags & RTF_CACHE)
113 return NULL;
114 else
Martin KaFai Lau3b471172015-02-12 16:14:08 -0800115 return dst_cow_metrics_generic(dst, old);
David S. Miller06582542011-01-27 14:58:42 -0800116}
117
David S. Millerf894cbf2012-07-02 21:52:24 -0700118static inline const void *choose_neigh_daddr(struct rt6_info *rt,
119 struct sk_buff *skb,
120 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500121{
122 struct in6_addr *p = &rt->rt6i_gateway;
123
David S. Millera7563f32012-01-26 16:29:16 -0500124 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500125 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700126 else if (skb)
127 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500128 return daddr;
129}
130
David S. Millerf894cbf2012-07-02 21:52:24 -0700131static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
132 struct sk_buff *skb,
133 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700134{
David S. Miller39232972012-01-26 15:22:32 -0500135 struct rt6_info *rt = (struct rt6_info *) dst;
136 struct neighbour *n;
137
David S. Millerf894cbf2012-07-02 21:52:24 -0700138 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000139 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500140 if (n)
141 return n;
142 return neigh_create(&nd_tbl, daddr, dst->dev);
143}
144
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800145static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .gc = ip6_dst_gc,
148 .gc_thresh = 1024,
149 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800150 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000151 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800152 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .destroy = ip6_dst_destroy,
154 .ifdown = ip6_dst_ifdown,
155 .negative_advice = ip6_negative_advice,
156 .link_failure = ip6_link_failure,
157 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700158 .redirect = rt6_do_redirect,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700159 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700160 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Steffen Klassertebb762f2011-11-23 02:12:51 +0000163static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800164{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000165 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
166
167 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800168}
169
David S. Miller6700c272012-07-17 03:29:28 -0700170static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
171 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700172{
173}
174
David S. Miller6700c272012-07-17 03:29:28 -0700175static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
176 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700177{
178}
179
Held Bernhard0972ddb2011-04-24 22:07:32 +0000180static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
181 unsigned long old)
182{
183 return NULL;
184}
185
David S. Miller14e50e52007-05-24 18:17:54 -0700186static struct dst_ops ip6_dst_blackhole_ops = {
187 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700188 .destroy = ip6_dst_destroy,
189 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000190 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800191 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700192 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700193 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000194 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700195 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700196};
197
David S. Miller62fa8a82011-01-26 20:51:05 -0800198static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800199 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800200};
201
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000202static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700203 .dst = {
204 .__refcnt = ATOMIC_INIT(1),
205 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000206 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700207 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700208 .input = ip6_pkt_discard,
209 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 },
211 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700212 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .rt6i_metric = ~(u32) 0,
214 .rt6i_ref = ATOMIC_INIT(1),
215};
216
Thomas Graf101367c2006-08-04 03:39:02 -0700217#ifdef CONFIG_IPV6_MULTIPLE_TABLES
218
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000219static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700220 .dst = {
221 .__refcnt = ATOMIC_INIT(1),
222 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000223 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700224 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700225 .input = ip6_pkt_prohibit,
226 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700227 },
228 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700229 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700230 .rt6i_metric = ~(u32) 0,
231 .rt6i_ref = ATOMIC_INIT(1),
232};
233
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000234static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700235 .dst = {
236 .__refcnt = ATOMIC_INIT(1),
237 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000238 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700239 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700240 .input = dst_discard,
Eric Dumazetaad88722014-04-15 13:47:15 -0400241 .output = dst_discard_sk,
Thomas Graf101367c2006-08-04 03:39:02 -0700242 },
243 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700244 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700245 .rt6i_metric = ~(u32) 0,
246 .rt6i_ref = ATOMIC_INIT(1),
247};
248
249#endif
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* allocate dst with ip6_dst_ops */
David S. Miller97bab732012-06-09 22:36:36 -0700252static inline struct rt6_info *ip6_dst_alloc(struct net *net,
David S. Miller957c6652011-06-24 15:25:00 -0700253 struct net_device *dev,
David S. Miller8b96d222012-06-11 02:01:56 -0700254 int flags,
255 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
David S. Miller97bab732012-06-09 22:36:36 -0700257 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000258 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700259
David S. Miller97bab732012-06-09 22:36:36 -0700260 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000261 struct dst_entry *dst = &rt->dst;
262
263 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000264 INIT_LIST_HEAD(&rt->rt6i_siblings);
David S. Miller97bab732012-06-09 22:36:36 -0700265 }
David S. Millercf911662011-04-28 14:31:47 -0700266 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static void ip6_dst_destroy(struct dst_entry *dst)
270{
271 struct rt6_info *rt = (struct rt6_info *)dst;
272 struct inet6_dev *idev = rt->rt6i_idev;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000273 struct dst_entry *from = dst->from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700275 dst_destroy_metrics_generic(dst);
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000276
David S. Miller38308472011-12-03 18:02:47 -0500277 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rt->rt6i_idev = NULL;
279 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900280 }
Gao feng1716a962012-04-06 00:13:10 +0000281
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000282 dst->from = NULL;
283 dst_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
287 int how)
288{
289 struct rt6_info *rt = (struct rt6_info *)dst;
290 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800291 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900292 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
David S. Miller97cac082012-07-02 22:43:47 -0700294 if (dev != loopback_dev) {
295 if (idev && idev->dev == dev) {
296 struct inet6_dev *loopback_idev =
297 in6_dev_get(loopback_dev);
298 if (loopback_idev) {
299 rt->rt6i_idev = loopback_idev;
300 in6_dev_put(idev);
301 }
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304}
305
Eric Dumazeta50feda2012-05-18 18:57:34 +0000306static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Gao feng1716a962012-04-06 00:13:10 +0000308 if (rt->rt6i_flags & RTF_EXPIRES) {
309 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000310 return true;
Gao feng1716a962012-04-06 00:13:10 +0000311 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000312 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000313 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000314 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000317/* Multipath route selection:
318 * Hash based function using packet header and flowlabel.
319 * Adapted from fib_info_hashfn()
320 */
321static int rt6_info_hash_nhsfn(unsigned int candidate_count,
322 const struct flowi6 *fl6)
323{
324 unsigned int val = fl6->flowi6_proto;
325
YOSHIFUJI Hideaki / 吉藤英明c08977b2013-01-13 05:02:29 +0000326 val ^= ipv6_addr_hash(&fl6->daddr);
327 val ^= ipv6_addr_hash(&fl6->saddr);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000328
329 /* Work only if this not encapsulated */
330 switch (fl6->flowi6_proto) {
331 case IPPROTO_UDP:
332 case IPPROTO_TCP:
333 case IPPROTO_SCTP:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000334 val ^= (__force u16)fl6->fl6_sport;
335 val ^= (__force u16)fl6->fl6_dport;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000336 break;
337
338 case IPPROTO_ICMPV6:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000339 val ^= (__force u16)fl6->fl6_icmp_type;
340 val ^= (__force u16)fl6->fl6_icmp_code;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000341 break;
342 }
343 /* RFC6438 recommands to use flowlabel */
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000344 val ^= (__force u32)fl6->flowlabel;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000345
346 /* Perhaps, we need to tune, this function? */
347 val = val ^ (val >> 7) ^ (val >> 12);
348 return val % candidate_count;
349}
350
351static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200352 struct flowi6 *fl6, int oif,
353 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000354{
355 struct rt6_info *sibling, *next_sibling;
356 int route_choosen;
357
358 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
359 /* Don't change the route, if route_choosen == 0
360 * (siblings does not include ourself)
361 */
362 if (route_choosen)
363 list_for_each_entry_safe(sibling, next_sibling,
364 &match->rt6i_siblings, rt6i_siblings) {
365 route_choosen--;
366 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200367 if (rt6_score_route(sibling, oif, strict) < 0)
368 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000369 match = sibling;
370 break;
371 }
372 }
373 return match;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700377 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
379
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800380static inline struct rt6_info *rt6_device_match(struct net *net,
381 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000382 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700384 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct rt6_info *local = NULL;
387 struct rt6_info *sprt;
388
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900389 if (!oif && ipv6_addr_any(saddr))
390 goto out;
391
Changli Gaod8d1f302010-06-10 23:31:35 -0700392 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500393 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900394
395 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (dev->ifindex == oif)
397 return sprt;
398 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500399 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700401 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900403 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 local->rt6i_idev->dev->ifindex == oif))
405 continue;
406 }
407 local = sprt;
408 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900409 } else {
410 if (ipv6_chk_addr(net, saddr, dev,
411 flags & RT6_LOOKUP_F_IFACE))
412 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900416 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (local)
418 return local;
419
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700420 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800421 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900423out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return rt;
425}
426
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800427#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200428struct __rt6_probe_work {
429 struct work_struct work;
430 struct in6_addr target;
431 struct net_device *dev;
432};
433
434static void rt6_probe_deferred(struct work_struct *w)
435{
436 struct in6_addr mcaddr;
437 struct __rt6_probe_work *work =
438 container_of(w, struct __rt6_probe_work, work);
439
440 addrconf_addr_solict_mult(&work->target, &mcaddr);
441 ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
442 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100443 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200444}
445
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800446static void rt6_probe(struct rt6_info *rt)
447{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000448 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800449 /*
450 * Okay, this does not seem to be appropriate
451 * for now, however, we need to check if it
452 * is really so; aka Router Reachability Probing.
453 *
454 * Router Reachability Probe MUST be rate-limited
455 * to no more than one per minute.
456 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000457 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000458 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000459 rcu_read_lock_bh();
460 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
461 if (neigh) {
462 write_lock(&neigh->lock);
463 if (neigh->nud_state & NUD_VALID)
464 goto out;
YOSHIFUJI Hideaki / 吉藤英明7ff74a52013-01-17 12:53:02 +0000465 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000466
467 if (!neigh ||
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800468 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200469 struct __rt6_probe_work *work;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800470
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200471 work = kmalloc(sizeof(*work), GFP_ATOMIC);
472
473 if (neigh && work)
Jiri Benc7e980562013-12-11 13:48:20 +0100474 __neigh_set_probe_once(neigh);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000475
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200476 if (neigh)
477 write_unlock(&neigh->lock);
478
479 if (work) {
480 INIT_WORK(&work->work, rt6_probe_deferred);
481 work->target = rt->rt6i_gateway;
482 dev_hold(rt->dst.dev);
483 work->dev = rt->dst.dev;
484 schedule_work(&work->work);
485 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000486 } else {
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000487out:
488 write_unlock(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000489 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000490 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800491}
492#else
493static inline void rt6_probe(struct rt6_info *rt)
494{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800495}
496#endif
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800499 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700501static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
David S. Millerd1918542011-12-28 20:19:20 -0500503 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700504 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800505 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700506 if ((dev->flags & IFF_LOOPBACK) &&
507 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
508 return 1;
509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200512static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000514 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200515 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000516
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700517 if (rt->rt6i_flags & RTF_NONEXTHOP ||
518 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200519 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000520
521 rcu_read_lock_bh();
522 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
523 if (neigh) {
524 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800525 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200526 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800527#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000528 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200529 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100530 else
531 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800532#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000533 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200534 } else {
535 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100536 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000537 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000538 rcu_read_unlock_bh();
539
Paul Marksa5a81f02012-12-03 10:26:54 +0000540 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800543static int rt6_score_route(struct rt6_info *rt, int oif,
544 int strict)
545{
Paul Marksa5a81f02012-12-03 10:26:54 +0000546 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900547
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700548 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700549 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200550 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800551#ifdef CONFIG_IPV6_ROUTER_PREF
552 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
553#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200554 if (strict & RT6_LOOKUP_F_REACHABLE) {
555 int n = rt6_check_neigh(rt);
556 if (n < 0)
557 return n;
558 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800559 return m;
560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
David S. Millerf11e6652007-03-24 20:36:25 -0700562static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200563 int *mpri, struct rt6_info *match,
564 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800565{
David S. Millerf11e6652007-03-24 20:36:25 -0700566 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200567 bool match_do_rr = false;
David S. Millerf11e6652007-03-24 20:36:25 -0700568
569 if (rt6_check_expired(rt))
570 goto out;
571
572 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100573 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200574 match_do_rr = true;
575 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100576 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700577 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700578 }
579
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200580 if (strict & RT6_LOOKUP_F_REACHABLE)
581 rt6_probe(rt);
582
Jiri Benc7e980562013-12-11 13:48:20 +0100583 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200584 if (m > *mpri) {
585 *do_rr = match_do_rr;
586 *mpri = m;
587 match = rt;
588 }
David S. Millerf11e6652007-03-24 20:36:25 -0700589out:
590 return match;
591}
592
593static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
594 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200595 u32 metric, int oif, int strict,
596 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700597{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700598 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800599 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
David S. Millerf11e6652007-03-24 20:36:25 -0700601 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700602 cont = NULL;
603 for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
604 if (rt->rt6i_metric != metric) {
605 cont = rt;
606 break;
607 }
608
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200609 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700610 }
611
612 for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
613 if (rt->rt6i_metric != metric) {
614 cont = rt;
615 break;
616 }
617
618 match = find_match(rt, oif, strict, &mpri, match, do_rr);
619 }
620
621 if (match || !cont)
622 return match;
623
624 for (rt = cont; rt; rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200625 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800626
David S. Millerf11e6652007-03-24 20:36:25 -0700627 return match;
628}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800629
David S. Millerf11e6652007-03-24 20:36:25 -0700630static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
631{
632 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800633 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200634 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
David S. Millerf11e6652007-03-24 20:36:25 -0700636 rt0 = fn->rr_ptr;
637 if (!rt0)
638 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200640 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
641 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200643 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700644 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700645
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800646 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700647 if (!next || next->rt6i_metric != rt0->rt6i_metric)
648 next = fn->leaf;
649
650 if (next != rt0)
651 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
David S. Millerd1918542011-12-28 20:19:20 -0500654 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000655 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800658#ifdef CONFIG_IPV6_ROUTE_INFO
659int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000660 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800661{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900662 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800663 struct route_info *rinfo = (struct route_info *) opt;
664 struct in6_addr prefix_buf, *prefix;
665 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900666 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800667 struct rt6_info *rt;
668
669 if (len < sizeof(struct route_info)) {
670 return -EINVAL;
671 }
672
673 /* Sanity check for prefix_len and length */
674 if (rinfo->length > 3) {
675 return -EINVAL;
676 } else if (rinfo->prefix_len > 128) {
677 return -EINVAL;
678 } else if (rinfo->prefix_len > 64) {
679 if (rinfo->length < 2) {
680 return -EINVAL;
681 }
682 } else if (rinfo->prefix_len > 0) {
683 if (rinfo->length < 1) {
684 return -EINVAL;
685 }
686 }
687
688 pref = rinfo->route_pref;
689 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000690 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800691
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900692 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800693
694 if (rinfo->length == 3)
695 prefix = (struct in6_addr *)rinfo->prefix;
696 else {
697 /* this function is safe */
698 ipv6_addr_prefix(&prefix_buf,
699 (struct in6_addr *)rinfo->prefix,
700 rinfo->prefix_len);
701 prefix = &prefix_buf;
702 }
703
Duan Jiongf104a562013-11-08 09:56:53 +0800704 if (rinfo->prefix_len == 0)
705 rt = rt6_get_dflt_router(gwaddr, dev);
706 else
707 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
708 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800709
710 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700711 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800712 rt = NULL;
713 }
714
715 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800716 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800717 pref);
718 else if (rt)
719 rt->rt6i_flags = RTF_ROUTEINFO |
720 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
721
722 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000723 if (!addrconf_finite_timeout(lifetime))
724 rt6_clean_expires(rt);
725 else
726 rt6_set_expires(rt, jiffies + HZ * lifetime);
727
Amerigo Wang94e187c2012-10-29 00:13:19 +0000728 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800729 }
730 return 0;
731}
732#endif
733
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700734static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
735 struct in6_addr *saddr)
736{
737 struct fib6_node *pn;
738 while (1) {
739 if (fn->fn_flags & RTN_TL_ROOT)
740 return NULL;
741 pn = fn->parent;
742 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
743 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
744 else
745 fn = pn;
746 if (fn->fn_flags & RTN_RTINFO)
747 return fn;
748 }
749}
Thomas Grafc71099a2006-08-04 23:20:06 -0700750
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800751static struct rt6_info *ip6_pol_route_lookup(struct net *net,
752 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500753 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct fib6_node *fn;
756 struct rt6_info *rt;
757
Thomas Grafc71099a2006-08-04 23:20:06 -0700758 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500759 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700760restart:
761 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500762 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000763 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200764 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700765 if (rt == net->ipv6.ip6_null_entry) {
766 fn = fib6_backtrack(fn, &fl6->saddr);
767 if (fn)
768 goto restart;
769 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700770 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700771 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700772 return rt;
773
774}
775
Ian Morris67ba4152014-08-24 21:53:10 +0100776struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
Florian Westphalea6e5742011-09-05 16:05:44 +0200777 int flags)
778{
779 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
780}
781EXPORT_SYMBOL_GPL(ip6_route_lookup);
782
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900783struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
784 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700785{
David S. Miller4c9483b2011-03-12 16:22:43 -0500786 struct flowi6 fl6 = {
787 .flowi6_oif = oif,
788 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700789 };
790 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700791 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700792
Thomas Grafadaa70b2006-10-13 15:01:03 -0700793 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500794 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700795 flags |= RT6_LOOKUP_F_HAS_SADDR;
796 }
797
David S. Miller4c9483b2011-03-12 16:22:43 -0500798 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700799 if (dst->error == 0)
800 return (struct rt6_info *) dst;
801
802 dst_release(dst);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return NULL;
805}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900806EXPORT_SYMBOL(rt6_lookup);
807
Thomas Grafc71099a2006-08-04 23:20:06 -0700808/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 It takes new route entry, the addition fails by any reason the
810 route is freed. In any case, if caller does not hold it, it may
811 be destroyed.
812 */
813
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100814static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
Florian Westphale715b6d2015-01-05 23:57:44 +0100815 struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700818 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Thomas Grafc71099a2006-08-04 23:20:06 -0700820 table = rt->rt6i_table;
821 write_lock_bh(&table->tb6_lock);
Florian Westphale715b6d2015-01-05 23:57:44 +0100822 err = fib6_add(&table->tb6_root, rt, info, mxc);
Thomas Grafc71099a2006-08-04 23:20:06 -0700823 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 return err;
826}
827
Thomas Graf40e22e82006-08-22 00:00:45 -0700828int ip6_ins_rt(struct rt6_info *rt)
829{
Florian Westphale715b6d2015-01-05 23:57:44 +0100830 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
831 struct mx6_config mxc = { .mx = NULL, };
832
833 return __ip6_ins_rt(rt, &info, &mxc);
Thomas Graf40e22e82006-08-22 00:00:45 -0700834}
835
Gao feng1716a962012-04-06 00:13:10 +0000836static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000837 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000838 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct rt6_info *rt;
841
842 /*
843 * Clone the route.
844 */
845
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000846 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (rt) {
Duan Jiong249a3632013-11-05 13:34:53 +0800849 if (ort->rt6i_dst.plen != 128 &&
850 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
851 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855#ifdef CONFIG_IPV6_SUBTREES
856 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000857 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 rt->rt6i_src.plen = 128;
859 }
860#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800863 return rt;
864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000866static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
867 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800868{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000869 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
870
YOSHIFUJI Hideaki / 吉藤英明887c95c2013-01-17 12:54:05 +0000871 if (rt)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800872 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800873 return rt;
874}
875
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800876static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500877 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700879 struct fib6_node *fn, *saved_fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800880 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700881 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800883 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700885 strict |= flags & RT6_LOOKUP_F_IFACE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700886 if (net->ipv6.devconf_all->forwarding == 0)
887 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700889redo_fib6_lookup_lock:
Thomas Grafc71099a2006-08-04 23:20:06 -0700890 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
David S. Miller4c9483b2011-03-12 16:22:43 -0500892 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700893 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700895redo_rt6_select:
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700896 rt = rt6_select(fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200897 if (rt->rt6i_nsiblings)
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700898 rt = rt6_multipath_select(rt, fl6, oif, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700899 if (rt == net->ipv6.ip6_null_entry) {
900 fn = fib6_backtrack(fn, &fl6->saddr);
901 if (fn)
902 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700903 else if (strict & RT6_LOOKUP_F_REACHABLE) {
904 /* also consider unreachable route */
905 strict &= ~RT6_LOOKUP_F_REACHABLE;
906 fn = saved_fn;
907 goto redo_rt6_select;
908 } else {
909 dst_hold(&rt->dst);
910 read_unlock_bh(&table->tb6_lock);
911 goto out2;
912 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700913 }
914
Changli Gaod8d1f302010-06-10 23:31:35 -0700915 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700916 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800917
Martin KaFai Lau94c77bb2014-10-20 13:42:44 -0700918 if (rt->rt6i_flags & RTF_CACHE)
919 goto out2;
920
YOSHIFUJI Hideaki / 吉藤英明c440f162013-01-17 12:53:32 +0000921 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
David S. Miller4c9483b2011-03-12 16:22:43 -0500922 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau653437d2015-04-28 13:03:05 -0700923 else if (!(rt->dst.flags & DST_HOST) || !(rt->dst.flags & RTF_LOCAL))
David S. Miller4c9483b2011-03-12 16:22:43 -0500924 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800925 else
926 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800927
Amerigo Wang94e187c2012-10-29 00:13:19 +0000928 ip6_rt_put(rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800929 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800930
Changli Gaod8d1f302010-06-10 23:31:35 -0700931 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800932 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700933 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800934 if (!err)
935 goto out2;
936 }
937
938 if (--attempts <= 0)
939 goto out2;
940
941 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700942 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800943 * released someone could insert this route. Relookup.
944 */
Amerigo Wang94e187c2012-10-29 00:13:19 +0000945 ip6_rt_put(rt);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700946 goto redo_fib6_lookup_lock;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948out2:
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700949 rt6_dst_from_metrics_check(rt);
Changli Gaod8d1f302010-06-10 23:31:35 -0700950 rt->dst.lastuse = jiffies;
951 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700952
953 return rt;
954}
955
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800956static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500957 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700958{
David S. Miller4c9483b2011-03-12 16:22:43 -0500959 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700960}
961
Shmulik Ladkani72331bc2012-04-01 04:03:45 +0000962static struct dst_entry *ip6_route_input_lookup(struct net *net,
963 struct net_device *dev,
964 struct flowi6 *fl6, int flags)
965{
966 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
967 flags |= RT6_LOOKUP_F_IFACE;
968
969 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
970}
971
Thomas Grafc71099a2006-08-04 23:20:06 -0700972void ip6_route_input(struct sk_buff *skb)
973{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000974 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900975 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700976 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500977 struct flowi6 fl6 = {
978 .flowi6_iif = skb->dev->ifindex,
979 .daddr = iph->daddr,
980 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +0000981 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -0500982 .flowi6_mark = skb->mark,
983 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700984 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700985
Shmulik Ladkani72331bc2012-04-01 04:03:45 +0000986 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -0700987}
988
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800989static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500990 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700991{
David S. Miller4c9483b2011-03-12 16:22:43 -0500992 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700993}
994
Ian Morris67ba4152014-08-24 21:53:10 +0100995struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500996 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700997{
998 int flags = 0;
999
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001000 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001001
David S. Miller4c9483b2011-03-12 16:22:43 -05001002 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001003 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001004
David S. Miller4c9483b2011-03-12 16:22:43 -05001005 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001006 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001007 else if (sk)
1008 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001009
David S. Miller4c9483b2011-03-12 16:22:43 -05001010 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001012EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David S. Miller2774c132011-03-01 14:59:04 -08001014struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001015{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001016 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001017 struct dst_entry *new = NULL;
1018
David S. Millerf5b0a872012-07-19 12:31:33 -07001019 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001020 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001021 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001022
Steffen Klassert81048912012-07-05 23:37:09 +00001023 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
Steffen Klassert81048912012-07-05 23:37:09 +00001024
David S. Miller14e50e52007-05-24 18:17:54 -07001025 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001026 new->input = dst_discard;
Eric Dumazetaad88722014-04-15 13:47:15 -04001027 new->output = dst_discard_sk;
David S. Miller14e50e52007-05-24 18:17:54 -07001028
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001029 if (dst_metrics_read_only(&ort->dst))
1030 new->_metrics = ort->dst._metrics;
1031 else
1032 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001033 rt->rt6i_idev = ort->rt6i_idev;
1034 if (rt->rt6i_idev)
1035 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001036
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001037 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001038 rt->rt6i_flags = ort->rt6i_flags;
David S. Miller14e50e52007-05-24 18:17:54 -07001039 rt->rt6i_metric = 0;
1040
1041 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1042#ifdef CONFIG_IPV6_SUBTREES
1043 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1044#endif
1045
1046 dst_free(new);
1047 }
1048
David S. Miller69ead7a2011-03-01 14:45:33 -08001049 dst_release(dst_orig);
1050 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001051}
David S. Miller14e50e52007-05-24 18:17:54 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * Destination cache support functions
1055 */
1056
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001057static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1058{
1059 if (rt->dst.from &&
1060 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1061 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1065{
1066 struct rt6_info *rt;
1067
1068 rt = (struct rt6_info *) dst;
1069
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001070 /* All IPV6 dsts are created with ->obsolete set to the value
1071 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1072 * into this function always.
1073 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001074 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1075 return NULL;
Li RongQinga4477c42012-11-07 21:56:33 +00001076
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001077 if (rt6_check_expired(rt))
1078 return NULL;
1079
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001080 rt6_dst_from_metrics_check(rt);
1081
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001082 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1086{
1087 struct rt6_info *rt = (struct rt6_info *) dst;
1088
1089 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001090 if (rt->rt6i_flags & RTF_CACHE) {
1091 if (rt6_check_expired(rt)) {
1092 ip6_del_rt(rt);
1093 dst = NULL;
1094 }
1095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001097 dst = NULL;
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001100 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103static void ip6_link_failure(struct sk_buff *skb)
1104{
1105 struct rt6_info *rt;
1106
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001107 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Dumazetadf30902009-06-02 05:19:30 +00001109 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001111 if (rt->rt6i_flags & RTF_CACHE) {
1112 dst_hold(&rt->dst);
1113 if (ip6_del_rt(rt))
1114 dst_free(&rt->dst);
1115 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rt->rt6i_node->fn_sernum = -1;
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
1120
David S. Miller6700c272012-07-17 03:29:28 -07001121static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1122 struct sk_buff *skb, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Ian Morris67ba4152014-08-24 21:53:10 +01001124 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
David S. Miller81aded22012-06-15 14:54:11 -07001126 dst_confirm(dst);
Martin KaFai Lau653437d2015-04-28 13:03:05 -07001127 if (mtu < dst_mtu(dst) && (rt6->rt6i_flags & RTF_CACHE)) {
David S. Miller81aded22012-06-15 14:54:11 -07001128 struct net *net = dev_net(dst->dev);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 rt6->rt6i_flags |= RTF_MODIFIED;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001131 if (mtu < IPV6_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 mtu = IPV6_MIN_MTU;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001133
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001134 rt6->rt6i_pmtu = mtu;
David S. Miller81aded22012-06-15 14:54:11 -07001135 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137}
1138
David S. Miller42ae66c2012-06-15 20:01:57 -07001139void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1140 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001141{
1142 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1143 struct dst_entry *dst;
1144 struct flowi6 fl6;
1145
1146 memset(&fl6, 0, sizeof(fl6));
1147 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001148 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001149 fl6.daddr = iph->daddr;
1150 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001151 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001152
1153 dst = ip6_route_output(net, NULL, &fl6);
1154 if (!dst->error)
David S. Miller6700c272012-07-17 03:29:28 -07001155 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001156 dst_release(dst);
1157}
1158EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1159
1160void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1161{
1162 ip6_update_pmtu(skb, sock_net(sk), mtu,
1163 sk->sk_bound_dev_if, sk->sk_mark);
1164}
1165EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1166
Duan Jiongb55b76b2013-09-04 19:44:21 +08001167/* Handle redirects */
1168struct ip6rd_flowi {
1169 struct flowi6 fl6;
1170 struct in6_addr gateway;
1171};
1172
1173static struct rt6_info *__ip6_route_redirect(struct net *net,
1174 struct fib6_table *table,
1175 struct flowi6 *fl6,
1176 int flags)
1177{
1178 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1179 struct rt6_info *rt;
1180 struct fib6_node *fn;
1181
1182 /* Get the "current" route for this destination and
1183 * check if the redirect has come from approriate router.
1184 *
1185 * RFC 4861 specifies that redirects should only be
1186 * accepted if they come from the nexthop to the target.
1187 * Due to the way the routes are chosen, this notion
1188 * is a bit fuzzy and one might need to check all possible
1189 * routes.
1190 */
1191
1192 read_lock_bh(&table->tb6_lock);
1193 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1194restart:
1195 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1196 if (rt6_check_expired(rt))
1197 continue;
1198 if (rt->dst.error)
1199 break;
1200 if (!(rt->rt6i_flags & RTF_GATEWAY))
1201 continue;
1202 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1203 continue;
1204 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1205 continue;
1206 break;
1207 }
1208
1209 if (!rt)
1210 rt = net->ipv6.ip6_null_entry;
1211 else if (rt->dst.error) {
1212 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001213 goto out;
1214 }
1215
1216 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001217 fn = fib6_backtrack(fn, &fl6->saddr);
1218 if (fn)
1219 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001220 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001221
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001222out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001223 dst_hold(&rt->dst);
1224
1225 read_unlock_bh(&table->tb6_lock);
1226
1227 return rt;
1228};
1229
1230static struct dst_entry *ip6_route_redirect(struct net *net,
1231 const struct flowi6 *fl6,
1232 const struct in6_addr *gateway)
1233{
1234 int flags = RT6_LOOKUP_F_HAS_SADDR;
1235 struct ip6rd_flowi rdfl;
1236
1237 rdfl.fl6 = *fl6;
1238 rdfl.gateway = *gateway;
1239
1240 return fib6_rule_lookup(net, &rdfl.fl6,
1241 flags, __ip6_route_redirect);
1242}
1243
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001244void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1245{
1246 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1247 struct dst_entry *dst;
1248 struct flowi6 fl6;
1249
1250 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001251 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001252 fl6.flowi6_oif = oif;
1253 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001254 fl6.daddr = iph->daddr;
1255 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001256 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001257
Duan Jiongb55b76b2013-09-04 19:44:21 +08001258 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1259 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001260 dst_release(dst);
1261}
1262EXPORT_SYMBOL_GPL(ip6_redirect);
1263
Duan Jiongc92a59e2013-08-22 12:07:35 +08001264void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1265 u32 mark)
1266{
1267 const struct ipv6hdr *iph = ipv6_hdr(skb);
1268 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1269 struct dst_entry *dst;
1270 struct flowi6 fl6;
1271
1272 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001273 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001274 fl6.flowi6_oif = oif;
1275 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001276 fl6.daddr = msg->dest;
1277 fl6.saddr = iph->daddr;
1278
Duan Jiongb55b76b2013-09-04 19:44:21 +08001279 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1280 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001281 dst_release(dst);
1282}
1283
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001284void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1285{
1286 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1287}
1288EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1289
David S. Miller0dbaee32010-12-13 12:52:14 -08001290static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
David S. Miller0dbaee32010-12-13 12:52:14 -08001292 struct net_device *dev = dst->dev;
1293 unsigned int mtu = dst_mtu(dst);
1294 struct net *net = dev_net(dev);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1297
Daniel Lezcano55786892008-03-04 13:47:47 -08001298 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1299 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001302 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1303 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1304 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * rely only on pmtu discovery"
1306 */
1307 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1308 mtu = IPV6_MAXPLEN;
1309 return mtu;
1310}
1311
Steffen Klassertebb762f2011-11-23 02:12:51 +00001312static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001313{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001314 const struct rt6_info *rt = (const struct rt6_info *)dst;
1315 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001316 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001317
1318 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001319 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001320
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001321 mtu = dst_metric_raw(dst, RTAX_MTU);
1322 if (mtu)
1323 goto out;
1324
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001325 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001326
1327 rcu_read_lock();
1328 idev = __in6_dev_get(dst->dev);
1329 if (idev)
1330 mtu = idev->cnf.mtu6;
1331 rcu_read_unlock();
1332
Eric Dumazet30f78d82014-04-10 21:23:36 -07001333out:
1334 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001335}
1336
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001337static struct dst_entry *icmp6_dst_gc_list;
1338static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001339
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001340struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001341 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
David S. Miller87a11572011-12-06 17:04:13 -05001343 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct rt6_info *rt;
1345 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001346 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
David S. Miller38308472011-12-03 18:02:47 -05001348 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001349 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
David S. Miller8b96d222012-06-11 02:01:56 -07001351 rt = ip6_dst_alloc(net, dev, 0, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001352 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001354 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto out;
1356 }
1357
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001358 rt->dst.flags |= DST_HOST;
1359 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001360 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001361 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001362 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001363 rt->rt6i_dst.plen = 128;
1364 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001365 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001367 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001368 rt->dst.next = icmp6_dst_gc_list;
1369 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001370 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Daniel Lezcano55786892008-03-04 13:47:47 -08001372 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David S. Miller87a11572011-12-06 17:04:13 -05001374 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376out:
David S. Miller87a11572011-12-06 17:04:13 -05001377 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001380int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001382 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001383 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001385 spin_lock_bh(&icmp6_dst_lock);
1386 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 while ((dst = *pprev) != NULL) {
1389 if (!atomic_read(&dst->__refcnt)) {
1390 *pprev = dst->next;
1391 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 } else {
1393 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001394 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
1397
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001398 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001399
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001400 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
David S. Miller1e493d12008-09-10 17:27:15 -07001403static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1404 void *arg)
1405{
1406 struct dst_entry *dst, **pprev;
1407
1408 spin_lock_bh(&icmp6_dst_lock);
1409 pprev = &icmp6_dst_gc_list;
1410 while ((dst = *pprev) != NULL) {
1411 struct rt6_info *rt = (struct rt6_info *) dst;
1412 if (func(rt, arg)) {
1413 *pprev = dst->next;
1414 dst_free(dst);
1415 } else {
1416 pprev = &dst->next;
1417 }
1418 }
1419 spin_unlock_bh(&icmp6_dst_lock);
1420}
1421
Daniel Lezcano569d3642008-01-18 03:56:57 -08001422static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001424 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001425 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1426 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1427 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1428 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1429 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001430 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Eric Dumazetfc66f952010-10-08 06:37:34 +00001432 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001433 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001434 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto out;
1436
Benjamin Thery6891a342008-03-04 13:49:47 -08001437 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001438 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001439 entries = dst_entries_get_slow(ops);
1440 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001441 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001443 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001444 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Florian Westphale715b6d2015-01-05 23:57:44 +01001447static int ip6_convert_metrics(struct mx6_config *mxc,
1448 const struct fib6_config *cfg)
1449{
1450 struct nlattr *nla;
1451 int remaining;
1452 u32 *mp;
1453
Ian Morris63159f22015-03-29 14:00:04 +01001454 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001455 return 0;
1456
1457 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1458 if (unlikely(!mp))
1459 return -ENOMEM;
1460
1461 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1462 int type = nla_type(nla);
1463
1464 if (type) {
Daniel Borkmannea697632015-01-05 23:57:47 +01001465 u32 val;
1466
Florian Westphale715b6d2015-01-05 23:57:44 +01001467 if (unlikely(type > RTAX_MAX))
1468 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001469 if (type == RTAX_CC_ALGO) {
1470 char tmp[TCP_CA_NAME_MAX];
Florian Westphale715b6d2015-01-05 23:57:44 +01001471
Daniel Borkmannea697632015-01-05 23:57:47 +01001472 nla_strlcpy(tmp, nla, sizeof(tmp));
1473 val = tcp_ca_get_key_by_name(tmp);
1474 if (val == TCP_CA_UNSPEC)
1475 goto err;
1476 } else {
1477 val = nla_get_u32(nla);
1478 }
1479
1480 mp[type - 1] = val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001481 __set_bit(type - 1, mxc->mx_valid);
1482 }
1483 }
1484
1485 mxc->mx = mp;
1486
1487 return 0;
1488 err:
1489 kfree(mp);
1490 return -EINVAL;
1491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Thomas Graf86872cb2006-08-22 00:01:08 -07001493int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001496 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct rt6_info *rt = NULL;
1498 struct net_device *dev = NULL;
1499 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001500 struct fib6_table *table;
Florian Westphale715b6d2015-01-05 23:57:44 +01001501 struct mx6_config mxc = { .mx = NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 int addr_type;
1503
Thomas Graf86872cb2006-08-22 00:01:08 -07001504 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EINVAL;
1506#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001507 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EINVAL;
1509#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001510 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001512 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (!dev)
1514 goto out;
1515 idev = in6_dev_get(dev);
1516 if (!idev)
1517 goto out;
1518 }
1519
Thomas Graf86872cb2006-08-22 00:01:08 -07001520 if (cfg->fc_metric == 0)
1521 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Matti Vaittinend71314b2011-11-14 00:14:49 +00001523 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001524 if (cfg->fc_nlinfo.nlh &&
1525 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001526 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001527 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001528 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001529 table = fib6_new_table(net, cfg->fc_table);
1530 }
1531 } else {
1532 table = fib6_new_table(net, cfg->fc_table);
1533 }
David S. Miller38308472011-12-03 18:02:47 -05001534
1535 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001536 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001537
Sabrina Dubrocac88507f2014-03-06 17:51:57 +01001538 rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
David S. Miller38308472011-12-03 18:02:47 -05001540 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 err = -ENOMEM;
1542 goto out;
1543 }
1544
Gao feng1716a962012-04-06 00:13:10 +00001545 if (cfg->fc_flags & RTF_EXPIRES)
1546 rt6_set_expires(rt, jiffies +
1547 clock_t_to_jiffies(cfg->fc_expires));
1548 else
1549 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Graf86872cb2006-08-22 00:01:08 -07001551 if (cfg->fc_protocol == RTPROT_UNSPEC)
1552 cfg->fc_protocol = RTPROT_BOOT;
1553 rt->rt6i_protocol = cfg->fc_protocol;
1554
1555 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001558 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001559 else if (cfg->fc_flags & RTF_LOCAL)
1560 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001562 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Changli Gaod8d1f302010-06-10 23:31:35 -07001564 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Thomas Graf86872cb2006-08-22 00:01:08 -07001566 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1567 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07001568 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001569 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001572 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1573 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574#endif
1575
Thomas Graf86872cb2006-08-22 00:01:08 -07001576 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 /* We cannot add true routes via loopback here,
1579 they would result in kernel looping; promote them to reject routes
1580 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001581 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001582 (dev && (dev->flags & IFF_LOOPBACK) &&
1583 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1584 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001586 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (dev) {
1588 dev_put(dev);
1589 in6_dev_put(idev);
1590 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001591 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 dev_hold(dev);
1593 idev = in6_dev_get(dev);
1594 if (!idev) {
1595 err = -ENODEV;
1596 goto out;
1597 }
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001600 switch (cfg->fc_type) {
1601 case RTN_BLACKHOLE:
1602 rt->dst.error = -EINVAL;
Eric Dumazetaad88722014-04-15 13:47:15 -04001603 rt->dst.output = dst_discard_sk;
Kamala R7150aed2013-12-02 19:55:21 +05301604 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001605 break;
1606 case RTN_PROHIBIT:
1607 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301608 rt->dst.output = ip6_pkt_prohibit_out;
1609 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001610 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001611 case RTN_THROW:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001612 default:
Kamala R7150aed2013-12-02 19:55:21 +05301613 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
1614 : -ENETUNREACH;
1615 rt->dst.output = ip6_pkt_discard_out;
1616 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001617 break;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto install_route;
1620 }
1621
Thomas Graf86872cb2006-08-22 00:01:08 -07001622 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001623 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int gwa_type;
1625
Thomas Graf86872cb2006-08-22 00:01:08 -07001626 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001627 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 gwa_type = ipv6_addr_type(gw_addr);
1629
1630 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1631 struct rt6_info *grt;
1632
1633 /* IPv6 strictly inhibits using not link-local
1634 addresses as nexthop address.
1635 Otherwise, router will not able to send redirects.
1636 It is very good, but in some (rare!) circumstances
1637 (SIT, PtP, NBMA NOARP links) it is handy to allow
1638 some exceptions. --ANK
1639 */
1640 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001641 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 goto out;
1643
Daniel Lezcano55786892008-03-04 13:47:47 -08001644 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001647 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto out;
1649 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001650 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001651 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto out;
1653 }
1654 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001655 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 idev = grt->rt6i_idev;
1657 dev_hold(dev);
1658 in6_dev_hold(grt->rt6i_idev);
1659 }
David S. Miller38308472011-12-03 18:02:47 -05001660 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001662 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (err)
1665 goto out;
1666 }
1667 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001668 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto out;
1670 }
1671
1672 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001673 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 goto out;
1675
Daniel Walterc3968a82011-04-13 21:10:57 +00001676 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1677 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1678 err = -EINVAL;
1679 goto out;
1680 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001681 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001682 rt->rt6i_prefsrc.plen = 128;
1683 } else
1684 rt->rt6i_prefsrc.plen = 0;
1685
Thomas Graf86872cb2006-08-22 00:01:08 -07001686 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001689 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001691 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001692
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001693 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001694
Florian Westphale715b6d2015-01-05 23:57:44 +01001695 err = ip6_convert_metrics(&mxc, cfg);
1696 if (err)
1697 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Florian Westphale715b6d2015-01-05 23:57:44 +01001699 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1700
1701 kfree(mxc.mx);
1702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703out:
1704 if (dev)
1705 dev_put(dev);
1706 if (idev)
1707 in6_dev_put(idev);
1708 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001709 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return err;
1711}
1712
Thomas Graf86872cb2006-08-22 00:01:08 -07001713static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001716 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001717 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Gao feng6825a262012-09-19 19:25:34 +00001719 if (rt == net->ipv6.ip6_null_entry) {
1720 err = -ENOENT;
1721 goto out;
1722 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07001723
Thomas Grafc71099a2006-08-04 23:20:06 -07001724 table = rt->rt6i_table;
1725 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07001726 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07001727 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Gao feng6825a262012-09-19 19:25:34 +00001729out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001730 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return err;
1732}
1733
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001734int ip6_del_rt(struct rt6_info *rt)
1735{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001736 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001737 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001738 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001739 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001740}
1741
Thomas Graf86872cb2006-08-22 00:01:08 -07001742static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Thomas Grafc71099a2006-08-04 23:20:06 -07001744 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct fib6_node *fn;
1746 struct rt6_info *rt;
1747 int err = -ESRCH;
1748
Daniel Lezcano55786892008-03-04 13:47:47 -08001749 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001750 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Thomas Grafc71099a2006-08-04 23:20:06 -07001753 read_lock_bh(&table->tb6_lock);
1754
1755 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001756 &cfg->fc_dst, cfg->fc_dst_len,
1757 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001760 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07001761 if ((rt->rt6i_flags & RTF_CACHE) &&
1762 !(cfg->fc_flags & RTF_CACHE))
1763 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001764 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001765 (!rt->dst.dev ||
1766 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001768 if (cfg->fc_flags & RTF_GATEWAY &&
1769 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001771 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001773 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001774 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Thomas Graf86872cb2006-08-22 00:01:08 -07001776 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001779 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 return err;
1782}
1783
David S. Miller6700c272012-07-17 03:29:28 -07001784static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001785{
David S. Millere8599ff2012-07-11 23:43:53 -07001786 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001787 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07001788 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001789 struct ndisc_options ndopts;
1790 struct inet6_dev *in6_dev;
1791 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001792 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07001793 int optlen, on_link;
1794 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07001795
Simon Horman29a3cad2013-05-28 20:34:26 +00001796 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001797 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07001798
1799 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07001800 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001801 return;
1802 }
1803
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001804 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07001805
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001806 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001807 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001808 return;
1809 }
1810
David S. Miller6e157b62012-07-12 00:05:02 -07001811 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001812 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001813 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001814 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07001815 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001816 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001817 return;
1818 }
1819
1820 in6_dev = __in6_dev_get(skb->dev);
1821 if (!in6_dev)
1822 return;
1823 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1824 return;
1825
1826 /* RFC2461 8.1:
1827 * The IP source address of the Redirect MUST be the same as the current
1828 * first-hop router for the specified ICMP Destination Address.
1829 */
1830
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001831 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001832 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1833 return;
1834 }
David S. Miller6e157b62012-07-12 00:05:02 -07001835
1836 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001837 if (ndopts.nd_opts_tgt_lladdr) {
1838 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1839 skb->dev);
1840 if (!lladdr) {
1841 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1842 return;
1843 }
1844 }
1845
David S. Miller6e157b62012-07-12 00:05:02 -07001846 rt = (struct rt6_info *) dst;
1847 if (rt == net->ipv6.ip6_null_entry) {
1848 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1849 return;
1850 }
1851
1852 /* Redirect received -> path was valid.
1853 * Look, redirects are sent only in response to data packets,
1854 * so that this nexthop apparently is reachable. --ANK
1855 */
1856 dst_confirm(&rt->dst);
1857
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001858 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07001859 if (!neigh)
1860 return;
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /*
1863 * We have finally decided to accept it.
1864 */
1865
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001866 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1868 NEIGH_UPDATE_F_OVERRIDE|
1869 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1870 NEIGH_UPDATE_F_ISROUTER))
1871 );
1872
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001873 nrt = ip6_rt_copy(rt, &msg->dest);
David S. Miller38308472011-12-03 18:02:47 -05001874 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto out;
1876
1877 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1878 if (on_link)
1879 nrt->rt6i_flags &= ~RTF_GATEWAY;
1880
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001881 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Thomas Graf40e22e82006-08-22 00:00:45 -07001883 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto out;
1885
Changli Gaod8d1f302010-06-10 23:31:35 -07001886 netevent.old = &rt->dst;
1887 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001888 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00001889 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07001890 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1891
David S. Miller38308472011-12-03 18:02:47 -05001892 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07001893 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001894 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
1897out:
David S. Millere8599ff2012-07-11 23:43:53 -07001898 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07001899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 * Misc support functions
1903 */
1904
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001905static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
1906{
1907 BUG_ON(from->dst.from);
1908
1909 rt->rt6i_flags &= ~RTF_EXPIRES;
1910 dst_hold(&from->dst);
1911 rt->dst.from = &from->dst;
1912 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
1913}
1914
Gao feng1716a962012-04-06 00:13:10 +00001915static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001916 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
David S. Millerd1918542011-12-28 20:19:20 -05001918 struct net *net = dev_net(ort->dst.dev);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001919 struct rt6_info *rt;
1920
1921 if (ort->rt6i_flags & RTF_CACHE)
1922 ort = (struct rt6_info *)ort->dst.from;
1923
1924 rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1925 ort->rt6i_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001928 rt->dst.input = ort->dst.input;
1929 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001930 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001932 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001933 rt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001934 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 rt->rt6i_idev = ort->rt6i_idev;
1936 if (rt->rt6i_idev)
1937 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001938 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Julian Anastasov550bab42013-10-20 15:43:04 +03001940 if (ort->rt6i_flags & RTF_GATEWAY)
1941 rt->rt6i_gateway = ort->rt6i_gateway;
1942 else
1943 rt->rt6i_gateway = *dest;
Gao feng1716a962012-04-06 00:13:10 +00001944 rt->rt6i_flags = ort->rt6i_flags;
Li RongQing24f5b852013-12-19 12:40:26 +08001945 rt6_set_from(rt, ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 rt->rt6i_metric = 0;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948#ifdef CONFIG_IPV6_SUBTREES
1949 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1950#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001951 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001952 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 return rt;
1955}
1956
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001957#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001958static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001959 const struct in6_addr *prefix, int prefixlen,
1960 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001961{
1962 struct fib6_node *fn;
1963 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001964 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001965
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001966 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05001967 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001968 return NULL;
1969
Li RongQing5744dd92012-09-11 21:59:01 +00001970 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001971 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001972 if (!fn)
1973 goto out;
1974
Changli Gaod8d1f302010-06-10 23:31:35 -07001975 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001976 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001977 continue;
1978 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1979 continue;
1980 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1981 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001982 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001983 break;
1984 }
1985out:
Li RongQing5744dd92012-09-11 21:59:01 +00001986 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001987 return rt;
1988}
1989
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001990static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001991 const struct in6_addr *prefix, int prefixlen,
1992 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00001993 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001994{
Thomas Graf86872cb2006-08-22 00:01:08 -07001995 struct fib6_config cfg = {
1996 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001997 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001998 .fc_ifindex = ifindex,
1999 .fc_dst_len = prefixlen,
2000 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2001 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002002 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002003 .fc_nlinfo.nlh = NULL,
2004 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002005 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002006
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002007 cfg.fc_dst = *prefix;
2008 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002009
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002010 /* We should treat it as a default route if prefix length is 0. */
2011 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002012 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002013
Thomas Graf86872cb2006-08-22 00:01:08 -07002014 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002015
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002016 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002017}
2018#endif
2019
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002020struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002023 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002025 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002026 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002027 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Li RongQing5744dd92012-09-11 21:59:01 +00002029 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002030 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002031 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002032 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2034 break;
2035 }
2036 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002037 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002038 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return rt;
2040}
2041
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002042struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002043 struct net_device *dev,
2044 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Thomas Graf86872cb2006-08-22 00:01:08 -07002046 struct fib6_config cfg = {
2047 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002048 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002049 .fc_ifindex = dev->ifindex,
2050 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2051 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002052 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002053 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002054 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002057 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Thomas Graf86872cb2006-08-22 00:01:08 -07002059 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return rt6_get_dflt_router(gwaddr, dev);
2062}
2063
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002064void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002067 struct fib6_table *table;
2068
2069 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002070 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002071 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002072 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002075 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002076 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002077 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2078 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002079 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002080 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002081 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto restart;
2083 }
2084 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002085 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Daniel Lezcano55786892008-03-04 13:47:47 -08002088static void rtmsg_to_fib6_config(struct net *net,
2089 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002090 struct fib6_config *cfg)
2091{
2092 memset(cfg, 0, sizeof(*cfg));
2093
2094 cfg->fc_table = RT6_TABLE_MAIN;
2095 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2096 cfg->fc_metric = rtmsg->rtmsg_metric;
2097 cfg->fc_expires = rtmsg->rtmsg_info;
2098 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2099 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2100 cfg->fc_flags = rtmsg->rtmsg_flags;
2101
Daniel Lezcano55786892008-03-04 13:47:47 -08002102 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002103
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002104 cfg->fc_dst = rtmsg->rtmsg_dst;
2105 cfg->fc_src = rtmsg->rtmsg_src;
2106 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002107}
2108
Daniel Lezcano55786892008-03-04 13:47:47 -08002109int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Thomas Graf86872cb2006-08-22 00:01:08 -07002111 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct in6_rtmsg rtmsg;
2113 int err;
2114
Ian Morris67ba4152014-08-24 21:53:10 +01002115 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 case SIOCADDRT: /* Add a route */
2117 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002118 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -EPERM;
2120 err = copy_from_user(&rtmsg, arg,
2121 sizeof(struct in6_rtmsg));
2122 if (err)
2123 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002124
Daniel Lezcano55786892008-03-04 13:47:47 -08002125 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 rtnl_lock();
2128 switch (cmd) {
2129 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002130 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 break;
2132 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002133 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 break;
2135 default:
2136 err = -EINVAL;
2137 }
2138 rtnl_unlock();
2139
2140 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 return -EINVAL;
2144}
2145
2146/*
2147 * Drop the packet on the floor
2148 */
2149
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002150static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002152 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002153 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002154 switch (ipstats_mib_noroutes) {
2155 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002156 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002157 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002158 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2159 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002160 break;
2161 }
2162 /* FALLTHROUGH */
2163 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002164 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2165 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002166 break;
2167 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002168 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 kfree_skb(skb);
2170 return 0;
2171}
2172
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002173static int ip6_pkt_discard(struct sk_buff *skb)
2174{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002175 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002176}
2177
Eric Dumazetaad88722014-04-15 13:47:15 -04002178static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Eric Dumazetadf30902009-06-02 05:19:30 +00002180 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002181 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002184static int ip6_pkt_prohibit(struct sk_buff *skb)
2185{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002186 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002187}
2188
Eric Dumazetaad88722014-04-15 13:47:15 -04002189static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002190{
Eric Dumazetadf30902009-06-02 05:19:30 +00002191 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002192 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*
2196 * Allocate a dst for local (unicast / anycast) address.
2197 */
2198
2199struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2200 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002201 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002203 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002204 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2205 DST_NOCOUNT, NULL);
2206 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return ERR_PTR(-ENOMEM);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 in6_dev_hold(idev);
2210
David S. Miller11d53b42011-06-24 15:23:34 -07002211 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002212 rt->dst.input = ip6_input;
2213 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002217 if (anycast)
2218 rt->rt6i_flags |= RTF_ANYCAST;
2219 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Julian Anastasov550bab42013-10-20 15:43:04 +03002222 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002223 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002225 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Changli Gaod8d1f302010-06-10 23:31:35 -07002227 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 return rt;
2230}
2231
Daniel Walterc3968a82011-04-13 21:10:57 +00002232int ip6_route_get_saddr(struct net *net,
2233 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002234 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002235 unsigned int prefs,
2236 struct in6_addr *saddr)
2237{
Ian Morris67ba4152014-08-24 21:53:10 +01002238 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
Daniel Walterc3968a82011-04-13 21:10:57 +00002239 int err = 0;
2240 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002241 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002242 else
2243 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2244 daddr, prefs, saddr);
2245 return err;
2246}
2247
2248/* remove deleted ip from prefsrc entries */
2249struct arg_dev_net_ip {
2250 struct net_device *dev;
2251 struct net *net;
2252 struct in6_addr *addr;
2253};
2254
2255static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2256{
2257 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2258 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2259 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2260
David S. Millerd1918542011-12-28 20:19:20 -05002261 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002262 rt != net->ipv6.ip6_null_entry &&
2263 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2264 /* remove prefsrc entry */
2265 rt->rt6i_prefsrc.plen = 0;
2266 }
2267 return 0;
2268}
2269
2270void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2271{
2272 struct net *net = dev_net(ifp->idev->dev);
2273 struct arg_dev_net_ip adni = {
2274 .dev = ifp->idev->dev,
2275 .net = net,
2276 .addr = &ifp->addr,
2277 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002278 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002279}
2280
Duan Jiongbe7a0102014-05-15 15:56:14 +08002281#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2282#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2283
2284/* Remove routers and update dst entries when gateway turn into host. */
2285static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2286{
2287 struct in6_addr *gateway = (struct in6_addr *)arg;
2288
2289 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2290 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2291 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2292 return -1;
2293 }
2294 return 0;
2295}
2296
2297void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2298{
2299 fib6_clean_all(net, fib6_clean_tohost, gateway);
2300}
2301
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002302struct arg_dev_net {
2303 struct net_device *dev;
2304 struct net *net;
2305};
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307static int fib6_ifdown(struct rt6_info *rt, void *arg)
2308{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002309 const struct arg_dev_net *adn = arg;
2310 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002311
David S. Millerd1918542011-12-28 20:19:20 -05002312 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002313 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return 0;
2317}
2318
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002319void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002321 struct arg_dev_net adn = {
2322 .dev = dev,
2323 .net = net,
2324 };
2325
Li RongQing0c3584d2013-12-27 16:32:38 +08002326 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002327 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Eric Dumazet95c96172012-04-15 05:58:06 +00002330struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002332 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333};
2334
2335static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2336{
2337 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2338 struct inet6_dev *idev;
2339
2340 /* In IPv6 pmtu discovery is not optional,
2341 so that RTAX_MTU lock cannot disable it.
2342 We still use this lock to block changes
2343 caused by addrconf/ndisc.
2344 */
2345
2346 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002347 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349
2350 /* For administrative MTU increase, there is no way to discover
2351 IPv6 PMTU increase, so PMTU increase should be updated here.
2352 Since RFC 1981 doesn't include administrative MTU increase
2353 update PMTU increase is a MUST. (i.e. jumbo frame)
2354 */
2355 /*
2356 If new MTU is less than route PMTU, this new MTU will be the
2357 lowest MTU in the path, update the route PMTU to reflect PMTU
2358 decreases; if new MTU is greater than route PMTU, and the
2359 old MTU is the lowest MTU in the path, update the route PMTU
2360 to reflect the increase. In this case if the other nodes' MTU
2361 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2362 PMTU discouvery.
2363 */
David S. Millerd1918542011-12-28 20:19:20 -05002364 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002365 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2366 if (rt->rt6i_flags & RTF_CACHE) {
2367 /* For RTF_CACHE with rt6i_pmtu == 0
2368 * (i.e. a redirected route),
2369 * the metrics of its rt->dst.from has already
2370 * been updated.
2371 */
2372 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2373 rt->rt6i_pmtu = arg->mtu;
2374 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2375 (dst_mtu(&rt->dst) < arg->mtu &&
2376 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2377 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2378 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 0;
2381}
2382
Eric Dumazet95c96172012-04-15 05:58:06 +00002383void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Thomas Grafc71099a2006-08-04 23:20:06 -07002385 struct rt6_mtu_change_arg arg = {
2386 .dev = dev,
2387 .mtu = mtu,
2388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Li RongQing0c3584d2013-12-27 16:32:38 +08002390 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002393static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002394 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002395 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002396 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002397 [RTA_PRIORITY] = { .type = NLA_U32 },
2398 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002399 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002400 [RTA_PREF] = { .type = NLA_U8 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002401};
2402
2403static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2404 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Thomas Graf86872cb2006-08-22 00:01:08 -07002406 struct rtmsg *rtm;
2407 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002408 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Graf86872cb2006-08-22 00:01:08 -07002411 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2412 if (err < 0)
2413 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Thomas Graf86872cb2006-08-22 00:01:08 -07002415 err = -EINVAL;
2416 rtm = nlmsg_data(nlh);
2417 memset(cfg, 0, sizeof(*cfg));
2418
2419 cfg->fc_table = rtm->rtm_table;
2420 cfg->fc_dst_len = rtm->rtm_dst_len;
2421 cfg->fc_src_len = rtm->rtm_src_len;
2422 cfg->fc_flags = RTF_UP;
2423 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002424 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002425
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002426 if (rtm->rtm_type == RTN_UNREACHABLE ||
2427 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002428 rtm->rtm_type == RTN_PROHIBIT ||
2429 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002430 cfg->fc_flags |= RTF_REJECT;
2431
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002432 if (rtm->rtm_type == RTN_LOCAL)
2433 cfg->fc_flags |= RTF_LOCAL;
2434
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002435 if (rtm->rtm_flags & RTM_F_CLONED)
2436 cfg->fc_flags |= RTF_CACHE;
2437
Eric W. Biederman15e47302012-09-07 20:12:54 +00002438 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002439 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002440 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002441
2442 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002443 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002444 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002446
2447 if (tb[RTA_DST]) {
2448 int plen = (rtm->rtm_dst_len + 7) >> 3;
2449
2450 if (nla_len(tb[RTA_DST]) < plen)
2451 goto errout;
2452
2453 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002455
2456 if (tb[RTA_SRC]) {
2457 int plen = (rtm->rtm_src_len + 7) >> 3;
2458
2459 if (nla_len(tb[RTA_SRC]) < plen)
2460 goto errout;
2461
2462 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002464
Daniel Walterc3968a82011-04-13 21:10:57 +00002465 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002466 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002467
Thomas Graf86872cb2006-08-22 00:01:08 -07002468 if (tb[RTA_OIF])
2469 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2470
2471 if (tb[RTA_PRIORITY])
2472 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2473
2474 if (tb[RTA_METRICS]) {
2475 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2476 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002478
2479 if (tb[RTA_TABLE])
2480 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2481
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002482 if (tb[RTA_MULTIPATH]) {
2483 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2484 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2485 }
2486
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002487 if (tb[RTA_PREF]) {
2488 pref = nla_get_u8(tb[RTA_PREF]);
2489 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2490 pref != ICMPV6_ROUTER_PREF_HIGH)
2491 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2492 cfg->fc_flags |= RTF_PREF(pref);
2493 }
2494
Thomas Graf86872cb2006-08-22 00:01:08 -07002495 err = 0;
2496errout:
2497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002500static int ip6_route_multipath(struct fib6_config *cfg, int add)
2501{
2502 struct fib6_config r_cfg;
2503 struct rtnexthop *rtnh;
2504 int remaining;
2505 int attrlen;
2506 int err = 0, last_err = 0;
2507
2508beginning:
2509 rtnh = (struct rtnexthop *)cfg->fc_mp;
2510 remaining = cfg->fc_mp_len;
2511
2512 /* Parse a Multipath Entry */
2513 while (rtnh_ok(rtnh, remaining)) {
2514 memcpy(&r_cfg, cfg, sizeof(*cfg));
2515 if (rtnh->rtnh_ifindex)
2516 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2517
2518 attrlen = rtnh_attrlen(rtnh);
2519 if (attrlen > 0) {
2520 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2521
2522 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2523 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002524 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002525 r_cfg.fc_flags |= RTF_GATEWAY;
2526 }
2527 }
2528 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2529 if (err) {
2530 last_err = err;
2531 /* If we are trying to remove a route, do not stop the
2532 * loop when ip6_route_del() fails (because next hop is
2533 * already gone), we should try to remove all next hops.
2534 */
2535 if (add) {
2536 /* If add fails, we should try to delete all
2537 * next hops that have been already added.
2538 */
2539 add = 0;
2540 goto beginning;
2541 }
2542 }
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002543 /* Because each route is added like a single route we remove
2544 * this flag after the first nexthop (if there is a collision,
2545 * we have already fail to add the first nexthop:
2546 * fib6_add_rt2node() has reject it).
2547 */
2548 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002549 rtnh = rtnh_next(rtnh, &remaining);
2550 }
2551
2552 return last_err;
2553}
2554
Ian Morris67ba4152014-08-24 21:53:10 +01002555static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Thomas Graf86872cb2006-08-22 00:01:08 -07002557 struct fib6_config cfg;
2558 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Thomas Graf86872cb2006-08-22 00:01:08 -07002560 err = rtm_to_fib6_config(skb, nlh, &cfg);
2561 if (err < 0)
2562 return err;
2563
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002564 if (cfg.fc_mp)
2565 return ip6_route_multipath(&cfg, 0);
2566 else
2567 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Ian Morris67ba4152014-08-24 21:53:10 +01002570static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Thomas Graf86872cb2006-08-22 00:01:08 -07002572 struct fib6_config cfg;
2573 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Thomas Graf86872cb2006-08-22 00:01:08 -07002575 err = rtm_to_fib6_config(skb, nlh, &cfg);
2576 if (err < 0)
2577 return err;
2578
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002579 if (cfg.fc_mp)
2580 return ip6_route_multipath(&cfg, 1);
2581 else
2582 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Thomas Graf339bf982006-11-10 14:10:15 -08002585static inline size_t rt6_nlmsg_size(void)
2586{
2587 return NLMSG_ALIGN(sizeof(struct rtmsg))
2588 + nla_total_size(16) /* RTA_SRC */
2589 + nla_total_size(16) /* RTA_DST */
2590 + nla_total_size(16) /* RTA_GATEWAY */
2591 + nla_total_size(16) /* RTA_PREFSRC */
2592 + nla_total_size(4) /* RTA_TABLE */
2593 + nla_total_size(4) /* RTA_IIF */
2594 + nla_total_size(4) /* RTA_OIF */
2595 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002596 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01002597 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002598 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2599 + nla_total_size(1); /* RTA_PREF */
Thomas Graf339bf982006-11-10 14:10:15 -08002600}
2601
Brian Haley191cd582008-08-14 15:33:21 -07002602static int rt6_fill_node(struct net *net,
2603 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002604 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002605 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002606 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002608 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002610 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002611 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002612 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 if (prefix) { /* user wants prefix routes only */
2615 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2616 /* success since this is not a prefix route */
2617 return 1;
2618 }
2619 }
2620
Eric W. Biederman15e47302012-09-07 20:12:54 +00002621 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002622 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002623 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002624
2625 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 rtm->rtm_family = AF_INET6;
2627 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2628 rtm->rtm_src_len = rt->rt6i_src.plen;
2629 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002630 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002631 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002632 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002633 table = RT6_TABLE_UNSPEC;
2634 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04002635 if (nla_put_u32(skb, RTA_TABLE, table))
2636 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002637 if (rt->rt6i_flags & RTF_REJECT) {
2638 switch (rt->dst.error) {
2639 case -EINVAL:
2640 rtm->rtm_type = RTN_BLACKHOLE;
2641 break;
2642 case -EACCES:
2643 rtm->rtm_type = RTN_PROHIBIT;
2644 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002645 case -EAGAIN:
2646 rtm->rtm_type = RTN_THROW;
2647 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002648 default:
2649 rtm->rtm_type = RTN_UNREACHABLE;
2650 break;
2651 }
2652 }
David S. Miller38308472011-12-03 18:02:47 -05002653 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002654 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002655 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 rtm->rtm_type = RTN_LOCAL;
2657 else
2658 rtm->rtm_type = RTN_UNICAST;
2659 rtm->rtm_flags = 0;
2660 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2661 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002662 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00002664 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2665 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2666 rtm->rtm_protocol = RTPROT_RA;
2667 else
2668 rtm->rtm_protocol = RTPROT_KERNEL;
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
David S. Miller38308472011-12-03 18:02:47 -05002671 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 rtm->rtm_flags |= RTM_F_CLONED;
2673
2674 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02002675 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04002676 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002677 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02002679 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002680 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681#ifdef CONFIG_IPV6_SUBTREES
2682 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02002683 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04002684 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002685 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04002686 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02002687 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002688 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002690 if (iif) {
2691#ifdef CONFIG_IPV6_MROUTE
2692 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002693 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002694 if (err <= 0) {
2695 if (!nowait) {
2696 if (err == 0)
2697 return 0;
2698 goto nla_put_failure;
2699 } else {
2700 if (err == -EMSGSIZE)
2701 goto nla_put_failure;
2702 }
2703 }
2704 } else
2705#endif
David S. Millerc78679e2012-04-01 20:27:33 -04002706 if (nla_put_u32(skb, RTA_IIF, iif))
2707 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002708 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04002710 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02002711 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002712 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002714
Daniel Walterc3968a82011-04-13 21:10:57 +00002715 if (rt->rt6i_prefsrc.plen) {
2716 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002717 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02002718 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002719 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00002720 }
2721
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002722 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
2723 if (rt->rt6i_pmtu)
2724 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
2725 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002726 goto nla_put_failure;
2727
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00002728 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02002729 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00002730 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002731 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002732
David S. Millerc78679e2012-04-01 20:27:33 -04002733 if (rt->dst.dev &&
2734 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2735 goto nla_put_failure;
2736 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2737 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00002738
2739 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002740
David S. Miller87a50692012-07-10 05:06:14 -07002741 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002742 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002744 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2745 goto nla_put_failure;
2746
Johannes Berg053c0952015-01-16 22:09:00 +01002747 nlmsg_end(skb, nlh);
2748 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002749
2750nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002751 nlmsg_cancel(skb, nlh);
2752 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Patrick McHardy1b43af52006-08-10 23:11:17 -07002755int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2758 int prefix;
2759
Thomas Graf2d7202b2006-08-22 00:01:27 -07002760 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2761 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2763 } else
2764 prefix = 0;
2765
Brian Haley191cd582008-08-14 15:33:21 -07002766 return rt6_fill_node(arg->net,
2767 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002768 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002769 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Ian Morris67ba4152014-08-24 21:53:10 +01002772static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002774 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002775 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002777 struct sk_buff *skb;
2778 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002779 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002780 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07002781
2782 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2783 if (err < 0)
2784 goto errout;
2785
2786 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002787 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002788
2789 if (tb[RTA_SRC]) {
2790 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2791 goto errout;
2792
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002793 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002794 }
2795
2796 if (tb[RTA_DST]) {
2797 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2798 goto errout;
2799
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002800 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002801 }
2802
2803 if (tb[RTA_IIF])
2804 iif = nla_get_u32(tb[RTA_IIF]);
2805
2806 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002807 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002808
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07002809 if (tb[RTA_MARK])
2810 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
2811
Thomas Grafab364a62006-08-22 00:01:47 -07002812 if (iif) {
2813 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002814 int flags = 0;
2815
Daniel Lezcano55786892008-03-04 13:47:47 -08002816 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002817 if (!dev) {
2818 err = -ENODEV;
2819 goto errout;
2820 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002821
2822 fl6.flowi6_iif = iif;
2823
2824 if (!ipv6_addr_any(&fl6.saddr))
2825 flags |= RT6_LOOKUP_F_HAS_SADDR;
2826
2827 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2828 flags);
2829 } else {
2830 fl6.flowi6_oif = oif;
2831
2832 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07002833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002836 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00002837 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07002838 err = -ENOBUFS;
2839 goto errout;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 /* Reserve room for dummy headers, this skb can pass
2843 through good chunk of routing engine.
2844 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002845 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2847
Changli Gaod8d1f302010-06-10 23:31:35 -07002848 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
David S. Miller4c9483b2011-03-12 16:22:43 -05002850 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002851 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002852 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002854 kfree_skb(skb);
2855 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857
Eric W. Biederman15e47302012-09-07 20:12:54 +00002858 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07002859errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
Thomas Graf86872cb2006-08-22 00:01:08 -07002863void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002866 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002867 u32 seq;
2868 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002870 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002871 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002872
Thomas Graf339bf982006-11-10 14:10:15 -08002873 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002874 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002875 goto errout;
2876
Brian Haley191cd582008-08-14 15:33:21 -07002877 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002878 event, info->portid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002879 if (err < 0) {
2880 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2881 WARN_ON(err == -EMSGSIZE);
2882 kfree_skb(skb);
2883 goto errout;
2884 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002885 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002886 info->nlh, gfp_any());
2887 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002888errout:
2889 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002890 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002893static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00002894 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002895{
Jiri Pirko351638e2013-05-28 01:30:21 +00002896 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002897 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002898
2899 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002900 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002901 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2902#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002903 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002904 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002905 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002906 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2907#endif
2908 }
2909
2910 return NOTIFY_OK;
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913/*
2914 * /proc
2915 */
2916
2917#ifdef CONFIG_PROC_FS
2918
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002919static const struct file_operations ipv6_route_proc_fops = {
2920 .owner = THIS_MODULE,
2921 .open = ipv6_route_open,
2922 .read = seq_read,
2923 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002924 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002925};
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2928{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002929 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002931 net->ipv6.rt6_stats->fib_nodes,
2932 net->ipv6.rt6_stats->fib_route_nodes,
2933 net->ipv6.rt6_stats->fib_rt_alloc,
2934 net->ipv6.rt6_stats->fib_rt_entries,
2935 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002936 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002937 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 return 0;
2940}
2941
2942static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2943{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002944 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002945}
2946
Arjan van de Ven9a321442007-02-12 00:55:35 -08002947static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 .owner = THIS_MODULE,
2949 .open = rt6_stats_seq_open,
2950 .read = seq_read,
2951 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002952 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953};
2954#endif /* CONFIG_PROC_FS */
2955
2956#ifdef CONFIG_SYSCTL
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958static
Joe Perchesfe2c6332013-06-11 23:04:25 -07002959int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 void __user *buffer, size_t *lenp, loff_t *ppos)
2961{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002962 struct net *net;
2963 int delay;
2964 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002966
2967 net = (struct net *)ctl->extra1;
2968 delay = net->ipv6.sysctl.flush_delay;
2969 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002970 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Joe Perchesfe2c6332013-06-11 23:04:25 -07002974struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002975 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002977 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002979 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002980 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 },
2982 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002984 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .maxlen = sizeof(int),
2986 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002987 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 },
2989 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002991 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .maxlen = sizeof(int),
2993 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002994 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 },
2996 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002998 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .maxlen = sizeof(int),
3000 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003001 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 },
3003 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003005 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .maxlen = sizeof(int),
3007 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003008 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 },
3010 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003012 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 .maxlen = sizeof(int),
3014 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003015 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 },
3017 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003019 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .maxlen = sizeof(int),
3021 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003022 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 },
3024 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003026 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 .maxlen = sizeof(int),
3028 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003029 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 },
3031 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003033 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .maxlen = sizeof(int),
3035 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003036 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 },
3038 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003040 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .maxlen = sizeof(int),
3042 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003043 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003045 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046};
3047
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003048struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003049{
3050 struct ctl_table *table;
3051
3052 table = kmemdup(ipv6_route_table_template,
3053 sizeof(ipv6_route_table_template),
3054 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003055
3056 if (table) {
3057 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003058 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003059 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003060 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3061 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3062 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3063 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3064 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3065 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3066 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003067 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003068
3069 /* Don't export sysctls to unprivileged users */
3070 if (net->user_ns != &init_user_ns)
3071 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003072 }
3073
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003074 return table;
3075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076#endif
3077
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003078static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003079{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003080 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003081
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003082 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3083 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003084
Eric Dumazetfc66f952010-10-08 06:37:34 +00003085 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3086 goto out_ip6_dst_ops;
3087
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003088 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3089 sizeof(*net->ipv6.ip6_null_entry),
3090 GFP_KERNEL);
3091 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003092 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003093 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003094 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003095 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003096 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3097 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003098
3099#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3100 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3101 sizeof(*net->ipv6.ip6_prohibit_entry),
3102 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003103 if (!net->ipv6.ip6_prohibit_entry)
3104 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003105 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003106 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003107 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003108 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3109 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003110
3111 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3112 sizeof(*net->ipv6.ip6_blk_hole_entry),
3113 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003114 if (!net->ipv6.ip6_blk_hole_entry)
3115 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003116 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003117 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003118 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003119 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3120 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003121#endif
3122
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003123 net->ipv6.sysctl.flush_delay = 0;
3124 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3125 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3126 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3127 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3128 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3129 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3130 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3131
Benjamin Thery6891a342008-03-04 13:49:47 -08003132 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3133
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003134 ret = 0;
3135out:
3136 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003137
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003138#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3139out_ip6_prohibit_entry:
3140 kfree(net->ipv6.ip6_prohibit_entry);
3141out_ip6_null_entry:
3142 kfree(net->ipv6.ip6_null_entry);
3143#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003144out_ip6_dst_entries:
3145 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003146out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003147 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003148}
3149
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003150static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003151{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003152 kfree(net->ipv6.ip6_null_entry);
3153#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3154 kfree(net->ipv6.ip6_prohibit_entry);
3155 kfree(net->ipv6.ip6_blk_hole_entry);
3156#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003157 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003158}
3159
Thomas Grafd1896342012-06-18 12:08:33 +00003160static int __net_init ip6_route_net_init_late(struct net *net)
3161{
3162#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003163 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3164 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003165#endif
3166 return 0;
3167}
3168
3169static void __net_exit ip6_route_net_exit_late(struct net *net)
3170{
3171#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003172 remove_proc_entry("ipv6_route", net->proc_net);
3173 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003174#endif
3175}
3176
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003177static struct pernet_operations ip6_route_net_ops = {
3178 .init = ip6_route_net_init,
3179 .exit = ip6_route_net_exit,
3180};
3181
David S. Millerc3426b42012-06-09 16:27:05 -07003182static int __net_init ipv6_inetpeer_init(struct net *net)
3183{
3184 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3185
3186 if (!bp)
3187 return -ENOMEM;
3188 inet_peer_base_init(bp);
3189 net->ipv6.peers = bp;
3190 return 0;
3191}
3192
3193static void __net_exit ipv6_inetpeer_exit(struct net *net)
3194{
3195 struct inet_peer_base *bp = net->ipv6.peers;
3196
3197 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003198 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003199 kfree(bp);
3200}
3201
David S. Miller2b823f72012-06-09 19:00:16 -07003202static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003203 .init = ipv6_inetpeer_init,
3204 .exit = ipv6_inetpeer_exit,
3205};
3206
Thomas Grafd1896342012-06-18 12:08:33 +00003207static struct pernet_operations ip6_route_net_late_ops = {
3208 .init = ip6_route_net_init_late,
3209 .exit = ip6_route_net_exit_late,
3210};
3211
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003212static struct notifier_block ip6_route_dev_notifier = {
3213 .notifier_call = ip6_route_dev_notify,
3214 .priority = 0,
3215};
3216
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003217int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003219 int ret;
3220
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003221 ret = -ENOMEM;
3222 ip6_dst_ops_template.kmem_cachep =
3223 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3224 SLAB_HWCACHE_ALIGN, NULL);
3225 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003226 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003227
Eric Dumazetfc66f952010-10-08 06:37:34 +00003228 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003229 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003230 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003231
David S. Millerc3426b42012-06-09 16:27:05 -07003232 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3233 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003234 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003235
David S. Miller7e52b332012-06-15 15:51:55 -07003236 ret = register_pernet_subsys(&ip6_route_net_ops);
3237 if (ret)
3238 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003239
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003240 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3241
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003242 /* Registering of the loopback is done before this portion of code,
3243 * the loopback reference in rt6_info will not be taken, do it
3244 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003245 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003246 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3247 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003248 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003249 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003250 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003251 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3252 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003253 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003254 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003255 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003256
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003257 ret = xfrm6_init();
3258 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003259 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003260
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003261 ret = fib6_rules_init();
3262 if (ret)
3263 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003264
Thomas Grafd1896342012-06-18 12:08:33 +00003265 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3266 if (ret)
3267 goto fib6_rules_init;
3268
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003269 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003270 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3271 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3272 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003273 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003274
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003275 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003276 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003277 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003278
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003279out:
3280 return ret;
3281
Thomas Grafd1896342012-06-18 12:08:33 +00003282out_register_late_subsys:
3283 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003284fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003285 fib6_rules_cleanup();
3286xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003287 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003288out_fib6_init:
3289 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003290out_register_subsys:
3291 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003292out_register_inetpeer:
3293 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003294out_dst_entries:
3295 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003296out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003297 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
3301void ip6_route_cleanup(void)
3302{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003303 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003304 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003305 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003308 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003309 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003310 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003311 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}