blob: 11dac21e658690cdf01d7eb41c7e653d142ad9d4 [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);
87static int ip6_pkt_discard_out(struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053088static int ip6_pkt_prohibit(struct sk_buff *skb);
89static int ip6_pkt_prohibit_out(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);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020095static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080097#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080098static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000099 const struct in6_addr *prefix, int prefixlen,
100 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +0000101 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800102static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000103 const struct in6_addr *prefix, int prefixlen,
104 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800105#endif
106
stephen hemmingere8243532013-12-29 14:03:31 -0800107static void rt6_bind_peer(struct rt6_info *rt, int create)
108{
109 struct inet_peer_base *base;
110 struct inet_peer *peer;
111
112 base = inetpeer_base_ptr(rt->_rt6i_peer);
113 if (!base)
114 return;
115
116 peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
117 if (peer) {
118 if (!rt6_set_peer(rt, peer))
119 inet_putpeer(peer);
120 }
121}
122
123static struct inet_peer *__rt6_get_peer(struct rt6_info *rt, int create)
124{
125 if (rt6_has_peer(rt))
126 return rt6_peer_ptr(rt);
127
128 rt6_bind_peer(rt, create);
129 return (rt6_has_peer(rt) ? rt6_peer_ptr(rt) : NULL);
130}
131
132static struct inet_peer *rt6_get_peer_create(struct rt6_info *rt)
133{
134 return __rt6_get_peer(rt, 1);
135}
136
David S. Miller06582542011-01-27 14:58:42 -0800137static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
138{
139 struct rt6_info *rt = (struct rt6_info *) dst;
140 struct inet_peer *peer;
141 u32 *p = NULL;
142
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000143 if (!(rt->dst.flags & DST_HOST))
144 return NULL;
145
David S. Millerfbfe95a2012-06-08 23:24:18 -0700146 peer = rt6_get_peer_create(rt);
David S. Miller06582542011-01-27 14:58:42 -0800147 if (peer) {
148 u32 *old_p = __DST_METRICS_PTR(old);
149 unsigned long prev, new;
150
151 p = peer->metrics;
152 if (inet_metrics_new(peer))
153 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
154
155 new = (unsigned long) p;
156 prev = cmpxchg(&dst->_metrics, old, new);
157
158 if (prev != old) {
159 p = __DST_METRICS_PTR(prev);
160 if (prev & DST_METRICS_READ_ONLY)
161 p = NULL;
162 }
163 }
164 return p;
165}
166
David S. Millerf894cbf2012-07-02 21:52:24 -0700167static inline const void *choose_neigh_daddr(struct rt6_info *rt,
168 struct sk_buff *skb,
169 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500170{
171 struct in6_addr *p = &rt->rt6i_gateway;
172
David S. Millera7563f32012-01-26 16:29:16 -0500173 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500174 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700175 else if (skb)
176 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500177 return daddr;
178}
179
David S. Millerf894cbf2012-07-02 21:52:24 -0700180static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
181 struct sk_buff *skb,
182 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700183{
David S. Miller39232972012-01-26 15:22:32 -0500184 struct rt6_info *rt = (struct rt6_info *) dst;
185 struct neighbour *n;
186
David S. Millerf894cbf2012-07-02 21:52:24 -0700187 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000188 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500189 if (n)
190 return n;
191 return neigh_create(&nd_tbl, daddr, dst->dev);
192}
193
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800194static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800196 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .gc = ip6_dst_gc,
198 .gc_thresh = 1024,
199 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800200 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000201 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800202 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .destroy = ip6_dst_destroy,
204 .ifdown = ip6_dst_ifdown,
205 .negative_advice = ip6_negative_advice,
206 .link_failure = ip6_link_failure,
207 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700208 .redirect = rt6_do_redirect,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700209 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700210 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Steffen Klassertebb762f2011-11-23 02:12:51 +0000213static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800214{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000215 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
216
217 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800218}
219
David S. Miller6700c272012-07-17 03:29:28 -0700220static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
221 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700222{
223}
224
David S. Miller6700c272012-07-17 03:29:28 -0700225static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
226 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700227{
228}
229
Held Bernhard0972ddb2011-04-24 22:07:32 +0000230static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
231 unsigned long old)
232{
233 return NULL;
234}
235
David S. Miller14e50e52007-05-24 18:17:54 -0700236static struct dst_ops ip6_dst_blackhole_ops = {
237 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800238 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700239 .destroy = ip6_dst_destroy,
240 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000241 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800242 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700243 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700244 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000245 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700246 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700247};
248
David S. Miller62fa8a82011-01-26 20:51:05 -0800249static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800250 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800251};
252
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000253static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700254 .dst = {
255 .__refcnt = ATOMIC_INIT(1),
256 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000257 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700258 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700259 .input = ip6_pkt_discard,
260 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 },
262 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700263 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .rt6i_metric = ~(u32) 0,
265 .rt6i_ref = ATOMIC_INIT(1),
266};
267
Thomas Graf101367c2006-08-04 03:39:02 -0700268#ifdef CONFIG_IPV6_MULTIPLE_TABLES
269
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000270static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700271 .dst = {
272 .__refcnt = ATOMIC_INIT(1),
273 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000274 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700275 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700276 .input = ip6_pkt_prohibit,
277 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700278 },
279 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700280 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700281 .rt6i_metric = ~(u32) 0,
282 .rt6i_ref = ATOMIC_INIT(1),
283};
284
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000285static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700286 .dst = {
287 .__refcnt = ATOMIC_INIT(1),
288 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000289 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700290 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700291 .input = dst_discard,
292 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700293 },
294 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700295 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700296 .rt6i_metric = ~(u32) 0,
297 .rt6i_ref = ATOMIC_INIT(1),
298};
299
300#endif
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* allocate dst with ip6_dst_ops */
David S. Miller97bab732012-06-09 22:36:36 -0700303static inline struct rt6_info *ip6_dst_alloc(struct net *net,
David S. Miller957c6652011-06-24 15:25:00 -0700304 struct net_device *dev,
David S. Miller8b96d222012-06-11 02:01:56 -0700305 int flags,
306 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David S. Miller97bab732012-06-09 22:36:36 -0700308 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000309 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700310
David S. Miller97bab732012-06-09 22:36:36 -0700311 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000312 struct dst_entry *dst = &rt->dst;
313
314 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
David S. Miller8b96d222012-06-11 02:01:56 -0700315 rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
fan.duca4c3fc2013-07-30 08:33:53 +0800316 rt->rt6i_genid = rt_genid_ipv6(net);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000317 INIT_LIST_HEAD(&rt->rt6i_siblings);
David S. Miller97bab732012-06-09 22:36:36 -0700318 }
David S. Millercf911662011-04-28 14:31:47 -0700319 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322static void ip6_dst_destroy(struct dst_entry *dst)
323{
324 struct rt6_info *rt = (struct rt6_info *)dst;
325 struct inet6_dev *idev = rt->rt6i_idev;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000326 struct dst_entry *from = dst->from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000328 if (!(rt->dst.flags & DST_HOST))
329 dst_destroy_metrics_generic(dst);
330
David S. Miller38308472011-12-03 18:02:47 -0500331 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 rt->rt6i_idev = NULL;
333 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900334 }
Gao feng1716a962012-04-06 00:13:10 +0000335
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000336 dst->from = NULL;
337 dst_release(from);
Gao feng1716a962012-04-06 00:13:10 +0000338
David S. Miller97bab732012-06-09 22:36:36 -0700339 if (rt6_has_peer(rt)) {
340 struct inet_peer *peer = rt6_peer_ptr(rt);
David S. Millerb3419362010-11-30 12:27:11 -0800341 inet_putpeer(peer);
342 }
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
346 int how)
347{
348 struct rt6_info *rt = (struct rt6_info *)dst;
349 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800350 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900351 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David S. Miller97cac082012-07-02 22:43:47 -0700353 if (dev != loopback_dev) {
354 if (idev && idev->dev == dev) {
355 struct inet6_dev *loopback_idev =
356 in6_dev_get(loopback_dev);
357 if (loopback_idev) {
358 rt->rt6i_idev = loopback_idev;
359 in6_dev_put(idev);
360 }
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363}
364
Eric Dumazeta50feda2012-05-18 18:57:34 +0000365static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Gao feng1716a962012-04-06 00:13:10 +0000367 if (rt->rt6i_flags & RTF_EXPIRES) {
368 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000369 return true;
Gao feng1716a962012-04-06 00:13:10 +0000370 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000371 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000372 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000373 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Eric Dumazeta50feda2012-05-18 18:57:34 +0000376static bool rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700377{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000378 return ipv6_addr_type(daddr) &
379 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700380}
381
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000382/* Multipath route selection:
383 * Hash based function using packet header and flowlabel.
384 * Adapted from fib_info_hashfn()
385 */
386static int rt6_info_hash_nhsfn(unsigned int candidate_count,
387 const struct flowi6 *fl6)
388{
389 unsigned int val = fl6->flowi6_proto;
390
YOSHIFUJI Hideaki / 吉藤英明c08977b2013-01-13 05:02:29 +0000391 val ^= ipv6_addr_hash(&fl6->daddr);
392 val ^= ipv6_addr_hash(&fl6->saddr);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000393
394 /* Work only if this not encapsulated */
395 switch (fl6->flowi6_proto) {
396 case IPPROTO_UDP:
397 case IPPROTO_TCP:
398 case IPPROTO_SCTP:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000399 val ^= (__force u16)fl6->fl6_sport;
400 val ^= (__force u16)fl6->fl6_dport;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000401 break;
402
403 case IPPROTO_ICMPV6:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000404 val ^= (__force u16)fl6->fl6_icmp_type;
405 val ^= (__force u16)fl6->fl6_icmp_code;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000406 break;
407 }
408 /* RFC6438 recommands to use flowlabel */
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000409 val ^= (__force u32)fl6->flowlabel;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000410
411 /* Perhaps, we need to tune, this function? */
412 val = val ^ (val >> 7) ^ (val >> 12);
413 return val % candidate_count;
414}
415
416static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200417 struct flowi6 *fl6, int oif,
418 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000419{
420 struct rt6_info *sibling, *next_sibling;
421 int route_choosen;
422
423 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
424 /* Don't change the route, if route_choosen == 0
425 * (siblings does not include ourself)
426 */
427 if (route_choosen)
428 list_for_each_entry_safe(sibling, next_sibling,
429 &match->rt6i_siblings, rt6i_siblings) {
430 route_choosen--;
431 if (route_choosen == 0) {
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200432 if (rt6_score_route(sibling, oif, strict) < 0)
433 break;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000434 match = sibling;
435 break;
436 }
437 }
438 return match;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700442 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
444
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800445static inline struct rt6_info *rt6_device_match(struct net *net,
446 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000447 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700449 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct rt6_info *local = NULL;
452 struct rt6_info *sprt;
453
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900454 if (!oif && ipv6_addr_any(saddr))
455 goto out;
456
Changli Gaod8d1f302010-06-10 23:31:35 -0700457 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500458 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900459
460 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (dev->ifindex == oif)
462 return sprt;
463 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500464 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700466 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900468 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 local->rt6i_idev->dev->ifindex == oif))
470 continue;
471 }
472 local = sprt;
473 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900474 } else {
475 if (ipv6_chk_addr(net, saddr, dev,
476 flags & RT6_LOOKUP_F_IFACE))
477 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900481 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (local)
483 return local;
484
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700485 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800486 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return rt;
490}
491
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800492#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200493struct __rt6_probe_work {
494 struct work_struct work;
495 struct in6_addr target;
496 struct net_device *dev;
497};
498
499static void rt6_probe_deferred(struct work_struct *w)
500{
501 struct in6_addr mcaddr;
502 struct __rt6_probe_work *work =
503 container_of(w, struct __rt6_probe_work, work);
504
505 addrconf_addr_solict_mult(&work->target, &mcaddr);
506 ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
507 dev_put(work->dev);
508 kfree(w);
509}
510
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800511static void rt6_probe(struct rt6_info *rt)
512{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000513 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800514 /*
515 * Okay, this does not seem to be appropriate
516 * for now, however, we need to check if it
517 * is really so; aka Router Reachability Probing.
518 *
519 * Router Reachability Probe MUST be rate-limited
520 * to no more than one per minute.
521 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000522 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000523 return;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000524 rcu_read_lock_bh();
525 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
526 if (neigh) {
527 write_lock(&neigh->lock);
528 if (neigh->nud_state & NUD_VALID)
529 goto out;
YOSHIFUJI Hideaki / 吉藤英明7ff74a52013-01-17 12:53:02 +0000530 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000531
532 if (!neigh ||
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800533 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200534 struct __rt6_probe_work *work;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800535
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200536 work = kmalloc(sizeof(*work), GFP_ATOMIC);
537
538 if (neigh && work)
Jiri Benc7e980562013-12-11 13:48:20 +0100539 __neigh_set_probe_once(neigh);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000540
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200541 if (neigh)
542 write_unlock(&neigh->lock);
543
544 if (work) {
545 INIT_WORK(&work->work, rt6_probe_deferred);
546 work->target = rt->rt6i_gateway;
547 dev_hold(rt->dst.dev);
548 work->dev = rt->dst.dev;
549 schedule_work(&work->work);
550 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000551 } else {
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000552out:
553 write_unlock(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000554 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000555 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800556}
557#else
558static inline void rt6_probe(struct rt6_info *rt)
559{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800560}
561#endif
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800564 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700566static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
David S. Millerd1918542011-12-28 20:19:20 -0500568 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700569 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800570 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700571 if ((dev->flags & IFF_LOOPBACK) &&
572 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
573 return 1;
574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200577static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000579 struct neighbour *neigh;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200580 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000581
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700582 if (rt->rt6i_flags & RTF_NONEXTHOP ||
583 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200584 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000585
586 rcu_read_lock_bh();
587 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
588 if (neigh) {
589 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800590 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200591 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800592#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000593 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200594 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100595 else
596 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800597#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000598 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200599 } else {
600 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100601 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000602 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000603 rcu_read_unlock_bh();
604
Paul Marksa5a81f02012-12-03 10:26:54 +0000605 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800606}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800608static int rt6_score_route(struct rt6_info *rt, int oif,
609 int strict)
610{
Paul Marksa5a81f02012-12-03 10:26:54 +0000611 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900612
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700613 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700614 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200615 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800616#ifdef CONFIG_IPV6_ROUTER_PREF
617 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
618#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200619 if (strict & RT6_LOOKUP_F_REACHABLE) {
620 int n = rt6_check_neigh(rt);
621 if (n < 0)
622 return n;
623 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800624 return m;
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David S. Millerf11e6652007-03-24 20:36:25 -0700627static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200628 int *mpri, struct rt6_info *match,
629 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800630{
David S. Millerf11e6652007-03-24 20:36:25 -0700631 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200632 bool match_do_rr = false;
David S. Millerf11e6652007-03-24 20:36:25 -0700633
634 if (rt6_check_expired(rt))
635 goto out;
636
637 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100638 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200639 match_do_rr = true;
640 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100641 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700642 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700643 }
644
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200645 if (strict & RT6_LOOKUP_F_REACHABLE)
646 rt6_probe(rt);
647
Jiri Benc7e980562013-12-11 13:48:20 +0100648 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200649 if (m > *mpri) {
650 *do_rr = match_do_rr;
651 *mpri = m;
652 match = rt;
653 }
David S. Millerf11e6652007-03-24 20:36:25 -0700654out:
655 return match;
656}
657
658static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
659 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200660 u32 metric, int oif, int strict,
661 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700662{
663 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800664 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David S. Millerf11e6652007-03-24 20:36:25 -0700666 match = NULL;
667 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700668 rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200669 match = find_match(rt, oif, strict, &mpri, match, do_rr);
David S. Millerf11e6652007-03-24 20:36:25 -0700670 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700671 rt = rt->dst.rt6_next)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200672 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800673
David S. Millerf11e6652007-03-24 20:36:25 -0700674 return match;
675}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800676
David S. Millerf11e6652007-03-24 20:36:25 -0700677static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
678{
679 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800680 struct net *net;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200681 bool do_rr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
David S. Millerf11e6652007-03-24 20:36:25 -0700683 rt0 = fn->rr_ptr;
684 if (!rt0)
685 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200687 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
688 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200690 if (do_rr) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700691 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700692
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800693 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700694 if (!next || next->rt6i_metric != rt0->rt6i_metric)
695 next = fn->leaf;
696
697 if (next != rt0)
698 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
David S. Millerd1918542011-12-28 20:19:20 -0500701 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000702 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800705#ifdef CONFIG_IPV6_ROUTE_INFO
706int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000707 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800708{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900709 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800710 struct route_info *rinfo = (struct route_info *) opt;
711 struct in6_addr prefix_buf, *prefix;
712 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900713 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800714 struct rt6_info *rt;
715
716 if (len < sizeof(struct route_info)) {
717 return -EINVAL;
718 }
719
720 /* Sanity check for prefix_len and length */
721 if (rinfo->length > 3) {
722 return -EINVAL;
723 } else if (rinfo->prefix_len > 128) {
724 return -EINVAL;
725 } else if (rinfo->prefix_len > 64) {
726 if (rinfo->length < 2) {
727 return -EINVAL;
728 }
729 } else if (rinfo->prefix_len > 0) {
730 if (rinfo->length < 1) {
731 return -EINVAL;
732 }
733 }
734
735 pref = rinfo->route_pref;
736 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000737 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800738
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900739 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800740
741 if (rinfo->length == 3)
742 prefix = (struct in6_addr *)rinfo->prefix;
743 else {
744 /* this function is safe */
745 ipv6_addr_prefix(&prefix_buf,
746 (struct in6_addr *)rinfo->prefix,
747 rinfo->prefix_len);
748 prefix = &prefix_buf;
749 }
750
Duan Jiongf104a562013-11-08 09:56:53 +0800751 if (rinfo->prefix_len == 0)
752 rt = rt6_get_dflt_router(gwaddr, dev);
753 else
754 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
755 gwaddr, dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800756
757 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700758 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800759 rt = NULL;
760 }
761
762 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800763 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800764 pref);
765 else if (rt)
766 rt->rt6i_flags = RTF_ROUTEINFO |
767 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
768
769 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000770 if (!addrconf_finite_timeout(lifetime))
771 rt6_clean_expires(rt);
772 else
773 rt6_set_expires(rt, jiffies + HZ * lifetime);
774
Amerigo Wang94e187c2012-10-29 00:13:19 +0000775 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800776 }
777 return 0;
778}
779#endif
780
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800781#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700782do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800783 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700784 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700785 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700786 if (fn->fn_flags & RTN_TL_ROOT) \
787 goto out; \
788 pn = fn->parent; \
789 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800790 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700791 else \
792 fn = pn; \
793 if (fn->fn_flags & RTN_RTINFO) \
794 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700795 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700796 } \
David S. Miller38308472011-12-03 18:02:47 -0500797} while (0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700798
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800799static struct rt6_info *ip6_pol_route_lookup(struct net *net,
800 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500801 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct fib6_node *fn;
804 struct rt6_info *rt;
805
Thomas Grafc71099a2006-08-04 23:20:06 -0700806 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500807 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700808restart:
809 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500810 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000811 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200812 rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
David S. Miller4c9483b2011-03-12 16:22:43 -0500813 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700814out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700815 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700816 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700817 return rt;
818
819}
820
Florian Westphalea6e5742011-09-05 16:05:44 +0200821struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
822 int flags)
823{
824 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
825}
826EXPORT_SYMBOL_GPL(ip6_route_lookup);
827
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900828struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
829 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700830{
David S. Miller4c9483b2011-03-12 16:22:43 -0500831 struct flowi6 fl6 = {
832 .flowi6_oif = oif,
833 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700834 };
835 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700836 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700837
Thomas Grafadaa70b2006-10-13 15:01:03 -0700838 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500839 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700840 flags |= RT6_LOOKUP_F_HAS_SADDR;
841 }
842
David S. Miller4c9483b2011-03-12 16:22:43 -0500843 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700844 if (dst->error == 0)
845 return (struct rt6_info *) dst;
846
847 dst_release(dst);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return NULL;
850}
851
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900852EXPORT_SYMBOL(rt6_lookup);
853
Thomas Grafc71099a2006-08-04 23:20:06 -0700854/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 It takes new route entry, the addition fails by any reason the
856 route is freed. In any case, if caller does not hold it, it may
857 be destroyed.
858 */
859
Thomas Graf86872cb2006-08-22 00:01:08 -0700860static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700863 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Thomas Grafc71099a2006-08-04 23:20:06 -0700865 table = rt->rt6i_table;
866 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700867 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700868 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 return err;
871}
872
Thomas Graf40e22e82006-08-22 00:00:45 -0700873int ip6_ins_rt(struct rt6_info *rt)
874{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800875 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -0500876 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800877 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800878 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700879}
880
Gao feng1716a962012-04-06 00:13:10 +0000881static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000882 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000883 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct rt6_info *rt;
886
887 /*
888 * Clone the route.
889 */
890
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000891 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (rt) {
Duan Jiong249a3632013-11-05 13:34:53 +0800894 if (ort->rt6i_dst.plen != 128 &&
895 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
896 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900#ifdef CONFIG_IPV6_SUBTREES
901 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000902 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 rt->rt6i_src.plen = 128;
904 }
905#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800908 return rt;
909}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000911static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
912 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800913{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000914 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
915
YOSHIFUJI Hideaki / 吉藤英明887c95c2013-01-17 12:54:05 +0000916 if (rt)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800917 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800918 return rt;
919}
920
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800921static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500922 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800925 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700926 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800928 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700929 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700931 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700934 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800936restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500937 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700940 rt = rt6_select(fn, oif, strict | reachable);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200941 if (rt->rt6i_nsiblings)
942 rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
David S. Miller4c9483b2011-03-12 16:22:43 -0500943 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800944 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800945 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Changli Gaod8d1f302010-06-10 23:31:35 -0700948 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700949 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800950
YOSHIFUJI Hideaki / 吉藤英明c440f162013-01-17 12:53:32 +0000951 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
David S. Miller4c9483b2011-03-12 16:22:43 -0500952 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800953 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500954 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800955 else
956 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800957
Amerigo Wang94e187c2012-10-29 00:13:19 +0000958 ip6_rt_put(rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800959 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800960
Changli Gaod8d1f302010-06-10 23:31:35 -0700961 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800962 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700963 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800964 if (!err)
965 goto out2;
966 }
967
968 if (--attempts <= 0)
969 goto out2;
970
971 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700972 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800973 * released someone could insert this route. Relookup.
974 */
Amerigo Wang94e187c2012-10-29 00:13:19 +0000975 ip6_rt_put(rt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800976 goto relookup;
977
978out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800979 if (reachable) {
980 reachable = 0;
981 goto restart_2;
982 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700983 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700984 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700986 rt->dst.lastuse = jiffies;
987 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700988
989 return rt;
990}
991
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800992static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500993 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700994{
David S. Miller4c9483b2011-03-12 16:22:43 -0500995 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700996}
997
Shmulik Ladkani72331bc2012-04-01 04:03:45 +0000998static struct dst_entry *ip6_route_input_lookup(struct net *net,
999 struct net_device *dev,
1000 struct flowi6 *fl6, int flags)
1001{
1002 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1003 flags |= RT6_LOOKUP_F_IFACE;
1004
1005 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1006}
1007
Thomas Grafc71099a2006-08-04 23:20:06 -07001008void ip6_route_input(struct sk_buff *skb)
1009{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001010 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001011 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001012 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -05001013 struct flowi6 fl6 = {
1014 .flowi6_iif = skb->dev->ifindex,
1015 .daddr = iph->daddr,
1016 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001017 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05001018 .flowi6_mark = skb->mark,
1019 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001020 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001021
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001022 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001023}
1024
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001025static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001026 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001027{
David S. Miller4c9483b2011-03-12 16:22:43 -05001028 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001029}
1030
Florian Westphal9c7a4f92011-03-22 19:17:36 -07001031struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001032 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001033{
1034 int flags = 0;
1035
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001036 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001037
David S. Miller4c9483b2011-03-12 16:22:43 -05001038 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001039 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001040
David S. Miller4c9483b2011-03-12 16:22:43 -05001041 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001042 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001043 else if (sk)
1044 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001045
David S. Miller4c9483b2011-03-12 16:22:43 -05001046 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001049EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
David S. Miller2774c132011-03-01 14:59:04 -08001051struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001052{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001053 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001054 struct dst_entry *new = NULL;
1055
David S. Millerf5b0a872012-07-19 12:31:33 -07001056 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001057 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001058 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001059
Steffen Klassert81048912012-07-05 23:37:09 +00001060 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
1061 rt6_init_peer(rt, net->ipv6.peers);
1062
David S. Miller14e50e52007-05-24 18:17:54 -07001063 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001064 new->input = dst_discard;
1065 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -07001066
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001067 if (dst_metrics_read_only(&ort->dst))
1068 new->_metrics = ort->dst._metrics;
1069 else
1070 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001071 rt->rt6i_idev = ort->rt6i_idev;
1072 if (rt->rt6i_idev)
1073 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001074
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001075 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001076 rt->rt6i_flags = ort->rt6i_flags;
David S. Miller14e50e52007-05-24 18:17:54 -07001077 rt->rt6i_metric = 0;
1078
1079 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1080#ifdef CONFIG_IPV6_SUBTREES
1081 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1082#endif
1083
1084 dst_free(new);
1085 }
1086
David S. Miller69ead7a2011-03-01 14:45:33 -08001087 dst_release(dst_orig);
1088 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001089}
David S. Miller14e50e52007-05-24 18:17:54 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/*
1092 * Destination cache support functions
1093 */
1094
1095static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1096{
1097 struct rt6_info *rt;
1098
1099 rt = (struct rt6_info *) dst;
1100
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001101 /* All IPV6 dsts are created with ->obsolete set to the value
1102 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1103 * into this function always.
1104 */
fan.duca4c3fc2013-07-30 08:33:53 +08001105 if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev)))
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001106 return NULL;
1107
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001108 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1109 return NULL;
Li RongQinga4477c42012-11-07 21:56:33 +00001110
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02001111 if (rt6_check_expired(rt))
1112 return NULL;
1113
1114 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{
1156 struct rt6_info *rt6 = (struct rt6_info*)dst;
1157
David S. Miller81aded22012-06-15 14:54:11 -07001158 dst_confirm(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
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;
1163 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -08001164 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -08001166 features |= RTAX_FEATURE_ALLFRAG;
1167 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
David S. Millerdefb3512010-12-08 21:16:57 -08001169 dst_metric_set(dst, RTAX_MTU, mtu);
David S. Miller81aded22012-06-15 14:54:11 -07001170 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
1173
David S. Miller42ae66c2012-06-15 20:01:57 -07001174void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1175 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001176{
1177 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1178 struct dst_entry *dst;
1179 struct flowi6 fl6;
1180
1181 memset(&fl6, 0, sizeof(fl6));
1182 fl6.flowi6_oif = oif;
1183 fl6.flowi6_mark = mark;
David S. Miller81aded22012-06-15 14:54:11 -07001184 fl6.daddr = iph->daddr;
1185 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001186 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller81aded22012-06-15 14:54:11 -07001187
1188 dst = ip6_route_output(net, NULL, &fl6);
1189 if (!dst->error)
David S. Miller6700c272012-07-17 03:29:28 -07001190 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001191 dst_release(dst);
1192}
1193EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1194
1195void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1196{
1197 ip6_update_pmtu(skb, sock_net(sk), mtu,
1198 sk->sk_bound_dev_if, sk->sk_mark);
1199}
1200EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1201
Duan Jiongb55b76b2013-09-04 19:44:21 +08001202/* Handle redirects */
1203struct ip6rd_flowi {
1204 struct flowi6 fl6;
1205 struct in6_addr gateway;
1206};
1207
1208static struct rt6_info *__ip6_route_redirect(struct net *net,
1209 struct fib6_table *table,
1210 struct flowi6 *fl6,
1211 int flags)
1212{
1213 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1214 struct rt6_info *rt;
1215 struct fib6_node *fn;
1216
1217 /* Get the "current" route for this destination and
1218 * check if the redirect has come from approriate router.
1219 *
1220 * RFC 4861 specifies that redirects should only be
1221 * accepted if they come from the nexthop to the target.
1222 * Due to the way the routes are chosen, this notion
1223 * is a bit fuzzy and one might need to check all possible
1224 * routes.
1225 */
1226
1227 read_lock_bh(&table->tb6_lock);
1228 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1229restart:
1230 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1231 if (rt6_check_expired(rt))
1232 continue;
1233 if (rt->dst.error)
1234 break;
1235 if (!(rt->rt6i_flags & RTF_GATEWAY))
1236 continue;
1237 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1238 continue;
1239 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1240 continue;
1241 break;
1242 }
1243
1244 if (!rt)
1245 rt = net->ipv6.ip6_null_entry;
1246 else if (rt->dst.error) {
1247 rt = net->ipv6.ip6_null_entry;
1248 goto out;
1249 }
1250 BACKTRACK(net, &fl6->saddr);
1251out:
1252 dst_hold(&rt->dst);
1253
1254 read_unlock_bh(&table->tb6_lock);
1255
1256 return rt;
1257};
1258
1259static struct dst_entry *ip6_route_redirect(struct net *net,
1260 const struct flowi6 *fl6,
1261 const struct in6_addr *gateway)
1262{
1263 int flags = RT6_LOOKUP_F_HAS_SADDR;
1264 struct ip6rd_flowi rdfl;
1265
1266 rdfl.fl6 = *fl6;
1267 rdfl.gateway = *gateway;
1268
1269 return fib6_rule_lookup(net, &rdfl.fl6,
1270 flags, __ip6_route_redirect);
1271}
1272
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001273void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1274{
1275 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1276 struct dst_entry *dst;
1277 struct flowi6 fl6;
1278
1279 memset(&fl6, 0, sizeof(fl6));
1280 fl6.flowi6_oif = oif;
1281 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001282 fl6.daddr = iph->daddr;
1283 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00001284 fl6.flowlabel = ip6_flowinfo(iph);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001285
Duan Jiongb55b76b2013-09-04 19:44:21 +08001286 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
1287 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001288 dst_release(dst);
1289}
1290EXPORT_SYMBOL_GPL(ip6_redirect);
1291
Duan Jiongc92a59e2013-08-22 12:07:35 +08001292void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1293 u32 mark)
1294{
1295 const struct ipv6hdr *iph = ipv6_hdr(skb);
1296 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1297 struct dst_entry *dst;
1298 struct flowi6 fl6;
1299
1300 memset(&fl6, 0, sizeof(fl6));
1301 fl6.flowi6_oif = oif;
1302 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001303 fl6.daddr = msg->dest;
1304 fl6.saddr = iph->daddr;
1305
Duan Jiongb55b76b2013-09-04 19:44:21 +08001306 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1307 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08001308 dst_release(dst);
1309}
1310
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001311void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1312{
1313 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1314}
1315EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1316
David S. Miller0dbaee32010-12-13 12:52:14 -08001317static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
David S. Miller0dbaee32010-12-13 12:52:14 -08001319 struct net_device *dev = dst->dev;
1320 unsigned int mtu = dst_mtu(dst);
1321 struct net *net = dev_net(dev);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1324
Daniel Lezcano55786892008-03-04 13:47:47 -08001325 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1326 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001329 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1330 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1331 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * rely only on pmtu discovery"
1333 */
1334 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1335 mtu = IPV6_MAXPLEN;
1336 return mtu;
1337}
1338
Steffen Klassertebb762f2011-11-23 02:12:51 +00001339static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001340{
David S. Millerd33e4552010-12-14 13:01:14 -08001341 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001342 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1343
1344 if (mtu)
1345 return mtu;
1346
1347 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001348
1349 rcu_read_lock();
1350 idev = __in6_dev_get(dst->dev);
1351 if (idev)
1352 mtu = idev->cnf.mtu6;
1353 rcu_read_unlock();
1354
1355 return mtu;
1356}
1357
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001358static struct dst_entry *icmp6_dst_gc_list;
1359static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001360
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001361struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05001362 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
David S. Miller87a11572011-12-06 17:04:13 -05001364 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct rt6_info *rt;
1366 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001367 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
David S. Miller38308472011-12-03 18:02:47 -05001369 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001370 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
David S. Miller8b96d222012-06-11 02:01:56 -07001372 rt = ip6_dst_alloc(net, dev, 0, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001373 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001375 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto out;
1377 }
1378
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001379 rt->dst.flags |= DST_HOST;
1380 rt->dst.output = ip6_output;
Changli Gaod8d1f302010-06-10 23:31:35 -07001381 atomic_set(&rt->dst.__refcnt, 1);
Julian Anastasov550bab42013-10-20 15:43:04 +03001382 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05001383 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001384 rt->rt6i_dst.plen = 128;
1385 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08001386 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001388 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001389 rt->dst.next = icmp6_dst_gc_list;
1390 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001391 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Daniel Lezcano55786892008-03-04 13:47:47 -08001393 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David S. Miller87a11572011-12-06 17:04:13 -05001395 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397out:
David S. Miller87a11572011-12-06 17:04:13 -05001398 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001401int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001403 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001404 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001406 spin_lock_bh(&icmp6_dst_lock);
1407 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 while ((dst = *pprev) != NULL) {
1410 if (!atomic_read(&dst->__refcnt)) {
1411 *pprev = dst->next;
1412 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 } else {
1414 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001415 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 }
1418
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001419 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001420
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001421 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
David S. Miller1e493d12008-09-10 17:27:15 -07001424static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1425 void *arg)
1426{
1427 struct dst_entry *dst, **pprev;
1428
1429 spin_lock_bh(&icmp6_dst_lock);
1430 pprev = &icmp6_dst_gc_list;
1431 while ((dst = *pprev) != NULL) {
1432 struct rt6_info *rt = (struct rt6_info *) dst;
1433 if (func(rt, arg)) {
1434 *pprev = dst->next;
1435 dst_free(dst);
1436 } else {
1437 pprev = &dst->next;
1438 }
1439 }
1440 spin_unlock_bh(&icmp6_dst_lock);
1441}
1442
Daniel Lezcano569d3642008-01-18 03:56:57 -08001443static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001445 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001446 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1447 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1448 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1449 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1450 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001451 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Dumazetfc66f952010-10-08 06:37:34 +00001453 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001454 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001455 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto out;
1457
Benjamin Thery6891a342008-03-04 13:49:47 -08001458 net->ipv6.ip6_rt_gc_expire++;
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001459 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, entries > rt_max_size);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001460 entries = dst_entries_get_slow(ops);
1461 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001462 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001464 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001465 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468/*
1469 *
1470 */
1471
Thomas Graf86872cb2006-08-22 00:01:08 -07001472int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001475 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct rt6_info *rt = NULL;
1477 struct net_device *dev = NULL;
1478 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001479 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int addr_type;
1481
Thomas Graf86872cb2006-08-22 00:01:08 -07001482 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
1484#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001485 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EINVAL;
1487#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001488 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001490 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (!dev)
1492 goto out;
1493 idev = in6_dev_get(dev);
1494 if (!idev)
1495 goto out;
1496 }
1497
Thomas Graf86872cb2006-08-22 00:01:08 -07001498 if (cfg->fc_metric == 0)
1499 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Matti Vaittinend71314b2011-11-14 00:14:49 +00001501 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001502 if (cfg->fc_nlinfo.nlh &&
1503 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001504 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001505 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001506 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001507 table = fib6_new_table(net, cfg->fc_table);
1508 }
1509 } else {
1510 table = fib6_new_table(net, cfg->fc_table);
1511 }
David S. Miller38308472011-12-03 18:02:47 -05001512
1513 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001514 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001515
David S. Miller8b96d222012-06-11 02:01:56 -07001516 rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
David S. Miller38308472011-12-03 18:02:47 -05001518 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 err = -ENOMEM;
1520 goto out;
1521 }
1522
Gao feng1716a962012-04-06 00:13:10 +00001523 if (cfg->fc_flags & RTF_EXPIRES)
1524 rt6_set_expires(rt, jiffies +
1525 clock_t_to_jiffies(cfg->fc_expires));
1526 else
1527 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Thomas Graf86872cb2006-08-22 00:01:08 -07001529 if (cfg->fc_protocol == RTPROT_UNSPEC)
1530 cfg->fc_protocol = RTPROT_BOOT;
1531 rt->rt6i_protocol = cfg->fc_protocol;
1532
1533 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001536 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001537 else if (cfg->fc_flags & RTF_LOCAL)
1538 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001540 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Changli Gaod8d1f302010-06-10 23:31:35 -07001542 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Thomas Graf86872cb2006-08-22 00:01:08 -07001544 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1545 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001547 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001549 if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1550 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1551 if (!metrics) {
1552 err = -ENOMEM;
1553 goto out;
1554 }
1555 dst_init_metrics(&rt->dst, metrics, 0);
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001558 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1559 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560#endif
1561
Thomas Graf86872cb2006-08-22 00:01:08 -07001562 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /* We cannot add true routes via loopback here,
1565 they would result in kernel looping; promote them to reject routes
1566 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001567 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001568 (dev && (dev->flags & IFF_LOOPBACK) &&
1569 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1570 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001572 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (dev) {
1574 dev_put(dev);
1575 in6_dev_put(idev);
1576 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001577 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 dev_hold(dev);
1579 idev = in6_dev_get(dev);
1580 if (!idev) {
1581 err = -ENODEV;
1582 goto out;
1583 }
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001586 switch (cfg->fc_type) {
1587 case RTN_BLACKHOLE:
1588 rt->dst.error = -EINVAL;
Kamala R7150aed2013-12-02 19:55:21 +05301589 rt->dst.output = dst_discard;
1590 rt->dst.input = dst_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001591 break;
1592 case RTN_PROHIBIT:
1593 rt->dst.error = -EACCES;
Kamala R7150aed2013-12-02 19:55:21 +05301594 rt->dst.output = ip6_pkt_prohibit_out;
1595 rt->dst.input = ip6_pkt_prohibit;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001596 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001597 case RTN_THROW:
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001598 default:
Kamala R7150aed2013-12-02 19:55:21 +05301599 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
1600 : -ENETUNREACH;
1601 rt->dst.output = ip6_pkt_discard_out;
1602 rt->dst.input = ip6_pkt_discard;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001603 break;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 goto install_route;
1606 }
1607
Thomas Graf86872cb2006-08-22 00:01:08 -07001608 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001609 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int gwa_type;
1611
Thomas Graf86872cb2006-08-22 00:01:08 -07001612 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001613 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 gwa_type = ipv6_addr_type(gw_addr);
1615
1616 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1617 struct rt6_info *grt;
1618
1619 /* IPv6 strictly inhibits using not link-local
1620 addresses as nexthop address.
1621 Otherwise, router will not able to send redirects.
1622 It is very good, but in some (rare!) circumstances
1623 (SIT, PtP, NBMA NOARP links) it is handy to allow
1624 some exceptions. --ANK
1625 */
1626 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001627 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out;
1629
Daniel Lezcano55786892008-03-04 13:47:47 -08001630 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001633 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 goto out;
1635 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001636 if (dev != grt->dst.dev) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00001637 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out;
1639 }
1640 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001641 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 idev = grt->rt6i_idev;
1643 dev_hold(dev);
1644 in6_dev_hold(grt->rt6i_idev);
1645 }
David S. Miller38308472011-12-03 18:02:47 -05001646 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 err = 0;
Amerigo Wang94e187c2012-10-29 00:13:19 +00001648 ip6_rt_put(grt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (err)
1651 goto out;
1652 }
1653 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001654 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto out;
1656 }
1657
1658 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001659 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto out;
1661
Daniel Walterc3968a82011-04-13 21:10:57 +00001662 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1663 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1664 err = -EINVAL;
1665 goto out;
1666 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001667 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001668 rt->rt6i_prefsrc.plen = 128;
1669 } else
1670 rt->rt6i_prefsrc.plen = 0;
1671
Thomas Graf86872cb2006-08-22 00:01:08 -07001672 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001675 if (cfg->fc_mx) {
1676 struct nlattr *nla;
1677 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Thomas Graf86872cb2006-08-22 00:01:08 -07001679 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001680 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001681
1682 if (type) {
1683 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 err = -EINVAL;
1685 goto out;
1686 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001687
David S. Millerdefb3512010-12-08 21:16:57 -08001688 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 }
1692
Changli Gaod8d1f302010-06-10 23:31:35 -07001693 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001695 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001696
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001697 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001698
Thomas Graf86872cb2006-08-22 00:01:08 -07001699 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701out:
1702 if (dev)
1703 dev_put(dev);
1704 if (idev)
1705 in6_dev_put(idev);
1706 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001707 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return err;
1709}
1710
Thomas Graf86872cb2006-08-22 00:01:08 -07001711static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001714 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001715 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Gao feng6825a262012-09-19 19:25:34 +00001717 if (rt == net->ipv6.ip6_null_entry) {
1718 err = -ENOENT;
1719 goto out;
1720 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07001721
Thomas Grafc71099a2006-08-04 23:20:06 -07001722 table = rt->rt6i_table;
1723 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07001724 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07001725 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Gao feng6825a262012-09-19 19:25:34 +00001727out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001728 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return err;
1730}
1731
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001732int ip6_del_rt(struct rt6_info *rt)
1733{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001734 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001735 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001736 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001737 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001738}
1739
Thomas Graf86872cb2006-08-22 00:01:08 -07001740static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Thomas Grafc71099a2006-08-04 23:20:06 -07001742 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct fib6_node *fn;
1744 struct rt6_info *rt;
1745 int err = -ESRCH;
1746
Daniel Lezcano55786892008-03-04 13:47:47 -08001747 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001748 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Thomas Grafc71099a2006-08-04 23:20:06 -07001751 read_lock_bh(&table->tb6_lock);
1752
1753 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001754 &cfg->fc_dst, cfg->fc_dst_len,
1755 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001758 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001759 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001760 (!rt->dst.dev ||
1761 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001763 if (cfg->fc_flags & RTF_GATEWAY &&
1764 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001766 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001768 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001769 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Thomas Graf86872cb2006-08-22 00:01:08 -07001771 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001774 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 return err;
1777}
1778
David S. Miller6700c272012-07-17 03:29:28 -07001779static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001780{
David S. Millere8599ff2012-07-11 23:43:53 -07001781 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001782 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07001783 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001784 struct ndisc_options ndopts;
1785 struct inet6_dev *in6_dev;
1786 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001787 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07001788 int optlen, on_link;
1789 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07001790
Simon Horman29a3cad2013-05-28 20:34:26 +00001791 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001792 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07001793
1794 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07001795 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001796 return;
1797 }
1798
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001799 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07001800
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001801 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001802 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001803 return;
1804 }
1805
David S. Miller6e157b62012-07-12 00:05:02 -07001806 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001807 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001808 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001809 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07001810 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001811 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001812 return;
1813 }
1814
1815 in6_dev = __in6_dev_get(skb->dev);
1816 if (!in6_dev)
1817 return;
1818 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1819 return;
1820
1821 /* RFC2461 8.1:
1822 * The IP source address of the Redirect MUST be the same as the current
1823 * first-hop router for the specified ICMP Destination Address.
1824 */
1825
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001826 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07001827 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1828 return;
1829 }
David S. Miller6e157b62012-07-12 00:05:02 -07001830
1831 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001832 if (ndopts.nd_opts_tgt_lladdr) {
1833 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1834 skb->dev);
1835 if (!lladdr) {
1836 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1837 return;
1838 }
1839 }
1840
David S. Miller6e157b62012-07-12 00:05:02 -07001841 rt = (struct rt6_info *) dst;
1842 if (rt == net->ipv6.ip6_null_entry) {
1843 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1844 return;
1845 }
1846
1847 /* Redirect received -> path was valid.
1848 * Look, redirects are sent only in response to data packets,
1849 * so that this nexthop apparently is reachable. --ANK
1850 */
1851 dst_confirm(&rt->dst);
1852
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001853 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07001854 if (!neigh)
1855 return;
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * We have finally decided to accept it.
1859 */
1860
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001861 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1863 NEIGH_UPDATE_F_OVERRIDE|
1864 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1865 NEIGH_UPDATE_F_ISROUTER))
1866 );
1867
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001868 nrt = ip6_rt_copy(rt, &msg->dest);
David S. Miller38308472011-12-03 18:02:47 -05001869 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out;
1871
1872 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1873 if (on_link)
1874 nrt->rt6i_flags &= ~RTF_GATEWAY;
1875
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001876 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Thomas Graf40e22e82006-08-22 00:00:45 -07001878 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto out;
1880
Changli Gaod8d1f302010-06-10 23:31:35 -07001881 netevent.old = &rt->dst;
1882 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001883 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00001884 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07001885 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1886
David S. Miller38308472011-12-03 18:02:47 -05001887 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07001888 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001889 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
1892out:
David S. Millere8599ff2012-07-11 23:43:53 -07001893 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07001894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 * Misc support functions
1898 */
1899
Gao feng1716a962012-04-06 00:13:10 +00001900static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001901 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
David S. Millerd1918542011-12-28 20:19:20 -05001903 struct net *net = dev_net(ort->dst.dev);
David S. Miller8b96d222012-06-11 02:01:56 -07001904 struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1905 ort->rt6i_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001908 rt->dst.input = ort->dst.input;
1909 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001910 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001912 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001913 rt->rt6i_dst.plen = 128;
David S. Millerdefb3512010-12-08 21:16:57 -08001914 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001915 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 rt->rt6i_idev = ort->rt6i_idev;
1917 if (rt->rt6i_idev)
1918 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001919 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Julian Anastasov550bab42013-10-20 15:43:04 +03001921 if (ort->rt6i_flags & RTF_GATEWAY)
1922 rt->rt6i_gateway = ort->rt6i_gateway;
1923 else
1924 rt->rt6i_gateway = *dest;
Gao feng1716a962012-04-06 00:13:10 +00001925 rt->rt6i_flags = ort->rt6i_flags;
Li RongQing24f5b852013-12-19 12:40:26 +08001926 rt6_set_from(rt, ort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 rt->rt6i_metric = 0;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929#ifdef CONFIG_IPV6_SUBTREES
1930 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1931#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001932 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001933 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935 return rt;
1936}
1937
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001938#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001939static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001940 const struct in6_addr *prefix, int prefixlen,
1941 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001942{
1943 struct fib6_node *fn;
1944 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001945 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001946
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001947 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05001948 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001949 return NULL;
1950
Li RongQing5744dd92012-09-11 21:59:01 +00001951 read_lock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001952 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001953 if (!fn)
1954 goto out;
1955
Changli Gaod8d1f302010-06-10 23:31:35 -07001956 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001957 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001958 continue;
1959 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1960 continue;
1961 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1962 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001963 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001964 break;
1965 }
1966out:
Li RongQing5744dd92012-09-11 21:59:01 +00001967 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001968 return rt;
1969}
1970
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001971static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001972 const struct in6_addr *prefix, int prefixlen,
1973 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00001974 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001975{
Thomas Graf86872cb2006-08-22 00:01:08 -07001976 struct fib6_config cfg = {
1977 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001978 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001979 .fc_ifindex = ifindex,
1980 .fc_dst_len = prefixlen,
1981 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1982 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00001983 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001984 .fc_nlinfo.nlh = NULL,
1985 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001986 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001987
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001988 cfg.fc_dst = *prefix;
1989 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07001990
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001991 /* We should treat it as a default route if prefix length is 0. */
1992 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001993 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001994
Thomas Graf86872cb2006-08-22 00:01:08 -07001995 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001996
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001997 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001998}
1999#endif
2000
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002001struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002004 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002006 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002007 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002008 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Li RongQing5744dd92012-09-11 21:59:01 +00002010 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002011 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002012 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08002013 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 ipv6_addr_equal(&rt->rt6i_gateway, addr))
2015 break;
2016 }
2017 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07002018 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00002019 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return rt;
2021}
2022
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002023struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08002024 struct net_device *dev,
2025 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Thomas Graf86872cb2006-08-22 00:01:08 -07002027 struct fib6_config cfg = {
2028 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08002029 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07002030 .fc_ifindex = dev->ifindex,
2031 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
2032 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002033 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08002034 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002035 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002036 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002038 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Thomas Graf86872cb2006-08-22 00:01:08 -07002040 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return rt6_get_dflt_router(gwaddr, dev);
2043}
2044
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002045void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002048 struct fib6_table *table;
2049
2050 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002051 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002052 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002056 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002057 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00002058 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
2059 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002060 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002061 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002062 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 goto restart;
2064 }
2065 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002066 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Daniel Lezcano55786892008-03-04 13:47:47 -08002069static void rtmsg_to_fib6_config(struct net *net,
2070 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002071 struct fib6_config *cfg)
2072{
2073 memset(cfg, 0, sizeof(*cfg));
2074
2075 cfg->fc_table = RT6_TABLE_MAIN;
2076 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2077 cfg->fc_metric = rtmsg->rtmsg_metric;
2078 cfg->fc_expires = rtmsg->rtmsg_info;
2079 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2080 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2081 cfg->fc_flags = rtmsg->rtmsg_flags;
2082
Daniel Lezcano55786892008-03-04 13:47:47 -08002083 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002084
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002085 cfg->fc_dst = rtmsg->rtmsg_dst;
2086 cfg->fc_src = rtmsg->rtmsg_src;
2087 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002088}
2089
Daniel Lezcano55786892008-03-04 13:47:47 -08002090int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Thomas Graf86872cb2006-08-22 00:01:08 -07002092 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct in6_rtmsg rtmsg;
2094 int err;
2095
2096 switch(cmd) {
2097 case SIOCADDRT: /* Add a route */
2098 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002099 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return -EPERM;
2101 err = copy_from_user(&rtmsg, arg,
2102 sizeof(struct in6_rtmsg));
2103 if (err)
2104 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002105
Daniel Lezcano55786892008-03-04 13:47:47 -08002106 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rtnl_lock();
2109 switch (cmd) {
2110 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002111 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 break;
2113 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002114 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 break;
2116 default:
2117 err = -EINVAL;
2118 }
2119 rtnl_unlock();
2120
2121 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 return -EINVAL;
2125}
2126
2127/*
2128 * Drop the packet on the floor
2129 */
2130
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002131static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002133 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002134 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002135 switch (ipstats_mib_noroutes) {
2136 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002137 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002138 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002139 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2140 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002141 break;
2142 }
2143 /* FALLTHROUGH */
2144 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002145 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2146 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002147 break;
2148 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002149 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 kfree_skb(skb);
2151 return 0;
2152}
2153
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002154static int ip6_pkt_discard(struct sk_buff *skb)
2155{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002156 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002157}
2158
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002159static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Eric Dumazetadf30902009-06-02 05:19:30 +00002161 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002162 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002165static int ip6_pkt_prohibit(struct sk_buff *skb)
2166{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002167 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002168}
2169
2170static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2171{
Eric Dumazetadf30902009-06-02 05:19:30 +00002172 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002173 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
2177 * Allocate a dst for local (unicast / anycast) address.
2178 */
2179
2180struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2181 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002182 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002184 struct net *net = dev_net(idev->dev);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01002185 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2186 DST_NOCOUNT, NULL);
2187 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return ERR_PTR(-ENOMEM);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 in6_dev_hold(idev);
2191
David S. Miller11d53b42011-06-24 15:23:34 -07002192 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002193 rt->dst.input = ip6_input;
2194 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002198 if (anycast)
2199 rt->rt6i_flags |= RTF_ANYCAST;
2200 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 rt->rt6i_flags |= RTF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Julian Anastasov550bab42013-10-20 15:43:04 +03002203 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002204 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002206 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Changli Gaod8d1f302010-06-10 23:31:35 -07002208 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 return rt;
2211}
2212
Daniel Walterc3968a82011-04-13 21:10:57 +00002213int ip6_route_get_saddr(struct net *net,
2214 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002215 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002216 unsigned int prefs,
2217 struct in6_addr *saddr)
2218{
2219 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2220 int err = 0;
2221 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002222 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002223 else
2224 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2225 daddr, prefs, saddr);
2226 return err;
2227}
2228
2229/* remove deleted ip from prefsrc entries */
2230struct arg_dev_net_ip {
2231 struct net_device *dev;
2232 struct net *net;
2233 struct in6_addr *addr;
2234};
2235
2236static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2237{
2238 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2239 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2240 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2241
David S. Millerd1918542011-12-28 20:19:20 -05002242 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002243 rt != net->ipv6.ip6_null_entry &&
2244 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2245 /* remove prefsrc entry */
2246 rt->rt6i_prefsrc.plen = 0;
2247 }
2248 return 0;
2249}
2250
2251void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2252{
2253 struct net *net = dev_net(ifp->idev->dev);
2254 struct arg_dev_net_ip adni = {
2255 .dev = ifp->idev->dev,
2256 .net = net,
2257 .addr = &ifp->addr,
2258 };
Li RongQing0c3584d2013-12-27 16:32:38 +08002259 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00002260}
2261
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002262struct arg_dev_net {
2263 struct net_device *dev;
2264 struct net *net;
2265};
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267static int fib6_ifdown(struct rt6_info *rt, void *arg)
2268{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002269 const struct arg_dev_net *adn = arg;
2270 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002271
David S. Millerd1918542011-12-28 20:19:20 -05002272 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002273 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277}
2278
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002279void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002281 struct arg_dev_net adn = {
2282 .dev = dev,
2283 .net = net,
2284 };
2285
Li RongQing0c3584d2013-12-27 16:32:38 +08002286 fib6_clean_all(net, fib6_ifdown, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002287 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Eric Dumazet95c96172012-04-15 05:58:06 +00002290struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002292 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293};
2294
2295static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2296{
2297 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2298 struct inet6_dev *idev;
2299
2300 /* In IPv6 pmtu discovery is not optional,
2301 so that RTAX_MTU lock cannot disable it.
2302 We still use this lock to block changes
2303 caused by addrconf/ndisc.
2304 */
2305
2306 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002307 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309
2310 /* For administrative MTU increase, there is no way to discover
2311 IPv6 PMTU increase, so PMTU increase should be updated here.
2312 Since RFC 1981 doesn't include administrative MTU increase
2313 update PMTU increase is a MUST. (i.e. jumbo frame)
2314 */
2315 /*
2316 If new MTU is less than route PMTU, this new MTU will be the
2317 lowest MTU in the path, update the route PMTU to reflect PMTU
2318 decreases; if new MTU is greater than route PMTU, and the
2319 old MTU is the lowest MTU in the path, update the route PMTU
2320 to reflect the increase. In this case if the other nodes' MTU
2321 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2322 PMTU discouvery.
2323 */
David S. Millerd1918542011-12-28 20:19:20 -05002324 if (rt->dst.dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002325 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2326 (dst_mtu(&rt->dst) >= arg->mtu ||
2327 (dst_mtu(&rt->dst) < arg->mtu &&
2328 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002329 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332}
2333
Eric Dumazet95c96172012-04-15 05:58:06 +00002334void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Thomas Grafc71099a2006-08-04 23:20:06 -07002336 struct rt6_mtu_change_arg arg = {
2337 .dev = dev,
2338 .mtu = mtu,
2339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Li RongQing0c3584d2013-12-27 16:32:38 +08002341 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002344static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002345 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002346 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002347 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002348 [RTA_PRIORITY] = { .type = NLA_U32 },
2349 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002350 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002351};
2352
2353static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2354 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Thomas Graf86872cb2006-08-22 00:01:08 -07002356 struct rtmsg *rtm;
2357 struct nlattr *tb[RTA_MAX+1];
2358 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Thomas Graf86872cb2006-08-22 00:01:08 -07002360 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2361 if (err < 0)
2362 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Thomas Graf86872cb2006-08-22 00:01:08 -07002364 err = -EINVAL;
2365 rtm = nlmsg_data(nlh);
2366 memset(cfg, 0, sizeof(*cfg));
2367
2368 cfg->fc_table = rtm->rtm_table;
2369 cfg->fc_dst_len = rtm->rtm_dst_len;
2370 cfg->fc_src_len = rtm->rtm_src_len;
2371 cfg->fc_flags = RTF_UP;
2372 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002373 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002374
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002375 if (rtm->rtm_type == RTN_UNREACHABLE ||
2376 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002377 rtm->rtm_type == RTN_PROHIBIT ||
2378 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002379 cfg->fc_flags |= RTF_REJECT;
2380
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002381 if (rtm->rtm_type == RTN_LOCAL)
2382 cfg->fc_flags |= RTF_LOCAL;
2383
Eric W. Biederman15e47302012-09-07 20:12:54 +00002384 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002385 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002386 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002387
2388 if (tb[RTA_GATEWAY]) {
2389 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2390 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002392
2393 if (tb[RTA_DST]) {
2394 int plen = (rtm->rtm_dst_len + 7) >> 3;
2395
2396 if (nla_len(tb[RTA_DST]) < plen)
2397 goto errout;
2398
2399 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002401
2402 if (tb[RTA_SRC]) {
2403 int plen = (rtm->rtm_src_len + 7) >> 3;
2404
2405 if (nla_len(tb[RTA_SRC]) < plen)
2406 goto errout;
2407
2408 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002410
Daniel Walterc3968a82011-04-13 21:10:57 +00002411 if (tb[RTA_PREFSRC])
2412 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2413
Thomas Graf86872cb2006-08-22 00:01:08 -07002414 if (tb[RTA_OIF])
2415 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2416
2417 if (tb[RTA_PRIORITY])
2418 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2419
2420 if (tb[RTA_METRICS]) {
2421 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2422 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002424
2425 if (tb[RTA_TABLE])
2426 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2427
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002428 if (tb[RTA_MULTIPATH]) {
2429 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2430 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2431 }
2432
Thomas Graf86872cb2006-08-22 00:01:08 -07002433 err = 0;
2434errout:
2435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002438static int ip6_route_multipath(struct fib6_config *cfg, int add)
2439{
2440 struct fib6_config r_cfg;
2441 struct rtnexthop *rtnh;
2442 int remaining;
2443 int attrlen;
2444 int err = 0, last_err = 0;
2445
2446beginning:
2447 rtnh = (struct rtnexthop *)cfg->fc_mp;
2448 remaining = cfg->fc_mp_len;
2449
2450 /* Parse a Multipath Entry */
2451 while (rtnh_ok(rtnh, remaining)) {
2452 memcpy(&r_cfg, cfg, sizeof(*cfg));
2453 if (rtnh->rtnh_ifindex)
2454 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2455
2456 attrlen = rtnh_attrlen(rtnh);
2457 if (attrlen > 0) {
2458 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2459
2460 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2461 if (nla) {
2462 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2463 r_cfg.fc_flags |= RTF_GATEWAY;
2464 }
2465 }
2466 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2467 if (err) {
2468 last_err = err;
2469 /* If we are trying to remove a route, do not stop the
2470 * loop when ip6_route_del() fails (because next hop is
2471 * already gone), we should try to remove all next hops.
2472 */
2473 if (add) {
2474 /* If add fails, we should try to delete all
2475 * next hops that have been already added.
2476 */
2477 add = 0;
2478 goto beginning;
2479 }
2480 }
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002481 /* Because each route is added like a single route we remove
2482 * this flag after the first nexthop (if there is a collision,
2483 * we have already fail to add the first nexthop:
2484 * fib6_add_rt2node() has reject it).
2485 */
2486 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002487 rtnh = rtnh_next(rtnh, &remaining);
2488 }
2489
2490 return last_err;
2491}
2492
Thomas Graf661d2962013-03-21 07:45:29 +00002493static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Thomas Graf86872cb2006-08-22 00:01:08 -07002495 struct fib6_config cfg;
2496 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Thomas Graf86872cb2006-08-22 00:01:08 -07002498 err = rtm_to_fib6_config(skb, nlh, &cfg);
2499 if (err < 0)
2500 return err;
2501
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002502 if (cfg.fc_mp)
2503 return ip6_route_multipath(&cfg, 0);
2504 else
2505 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Thomas Graf661d2962013-03-21 07:45:29 +00002508static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Thomas Graf86872cb2006-08-22 00:01:08 -07002510 struct fib6_config cfg;
2511 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Thomas Graf86872cb2006-08-22 00:01:08 -07002513 err = rtm_to_fib6_config(skb, nlh, &cfg);
2514 if (err < 0)
2515 return err;
2516
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002517 if (cfg.fc_mp)
2518 return ip6_route_multipath(&cfg, 1);
2519 else
2520 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Thomas Graf339bf982006-11-10 14:10:15 -08002523static inline size_t rt6_nlmsg_size(void)
2524{
2525 return NLMSG_ALIGN(sizeof(struct rtmsg))
2526 + nla_total_size(16) /* RTA_SRC */
2527 + nla_total_size(16) /* RTA_DST */
2528 + nla_total_size(16) /* RTA_GATEWAY */
2529 + nla_total_size(16) /* RTA_PREFSRC */
2530 + nla_total_size(4) /* RTA_TABLE */
2531 + nla_total_size(4) /* RTA_IIF */
2532 + nla_total_size(4) /* RTA_OIF */
2533 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002534 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002535 + nla_total_size(sizeof(struct rta_cacheinfo));
2536}
2537
Brian Haley191cd582008-08-14 15:33:21 -07002538static int rt6_fill_node(struct net *net,
2539 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002540 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002541 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002542 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002545 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002546 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002547 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 if (prefix) { /* user wants prefix routes only */
2550 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2551 /* success since this is not a prefix route */
2552 return 1;
2553 }
2554 }
2555
Eric W. Biederman15e47302012-09-07 20:12:54 +00002556 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002557 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002558 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002559
2560 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 rtm->rtm_family = AF_INET6;
2562 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2563 rtm->rtm_src_len = rt->rt6i_src.plen;
2564 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002565 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002566 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002567 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002568 table = RT6_TABLE_UNSPEC;
2569 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04002570 if (nla_put_u32(skb, RTA_TABLE, table))
2571 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002572 if (rt->rt6i_flags & RTF_REJECT) {
2573 switch (rt->dst.error) {
2574 case -EINVAL:
2575 rtm->rtm_type = RTN_BLACKHOLE;
2576 break;
2577 case -EACCES:
2578 rtm->rtm_type = RTN_PROHIBIT;
2579 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002580 case -EAGAIN:
2581 rtm->rtm_type = RTN_THROW;
2582 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002583 default:
2584 rtm->rtm_type = RTN_UNREACHABLE;
2585 break;
2586 }
2587 }
David S. Miller38308472011-12-03 18:02:47 -05002588 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002589 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002590 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 rtm->rtm_type = RTN_LOCAL;
2592 else
2593 rtm->rtm_type = RTN_UNICAST;
2594 rtm->rtm_flags = 0;
2595 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2596 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002597 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00002599 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2600 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2601 rtm->rtm_protocol = RTPROT_RA;
2602 else
2603 rtm->rtm_protocol = RTPROT_KERNEL;
2604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
David S. Miller38308472011-12-03 18:02:47 -05002606 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 rtm->rtm_flags |= RTM_F_CLONED;
2608
2609 if (dst) {
David S. Millerc78679e2012-04-01 20:27:33 -04002610 if (nla_put(skb, RTA_DST, 16, dst))
2611 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002612 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 } else if (rtm->rtm_dst_len)
David S. Millerc78679e2012-04-01 20:27:33 -04002614 if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2615 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616#ifdef CONFIG_IPV6_SUBTREES
2617 if (src) {
David S. Millerc78679e2012-04-01 20:27:33 -04002618 if (nla_put(skb, RTA_SRC, 16, src))
2619 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002620 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04002621 } else if (rtm->rtm_src_len &&
2622 nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2623 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002625 if (iif) {
2626#ifdef CONFIG_IPV6_MROUTE
2627 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002628 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002629 if (err <= 0) {
2630 if (!nowait) {
2631 if (err == 0)
2632 return 0;
2633 goto nla_put_failure;
2634 } else {
2635 if (err == -EMSGSIZE)
2636 goto nla_put_failure;
2637 }
2638 }
2639 } else
2640#endif
David S. Millerc78679e2012-04-01 20:27:33 -04002641 if (nla_put_u32(skb, RTA_IIF, iif))
2642 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002643 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04002645 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2646 nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2647 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002649
Daniel Walterc3968a82011-04-13 21:10:57 +00002650 if (rt->rt6i_prefsrc.plen) {
2651 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002652 saddr_buf = rt->rt6i_prefsrc.addr;
David S. Millerc78679e2012-04-01 20:27:33 -04002653 if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2654 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00002655 }
2656
David S. Millerdefb3512010-12-08 21:16:57 -08002657 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002658 goto nla_put_failure;
2659
YOSHIFUJI Hideaki / 吉藤英明dd0cbf22013-01-17 12:53:15 +00002660 if (rt->rt6i_flags & RTF_GATEWAY) {
2661 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00002662 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002663 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002664
David S. Millerc78679e2012-04-01 20:27:33 -04002665 if (rt->dst.dev &&
2666 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2667 goto nla_put_failure;
2668 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2669 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00002670
2671 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002672
David S. Miller87a50692012-07-10 05:06:14 -07002673 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002674 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Thomas Graf2d7202b2006-08-22 00:01:27 -07002676 return nlmsg_end(skb, nlh);
2677
2678nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002679 nlmsg_cancel(skb, nlh);
2680 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Patrick McHardy1b43af52006-08-10 23:11:17 -07002683int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2686 int prefix;
2687
Thomas Graf2d7202b2006-08-22 00:01:27 -07002688 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2689 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2691 } else
2692 prefix = 0;
2693
Brian Haley191cd582008-08-14 15:33:21 -07002694 return rt6_fill_node(arg->net,
2695 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002696 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002697 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Thomas Graf661d2962013-03-21 07:45:29 +00002700static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002702 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002703 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002705 struct sk_buff *skb;
2706 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002707 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002708 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07002709
2710 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2711 if (err < 0)
2712 goto errout;
2713
2714 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002715 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002716
2717 if (tb[RTA_SRC]) {
2718 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2719 goto errout;
2720
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002721 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002722 }
2723
2724 if (tb[RTA_DST]) {
2725 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2726 goto errout;
2727
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002728 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002729 }
2730
2731 if (tb[RTA_IIF])
2732 iif = nla_get_u32(tb[RTA_IIF]);
2733
2734 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002735 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002736
2737 if (iif) {
2738 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002739 int flags = 0;
2740
Daniel Lezcano55786892008-03-04 13:47:47 -08002741 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002742 if (!dev) {
2743 err = -ENODEV;
2744 goto errout;
2745 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002746
2747 fl6.flowi6_iif = iif;
2748
2749 if (!ipv6_addr_any(&fl6.saddr))
2750 flags |= RT6_LOOKUP_F_HAS_SADDR;
2751
2752 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2753 flags);
2754 } else {
2755 fl6.flowi6_oif = oif;
2756
2757 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07002758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002761 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00002762 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07002763 err = -ENOBUFS;
2764 goto errout;
2765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 /* Reserve room for dummy headers, this skb can pass
2768 through good chunk of routing engine.
2769 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002770 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2772
Changli Gaod8d1f302010-06-10 23:31:35 -07002773 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
David S. Miller4c9483b2011-03-12 16:22:43 -05002775 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002776 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002777 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002779 kfree_skb(skb);
2780 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782
Eric W. Biederman15e47302012-09-07 20:12:54 +00002783 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07002784errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
Thomas Graf86872cb2006-08-22 00:01:08 -07002788void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002791 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002792 u32 seq;
2793 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002795 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002796 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002797
Thomas Graf339bf982006-11-10 14:10:15 -08002798 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002799 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002800 goto errout;
2801
Brian Haley191cd582008-08-14 15:33:21 -07002802 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002803 event, info->portid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002804 if (err < 0) {
2805 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2806 WARN_ON(err == -EMSGSIZE);
2807 kfree_skb(skb);
2808 goto errout;
2809 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002810 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002811 info->nlh, gfp_any());
2812 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002813errout:
2814 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002815 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002818static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00002819 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002820{
Jiri Pirko351638e2013-05-28 01:30:21 +00002821 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002822 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002823
2824 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002825 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002826 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2827#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002828 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002829 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002830 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002831 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2832#endif
2833 }
2834
2835 return NOTIFY_OK;
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838/*
2839 * /proc
2840 */
2841
2842#ifdef CONFIG_PROC_FS
2843
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002844static const struct file_operations ipv6_route_proc_fops = {
2845 .owner = THIS_MODULE,
2846 .open = ipv6_route_open,
2847 .read = seq_read,
2848 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002849 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002850};
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2853{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002854 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002856 net->ipv6.rt6_stats->fib_nodes,
2857 net->ipv6.rt6_stats->fib_route_nodes,
2858 net->ipv6.rt6_stats->fib_rt_alloc,
2859 net->ipv6.rt6_stats->fib_rt_entries,
2860 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002861 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002862 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 return 0;
2865}
2866
2867static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2868{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002869 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002870}
2871
Arjan van de Ven9a321442007-02-12 00:55:35 -08002872static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 .owner = THIS_MODULE,
2874 .open = rt6_stats_seq_open,
2875 .read = seq_read,
2876 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002877 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878};
2879#endif /* CONFIG_PROC_FS */
2880
2881#ifdef CONFIG_SYSCTL
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883static
Joe Perchesfe2c6332013-06-11 23:04:25 -07002884int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 void __user *buffer, size_t *lenp, loff_t *ppos)
2886{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002887 struct net *net;
2888 int delay;
2889 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002891
2892 net = (struct net *)ctl->extra1;
2893 delay = net->ipv6.sysctl.flush_delay;
2894 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002895 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
Joe Perchesfe2c6332013-06-11 23:04:25 -07002899struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002900 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002902 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002904 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002905 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 },
2907 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002909 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 .maxlen = sizeof(int),
2911 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002912 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 },
2914 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002916 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 .maxlen = sizeof(int),
2918 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002919 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 },
2921 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002923 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 .maxlen = sizeof(int),
2925 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002926 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 },
2928 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002930 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 .maxlen = sizeof(int),
2932 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002933 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 },
2935 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002937 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 .maxlen = sizeof(int),
2939 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002940 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 },
2942 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002944 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 .maxlen = sizeof(int),
2946 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002947 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 },
2949 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002951 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 .maxlen = sizeof(int),
2953 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002954 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 },
2956 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002958 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .maxlen = sizeof(int),
2960 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002961 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 },
2963 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002965 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 .maxlen = sizeof(int),
2967 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002968 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002970 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971};
2972
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002973struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002974{
2975 struct ctl_table *table;
2976
2977 table = kmemdup(ipv6_route_table_template,
2978 sizeof(ipv6_route_table_template),
2979 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002980
2981 if (table) {
2982 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002983 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002984 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002985 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2986 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2987 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2988 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2989 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2990 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2991 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002992 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00002993
2994 /* Don't export sysctls to unprivileged users */
2995 if (net->user_ns != &init_user_ns)
2996 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002997 }
2998
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002999 return table;
3000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001#endif
3002
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003003static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003004{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003005 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003006
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003007 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3008 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003009
Eric Dumazetfc66f952010-10-08 06:37:34 +00003010 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3011 goto out_ip6_dst_ops;
3012
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003013 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3014 sizeof(*net->ipv6.ip6_null_entry),
3015 GFP_KERNEL);
3016 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003017 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003018 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003019 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003020 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003021 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3022 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003023
3024#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3025 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3026 sizeof(*net->ipv6.ip6_prohibit_entry),
3027 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003028 if (!net->ipv6.ip6_prohibit_entry)
3029 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003030 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003031 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003032 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003033 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3034 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003035
3036 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3037 sizeof(*net->ipv6.ip6_blk_hole_entry),
3038 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003039 if (!net->ipv6.ip6_blk_hole_entry)
3040 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003041 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003042 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003043 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003044 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3045 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003046#endif
3047
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003048 net->ipv6.sysctl.flush_delay = 0;
3049 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3050 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3051 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3052 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3053 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3054 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3055 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3056
Benjamin Thery6891a342008-03-04 13:49:47 -08003057 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3058
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003059 ret = 0;
3060out:
3061 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003062
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003063#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3064out_ip6_prohibit_entry:
3065 kfree(net->ipv6.ip6_prohibit_entry);
3066out_ip6_null_entry:
3067 kfree(net->ipv6.ip6_null_entry);
3068#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003069out_ip6_dst_entries:
3070 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003071out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003072 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003073}
3074
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003075static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003076{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003077 kfree(net->ipv6.ip6_null_entry);
3078#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3079 kfree(net->ipv6.ip6_prohibit_entry);
3080 kfree(net->ipv6.ip6_blk_hole_entry);
3081#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003082 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003083}
3084
Thomas Grafd1896342012-06-18 12:08:33 +00003085static int __net_init ip6_route_net_init_late(struct net *net)
3086{
3087#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003088 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3089 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00003090#endif
3091 return 0;
3092}
3093
3094static void __net_exit ip6_route_net_exit_late(struct net *net)
3095{
3096#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003097 remove_proc_entry("ipv6_route", net->proc_net);
3098 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00003099#endif
3100}
3101
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003102static struct pernet_operations ip6_route_net_ops = {
3103 .init = ip6_route_net_init,
3104 .exit = ip6_route_net_exit,
3105};
3106
David S. Millerc3426b42012-06-09 16:27:05 -07003107static int __net_init ipv6_inetpeer_init(struct net *net)
3108{
3109 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3110
3111 if (!bp)
3112 return -ENOMEM;
3113 inet_peer_base_init(bp);
3114 net->ipv6.peers = bp;
3115 return 0;
3116}
3117
3118static void __net_exit ipv6_inetpeer_exit(struct net *net)
3119{
3120 struct inet_peer_base *bp = net->ipv6.peers;
3121
3122 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003123 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003124 kfree(bp);
3125}
3126
David S. Miller2b823f72012-06-09 19:00:16 -07003127static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003128 .init = ipv6_inetpeer_init,
3129 .exit = ipv6_inetpeer_exit,
3130};
3131
Thomas Grafd1896342012-06-18 12:08:33 +00003132static struct pernet_operations ip6_route_net_late_ops = {
3133 .init = ip6_route_net_init_late,
3134 .exit = ip6_route_net_exit_late,
3135};
3136
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003137static struct notifier_block ip6_route_dev_notifier = {
3138 .notifier_call = ip6_route_dev_notify,
3139 .priority = 0,
3140};
3141
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003142int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003144 int ret;
3145
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003146 ret = -ENOMEM;
3147 ip6_dst_ops_template.kmem_cachep =
3148 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3149 SLAB_HWCACHE_ALIGN, NULL);
3150 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003151 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003152
Eric Dumazetfc66f952010-10-08 06:37:34 +00003153 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003154 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003155 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003156
David S. Millerc3426b42012-06-09 16:27:05 -07003157 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3158 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003159 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003160
David S. Miller7e52b332012-06-15 15:51:55 -07003161 ret = register_pernet_subsys(&ip6_route_net_ops);
3162 if (ret)
3163 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003164
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003165 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3166
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003167 /* Registering of the loopback is done before this portion of code,
3168 * the loopback reference in rt6_info will not be taken, do it
3169 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003170 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003171 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3172 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003173 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003174 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003175 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003176 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3177 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003178 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003179 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003180 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003181
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003182 ret = xfrm6_init();
3183 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003184 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003185
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003186 ret = fib6_rules_init();
3187 if (ret)
3188 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003189
Thomas Grafd1896342012-06-18 12:08:33 +00003190 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3191 if (ret)
3192 goto fib6_rules_init;
3193
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003194 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003195 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3196 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3197 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003198 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003199
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003200 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003201 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003202 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003203
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003204out:
3205 return ret;
3206
Thomas Grafd1896342012-06-18 12:08:33 +00003207out_register_late_subsys:
3208 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003209fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003210 fib6_rules_cleanup();
3211xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003212 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003213out_fib6_init:
3214 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003215out_register_subsys:
3216 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003217out_register_inetpeer:
3218 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003219out_dst_entries:
3220 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003221out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003222 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
3226void ip6_route_cleanup(void)
3227{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003228 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003229 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003230 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003233 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003234 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003235 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003236 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}