blob: 4d6eb5d90cb4bde2c8c9ce570c69281e3187ca43 [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
stephen hemmingere8243532013-12-29 14:03:31 -0800108static void rt6_bind_peer(struct rt6_info *rt, int create)
109{
110 struct inet_peer_base *base;
111 struct inet_peer *peer;
112
113 base = inetpeer_base_ptr(rt->_rt6i_peer);
114 if (!base)
115 return;
116
117 peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
118 if (peer) {
119 if (!rt6_set_peer(rt, peer))
120 inet_putpeer(peer);
121 }
122}
123
124static struct inet_peer *__rt6_get_peer(struct rt6_info *rt, int create)
125{
126 if (rt6_has_peer(rt))
127 return rt6_peer_ptr(rt);
128
129 rt6_bind_peer(rt, create);
130 return (rt6_has_peer(rt) ? rt6_peer_ptr(rt) : NULL);
131}
132
133static struct inet_peer *rt6_get_peer_create(struct rt6_info *rt)
134{
135 return __rt6_get_peer(rt, 1);
136}
137
David S. Miller06582542011-01-27 14:58:42 -0800138static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
139{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700140 struct rt6_info *rt = (struct rt6_info *)dst;
David S. Miller06582542011-01-27 14:58:42 -0800141
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700142 if (rt->rt6i_flags & RTF_CACHE)
143 return NULL;
144 else
Martin KaFai Lau3b471172015-02-12 16:14:08 -0800145 return dst_cow_metrics_generic(dst, old);
David S. Miller06582542011-01-27 14:58:42 -0800146}
147
David S. Millerf894cbf2012-07-02 21:52:24 -0700148static inline const void *choose_neigh_daddr(struct rt6_info *rt,
149 struct sk_buff *skb,
150 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500151{
152 struct in6_addr *p = &rt->rt6i_gateway;
153
David S. Millera7563f32012-01-26 16:29:16 -0500154 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500155 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700156 else if (skb)
157 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500158 return daddr;
159}
160
David S. Millerf894cbf2012-07-02 21:52:24 -0700161static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
162 struct sk_buff *skb,
163 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700164{
David S. Miller39232972012-01-26 15:22:32 -0500165 struct rt6_info *rt = (struct rt6_info *) dst;
166 struct neighbour *n;
167
David S. Millerf894cbf2012-07-02 21:52:24 -0700168 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000169 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500170 if (n)
171 return n;
172 return neigh_create(&nd_tbl, daddr, dst->dev);
173}
174
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800175static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .gc = ip6_dst_gc,
178 .gc_thresh = 1024,
179 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800180 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000181 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800182 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .destroy = ip6_dst_destroy,
184 .ifdown = ip6_dst_ifdown,
185 .negative_advice = ip6_negative_advice,
186 .link_failure = ip6_link_failure,
187 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700188 .redirect = rt6_do_redirect,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700189 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700190 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Steffen Klassertebb762f2011-11-23 02:12:51 +0000193static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800194{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000195 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
196
197 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800198}
199
David S. Miller6700c272012-07-17 03:29:28 -0700200static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
201 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700202{
203}
204
David S. Miller6700c272012-07-17 03:29:28 -0700205static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
206 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700207{
208}
209
Held Bernhard0972ddb2011-04-24 22:07:32 +0000210static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
211 unsigned long old)
212{
213 return NULL;
214}
215
David S. Miller14e50e52007-05-24 18:17:54 -0700216static struct dst_ops ip6_dst_blackhole_ops = {
217 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700218 .destroy = ip6_dst_destroy,
219 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000220 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800221 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700222 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700223 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000224 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700225 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700226};
227
David S. Miller62fa8a82011-01-26 20:51:05 -0800228static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800229 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800230};
231
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000232static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700233 .dst = {
234 .__refcnt = ATOMIC_INIT(1),
235 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000236 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700237 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700238 .input = ip6_pkt_discard,
239 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 },
241 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700242 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .rt6i_metric = ~(u32) 0,
244 .rt6i_ref = ATOMIC_INIT(1),
245};
246
Thomas Graf101367c2006-08-04 03:39:02 -0700247#ifdef CONFIG_IPV6_MULTIPLE_TABLES
248
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000249static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700250 .dst = {
251 .__refcnt = ATOMIC_INIT(1),
252 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000253 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700254 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700255 .input = ip6_pkt_prohibit,
256 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700257 },
258 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700259 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700260 .rt6i_metric = ~(u32) 0,
261 .rt6i_ref = ATOMIC_INIT(1),
262};
263
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000264static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700265 .dst = {
266 .__refcnt = ATOMIC_INIT(1),
267 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000268 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700269 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700270 .input = dst_discard,
Eric Dumazetaad88722014-04-15 13:47:15 -0400271 .output = dst_discard_sk,
Thomas Graf101367c2006-08-04 03:39:02 -0700272 },
273 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700274 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700275 .rt6i_metric = ~(u32) 0,
276 .rt6i_ref = ATOMIC_INIT(1),
277};
278
279#endif
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/* allocate dst with ip6_dst_ops */
David S. Miller97bab732012-06-09 22:36:36 -0700282static inline struct rt6_info *ip6_dst_alloc(struct net *net,
David S. Miller957c6652011-06-24 15:25:00 -0700283 struct net_device *dev,
David S. Miller8b96d222012-06-11 02:01:56 -0700284 int flags,
285 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
David S. Miller97bab732012-06-09 22:36:36 -0700287 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000288 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700289
David S. Miller97bab732012-06-09 22:36:36 -0700290 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000291 struct dst_entry *dst = &rt->dst;
292
293 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
David S. Miller8b96d222012-06-11 02:01:56 -0700294 rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000295 INIT_LIST_HEAD(&rt->rt6i_siblings);
David S. Miller97bab732012-06-09 22:36:36 -0700296 }
David S. Millercf911662011-04-28 14:31:47 -0700297 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static void ip6_dst_destroy(struct dst_entry *dst)
301{
302 struct rt6_info *rt = (struct rt6_info *)dst;
303 struct inet6_dev *idev = rt->rt6i_idev;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000304 struct dst_entry *from = dst->from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700306 dst_destroy_metrics_generic(dst);
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000307
David S. Miller38308472011-12-03 18:02:47 -0500308 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 rt->rt6i_idev = NULL;
310 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900311 }
Gao feng1716a962012-04-06 00:13:10 +0000312
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000313 dst->from = NULL;
314 dst_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
318 int how)
319{
320 struct rt6_info *rt = (struct rt6_info *)dst;
321 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800322 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900323 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David S. Miller97cac082012-07-02 22:43:47 -0700325 if (dev != loopback_dev) {
326 if (idev && idev->dev == dev) {
327 struct inet6_dev *loopback_idev =
328 in6_dev_get(loopback_dev);
329 if (loopback_idev) {
330 rt->rt6i_idev = loopback_idev;
331 in6_dev_put(idev);
332 }
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335}
336
Eric Dumazeta50feda2012-05-18 18:57:34 +0000337static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Gao feng1716a962012-04-06 00:13:10 +0000339 if (rt->rt6i_flags & RTF_EXPIRES) {
340 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000341 return true;
Gao feng1716a962012-04-06 00:13:10 +0000342 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000343 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000344 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000345 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000348/* Multipath route selection:
349 * Hash based function using packet header and flowlabel.
350 * Adapted from fib_info_hashfn()
351 */
352static int rt6_info_hash_nhsfn(unsigned int candidate_count,
353 const struct flowi6 *fl6)
354{
355 unsigned int val = fl6->flowi6_proto;
356
YOSHIFUJI Hideaki / 吉藤英明c08977b2013-01-13 05:02:29 +0000357 val ^= ipv6_addr_hash(&fl6->daddr);
358 val ^= ipv6_addr_hash(&fl6->saddr);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000359
360 /* Work only if this not encapsulated */
361 switch (fl6->flowi6_proto) {
362 case IPPROTO_UDP:
363 case IPPROTO_TCP:
364 case IPPROTO_SCTP:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000365 val ^= (__force u16)fl6->fl6_sport;
366 val ^= (__force u16)fl6->fl6_dport;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000367 break;
368
369 case IPPROTO_ICMPV6:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000370 val ^= (__force u16)fl6->fl6_icmp_type;
371 val ^= (__force u16)fl6->fl6_icmp_code;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000372 break;
373 }
374 /* RFC6438 recommands to use flowlabel */
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000375 val ^= (__force u32)fl6->flowlabel;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000376
377 /* Perhaps, we need to tune, this function? */
378 val = val ^ (val >> 7) ^ (val >> 12);
379 return val % candidate_count;
380}
381
382static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200383 struct flowi6 *fl6, int oif,
384 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000385{
386 struct rt6_info *sibling, *next_sibling;
387 int route_choosen;
388
389 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
390 /* Don't change the route, if route_choosen == 0
391 * (siblings does not include ourself)
392 */
393 if (route_choosen)
394 list_for_each_entry_safe(sibling, next_sibling,
395 &match->rt6i_siblings, rt6i_siblings) {
396 route_choosen--;
397 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200398 if (rt6_score_route(sibling, oif, strict) < 0)
399 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000400 match = sibling;
401 break;
402 }
403 }
404 return match;
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700408 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800411static inline struct rt6_info *rt6_device_match(struct net *net,
412 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000413 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700415 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct rt6_info *local = NULL;
418 struct rt6_info *sprt;
419
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900420 if (!oif && ipv6_addr_any(saddr))
421 goto out;
422
Changli Gaod8d1f302010-06-10 23:31:35 -0700423 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500424 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900425
426 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (dev->ifindex == oif)
428 return sprt;
429 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500430 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700432 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900434 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 local->rt6i_idev->dev->ifindex == oif))
436 continue;
437 }
438 local = sprt;
439 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900440 } else {
441 if (ipv6_chk_addr(net, saddr, dev,
442 flags & RT6_LOOKUP_F_IFACE))
443 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900447 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (local)
449 return local;
450
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700451 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800452 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900454out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return rt;
456}
457
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800458#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200459struct __rt6_probe_work {
460 struct work_struct work;
461 struct in6_addr target;
462 struct net_device *dev;
463};
464
465static void rt6_probe_deferred(struct work_struct *w)
466{
467 struct in6_addr mcaddr;
468 struct __rt6_probe_work *work =
469 container_of(w, struct __rt6_probe_work, work);
470
471 addrconf_addr_solict_mult(&work->target, &mcaddr);
472 ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
473 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100474 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200475}
476
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800477static void rt6_probe(struct rt6_info *rt)
478{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000479 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800480 /*
481 * Okay, this does not seem to be appropriate
482 * for now, however, we need to check if it
483 * is really so; aka Router Reachability Probing.
484 *
485 * Router Reachability Probe MUST be rate-limited
486 * to no more than one per minute.
487 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000488 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000489 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000490 rcu_read_lock_bh();
491 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
492 if (neigh) {
493 write_lock(&neigh->lock);
494 if (neigh->nud_state & NUD_VALID)
495 goto out;
YOSHIFUJI Hideaki / 吉藤英明7ff74a52013-01-17 12:53:02 +0000496 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000497
498 if (!neigh ||
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800499 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200500 struct __rt6_probe_work *work;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800501
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200502 work = kmalloc(sizeof(*work), GFP_ATOMIC);
503
504 if (neigh && work)
Jiri Benc7e980562013-12-11 13:48:20 +0100505 __neigh_set_probe_once(neigh);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000506
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200507 if (neigh)
508 write_unlock(&neigh->lock);
509
510 if (work) {
511 INIT_WORK(&work->work, rt6_probe_deferred);
512 work->target = rt->rt6i_gateway;
513 dev_hold(rt->dst.dev);
514 work->dev = rt->dst.dev;
515 schedule_work(&work->work);
516 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000517 } else {
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000518out:
519 write_unlock(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000520 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000521 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800522}
523#else
524static inline void rt6_probe(struct rt6_info *rt)
525{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800526}
527#endif
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800530 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700532static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
David S. Millerd1918542011-12-28 20:19:20 -0500534 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700535 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800536 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700537 if ((dev->flags & IFF_LOOPBACK) &&
538 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
539 return 1;
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200543static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000545 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200546 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000547
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700548 if (rt->rt6i_flags & RTF_NONEXTHOP ||
549 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200550 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000551
552 rcu_read_lock_bh();
553 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
554 if (neigh) {
555 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800556 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200557 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800558#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000559 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200560 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100561 else
562 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800563#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000564 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200565 } else {
566 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100567 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000568 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000569 rcu_read_unlock_bh();
570
Paul Marksa5a81f02012-12-03 10:26:54 +0000571 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800572}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800574static int rt6_score_route(struct rt6_info *rt, int oif,
575 int strict)
576{
Paul Marksa5a81f02012-12-03 10:26:54 +0000577 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900578
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700579 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700580 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200581 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800582#ifdef CONFIG_IPV6_ROUTER_PREF
583 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
584#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200585 if (strict & RT6_LOOKUP_F_REACHABLE) {
586 int n = rt6_check_neigh(rt);
587 if (n < 0)
588 return n;
589 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800590 return m;
591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
David S. Millerf11e6652007-03-24 20:36:25 -0700593static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200594 int *mpri, struct rt6_info *match,
595 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800596{
David S. Millerf11e6652007-03-24 20:36:25 -0700597 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200598 bool match_do_rr = false;
David S. Millerf11e6652007-03-24 20:36:25 -0700599
600 if (rt6_check_expired(rt))
601 goto out;
602
603 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100604 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200605 match_do_rr = true;
606 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100607 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700608 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700609 }
610
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200611 if (strict & RT6_LOOKUP_F_REACHABLE)
612 rt6_probe(rt);
613
Jiri Benc7e980562013-12-11 13:48:20 +0100614 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200615 if (m > *mpri) {
616 *do_rr = match_do_rr;
617 *mpri = m;
618 match = rt;
619 }
David S. Millerf11e6652007-03-24 20:36:25 -0700620out:
621 return match;
622}
623
624static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
625 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200626 u32 metric, int oif, int strict,
627 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700628{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700629 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800630 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David S. Millerf11e6652007-03-24 20:36:25 -0700632 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700633 cont = NULL;
634 for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
635 if (rt->rt6i_metric != metric) {
636 cont = rt;
637 break;
638 }
639
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200640 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700641 }
642
643 for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
644 if (rt->rt6i_metric != metric) {
645 cont = rt;
646 break;
647 }
648
649 match = find_match(rt, oif, strict, &mpri, match, do_rr);
650 }
651
652 if (match || !cont)
653 return match;
654
655 for (rt = cont; rt; rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200656 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800657
David S. Millerf11e6652007-03-24 20:36:25 -0700658 return match;
659}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800660
David S. Millerf11e6652007-03-24 20:36:25 -0700661static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
662{
663 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800664 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200665 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
David S. Millerf11e6652007-03-24 20:36:25 -0700667 rt0 = fn->rr_ptr;
668 if (!rt0)
669 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200671 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
672 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200674 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700675 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700676
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800677 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700678 if (!next || next->rt6i_metric != rt0->rt6i_metric)
679 next = fn->leaf;
680
681 if (next != rt0)
682 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
David S. Millerd1918542011-12-28 20:19:20 -0500685 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000686 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800689#ifdef CONFIG_IPV6_ROUTE_INFO
690int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000691 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800692{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900693 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800694 struct route_info *rinfo = (struct route_info *) opt;
695 struct in6_addr prefix_buf, *prefix;
696 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900697 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800698 struct rt6_info *rt;
699
700 if (len < sizeof(struct route_info)) {
701 return -EINVAL;
702 }
703
704 /* Sanity check for prefix_len and length */
705 if (rinfo->length > 3) {
706 return -EINVAL;
707 } else if (rinfo->prefix_len > 128) {
708 return -EINVAL;
709 } else if (rinfo->prefix_len > 64) {
710 if (rinfo->length < 2) {
711 return -EINVAL;
712 }
713 } else if (rinfo->prefix_len > 0) {
714 if (rinfo->length < 1) {
715 return -EINVAL;
716 }
717 }
718
719 pref = rinfo->route_pref;
720 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000721 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800722
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900723 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800724
725 if (rinfo->length == 3)
726 prefix = (struct in6_addr *)rinfo->prefix;
727 else {
728 /* this function is safe */
729 ipv6_addr_prefix(&prefix_buf,
730 (struct in6_addr *)rinfo->prefix,
731 rinfo->prefix_len);
732 prefix = &prefix_buf;
733 }
734
Duan Jiongf104a562013-11-08 09:56:53 +0800735 if (rinfo->prefix_len == 0)
736 rt = rt6_get_dflt_router(gwaddr, dev);
737 else
738 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
739 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800740
741 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700742 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800743 rt = NULL;
744 }
745
746 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800747 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800748 pref);
749 else if (rt)
750 rt->rt6i_flags = RTF_ROUTEINFO |
751 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
752
753 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000754 if (!addrconf_finite_timeout(lifetime))
755 rt6_clean_expires(rt);
756 else
757 rt6_set_expires(rt, jiffies + HZ * lifetime);
758
Amerigo Wang94e187c2012-10-29 00:13:19 +0000759 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800760 }
761 return 0;
762}
763#endif
764
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700765static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
766 struct in6_addr *saddr)
767{
768 struct fib6_node *pn;
769 while (1) {
770 if (fn->fn_flags & RTN_TL_ROOT)
771 return NULL;
772 pn = fn->parent;
773 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
774 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
775 else
776 fn = pn;
777 if (fn->fn_flags & RTN_RTINFO)
778 return fn;
779 }
780}
Thomas Grafc71099a2006-08-04 23:20:06 -0700781
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800782static struct rt6_info *ip6_pol_route_lookup(struct net *net,
783 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500784 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct fib6_node *fn;
787 struct rt6_info *rt;
788
Thomas Grafc71099a2006-08-04 23:20:06 -0700789 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500790 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700791restart:
792 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500793 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000794 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200795 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700796 if (rt == net->ipv6.ip6_null_entry) {
797 fn = fib6_backtrack(fn, &fl6->saddr);
798 if (fn)
799 goto restart;
800 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700801 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700802 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700803 return rt;
804
805}
806
Ian Morris67ba4152014-08-24 21:53:10 +0100807struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
Florian Westphalea6e5742011-09-05 16:05:44 +0200808 int flags)
809{
810 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
811}
812EXPORT_SYMBOL_GPL(ip6_route_lookup);
813
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900814struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
815 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700816{
David S. Miller4c9483b2011-03-12 16:22:43 -0500817 struct flowi6 fl6 = {
818 .flowi6_oif = oif,
819 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700820 };
821 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700822 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700823
Thomas Grafadaa70b2006-10-13 15:01:03 -0700824 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500825 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700826 flags |= RT6_LOOKUP_F_HAS_SADDR;
827 }
828
David S. Miller4c9483b2011-03-12 16:22:43 -0500829 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700830 if (dst->error == 0)
831 return (struct rt6_info *) dst;
832
833 dst_release(dst);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return NULL;
836}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900837EXPORT_SYMBOL(rt6_lookup);
838
Thomas Grafc71099a2006-08-04 23:20:06 -0700839/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 It takes new route entry, the addition fails by any reason the
841 route is freed. In any case, if caller does not hold it, it may
842 be destroyed.
843 */
844
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100845static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
Florian Westphale715b6d2015-01-05 23:57:44 +0100846 struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700849 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Thomas Grafc71099a2006-08-04 23:20:06 -0700851 table = rt->rt6i_table;
852 write_lock_bh(&table->tb6_lock);
Florian Westphale715b6d2015-01-05 23:57:44 +0100853 err = fib6_add(&table->tb6_root, rt, info, mxc);
Thomas Grafc71099a2006-08-04 23:20:06 -0700854 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return err;
857}
858
Thomas Graf40e22e82006-08-22 00:00:45 -0700859int ip6_ins_rt(struct rt6_info *rt)
860{
Florian Westphale715b6d2015-01-05 23:57:44 +0100861 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
862 struct mx6_config mxc = { .mx = NULL, };
863
864 return __ip6_ins_rt(rt, &info, &mxc);
Thomas Graf40e22e82006-08-22 00:00:45 -0700865}
866
Gao feng1716a962012-04-06 00:13:10 +0000867static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000868 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000869 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct rt6_info *rt;
872
873 /*
874 * Clone the route.
875 */
876
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000877 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (rt) {
Duan Jiong249a3632013-11-05 13:34:53 +0800880 if (ort->rt6i_dst.plen != 128 &&
881 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
882 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886#ifdef CONFIG_IPV6_SUBTREES
887 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000888 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 rt->rt6i_src.plen = 128;
890 }
891#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800894 return rt;
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000897static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
898 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800899{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000900 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
901
YOSHIFUJI Hideaki / 吉藤英明887c95c2013-01-17 12:54:05 +0000902 if (rt)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800903 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800904 return rt;
905}
906
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800907static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500908 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700910 struct fib6_node *fn, *saved_fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800911 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700912 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800914 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700916 strict |= flags & RT6_LOOKUP_F_IFACE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700917 if (net->ipv6.devconf_all->forwarding == 0)
918 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700920redo_fib6_lookup_lock:
Thomas Grafc71099a2006-08-04 23:20:06 -0700921 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
David S. Miller4c9483b2011-03-12 16:22:43 -0500923 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700924 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700926redo_rt6_select:
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700927 rt = rt6_select(fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200928 if (rt->rt6i_nsiblings)
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700929 rt = rt6_multipath_select(rt, fl6, oif, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700930 if (rt == net->ipv6.ip6_null_entry) {
931 fn = fib6_backtrack(fn, &fl6->saddr);
932 if (fn)
933 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -0700934 else if (strict & RT6_LOOKUP_F_REACHABLE) {
935 /* also consider unreachable route */
936 strict &= ~RT6_LOOKUP_F_REACHABLE;
937 fn = saved_fn;
938 goto redo_rt6_select;
939 } else {
940 dst_hold(&rt->dst);
941 read_unlock_bh(&table->tb6_lock);
942 goto out2;
943 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700944 }
945
Changli Gaod8d1f302010-06-10 23:31:35 -0700946 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700947 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800948
Martin KaFai Lau94c77bb2014-10-20 13:42:44 -0700949 if (rt->rt6i_flags & RTF_CACHE)
950 goto out2;
951
YOSHIFUJI Hideaki / 吉藤英明c440f162013-01-17 12:53:32 +0000952 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
David S. Miller4c9483b2011-03-12 16:22:43 -0500953 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau653437d2015-04-28 13:03:05 -0700954 else if (!(rt->dst.flags & DST_HOST) || !(rt->dst.flags & RTF_LOCAL))
David S. Miller4c9483b2011-03-12 16:22:43 -0500955 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800956 else
957 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800958
Amerigo Wang94e187c2012-10-29 00:13:19 +0000959 ip6_rt_put(rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800960 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800961
Changli Gaod8d1f302010-06-10 23:31:35 -0700962 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800963 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700964 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800965 if (!err)
966 goto out2;
967 }
968
969 if (--attempts <= 0)
970 goto out2;
971
972 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700973 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800974 * released someone could insert this route. Relookup.
975 */
Amerigo Wang94e187c2012-10-29 00:13:19 +0000976 ip6_rt_put(rt);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700977 goto redo_fib6_lookup_lock;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979out2:
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700980 rt6_dst_from_metrics_check(rt);
Changli Gaod8d1f302010-06-10 23:31:35 -0700981 rt->dst.lastuse = jiffies;
982 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700983
984 return rt;
985}
986
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800987static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500988 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700989{
David S. Miller4c9483b2011-03-12 16:22:43 -0500990 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700991}
992
Shmulik Ladkani72331bc2012-04-01 04:03:45 +0000993static struct dst_entry *ip6_route_input_lookup(struct net *net,
994 struct net_device *dev,
995 struct flowi6 *fl6, int flags)
996{
997 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
998 flags |= RT6_LOOKUP_F_IFACE;
999
1000 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1001}
1002
Thomas Grafc71099a2006-08-04 23:20:06 -07001003void ip6_route_input(struct sk_buff *skb)
1004{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001005 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001006 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001007 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -05001008 struct flowi6 fl6 = {
1009 .flowi6_iif = skb->dev->ifindex,
1010 .daddr = iph->daddr,
1011 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001012 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05001013 .flowi6_mark = skb->mark,
1014 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001015 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001016
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001017 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001018}
1019
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001020static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001021 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001022{
David S. Miller4c9483b2011-03-12 16:22:43 -05001023 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001024}
1025
Ian Morris67ba4152014-08-24 21:53:10 +01001026struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001027 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001028{
1029 int flags = 0;
1030
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001031 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001032
David S. Miller4c9483b2011-03-12 16:22:43 -05001033 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001034 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001035
David S. Miller4c9483b2011-03-12 16:22:43 -05001036 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001037 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001038 else if (sk)
1039 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001040
David S. Miller4c9483b2011-03-12 16:22:43 -05001041 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001043EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
David S. Miller2774c132011-03-01 14:59:04 -08001045struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001046{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001047 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001048 struct dst_entry *new = NULL;
1049
David S. Millerf5b0a872012-07-19 12:31:33 -07001050 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001051 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001052 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001053
Steffen Klassert81048912012-07-05 23:37:09 +00001054 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
1055 rt6_init_peer(rt, net->ipv6.peers);
1056
David S. Miller14e50e52007-05-24 18:17:54 -07001057 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001058 new->input = dst_discard;
Eric Dumazetaad88722014-04-15 13:47:15 -04001059 new->output = dst_discard_sk;
David S. Miller14e50e52007-05-24 18:17:54 -07001060
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001061 if (dst_metrics_read_only(&ort->dst))
1062 new->_metrics = ort->dst._metrics;
1063 else
1064 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001065 rt->rt6i_idev = ort->rt6i_idev;
1066 if (rt->rt6i_idev)
1067 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001068
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001069 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001070 rt->rt6i_flags = ort->rt6i_flags;
David S. Miller14e50e52007-05-24 18:17:54 -07001071 rt->rt6i_metric = 0;
1072
1073 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1074#ifdef CONFIG_IPV6_SUBTREES
1075 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1076#endif
1077
1078 dst_free(new);
1079 }
1080
David S. Miller69ead7a2011-03-01 14:45:33 -08001081 dst_release(dst_orig);
1082 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001083}
David S. Miller14e50e52007-05-24 18:17:54 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/*
1086 * Destination cache support functions
1087 */
1088
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001089static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1090{
1091 if (rt->dst.from &&
1092 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1093 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1097{
1098 struct rt6_info *rt;
1099
1100 rt = (struct rt6_info *) dst;
1101
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001102 /* All IPV6 dsts are created with ->obsolete set to the value
1103 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1104 * into this function always.
1105 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001106 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1107 return NULL;
Li RongQinga4477c42012-11-07 21:56:33 +00001108
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001109 if (rt6_check_expired(rt))
1110 return NULL;
1111
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001112 rt6_dst_from_metrics_check(rt);
1113
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001114 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1118{
1119 struct rt6_info *rt = (struct rt6_info *) dst;
1120
1121 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001122 if (rt->rt6i_flags & RTF_CACHE) {
1123 if (rt6_check_expired(rt)) {
1124 ip6_del_rt(rt);
1125 dst = NULL;
1126 }
1127 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001129 dst = NULL;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001132 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static void ip6_link_failure(struct sk_buff *skb)
1136{
1137 struct rt6_info *rt;
1138
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001139 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Eric Dumazetadf30902009-06-02 05:19:30 +00001141 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001143 if (rt->rt6i_flags & RTF_CACHE) {
1144 dst_hold(&rt->dst);
1145 if (ip6_del_rt(rt))
1146 dst_free(&rt->dst);
1147 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 rt->rt6i_node->fn_sernum = -1;
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151}
1152
David S. Miller6700c272012-07-17 03:29:28 -07001153static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1154 struct sk_buff *skb, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Ian Morris67ba4152014-08-24 21:53:10 +01001156 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
David S. Miller81aded22012-06-15 14:54:11 -07001158 dst_confirm(dst);
Martin KaFai Lau653437d2015-04-28 13:03:05 -07001159 if (mtu < dst_mtu(dst) && (rt6->rt6i_flags & RTF_CACHE)) {
David S. Miller81aded22012-06-15 14:54:11 -07001160 struct net *net = dev_net(dst->dev);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rt6->rt6i_flags |= RTF_MODIFIED;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001163 if (mtu < IPV6_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 mtu = IPV6_MIN_MTU;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01001165
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001166 rt6->rt6i_pmtu = mtu;
David S. Miller81aded22012-06-15 14:54:11 -07001167 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169}
1170
David S. Miller42ae66c2012-06-15 20:01:57 -07001171void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1172 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001173{
1174 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1175 struct dst_entry *dst;
1176 struct flowi6 fl6;
1177
1178 memset(&fl6, 0, sizeof(fl6));
1179 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07001180 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07001181 fl6.daddr = iph->daddr;
1182 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001183 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001184
1185 dst = ip6_route_output(net, NULL, &fl6);
1186 if (!dst->error)
David S. Miller6700c272012-07-17 03:29:28 -07001187 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001188 dst_release(dst);
1189}
1190EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1191
1192void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1193{
1194 ip6_update_pmtu(skb, sock_net(sk), mtu,
1195 sk->sk_bound_dev_if, sk->sk_mark);
1196}
1197EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1198
Duan Jiongb55b76b2013-09-04 19:44:21 +08001199/* Handle redirects */
1200struct ip6rd_flowi {
1201 struct flowi6 fl6;
1202 struct in6_addr gateway;
1203};
1204
1205static struct rt6_info *__ip6_route_redirect(struct net *net,
1206 struct fib6_table *table,
1207 struct flowi6 *fl6,
1208 int flags)
1209{
1210 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1211 struct rt6_info *rt;
1212 struct fib6_node *fn;
1213
1214 /* Get the "current" route for this destination and
1215 * check if the redirect has come from approriate router.
1216 *
1217 * RFC 4861 specifies that redirects should only be
1218 * accepted if they come from the nexthop to the target.
1219 * Due to the way the routes are chosen, this notion
1220 * is a bit fuzzy and one might need to check all possible
1221 * routes.
1222 */
1223
1224 read_lock_bh(&table->tb6_lock);
1225 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1226restart:
1227 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1228 if (rt6_check_expired(rt))
1229 continue;
1230 if (rt->dst.error)
1231 break;
1232 if (!(rt->rt6i_flags & RTF_GATEWAY))
1233 continue;
1234 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1235 continue;
1236 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1237 continue;
1238 break;
1239 }
1240
1241 if (!rt)
1242 rt = net->ipv6.ip6_null_entry;
1243 else if (rt->dst.error) {
1244 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001245 goto out;
1246 }
1247
1248 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001249 fn = fib6_backtrack(fn, &fl6->saddr);
1250 if (fn)
1251 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08001252 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001253
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08001254out:
Duan Jiongb55b76b2013-09-04 19:44:21 +08001255 dst_hold(&rt->dst);
1256
1257 read_unlock_bh(&table->tb6_lock);
1258
1259 return rt;
1260};
1261
1262static struct dst_entry *ip6_route_redirect(struct net *net,
1263 const struct flowi6 *fl6,
1264 const struct in6_addr *gateway)
1265{
1266 int flags = RT6_LOOKUP_F_HAS_SADDR;
1267 struct ip6rd_flowi rdfl;
1268
1269 rdfl.fl6 = *fl6;
1270 rdfl.gateway = *gateway;
1271
1272 return fib6_rule_lookup(net, &rdfl.fl6,
1273 flags, __ip6_route_redirect);
1274}
1275
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001276void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1277{
1278 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1279 struct dst_entry *dst;
1280 struct flowi6 fl6;
1281
1282 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001283 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001284 fl6.flowi6_oif = oif;
1285 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001286 fl6.daddr = iph->daddr;
1287 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001288 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001289
Duan Jiongb55b76b2013-09-04 19:44:21 +08001290 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1291 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001292 dst_release(dst);
1293}
1294EXPORT_SYMBOL_GPL(ip6_redirect);
1295
Duan Jiongc92a59e2013-08-22 12:07:35 +08001296void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1297 u32 mark)
1298{
1299 const struct ipv6hdr *iph = ipv6_hdr(skb);
1300 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1301 struct dst_entry *dst;
1302 struct flowi6 fl6;
1303
1304 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03001305 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001306 fl6.flowi6_oif = oif;
1307 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001308 fl6.daddr = msg->dest;
1309 fl6.saddr = iph->daddr;
1310
Duan Jiongb55b76b2013-09-04 19:44:21 +08001311 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1312 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001313 dst_release(dst);
1314}
1315
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001316void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1317{
1318 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1319}
1320EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1321
David S. Miller0dbaee32010-12-13 12:52:14 -08001322static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
David S. Miller0dbaee32010-12-13 12:52:14 -08001324 struct net_device *dev = dst->dev;
1325 unsigned int mtu = dst_mtu(dst);
1326 struct net *net = dev_net(dev);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1329
Daniel Lezcano55786892008-03-04 13:47:47 -08001330 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1331 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001334 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1335 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1336 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * rely only on pmtu discovery"
1338 */
1339 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1340 mtu = IPV6_MAXPLEN;
1341 return mtu;
1342}
1343
Steffen Klassertebb762f2011-11-23 02:12:51 +00001344static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001345{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001346 const struct rt6_info *rt = (const struct rt6_info *)dst;
1347 unsigned int mtu = rt->rt6i_pmtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001348 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001349
1350 if (mtu)
Eric Dumazet30f78d82014-04-10 21:23:36 -07001351 goto out;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001352
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001353 mtu = dst_metric_raw(dst, RTAX_MTU);
1354 if (mtu)
1355 goto out;
1356
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001357 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001358
1359 rcu_read_lock();
1360 idev = __in6_dev_get(dst->dev);
1361 if (idev)
1362 mtu = idev->cnf.mtu6;
1363 rcu_read_unlock();
1364
Eric Dumazet30f78d82014-04-10 21:23:36 -07001365out:
1366 return min_t(unsigned int, mtu, IP6_MAX_MTU);
David S. Millerd33e4552010-12-14 13:01:14 -08001367}
1368
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001369static struct dst_entry *icmp6_dst_gc_list;
1370static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001371
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001372struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001373 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
David S. Miller87a11572011-12-06 17:04:13 -05001375 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct rt6_info *rt;
1377 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001378 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
David S. Miller38308472011-12-03 18:02:47 -05001380 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001381 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David S. Miller8b96d222012-06-11 02:01:56 -07001383 rt = ip6_dst_alloc(net, dev, 0, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001384 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001386 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto out;
1388 }
1389
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001390 rt->dst.flags |= DST_HOST;
1391 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001392 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001393 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001394 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001395 rt->rt6i_dst.plen = 128;
1396 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001397 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001399 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001400 rt->dst.next = icmp6_dst_gc_list;
1401 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001402 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Daniel Lezcano55786892008-03-04 13:47:47 -08001404 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
David S. Miller87a11572011-12-06 17:04:13 -05001406 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out:
David S. Miller87a11572011-12-06 17:04:13 -05001409 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001412int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001414 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001415 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001417 spin_lock_bh(&icmp6_dst_lock);
1418 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 while ((dst = *pprev) != NULL) {
1421 if (!atomic_read(&dst->__refcnt)) {
1422 *pprev = dst->next;
1423 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 } else {
1425 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001426 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
1429
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001430 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001431
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001432 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
David S. Miller1e493d12008-09-10 17:27:15 -07001435static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1436 void *arg)
1437{
1438 struct dst_entry *dst, **pprev;
1439
1440 spin_lock_bh(&icmp6_dst_lock);
1441 pprev = &icmp6_dst_gc_list;
1442 while ((dst = *pprev) != NULL) {
1443 struct rt6_info *rt = (struct rt6_info *) dst;
1444 if (func(rt, arg)) {
1445 *pprev = dst->next;
1446 dst_free(dst);
1447 } else {
1448 pprev = &dst->next;
1449 }
1450 }
1451 spin_unlock_bh(&icmp6_dst_lock);
1452}
1453
Daniel Lezcano569d3642008-01-18 03:56:57 -08001454static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001456 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001457 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1458 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1459 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1460 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1461 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001462 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Eric Dumazetfc66f952010-10-08 06:37:34 +00001464 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001465 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001466 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto out;
1468
Benjamin Thery6891a342008-03-04 13:49:47 -08001469 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08001470 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001471 entries = dst_entries_get_slow(ops);
1472 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001473 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001475 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001476 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Florian Westphale715b6d2015-01-05 23:57:44 +01001479static int ip6_convert_metrics(struct mx6_config *mxc,
1480 const struct fib6_config *cfg)
1481{
1482 struct nlattr *nla;
1483 int remaining;
1484 u32 *mp;
1485
Ian Morris63159f22015-03-29 14:00:04 +01001486 if (!cfg->fc_mx)
Florian Westphale715b6d2015-01-05 23:57:44 +01001487 return 0;
1488
1489 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1490 if (unlikely(!mp))
1491 return -ENOMEM;
1492
1493 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1494 int type = nla_type(nla);
1495
1496 if (type) {
Daniel Borkmannea697632015-01-05 23:57:47 +01001497 u32 val;
1498
Florian Westphale715b6d2015-01-05 23:57:44 +01001499 if (unlikely(type > RTAX_MAX))
1500 goto err;
Daniel Borkmannea697632015-01-05 23:57:47 +01001501 if (type == RTAX_CC_ALGO) {
1502 char tmp[TCP_CA_NAME_MAX];
Florian Westphale715b6d2015-01-05 23:57:44 +01001503
Daniel Borkmannea697632015-01-05 23:57:47 +01001504 nla_strlcpy(tmp, nla, sizeof(tmp));
1505 val = tcp_ca_get_key_by_name(tmp);
1506 if (val == TCP_CA_UNSPEC)
1507 goto err;
1508 } else {
1509 val = nla_get_u32(nla);
1510 }
1511
1512 mp[type - 1] = val;
Florian Westphale715b6d2015-01-05 23:57:44 +01001513 __set_bit(type - 1, mxc->mx_valid);
1514 }
1515 }
1516
1517 mxc->mx = mp;
1518
1519 return 0;
1520 err:
1521 kfree(mp);
1522 return -EINVAL;
1523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Thomas Graf86872cb2006-08-22 00:01:08 -07001525int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001528 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 struct rt6_info *rt = NULL;
1530 struct net_device *dev = NULL;
1531 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001532 struct fib6_table *table;
Florian Westphale715b6d2015-01-05 23:57:44 +01001533 struct mx6_config mxc = { .mx = NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int addr_type;
1535
Thomas Graf86872cb2006-08-22 00:01:08 -07001536 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EINVAL;
1538#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001539 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EINVAL;
1541#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001542 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001544 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!dev)
1546 goto out;
1547 idev = in6_dev_get(dev);
1548 if (!idev)
1549 goto out;
1550 }
1551
Thomas Graf86872cb2006-08-22 00:01:08 -07001552 if (cfg->fc_metric == 0)
1553 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Matti Vaittinend71314b2011-11-14 00:14:49 +00001555 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001556 if (cfg->fc_nlinfo.nlh &&
1557 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001558 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001559 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001560 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001561 table = fib6_new_table(net, cfg->fc_table);
1562 }
1563 } else {
1564 table = fib6_new_table(net, cfg->fc_table);
1565 }
David S. Miller38308472011-12-03 18:02:47 -05001566
1567 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001568 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001569
Sabrina Dubrocac88507f2014-03-06 17:51:57 +01001570 rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
David S. Miller38308472011-12-03 18:02:47 -05001572 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 err = -ENOMEM;
1574 goto out;
1575 }
1576
Gao feng1716a962012-04-06 00:13:10 +00001577 if (cfg->fc_flags & RTF_EXPIRES)
1578 rt6_set_expires(rt, jiffies +
1579 clock_t_to_jiffies(cfg->fc_expires));
1580 else
1581 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Thomas Graf86872cb2006-08-22 00:01:08 -07001583 if (cfg->fc_protocol == RTPROT_UNSPEC)
1584 cfg->fc_protocol = RTPROT_BOOT;
1585 rt->rt6i_protocol = cfg->fc_protocol;
1586
1587 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001590 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001591 else if (cfg->fc_flags & RTF_LOCAL)
1592 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001594 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Changli Gaod8d1f302010-06-10 23:31:35 -07001596 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Thomas Graf86872cb2006-08-22 00:01:08 -07001598 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1599 rt->rt6i_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001600 if (rt->rt6i_dst.plen == 128) {
1601 rt->dst.flags |= DST_HOST;
1602 dst_metrics_set_force_overwrite(&rt->dst);
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001603 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001606 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1607 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608#endif
1609
Thomas Graf86872cb2006-08-22 00:01:08 -07001610 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /* We cannot add true routes via loopback here,
1613 they would result in kernel looping; promote them to reject routes
1614 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001615 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001616 (dev && (dev->flags & IFF_LOOPBACK) &&
1617 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1618 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001620 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (dev) {
1622 dev_put(dev);
1623 in6_dev_put(idev);
1624 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001625 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 dev_hold(dev);
1627 idev = in6_dev_get(dev);
1628 if (!idev) {
1629 err = -ENODEV;
1630 goto out;
1631 }
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001634 switch (cfg->fc_type) {
1635 case RTN_BLACKHOLE:
1636 rt->dst.error = -EINVAL;
Eric Dumazetaad88722014-04-15 13:47:15 -04001637 rt->dst.output = dst_discard_sk;
Kamala R7150aed2013-12-02 19:55:21 +05301638 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001639 break;
1640 case RTN_PROHIBIT:
1641 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301642 rt->dst.output = ip6_pkt_prohibit_out;
1643 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001644 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001645 case RTN_THROW:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001646 default:
Kamala R7150aed2013-12-02 19:55:21 +05301647 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
1648 : -ENETUNREACH;
1649 rt->dst.output = ip6_pkt_discard_out;
1650 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001651 break;
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto install_route;
1654 }
1655
Thomas Graf86872cb2006-08-22 00:01:08 -07001656 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001657 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int gwa_type;
1659
Thomas Graf86872cb2006-08-22 00:01:08 -07001660 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001661 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 gwa_type = ipv6_addr_type(gw_addr);
1663
1664 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1665 struct rt6_info *grt;
1666
1667 /* IPv6 strictly inhibits using not link-local
1668 addresses as nexthop address.
1669 Otherwise, router will not able to send redirects.
1670 It is very good, but in some (rare!) circumstances
1671 (SIT, PtP, NBMA NOARP links) it is handy to allow
1672 some exceptions. --ANK
1673 */
1674 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001675 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto out;
1677
Daniel Lezcano55786892008-03-04 13:47:47 -08001678 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001681 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 goto out;
1683 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001684 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001685 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto out;
1687 }
1688 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001689 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 idev = grt->rt6i_idev;
1691 dev_hold(dev);
1692 in6_dev_hold(grt->rt6i_idev);
1693 }
David S. Miller38308472011-12-03 18:02:47 -05001694 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001696 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 if (err)
1699 goto out;
1700 }
1701 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001702 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto out;
1704 }
1705
1706 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001707 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto out;
1709
Daniel Walterc3968a82011-04-13 21:10:57 +00001710 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1711 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1712 err = -EINVAL;
1713 goto out;
1714 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001715 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001716 rt->rt6i_prefsrc.plen = 128;
1717 } else
1718 rt->rt6i_prefsrc.plen = 0;
1719
Thomas Graf86872cb2006-08-22 00:01:08 -07001720 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722install_route:
Changli Gaod8d1f302010-06-10 23:31:35 -07001723 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001725 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001726
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001727 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001728
Florian Westphale715b6d2015-01-05 23:57:44 +01001729 err = ip6_convert_metrics(&mxc, cfg);
1730 if (err)
1731 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Florian Westphale715b6d2015-01-05 23:57:44 +01001733 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1734
1735 kfree(mxc.mx);
1736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737out:
1738 if (dev)
1739 dev_put(dev);
1740 if (idev)
1741 in6_dev_put(idev);
1742 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001743 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return err;
1745}
1746
Thomas Graf86872cb2006-08-22 00:01:08 -07001747static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001750 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001751 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Gao feng6825a262012-09-19 19:25:34 +00001753 if (rt == net->ipv6.ip6_null_entry) {
1754 err = -ENOENT;
1755 goto out;
1756 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07001757
Thomas Grafc71099a2006-08-04 23:20:06 -07001758 table = rt->rt6i_table;
1759 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07001760 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07001761 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Gao feng6825a262012-09-19 19:25:34 +00001763out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001764 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return err;
1766}
1767
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001768int ip6_del_rt(struct rt6_info *rt)
1769{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001770 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001771 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001772 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001773 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001774}
1775
Thomas Graf86872cb2006-08-22 00:01:08 -07001776static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Thomas Grafc71099a2006-08-04 23:20:06 -07001778 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct fib6_node *fn;
1780 struct rt6_info *rt;
1781 int err = -ESRCH;
1782
Daniel Lezcano55786892008-03-04 13:47:47 -08001783 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001784 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Thomas Grafc71099a2006-08-04 23:20:06 -07001787 read_lock_bh(&table->tb6_lock);
1788
1789 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001790 &cfg->fc_dst, cfg->fc_dst_len,
1791 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001794 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07001795 if ((rt->rt6i_flags & RTF_CACHE) &&
1796 !(cfg->fc_flags & RTF_CACHE))
1797 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001799 (!rt->dst.dev ||
1800 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001802 if (cfg->fc_flags & RTF_GATEWAY &&
1803 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001807 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001808 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Thomas Graf86872cb2006-08-22 00:01:08 -07001810 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001813 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 return err;
1816}
1817
David S. Miller6700c272012-07-17 03:29:28 -07001818static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001819{
David S. Millere8599ff2012-07-11 23:43:53 -07001820 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001821 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07001822 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001823 struct ndisc_options ndopts;
1824 struct inet6_dev *in6_dev;
1825 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001826 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07001827 int optlen, on_link;
1828 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07001829
Simon Horman29a3cad2013-05-28 20:34:26 +00001830 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001831 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07001832
1833 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07001834 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001835 return;
1836 }
1837
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001838 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07001839
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001840 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001841 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001842 return;
1843 }
1844
David S. Miller6e157b62012-07-12 00:05:02 -07001845 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001846 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001847 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001848 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07001849 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001850 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001851 return;
1852 }
1853
1854 in6_dev = __in6_dev_get(skb->dev);
1855 if (!in6_dev)
1856 return;
1857 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1858 return;
1859
1860 /* RFC2461 8.1:
1861 * The IP source address of the Redirect MUST be the same as the current
1862 * first-hop router for the specified ICMP Destination Address.
1863 */
1864
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001865 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001866 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1867 return;
1868 }
David S. Miller6e157b62012-07-12 00:05:02 -07001869
1870 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001871 if (ndopts.nd_opts_tgt_lladdr) {
1872 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1873 skb->dev);
1874 if (!lladdr) {
1875 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1876 return;
1877 }
1878 }
1879
David S. Miller6e157b62012-07-12 00:05:02 -07001880 rt = (struct rt6_info *) dst;
1881 if (rt == net->ipv6.ip6_null_entry) {
1882 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1883 return;
1884 }
1885
1886 /* Redirect received -> path was valid.
1887 * Look, redirects are sent only in response to data packets,
1888 * so that this nexthop apparently is reachable. --ANK
1889 */
1890 dst_confirm(&rt->dst);
1891
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001892 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07001893 if (!neigh)
1894 return;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
1897 * We have finally decided to accept it.
1898 */
1899
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001900 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1902 NEIGH_UPDATE_F_OVERRIDE|
1903 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1904 NEIGH_UPDATE_F_ISROUTER))
1905 );
1906
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001907 nrt = ip6_rt_copy(rt, &msg->dest);
David S. Miller38308472011-12-03 18:02:47 -05001908 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto out;
1910
1911 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1912 if (on_link)
1913 nrt->rt6i_flags &= ~RTF_GATEWAY;
1914
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001915 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Thomas Graf40e22e82006-08-22 00:00:45 -07001917 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 goto out;
1919
Changli Gaod8d1f302010-06-10 23:31:35 -07001920 netevent.old = &rt->dst;
1921 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001922 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00001923 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07001924 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1925
David S. Miller38308472011-12-03 18:02:47 -05001926 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07001927 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001928 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930
1931out:
David S. Millere8599ff2012-07-11 23:43:53 -07001932 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07001933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * Misc support functions
1937 */
1938
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001939static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
1940{
1941 BUG_ON(from->dst.from);
1942
1943 rt->rt6i_flags &= ~RTF_EXPIRES;
1944 dst_hold(&from->dst);
1945 rt->dst.from = &from->dst;
1946 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
1947}
1948
Gao feng1716a962012-04-06 00:13:10 +00001949static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001950 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
David S. Millerd1918542011-12-28 20:19:20 -05001952 struct net *net = dev_net(ort->dst.dev);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07001953 struct rt6_info *rt;
1954
1955 if (ort->rt6i_flags & RTF_CACHE)
1956 ort = (struct rt6_info *)ort->dst.from;
1957
1958 rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1959 ort->rt6i_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001962 rt->dst.input = ort->dst.input;
1963 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001964 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001966 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001967 rt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001968 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rt->rt6i_idev = ort->rt6i_idev;
1970 if (rt->rt6i_idev)
1971 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001972 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Julian Anastasov550bab42013-10-20 15:43:04 +03001974 if (ort->rt6i_flags & RTF_GATEWAY)
1975 rt->rt6i_gateway = ort->rt6i_gateway;
1976 else
1977 rt->rt6i_gateway = *dest;
Gao feng1716a962012-04-06 00:13:10 +00001978 rt->rt6i_flags = ort->rt6i_flags;
Li RongQing24f5b852013-12-19 12:40:26 +08001979 rt6_set_from(rt, ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 rt->rt6i_metric = 0;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#ifdef CONFIG_IPV6_SUBTREES
1983 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1984#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001985 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001986 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 return rt;
1989}
1990
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001991#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001992static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001993 const struct in6_addr *prefix, int prefixlen,
1994 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001995{
1996 struct fib6_node *fn;
1997 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001998 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001999
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002000 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05002001 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002002 return NULL;
2003
Li RongQing5744dd92012-09-11 21:59:01 +00002004 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002005 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002006 if (!fn)
2007 goto out;
2008
Changli Gaod8d1f302010-06-10 23:31:35 -07002009 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002010 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002011 continue;
2012 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
2013 continue;
2014 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
2015 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07002016 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002017 break;
2018 }
2019out:
Li RongQing5744dd92012-09-11 21:59:01 +00002020 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002021 return rt;
2022}
2023
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002024static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002025 const struct in6_addr *prefix, int prefixlen,
2026 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00002027 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002028{
Thomas Graf86872cb2006-08-22 00:01:08 -07002029 struct fib6_config cfg = {
2030 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002031 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002032 .fc_ifindex = ifindex,
2033 .fc_dst_len = prefixlen,
2034 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
2035 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002036 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002037 .fc_nlinfo.nlh = NULL,
2038 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07002039 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002040
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002041 cfg.fc_dst = *prefix;
2042 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07002043
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08002044 /* We should treat it as a default route if prefix length is 0. */
2045 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07002046 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002047
Thomas Graf86872cb2006-08-22 00:01:08 -07002048 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002049
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08002050 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08002051}
2052#endif
2053
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002054struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002057 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002059 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002060 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002061 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Li RongQing5744dd92012-09-11 21:59:01 +00002063 read_lock_bh(&table->tb6_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002064 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002065 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002066 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2068 break;
2069 }
2070 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002071 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002072 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return rt;
2074}
2075
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002076struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002077 struct net_device *dev,
2078 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Thomas Graf86872cb2006-08-22 00:01:08 -07002080 struct fib6_config cfg = {
2081 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002082 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002083 .fc_ifindex = dev->ifindex,
2084 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2085 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002086 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002087 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002088 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002091 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Thomas Graf86872cb2006-08-22 00:01:08 -07002093 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return rt6_get_dflt_router(gwaddr, dev);
2096}
2097
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002098void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002101 struct fib6_table *table;
2102
2103 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002104 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002105 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002109 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002110 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002111 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2112 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002113 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002114 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002115 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto restart;
2117 }
2118 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002119 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Daniel Lezcano55786892008-03-04 13:47:47 -08002122static void rtmsg_to_fib6_config(struct net *net,
2123 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002124 struct fib6_config *cfg)
2125{
2126 memset(cfg, 0, sizeof(*cfg));
2127
2128 cfg->fc_table = RT6_TABLE_MAIN;
2129 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2130 cfg->fc_metric = rtmsg->rtmsg_metric;
2131 cfg->fc_expires = rtmsg->rtmsg_info;
2132 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2133 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2134 cfg->fc_flags = rtmsg->rtmsg_flags;
2135
Daniel Lezcano55786892008-03-04 13:47:47 -08002136 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002137
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002138 cfg->fc_dst = rtmsg->rtmsg_dst;
2139 cfg->fc_src = rtmsg->rtmsg_src;
2140 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002141}
2142
Daniel Lezcano55786892008-03-04 13:47:47 -08002143int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Thomas Graf86872cb2006-08-22 00:01:08 -07002145 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct in6_rtmsg rtmsg;
2147 int err;
2148
Ian Morris67ba4152014-08-24 21:53:10 +01002149 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 case SIOCADDRT: /* Add a route */
2151 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002152 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return -EPERM;
2154 err = copy_from_user(&rtmsg, arg,
2155 sizeof(struct in6_rtmsg));
2156 if (err)
2157 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002158
Daniel Lezcano55786892008-03-04 13:47:47 -08002159 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 rtnl_lock();
2162 switch (cmd) {
2163 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002164 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 break;
2166 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002167 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 default:
2170 err = -EINVAL;
2171 }
2172 rtnl_unlock();
2173
2174 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 return -EINVAL;
2178}
2179
2180/*
2181 * Drop the packet on the floor
2182 */
2183
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002184static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002186 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002187 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002188 switch (ipstats_mib_noroutes) {
2189 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002190 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002191 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002192 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2193 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002194 break;
2195 }
2196 /* FALLTHROUGH */
2197 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002198 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2199 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002200 break;
2201 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002202 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 kfree_skb(skb);
2204 return 0;
2205}
2206
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002207static int ip6_pkt_discard(struct sk_buff *skb)
2208{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002209 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002210}
2211
Eric Dumazetaad88722014-04-15 13:47:15 -04002212static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Eric Dumazetadf30902009-06-02 05:19:30 +00002214 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002215 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002218static int ip6_pkt_prohibit(struct sk_buff *skb)
2219{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002220 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002221}
2222
Eric Dumazetaad88722014-04-15 13:47:15 -04002223static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002224{
Eric Dumazetadf30902009-06-02 05:19:30 +00002225 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002226 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002227}
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229/*
2230 * Allocate a dst for local (unicast / anycast) address.
2231 */
2232
2233struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2234 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002235 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002237 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002238 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2239 DST_NOCOUNT, NULL);
2240 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return ERR_PTR(-ENOMEM);
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 in6_dev_hold(idev);
2244
David S. Miller11d53b42011-06-24 15:23:34 -07002245 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002246 rt->dst.input = ip6_input;
2247 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002251 if (anycast)
2252 rt->rt6i_flags |= RTF_ANYCAST;
2253 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Julian Anastasov550bab42013-10-20 15:43:04 +03002256 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002257 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002259 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Changli Gaod8d1f302010-06-10 23:31:35 -07002261 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 return rt;
2264}
2265
Daniel Walterc3968a82011-04-13 21:10:57 +00002266int ip6_route_get_saddr(struct net *net,
2267 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002268 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002269 unsigned int prefs,
2270 struct in6_addr *saddr)
2271{
Ian Morris67ba4152014-08-24 21:53:10 +01002272 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
Daniel Walterc3968a82011-04-13 21:10:57 +00002273 int err = 0;
2274 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002275 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002276 else
2277 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2278 daddr, prefs, saddr);
2279 return err;
2280}
2281
2282/* remove deleted ip from prefsrc entries */
2283struct arg_dev_net_ip {
2284 struct net_device *dev;
2285 struct net *net;
2286 struct in6_addr *addr;
2287};
2288
2289static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2290{
2291 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2292 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2293 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2294
David S. Millerd1918542011-12-28 20:19:20 -05002295 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002296 rt != net->ipv6.ip6_null_entry &&
2297 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2298 /* remove prefsrc entry */
2299 rt->rt6i_prefsrc.plen = 0;
2300 }
2301 return 0;
2302}
2303
2304void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2305{
2306 struct net *net = dev_net(ifp->idev->dev);
2307 struct arg_dev_net_ip adni = {
2308 .dev = ifp->idev->dev,
2309 .net = net,
2310 .addr = &ifp->addr,
2311 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002312 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002313}
2314
Duan Jiongbe7a0102014-05-15 15:56:14 +08002315#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2316#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2317
2318/* Remove routers and update dst entries when gateway turn into host. */
2319static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2320{
2321 struct in6_addr *gateway = (struct in6_addr *)arg;
2322
2323 if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2324 ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2325 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2326 return -1;
2327 }
2328 return 0;
2329}
2330
2331void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2332{
2333 fib6_clean_all(net, fib6_clean_tohost, gateway);
2334}
2335
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002336struct arg_dev_net {
2337 struct net_device *dev;
2338 struct net *net;
2339};
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static int fib6_ifdown(struct rt6_info *rt, void *arg)
2342{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002343 const struct arg_dev_net *adn = arg;
2344 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002345
David S. Millerd1918542011-12-28 20:19:20 -05002346 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002347 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return 0;
2351}
2352
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002353void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002355 struct arg_dev_net adn = {
2356 .dev = dev,
2357 .net = net,
2358 };
2359
Li RongQing0c3584d2013-12-27 16:32:38 +08002360 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002361 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Eric Dumazet95c96172012-04-15 05:58:06 +00002364struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002366 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367};
2368
2369static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2370{
2371 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2372 struct inet6_dev *idev;
2373
2374 /* In IPv6 pmtu discovery is not optional,
2375 so that RTAX_MTU lock cannot disable it.
2376 We still use this lock to block changes
2377 caused by addrconf/ndisc.
2378 */
2379
2380 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002381 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return 0;
2383
2384 /* For administrative MTU increase, there is no way to discover
2385 IPv6 PMTU increase, so PMTU increase should be updated here.
2386 Since RFC 1981 doesn't include administrative MTU increase
2387 update PMTU increase is a MUST. (i.e. jumbo frame)
2388 */
2389 /*
2390 If new MTU is less than route PMTU, this new MTU will be the
2391 lowest MTU in the path, update the route PMTU to reflect PMTU
2392 decreases; if new MTU is greater than route PMTU, and the
2393 old MTU is the lowest MTU in the path, update the route PMTU
2394 to reflect the increase. In this case if the other nodes' MTU
2395 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2396 PMTU discouvery.
2397 */
David S. Millerd1918542011-12-28 20:19:20 -05002398 if (rt->dst.dev == arg->dev &&
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002399 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
2400 if (rt->rt6i_flags & RTF_CACHE) {
2401 /* For RTF_CACHE with rt6i_pmtu == 0
2402 * (i.e. a redirected route),
2403 * the metrics of its rt->dst.from has already
2404 * been updated.
2405 */
2406 if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
2407 rt->rt6i_pmtu = arg->mtu;
2408 } else if (dst_mtu(&rt->dst) >= arg->mtu ||
2409 (dst_mtu(&rt->dst) < arg->mtu &&
2410 dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2411 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2412 }
Simon Arlott566cfd82007-07-26 00:09:55 -07002413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return 0;
2415}
2416
Eric Dumazet95c96172012-04-15 05:58:06 +00002417void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Thomas Grafc71099a2006-08-04 23:20:06 -07002419 struct rt6_mtu_change_arg arg = {
2420 .dev = dev,
2421 .mtu = mtu,
2422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Li RongQing0c3584d2013-12-27 16:32:38 +08002424 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002427static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002428 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002429 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002430 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002431 [RTA_PRIORITY] = { .type = NLA_U32 },
2432 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002433 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002434 [RTA_PREF] = { .type = NLA_U8 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002435};
2436
2437static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2438 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Thomas Graf86872cb2006-08-22 00:01:08 -07002440 struct rtmsg *rtm;
2441 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002442 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07002443 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Thomas Graf86872cb2006-08-22 00:01:08 -07002445 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2446 if (err < 0)
2447 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Thomas Graf86872cb2006-08-22 00:01:08 -07002449 err = -EINVAL;
2450 rtm = nlmsg_data(nlh);
2451 memset(cfg, 0, sizeof(*cfg));
2452
2453 cfg->fc_table = rtm->rtm_table;
2454 cfg->fc_dst_len = rtm->rtm_dst_len;
2455 cfg->fc_src_len = rtm->rtm_src_len;
2456 cfg->fc_flags = RTF_UP;
2457 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002458 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002459
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002460 if (rtm->rtm_type == RTN_UNREACHABLE ||
2461 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002462 rtm->rtm_type == RTN_PROHIBIT ||
2463 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002464 cfg->fc_flags |= RTF_REJECT;
2465
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002466 if (rtm->rtm_type == RTN_LOCAL)
2467 cfg->fc_flags |= RTF_LOCAL;
2468
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002469 if (rtm->rtm_flags & RTM_F_CLONED)
2470 cfg->fc_flags |= RTF_CACHE;
2471
Eric W. Biederman15e47302012-09-07 20:12:54 +00002472 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002473 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002474 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002475
2476 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002477 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07002478 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002480
2481 if (tb[RTA_DST]) {
2482 int plen = (rtm->rtm_dst_len + 7) >> 3;
2483
2484 if (nla_len(tb[RTA_DST]) < plen)
2485 goto errout;
2486
2487 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002489
2490 if (tb[RTA_SRC]) {
2491 int plen = (rtm->rtm_src_len + 7) >> 3;
2492
2493 if (nla_len(tb[RTA_SRC]) < plen)
2494 goto errout;
2495
2496 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002498
Daniel Walterc3968a82011-04-13 21:10:57 +00002499 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02002500 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00002501
Thomas Graf86872cb2006-08-22 00:01:08 -07002502 if (tb[RTA_OIF])
2503 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2504
2505 if (tb[RTA_PRIORITY])
2506 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2507
2508 if (tb[RTA_METRICS]) {
2509 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2510 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002512
2513 if (tb[RTA_TABLE])
2514 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2515
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002516 if (tb[RTA_MULTIPATH]) {
2517 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2518 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2519 }
2520
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002521 if (tb[RTA_PREF]) {
2522 pref = nla_get_u8(tb[RTA_PREF]);
2523 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2524 pref != ICMPV6_ROUTER_PREF_HIGH)
2525 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2526 cfg->fc_flags |= RTF_PREF(pref);
2527 }
2528
Thomas Graf86872cb2006-08-22 00:01:08 -07002529 err = 0;
2530errout:
2531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002534static int ip6_route_multipath(struct fib6_config *cfg, int add)
2535{
2536 struct fib6_config r_cfg;
2537 struct rtnexthop *rtnh;
2538 int remaining;
2539 int attrlen;
2540 int err = 0, last_err = 0;
2541
2542beginning:
2543 rtnh = (struct rtnexthop *)cfg->fc_mp;
2544 remaining = cfg->fc_mp_len;
2545
2546 /* Parse a Multipath Entry */
2547 while (rtnh_ok(rtnh, remaining)) {
2548 memcpy(&r_cfg, cfg, sizeof(*cfg));
2549 if (rtnh->rtnh_ifindex)
2550 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2551
2552 attrlen = rtnh_attrlen(rtnh);
2553 if (attrlen > 0) {
2554 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2555
2556 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2557 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002558 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002559 r_cfg.fc_flags |= RTF_GATEWAY;
2560 }
2561 }
2562 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2563 if (err) {
2564 last_err = err;
2565 /* If we are trying to remove a route, do not stop the
2566 * loop when ip6_route_del() fails (because next hop is
2567 * already gone), we should try to remove all next hops.
2568 */
2569 if (add) {
2570 /* If add fails, we should try to delete all
2571 * next hops that have been already added.
2572 */
2573 add = 0;
2574 goto beginning;
2575 }
2576 }
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002577 /* Because each route is added like a single route we remove
2578 * this flag after the first nexthop (if there is a collision,
2579 * we have already fail to add the first nexthop:
2580 * fib6_add_rt2node() has reject it).
2581 */
2582 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002583 rtnh = rtnh_next(rtnh, &remaining);
2584 }
2585
2586 return last_err;
2587}
2588
Ian Morris67ba4152014-08-24 21:53:10 +01002589static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Thomas Graf86872cb2006-08-22 00:01:08 -07002591 struct fib6_config cfg;
2592 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Thomas Graf86872cb2006-08-22 00:01:08 -07002594 err = rtm_to_fib6_config(skb, nlh, &cfg);
2595 if (err < 0)
2596 return err;
2597
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002598 if (cfg.fc_mp)
2599 return ip6_route_multipath(&cfg, 0);
2600 else
2601 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Ian Morris67ba4152014-08-24 21:53:10 +01002604static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Thomas Graf86872cb2006-08-22 00:01:08 -07002606 struct fib6_config cfg;
2607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Thomas Graf86872cb2006-08-22 00:01:08 -07002609 err = rtm_to_fib6_config(skb, nlh, &cfg);
2610 if (err < 0)
2611 return err;
2612
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002613 if (cfg.fc_mp)
2614 return ip6_route_multipath(&cfg, 1);
2615 else
2616 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Thomas Graf339bf982006-11-10 14:10:15 -08002619static inline size_t rt6_nlmsg_size(void)
2620{
2621 return NLMSG_ALIGN(sizeof(struct rtmsg))
2622 + nla_total_size(16) /* RTA_SRC */
2623 + nla_total_size(16) /* RTA_DST */
2624 + nla_total_size(16) /* RTA_GATEWAY */
2625 + nla_total_size(16) /* RTA_PREFSRC */
2626 + nla_total_size(4) /* RTA_TABLE */
2627 + nla_total_size(4) /* RTA_IIF */
2628 + nla_total_size(4) /* RTA_OIF */
2629 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002630 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01002631 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002632 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2633 + nla_total_size(1); /* RTA_PREF */
Thomas Graf339bf982006-11-10 14:10:15 -08002634}
2635
Brian Haley191cd582008-08-14 15:33:21 -07002636static int rt6_fill_node(struct net *net,
2637 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002638 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002639 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002640 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002642 u32 metrics[RTAX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002644 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002645 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002646 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 if (prefix) { /* user wants prefix routes only */
2649 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2650 /* success since this is not a prefix route */
2651 return 1;
2652 }
2653 }
2654
Eric W. Biederman15e47302012-09-07 20:12:54 +00002655 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002656 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002657 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002658
2659 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 rtm->rtm_family = AF_INET6;
2661 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2662 rtm->rtm_src_len = rt->rt6i_src.plen;
2663 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002664 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002665 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002666 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002667 table = RT6_TABLE_UNSPEC;
2668 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04002669 if (nla_put_u32(skb, RTA_TABLE, table))
2670 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002671 if (rt->rt6i_flags & RTF_REJECT) {
2672 switch (rt->dst.error) {
2673 case -EINVAL:
2674 rtm->rtm_type = RTN_BLACKHOLE;
2675 break;
2676 case -EACCES:
2677 rtm->rtm_type = RTN_PROHIBIT;
2678 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002679 case -EAGAIN:
2680 rtm->rtm_type = RTN_THROW;
2681 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002682 default:
2683 rtm->rtm_type = RTN_UNREACHABLE;
2684 break;
2685 }
2686 }
David S. Miller38308472011-12-03 18:02:47 -05002687 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002688 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002689 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 rtm->rtm_type = RTN_LOCAL;
2691 else
2692 rtm->rtm_type = RTN_UNICAST;
2693 rtm->rtm_flags = 0;
2694 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2695 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002696 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00002698 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2699 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2700 rtm->rtm_protocol = RTPROT_RA;
2701 else
2702 rtm->rtm_protocol = RTPROT_KERNEL;
2703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
David S. Miller38308472011-12-03 18:02:47 -05002705 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 rtm->rtm_flags |= RTM_F_CLONED;
2707
2708 if (dst) {
Jiri Benc930345e2015-03-29 16:59:25 +02002709 if (nla_put_in6_addr(skb, RTA_DST, dst))
David S. Millerc78679e2012-04-01 20:27:33 -04002710 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002711 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02002713 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002714 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715#ifdef CONFIG_IPV6_SUBTREES
2716 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02002717 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04002718 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002719 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04002720 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02002721 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04002722 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002724 if (iif) {
2725#ifdef CONFIG_IPV6_MROUTE
2726 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002727 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002728 if (err <= 0) {
2729 if (!nowait) {
2730 if (err == 0)
2731 return 0;
2732 goto nla_put_failure;
2733 } else {
2734 if (err == -EMSGSIZE)
2735 goto nla_put_failure;
2736 }
2737 }
2738 } else
2739#endif
David S. Millerc78679e2012-04-01 20:27:33 -04002740 if (nla_put_u32(skb, RTA_IIF, iif))
2741 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002742 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04002744 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02002745 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002746 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002748
Daniel Walterc3968a82011-04-13 21:10:57 +00002749 if (rt->rt6i_prefsrc.plen) {
2750 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002751 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02002752 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04002753 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00002754 }
2755
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002756 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
2757 if (rt->rt6i_pmtu)
2758 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
2759 if (rtnetlink_put_metrics(skb, metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002760 goto nla_put_failure;
2761
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00002762 if (rt->rt6i_flags & RTF_GATEWAY) {
Jiri Benc930345e2015-03-29 16:59:25 +02002763 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00002764 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002765 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002766
David S. Millerc78679e2012-04-01 20:27:33 -04002767 if (rt->dst.dev &&
2768 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2769 goto nla_put_failure;
2770 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2771 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00002772
2773 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002774
David S. Miller87a50692012-07-10 05:06:14 -07002775 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002776 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01002778 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2779 goto nla_put_failure;
2780
Johannes Berg053c0952015-01-16 22:09:00 +01002781 nlmsg_end(skb, nlh);
2782 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002783
2784nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002785 nlmsg_cancel(skb, nlh);
2786 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
Patrick McHardy1b43af52006-08-10 23:11:17 -07002789int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2792 int prefix;
2793
Thomas Graf2d7202b2006-08-22 00:01:27 -07002794 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2795 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2797 } else
2798 prefix = 0;
2799
Brian Haley191cd582008-08-14 15:33:21 -07002800 return rt6_fill_node(arg->net,
2801 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002802 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002803 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
Ian Morris67ba4152014-08-24 21:53:10 +01002806static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002808 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002809 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002811 struct sk_buff *skb;
2812 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002813 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002814 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07002815
2816 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2817 if (err < 0)
2818 goto errout;
2819
2820 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002821 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002822
2823 if (tb[RTA_SRC]) {
2824 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2825 goto errout;
2826
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002827 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002828 }
2829
2830 if (tb[RTA_DST]) {
2831 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2832 goto errout;
2833
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002834 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002835 }
2836
2837 if (tb[RTA_IIF])
2838 iif = nla_get_u32(tb[RTA_IIF]);
2839
2840 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002841 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002842
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07002843 if (tb[RTA_MARK])
2844 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
2845
Thomas Grafab364a62006-08-22 00:01:47 -07002846 if (iif) {
2847 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002848 int flags = 0;
2849
Daniel Lezcano55786892008-03-04 13:47:47 -08002850 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002851 if (!dev) {
2852 err = -ENODEV;
2853 goto errout;
2854 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002855
2856 fl6.flowi6_iif = iif;
2857
2858 if (!ipv6_addr_any(&fl6.saddr))
2859 flags |= RT6_LOOKUP_F_HAS_SADDR;
2860
2861 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2862 flags);
2863 } else {
2864 fl6.flowi6_oif = oif;
2865
2866 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07002867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002870 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00002871 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07002872 err = -ENOBUFS;
2873 goto errout;
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 /* Reserve room for dummy headers, this skb can pass
2877 through good chunk of routing engine.
2878 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002879 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2881
Changli Gaod8d1f302010-06-10 23:31:35 -07002882 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
David S. Miller4c9483b2011-03-12 16:22:43 -05002884 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002885 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002886 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002888 kfree_skb(skb);
2889 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891
Eric W. Biederman15e47302012-09-07 20:12:54 +00002892 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07002893errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
Thomas Graf86872cb2006-08-22 00:01:08 -07002897void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002900 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002901 u32 seq;
2902 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002904 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002905 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002906
Thomas Graf339bf982006-11-10 14:10:15 -08002907 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002908 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002909 goto errout;
2910
Brian Haley191cd582008-08-14 15:33:21 -07002911 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002912 event, info->portid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002913 if (err < 0) {
2914 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2915 WARN_ON(err == -EMSGSIZE);
2916 kfree_skb(skb);
2917 goto errout;
2918 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002919 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002920 info->nlh, gfp_any());
2921 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002922errout:
2923 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002924 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002927static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00002928 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002929{
Jiri Pirko351638e2013-05-28 01:30:21 +00002930 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002931 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002932
2933 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002934 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002935 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2936#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002937 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002938 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002939 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002940 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2941#endif
2942 }
2943
2944 return NOTIFY_OK;
2945}
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947/*
2948 * /proc
2949 */
2950
2951#ifdef CONFIG_PROC_FS
2952
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002953static const struct file_operations ipv6_route_proc_fops = {
2954 .owner = THIS_MODULE,
2955 .open = ipv6_route_open,
2956 .read = seq_read,
2957 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002958 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002959};
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2962{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002963 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002965 net->ipv6.rt6_stats->fib_nodes,
2966 net->ipv6.rt6_stats->fib_route_nodes,
2967 net->ipv6.rt6_stats->fib_rt_alloc,
2968 net->ipv6.rt6_stats->fib_rt_entries,
2969 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002970 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002971 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 return 0;
2974}
2975
2976static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2977{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002978 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002979}
2980
Arjan van de Ven9a321442007-02-12 00:55:35 -08002981static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .owner = THIS_MODULE,
2983 .open = rt6_stats_seq_open,
2984 .read = seq_read,
2985 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002986 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987};
2988#endif /* CONFIG_PROC_FS */
2989
2990#ifdef CONFIG_SYSCTL
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992static
Joe Perchesfe2c6332013-06-11 23:04:25 -07002993int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 void __user *buffer, size_t *lenp, loff_t *ppos)
2995{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002996 struct net *net;
2997 int delay;
2998 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003000
3001 net = (struct net *)ctl->extra1;
3002 delay = net->ipv6.sysctl.flush_delay;
3003 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02003004 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Joe Perchesfe2c6332013-06-11 23:04:25 -07003008struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003009 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08003011 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07003013 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003014 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 },
3016 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003018 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 .maxlen = sizeof(int),
3020 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003021 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 },
3023 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08003025 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 .maxlen = sizeof(int),
3027 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003028 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 },
3030 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003032 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 .maxlen = sizeof(int),
3034 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003035 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 },
3037 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08003039 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 .maxlen = sizeof(int),
3041 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003042 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 },
3044 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08003046 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 .maxlen = sizeof(int),
3048 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003049 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 },
3051 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08003053 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 .maxlen = sizeof(int),
3055 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003056 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 },
3058 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08003060 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 .maxlen = sizeof(int),
3062 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003063 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 },
3065 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08003067 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 .maxlen = sizeof(int),
3069 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07003070 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 },
3072 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08003074 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 .maxlen = sizeof(int),
3076 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003077 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003079 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080};
3081
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003082struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003083{
3084 struct ctl_table *table;
3085
3086 table = kmemdup(ipv6_route_table_template,
3087 sizeof(ipv6_route_table_template),
3088 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003089
3090 if (table) {
3091 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003092 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003093 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003094 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3095 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3096 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3097 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3098 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3099 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3100 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003101 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003102
3103 /* Don't export sysctls to unprivileged users */
3104 if (net->user_ns != &init_user_ns)
3105 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003106 }
3107
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003108 return table;
3109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110#endif
3111
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003112static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003113{
Pavel Emelyanov633d4242008-04-21 14:25:23 -07003114 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003115
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003116 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3117 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003118
Eric Dumazetfc66f952010-10-08 06:37:34 +00003119 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3120 goto out_ip6_dst_ops;
3121
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003122 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3123 sizeof(*net->ipv6.ip6_null_entry),
3124 GFP_KERNEL);
3125 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003126 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003127 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003128 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003129 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003130 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3131 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003132
3133#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3134 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3135 sizeof(*net->ipv6.ip6_prohibit_entry),
3136 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003137 if (!net->ipv6.ip6_prohibit_entry)
3138 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003139 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003140 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003141 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003142 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3143 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003144
3145 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3146 sizeof(*net->ipv6.ip6_blk_hole_entry),
3147 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003148 if (!net->ipv6.ip6_blk_hole_entry)
3149 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003150 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003151 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003152 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003153 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3154 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003155#endif
3156
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003157 net->ipv6.sysctl.flush_delay = 0;
3158 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3159 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3160 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3161 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3162 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3163 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3164 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3165
Benjamin Thery6891a342008-03-04 13:49:47 -08003166 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3167
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003168 ret = 0;
3169out:
3170 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003171
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003172#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3173out_ip6_prohibit_entry:
3174 kfree(net->ipv6.ip6_prohibit_entry);
3175out_ip6_null_entry:
3176 kfree(net->ipv6.ip6_null_entry);
3177#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003178out_ip6_dst_entries:
3179 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003180out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003181 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003182}
3183
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003184static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003185{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003186 kfree(net->ipv6.ip6_null_entry);
3187#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3188 kfree(net->ipv6.ip6_prohibit_entry);
3189 kfree(net->ipv6.ip6_blk_hole_entry);
3190#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003191 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003192}
3193
Thomas Grafd1896342012-06-18 12:08:33 +00003194static int __net_init ip6_route_net_init_late(struct net *net)
3195{
3196#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003197 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3198 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003199#endif
3200 return 0;
3201}
3202
3203static void __net_exit ip6_route_net_exit_late(struct net *net)
3204{
3205#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003206 remove_proc_entry("ipv6_route", net->proc_net);
3207 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003208#endif
3209}
3210
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003211static struct pernet_operations ip6_route_net_ops = {
3212 .init = ip6_route_net_init,
3213 .exit = ip6_route_net_exit,
3214};
3215
David S. Millerc3426b42012-06-09 16:27:05 -07003216static int __net_init ipv6_inetpeer_init(struct net *net)
3217{
3218 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3219
3220 if (!bp)
3221 return -ENOMEM;
3222 inet_peer_base_init(bp);
3223 net->ipv6.peers = bp;
3224 return 0;
3225}
3226
3227static void __net_exit ipv6_inetpeer_exit(struct net *net)
3228{
3229 struct inet_peer_base *bp = net->ipv6.peers;
3230
3231 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003232 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003233 kfree(bp);
3234}
3235
David S. Miller2b823f72012-06-09 19:00:16 -07003236static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003237 .init = ipv6_inetpeer_init,
3238 .exit = ipv6_inetpeer_exit,
3239};
3240
Thomas Grafd1896342012-06-18 12:08:33 +00003241static struct pernet_operations ip6_route_net_late_ops = {
3242 .init = ip6_route_net_init_late,
3243 .exit = ip6_route_net_exit_late,
3244};
3245
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003246static struct notifier_block ip6_route_dev_notifier = {
3247 .notifier_call = ip6_route_dev_notify,
3248 .priority = 0,
3249};
3250
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003251int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003253 int ret;
3254
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003255 ret = -ENOMEM;
3256 ip6_dst_ops_template.kmem_cachep =
3257 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3258 SLAB_HWCACHE_ALIGN, NULL);
3259 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003260 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003261
Eric Dumazetfc66f952010-10-08 06:37:34 +00003262 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003263 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003264 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003265
David S. Millerc3426b42012-06-09 16:27:05 -07003266 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3267 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003268 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003269
David S. Miller7e52b332012-06-15 15:51:55 -07003270 ret = register_pernet_subsys(&ip6_route_net_ops);
3271 if (ret)
3272 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003273
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003274 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3275
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003276 /* Registering of the loopback is done before this portion of code,
3277 * the loopback reference in rt6_info will not be taken, do it
3278 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003279 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003280 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3281 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003282 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003283 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003284 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003285 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3286 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003287 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003288 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003289 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003290
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003291 ret = xfrm6_init();
3292 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003293 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003294
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003295 ret = fib6_rules_init();
3296 if (ret)
3297 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003298
Thomas Grafd1896342012-06-18 12:08:33 +00003299 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3300 if (ret)
3301 goto fib6_rules_init;
3302
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003303 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003304 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3305 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3306 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003307 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003308
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003309 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003310 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003311 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003312
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003313out:
3314 return ret;
3315
Thomas Grafd1896342012-06-18 12:08:33 +00003316out_register_late_subsys:
3317 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003318fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003319 fib6_rules_cleanup();
3320xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003321 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003322out_fib6_init:
3323 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003324out_register_subsys:
3325 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003326out_register_inetpeer:
3327 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003328out_dst_entries:
3329 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003330out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003331 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
3335void ip6_route_cleanup(void)
3336{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003337 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003338 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003339 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003342 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003343 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003344 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003345 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}