blob: 9c7b5d80049563b02b152c1535c1735d98bce173 [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
Gao feng1716a962012-04-06 00:13:10 +000068static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +000069 const struct in6_addr *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080071static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000072static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static struct dst_entry *ip6_negative_advice(struct dst_entry *);
74static void ip6_dst_destroy(struct dst_entry *);
75static void ip6_dst_ifdown(struct dst_entry *,
76 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080077static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int ip6_pkt_discard(struct sk_buff *skb);
80static int ip6_pkt_discard_out(struct sk_buff *skb);
81static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070082static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
83 struct sk_buff *skb, u32 mtu);
84static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
85 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080087#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080088static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000089 const struct in6_addr *prefix, int prefixlen,
90 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +000091 unsigned int pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080092static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000093 const struct in6_addr *prefix, int prefixlen,
94 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080095#endif
96
David S. Miller06582542011-01-27 14:58:42 -080097static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
98{
99 struct rt6_info *rt = (struct rt6_info *) dst;
100 struct inet_peer *peer;
101 u32 *p = NULL;
102
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000103 if (!(rt->dst.flags & DST_HOST))
104 return NULL;
105
David S. Millerfbfe95a2012-06-08 23:24:18 -0700106 peer = rt6_get_peer_create(rt);
David S. Miller06582542011-01-27 14:58:42 -0800107 if (peer) {
108 u32 *old_p = __DST_METRICS_PTR(old);
109 unsigned long prev, new;
110
111 p = peer->metrics;
112 if (inet_metrics_new(peer))
113 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
114
115 new = (unsigned long) p;
116 prev = cmpxchg(&dst->_metrics, old, new);
117
118 if (prev != old) {
119 p = __DST_METRICS_PTR(prev);
120 if (prev & DST_METRICS_READ_ONLY)
121 p = NULL;
122 }
123 }
124 return p;
125}
126
David S. Millerf894cbf2012-07-02 21:52:24 -0700127static inline const void *choose_neigh_daddr(struct rt6_info *rt,
128 struct sk_buff *skb,
129 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500130{
131 struct in6_addr *p = &rt->rt6i_gateway;
132
David S. Millera7563f32012-01-26 16:29:16 -0500133 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500134 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700135 else if (skb)
136 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500137 return daddr;
138}
139
David S. Millerf894cbf2012-07-02 21:52:24 -0700140static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
141 struct sk_buff *skb,
142 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700143{
David S. Miller39232972012-01-26 15:22:32 -0500144 struct rt6_info *rt = (struct rt6_info *) dst;
145 struct neighbour *n;
146
David S. Millerf894cbf2012-07-02 21:52:24 -0700147 daddr = choose_neigh_daddr(rt, skb, daddr);
David S. Miller39232972012-01-26 15:22:32 -0500148 n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500149 if (n)
150 return n;
151 return neigh_create(&nd_tbl, daddr, dst->dev);
152}
153
David S. Miller8ade06c2011-12-29 18:51:57 -0500154static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
David S. Millerf83c7792011-12-28 15:41:23 -0500155{
David S. Miller8ade06c2011-12-29 18:51:57 -0500156 struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
157 if (!n) {
158 n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
159 if (IS_ERR(n))
160 return PTR_ERR(n);
161 }
David S. Miller97cac082012-07-02 22:43:47 -0700162 rt->n = n;
David S. Millerf83c7792011-12-28 15:41:23 -0500163
164 return 0;
David S. Millerd3aaeb32011-07-18 00:40:17 -0700165}
166
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800167static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800169 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .gc = ip6_dst_gc,
171 .gc_thresh = 1024,
172 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800173 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000174 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800175 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .destroy = ip6_dst_destroy,
177 .ifdown = ip6_dst_ifdown,
178 .negative_advice = ip6_negative_advice,
179 .link_failure = ip6_link_failure,
180 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700181 .redirect = rt6_do_redirect,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700182 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700183 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Steffen Klassertebb762f2011-11-23 02:12:51 +0000186static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800187{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000188 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
189
190 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800191}
192
David S. Miller6700c272012-07-17 03:29:28 -0700193static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
194 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700195{
196}
197
David S. Miller6700c272012-07-17 03:29:28 -0700198static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
199 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700200{
201}
202
Held Bernhard0972ddb2011-04-24 22:07:32 +0000203static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
204 unsigned long old)
205{
206 return NULL;
207}
208
David S. Miller14e50e52007-05-24 18:17:54 -0700209static struct dst_ops ip6_dst_blackhole_ops = {
210 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800211 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700212 .destroy = ip6_dst_destroy,
213 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000214 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800215 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700216 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700217 .redirect = ip6_rt_blackhole_redirect,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000218 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700219 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700220};
221
David S. Miller62fa8a82011-01-26 20:51:05 -0800222static const u32 ip6_template_metrics[RTAX_MAX] = {
223 [RTAX_HOPLIMIT - 1] = 255,
224};
225
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000226static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700227 .dst = {
228 .__refcnt = ATOMIC_INIT(1),
229 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000230 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700231 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700232 .input = ip6_pkt_discard,
233 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 },
235 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700236 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .rt6i_metric = ~(u32) 0,
238 .rt6i_ref = ATOMIC_INIT(1),
239};
240
Thomas Graf101367c2006-08-04 03:39:02 -0700241#ifdef CONFIG_IPV6_MULTIPLE_TABLES
242
David S. Miller6723ab52006-10-18 21:20:57 -0700243static int ip6_pkt_prohibit(struct sk_buff *skb);
244static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700245
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000246static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700247 .dst = {
248 .__refcnt = ATOMIC_INIT(1),
249 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000250 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700251 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700252 .input = ip6_pkt_prohibit,
253 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700254 },
255 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700256 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700257 .rt6i_metric = ~(u32) 0,
258 .rt6i_ref = ATOMIC_INIT(1),
259};
260
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000261static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700262 .dst = {
263 .__refcnt = ATOMIC_INIT(1),
264 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000265 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700266 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700267 .input = dst_discard,
268 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700269 },
270 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700271 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700272 .rt6i_metric = ~(u32) 0,
273 .rt6i_ref = ATOMIC_INIT(1),
274};
275
276#endif
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* allocate dst with ip6_dst_ops */
David S. Miller97bab732012-06-09 22:36:36 -0700279static inline struct rt6_info *ip6_dst_alloc(struct net *net,
David S. Miller957c6652011-06-24 15:25:00 -0700280 struct net_device *dev,
David S. Miller8b96d222012-06-11 02:01:56 -0700281 int flags,
282 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
David S. Miller97bab732012-06-09 22:36:36 -0700284 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000285 0, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700286
David S. Miller97bab732012-06-09 22:36:36 -0700287 if (rt) {
Steffen Klassert81048912012-07-05 23:37:09 +0000288 struct dst_entry *dst = &rt->dst;
289
290 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
David S. Miller8b96d222012-06-11 02:01:56 -0700291 rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +0000292 rt->rt6i_genid = rt_genid(net);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000293 INIT_LIST_HEAD(&rt->rt6i_siblings);
294 rt->rt6i_nsiblings = 0;
David S. Miller97bab732012-06-09 22:36:36 -0700295 }
David S. Millercf911662011-04-28 14:31:47 -0700296 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void ip6_dst_destroy(struct dst_entry *dst)
300{
301 struct rt6_info *rt = (struct rt6_info *)dst;
302 struct inet6_dev *idev = rt->rt6i_idev;
303
David S. Miller97cac082012-07-02 22:43:47 -0700304 if (rt->n)
305 neigh_release(rt->n);
306
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000307 if (!(rt->dst.flags & DST_HOST))
308 dst_destroy_metrics_generic(dst);
309
David S. Miller38308472011-12-03 18:02:47 -0500310 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 rt->rt6i_idev = NULL;
312 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900313 }
Gao feng1716a962012-04-06 00:13:10 +0000314
315 if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
316 dst_release(dst->from);
317
David S. Miller97bab732012-06-09 22:36:36 -0700318 if (rt6_has_peer(rt)) {
319 struct inet_peer *peer = rt6_peer_ptr(rt);
David S. Millerb3419362010-11-30 12:27:11 -0800320 inet_putpeer(peer);
321 }
322}
323
David S. Miller6431cbc2011-02-07 20:38:06 -0800324static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
325
326static u32 rt6_peer_genid(void)
327{
328 return atomic_read(&__rt6_peer_genid);
329}
330
David S. Millerb3419362010-11-30 12:27:11 -0800331void rt6_bind_peer(struct rt6_info *rt, int create)
332{
David S. Miller97bab732012-06-09 22:36:36 -0700333 struct inet_peer_base *base;
David S. Millerb3419362010-11-30 12:27:11 -0800334 struct inet_peer *peer;
335
David S. Miller97bab732012-06-09 22:36:36 -0700336 base = inetpeer_base_ptr(rt->_rt6i_peer);
337 if (!base)
338 return;
339
340 peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
David S. Miller7b34ca22012-06-11 04:13:57 -0700341 if (peer) {
342 if (!rt6_set_peer(rt, peer))
343 inet_putpeer(peer);
344 else
345 rt->rt6i_peer_genid = rt6_peer_genid();
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
350 int how)
351{
352 struct rt6_info *rt = (struct rt6_info *)dst;
353 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800354 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900355 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David S. Miller97cac082012-07-02 22:43:47 -0700357 if (dev != loopback_dev) {
358 if (idev && idev->dev == dev) {
359 struct inet6_dev *loopback_idev =
360 in6_dev_get(loopback_dev);
361 if (loopback_idev) {
362 rt->rt6i_idev = loopback_idev;
363 in6_dev_put(idev);
364 }
365 }
366 if (rt->n && rt->n->dev == dev) {
367 rt->n->dev = loopback_dev;
368 dev_hold(loopback_dev);
369 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 }
372}
373
Eric Dumazeta50feda2012-05-18 18:57:34 +0000374static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Gao feng1716a962012-04-06 00:13:10 +0000376 if (rt->rt6i_flags & RTF_EXPIRES) {
377 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000378 return true;
Gao feng1716a962012-04-06 00:13:10 +0000379 } else if (rt->dst.from) {
Li RongQing3fd91fb2012-09-13 19:54:57 +0000380 return rt6_check_expired((struct rt6_info *) rt->dst.from);
Gao feng1716a962012-04-06 00:13:10 +0000381 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000382 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Eric Dumazeta50feda2012-05-18 18:57:34 +0000385static bool rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700386{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000387 return ipv6_addr_type(daddr) &
388 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700389}
390
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000391/* Multipath route selection:
392 * Hash based function using packet header and flowlabel.
393 * Adapted from fib_info_hashfn()
394 */
395static int rt6_info_hash_nhsfn(unsigned int candidate_count,
396 const struct flowi6 *fl6)
397{
398 unsigned int val = fl6->flowi6_proto;
399
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000400 val ^= (__force u32)fl6->daddr.s6_addr32[0];
401 val ^= (__force u32)fl6->daddr.s6_addr32[1];
402 val ^= (__force u32)fl6->daddr.s6_addr32[2];
403 val ^= (__force u32)fl6->daddr.s6_addr32[3];
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000404
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000405 val ^= (__force u32)fl6->saddr.s6_addr32[0];
406 val ^= (__force u32)fl6->saddr.s6_addr32[1];
407 val ^= (__force u32)fl6->saddr.s6_addr32[2];
408 val ^= (__force u32)fl6->saddr.s6_addr32[3];
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000409
410 /* Work only if this not encapsulated */
411 switch (fl6->flowi6_proto) {
412 case IPPROTO_UDP:
413 case IPPROTO_TCP:
414 case IPPROTO_SCTP:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000415 val ^= (__force u16)fl6->fl6_sport;
416 val ^= (__force u16)fl6->fl6_dport;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000417 break;
418
419 case IPPROTO_ICMPV6:
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000420 val ^= (__force u16)fl6->fl6_icmp_type;
421 val ^= (__force u16)fl6->fl6_icmp_code;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000422 break;
423 }
424 /* RFC6438 recommands to use flowlabel */
Nicolas Dichtelb3ce5ae2012-10-22 23:35:06 +0000425 val ^= (__force u32)fl6->flowlabel;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000426
427 /* Perhaps, we need to tune, this function? */
428 val = val ^ (val >> 7) ^ (val >> 12);
429 return val % candidate_count;
430}
431
432static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
433 struct flowi6 *fl6)
434{
435 struct rt6_info *sibling, *next_sibling;
436 int route_choosen;
437
438 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
439 /* Don't change the route, if route_choosen == 0
440 * (siblings does not include ourself)
441 */
442 if (route_choosen)
443 list_for_each_entry_safe(sibling, next_sibling,
444 &match->rt6i_siblings, rt6i_siblings) {
445 route_choosen--;
446 if (route_choosen == 0) {
447 match = sibling;
448 break;
449 }
450 }
451 return match;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700455 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800458static inline struct rt6_info *rt6_device_match(struct net *net,
459 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000460 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700462 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct rt6_info *local = NULL;
465 struct rt6_info *sprt;
466
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900467 if (!oif && ipv6_addr_any(saddr))
468 goto out;
469
Changli Gaod8d1f302010-06-10 23:31:35 -0700470 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500471 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900472
473 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (dev->ifindex == oif)
475 return sprt;
476 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500477 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700479 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900481 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 local->rt6i_idev->dev->ifindex == oif))
483 continue;
484 }
485 local = sprt;
486 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900487 } else {
488 if (ipv6_chk_addr(net, saddr, dev,
489 flags & RT6_LOOKUP_F_IFACE))
490 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900494 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (local)
496 return local;
497
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700498 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800499 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900501out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return rt;
503}
504
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800505#ifdef CONFIG_IPV6_ROUTER_PREF
506static void rt6_probe(struct rt6_info *rt)
507{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000508 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800509 /*
510 * Okay, this does not seem to be appropriate
511 * for now, however, we need to check if it
512 * is really so; aka Router Reachability Probing.
513 *
514 * Router Reachability Probe MUST be rate-limited
515 * to no more than one per minute.
516 */
David S. Miller97cac082012-07-02 22:43:47 -0700517 neigh = rt ? rt->n : NULL;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800518 if (!neigh || (neigh->nud_state & NUD_VALID))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000519 return;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800520 read_lock_bh(&neigh->lock);
521 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800522 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800523 struct in6_addr mcaddr;
524 struct in6_addr *target;
525
526 neigh->updated = jiffies;
527 read_unlock_bh(&neigh->lock);
528
529 target = (struct in6_addr *)&neigh->primary_key;
530 addrconf_addr_solict_mult(target, &mcaddr);
David S. Millerd1918542011-12-28 20:19:20 -0500531 ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000532 } else {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800533 read_unlock_bh(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000534 }
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800535}
536#else
537static inline void rt6_probe(struct rt6_info *rt)
538{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800539}
540#endif
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800543 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700545static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
David S. Millerd1918542011-12-28 20:19:20 -0500547 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700548 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800549 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700550 if ((dev->flags & IFF_LOOPBACK) &&
551 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
552 return 1;
553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Dave Jonesb6f99a22007-03-22 12:27:49 -0700556static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000558 struct neighbour *neigh;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800559 int m;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000560
David S. Miller97cac082012-07-02 22:43:47 -0700561 neigh = rt->n;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700562 if (rt->rt6i_flags & RTF_NONEXTHOP ||
563 !(rt->rt6i_flags & RTF_GATEWAY))
564 m = 1;
565 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800566 read_lock_bh(&neigh->lock);
567 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700568 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800569#ifdef CONFIG_IPV6_ROUTER_PREF
570 else if (neigh->nud_state & NUD_FAILED)
571 m = 0;
572#endif
573 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800574 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800575 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800576 } else
577 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800578 return m;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800581static int rt6_score_route(struct rt6_info *rt, int oif,
582 int strict)
583{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700584 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900585
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700586 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700587 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800588 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800589#ifdef CONFIG_IPV6_ROUTER_PREF
590 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
591#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700592 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800593 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800594 return -1;
595 return m;
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David S. Millerf11e6652007-03-24 20:36:25 -0700598static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
599 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800600{
David S. Millerf11e6652007-03-24 20:36:25 -0700601 int m;
602
603 if (rt6_check_expired(rt))
604 goto out;
605
606 m = rt6_score_route(rt, oif, strict);
607 if (m < 0)
608 goto out;
609
610 if (m > *mpri) {
611 if (strict & RT6_LOOKUP_F_REACHABLE)
612 rt6_probe(match);
613 *mpri = m;
614 match = rt;
615 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
616 rt6_probe(rt);
617 }
618
619out:
620 return match;
621}
622
623static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
624 struct rt6_info *rr_head,
625 u32 metric, int oif, int strict)
626{
627 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800628 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David S. Millerf11e6652007-03-24 20:36:25 -0700630 match = NULL;
631 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700632 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700633 match = find_match(rt, oif, strict, &mpri, match);
634 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700635 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700636 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800637
David S. Millerf11e6652007-03-24 20:36:25 -0700638 return match;
639}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800640
David S. Millerf11e6652007-03-24 20:36:25 -0700641static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
642{
643 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800644 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
David S. Millerf11e6652007-03-24 20:36:25 -0700646 rt0 = fn->rr_ptr;
647 if (!rt0)
648 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
David S. Millerf11e6652007-03-24 20:36:25 -0700650 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800652 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700653 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700654 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700655
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800656 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700657 if (!next || next->rt6i_metric != rt0->rt6i_metric)
658 next = fn->leaf;
659
660 if (next != rt0)
661 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
David S. Millerd1918542011-12-28 20:19:20 -0500664 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000665 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800668#ifdef CONFIG_IPV6_ROUTE_INFO
669int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000670 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800671{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900672 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800673 struct route_info *rinfo = (struct route_info *) opt;
674 struct in6_addr prefix_buf, *prefix;
675 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900676 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800677 struct rt6_info *rt;
678
679 if (len < sizeof(struct route_info)) {
680 return -EINVAL;
681 }
682
683 /* Sanity check for prefix_len and length */
684 if (rinfo->length > 3) {
685 return -EINVAL;
686 } else if (rinfo->prefix_len > 128) {
687 return -EINVAL;
688 } else if (rinfo->prefix_len > 64) {
689 if (rinfo->length < 2) {
690 return -EINVAL;
691 }
692 } else if (rinfo->prefix_len > 0) {
693 if (rinfo->length < 1) {
694 return -EINVAL;
695 }
696 }
697
698 pref = rinfo->route_pref;
699 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000700 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800701
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900702 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800703
704 if (rinfo->length == 3)
705 prefix = (struct in6_addr *)rinfo->prefix;
706 else {
707 /* this function is safe */
708 ipv6_addr_prefix(&prefix_buf,
709 (struct in6_addr *)rinfo->prefix,
710 rinfo->prefix_len);
711 prefix = &prefix_buf;
712 }
713
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800714 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
715 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800716
717 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700718 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800719 rt = NULL;
720 }
721
722 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800723 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800724 pref);
725 else if (rt)
726 rt->rt6i_flags = RTF_ROUTEINFO |
727 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
728
729 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000730 if (!addrconf_finite_timeout(lifetime))
731 rt6_clean_expires(rt);
732 else
733 rt6_set_expires(rt, jiffies + HZ * lifetime);
734
Changli Gaod8d1f302010-06-10 23:31:35 -0700735 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800736 }
737 return 0;
738}
739#endif
740
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800741#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700742do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800743 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700744 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700745 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700746 if (fn->fn_flags & RTN_TL_ROOT) \
747 goto out; \
748 pn = fn->parent; \
749 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800750 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700751 else \
752 fn = pn; \
753 if (fn->fn_flags & RTN_RTINFO) \
754 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700755 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700756 } \
David S. Miller38308472011-12-03 18:02:47 -0500757} while (0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700758
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800759static struct rt6_info *ip6_pol_route_lookup(struct net *net,
760 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500761 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct fib6_node *fn;
764 struct rt6_info *rt;
765
Thomas Grafc71099a2006-08-04 23:20:06 -0700766 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500767 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700768restart:
769 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500770 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000771 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
772 rt = rt6_multipath_select(rt, fl6);
David S. Miller4c9483b2011-03-12 16:22:43 -0500773 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700774out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700775 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700776 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700777 return rt;
778
779}
780
Florian Westphalea6e5742011-09-05 16:05:44 +0200781struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
782 int flags)
783{
784 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
785}
786EXPORT_SYMBOL_GPL(ip6_route_lookup);
787
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900788struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
789 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700790{
David S. Miller4c9483b2011-03-12 16:22:43 -0500791 struct flowi6 fl6 = {
792 .flowi6_oif = oif,
793 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700794 };
795 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700796 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700797
Thomas Grafadaa70b2006-10-13 15:01:03 -0700798 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500799 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700800 flags |= RT6_LOOKUP_F_HAS_SADDR;
801 }
802
David S. Miller4c9483b2011-03-12 16:22:43 -0500803 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700804 if (dst->error == 0)
805 return (struct rt6_info *) dst;
806
807 dst_release(dst);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return NULL;
810}
811
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900812EXPORT_SYMBOL(rt6_lookup);
813
Thomas Grafc71099a2006-08-04 23:20:06 -0700814/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 It takes new route entry, the addition fails by any reason the
816 route is freed. In any case, if caller does not hold it, it may
817 be destroyed.
818 */
819
Thomas Graf86872cb2006-08-22 00:01:08 -0700820static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700823 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Thomas Grafc71099a2006-08-04 23:20:06 -0700825 table = rt->rt6i_table;
826 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700827 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700828 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 return err;
831}
832
Thomas Graf40e22e82006-08-22 00:00:45 -0700833int ip6_ins_rt(struct rt6_info *rt)
834{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800835 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -0500836 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800837 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800838 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700839}
840
Gao feng1716a962012-04-06 00:13:10 +0000841static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000842 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000843 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct rt6_info *rt;
846
847 /*
848 * Clone the route.
849 */
850
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000851 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800854 int attempts = !in_softirq();
855
David S. Miller38308472011-12-03 18:02:47 -0500856 if (!(rt->rt6i_flags & RTF_GATEWAY)) {
David S. Millerbb3c3682011-12-13 17:35:06 -0500857 if (ort->rt6i_dst.plen != 128 &&
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000858 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900859 rt->rt6i_flags |= RTF_ANYCAST;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000860 rt->rt6i_gateway = *daddr;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865#ifdef CONFIG_IPV6_SUBTREES
866 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000867 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 rt->rt6i_src.plen = 128;
869 }
870#endif
871
David S. Miller14deae42009-01-04 16:04:39 -0800872 retry:
David S. Miller8ade06c2011-12-29 18:51:57 -0500873 if (rt6_bind_neighbour(rt, rt->dst.dev)) {
David S. Millerd1918542011-12-28 20:19:20 -0500874 struct net *net = dev_net(rt->dst.dev);
David S. Miller14deae42009-01-04 16:04:39 -0800875 int saved_rt_min_interval =
876 net->ipv6.sysctl.ip6_rt_gc_min_interval;
877 int saved_rt_elasticity =
878 net->ipv6.sysctl.ip6_rt_gc_elasticity;
879
880 if (attempts-- > 0) {
881 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
882 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
883
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000884 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800885
886 net->ipv6.sysctl.ip6_rt_gc_elasticity =
887 saved_rt_elasticity;
888 net->ipv6.sysctl.ip6_rt_gc_min_interval =
889 saved_rt_min_interval;
890 goto retry;
891 }
892
Joe Perchesf3213832012-05-15 14:11:53 +0000893 net_warn_ratelimited("Neighbour table overflow\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700894 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800895 return NULL;
896 }
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800899 return rt;
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000902static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
903 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800904{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000905 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
906
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800907 if (rt) {
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800908 rt->rt6i_flags |= RTF_CACHE;
David S. Miller97cac082012-07-02 22:43:47 -0700909 rt->n = neigh_clone(ort->n);
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800910 }
911 return rt;
912}
913
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800914static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500915 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800918 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700919 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800921 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700922 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700924 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700927 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800929restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500930 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700933 rt = rt6_select(fn, oif, strict | reachable);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000934 if (rt->rt6i_nsiblings && oif == 0)
935 rt = rt6_multipath_select(rt, fl6);
David S. Miller4c9483b2011-03-12 16:22:43 -0500936 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800937 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800938 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Changli Gaod8d1f302010-06-10 23:31:35 -0700941 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700942 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800943
David S. Miller97cac082012-07-02 22:43:47 -0700944 if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500945 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800946 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500947 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800948 else
949 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800950
Changli Gaod8d1f302010-06-10 23:31:35 -0700951 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800952 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800953
Changli Gaod8d1f302010-06-10 23:31:35 -0700954 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800955 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700956 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800957 if (!err)
958 goto out2;
959 }
960
961 if (--attempts <= 0)
962 goto out2;
963
964 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700965 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800966 * released someone could insert this route. Relookup.
967 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700968 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800969 goto relookup;
970
971out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800972 if (reachable) {
973 reachable = 0;
974 goto restart_2;
975 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700976 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700977 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700979 rt->dst.lastuse = jiffies;
980 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700981
982 return rt;
983}
984
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800985static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500986 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700987{
David S. Miller4c9483b2011-03-12 16:22:43 -0500988 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700989}
990
Shmulik Ladkani72331bc2012-04-01 04:03:45 +0000991static struct dst_entry *ip6_route_input_lookup(struct net *net,
992 struct net_device *dev,
993 struct flowi6 *fl6, int flags)
994{
995 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
996 flags |= RT6_LOOKUP_F_IFACE;
997
998 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
999}
1000
Thomas Grafc71099a2006-08-04 23:20:06 -07001001void ip6_route_input(struct sk_buff *skb)
1002{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001003 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001004 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001005 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -05001006 struct flowi6 fl6 = {
1007 .flowi6_iif = skb->dev->ifindex,
1008 .daddr = iph->daddr,
1009 .saddr = iph->saddr,
David S. Miller38308472011-12-03 18:02:47 -05001010 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
David S. Miller4c9483b2011-03-12 16:22:43 -05001011 .flowi6_mark = skb->mark,
1012 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001013 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001014
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001015 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07001016}
1017
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001018static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001019 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07001020{
David S. Miller4c9483b2011-03-12 16:22:43 -05001021 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07001022}
1023
Florian Westphal9c7a4f9c2011-03-22 19:17:36 -07001024struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001025 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -07001026{
1027 int flags = 0;
1028
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001029 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00001030
David S. Miller4c9483b2011-03-12 16:22:43 -05001031 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001032 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07001033
David S. Miller4c9483b2011-03-12 16:22:43 -05001034 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -07001035 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00001036 else if (sk)
1037 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07001038
David S. Miller4c9483b2011-03-12 16:22:43 -05001039 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001042EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
David S. Miller2774c132011-03-01 14:59:04 -08001044struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07001045{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001046 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07001047 struct dst_entry *new = NULL;
1048
David S. Millerf5b0a872012-07-19 12:31:33 -07001049 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07001050 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001051 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07001052
Steffen Klassert81048912012-07-05 23:37:09 +00001053 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
1054 rt6_init_peer(rt, net->ipv6.peers);
1055
David S. Miller14e50e52007-05-24 18:17:54 -07001056 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08001057 new->input = dst_discard;
1058 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -07001059
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001060 if (dst_metrics_read_only(&ort->dst))
1061 new->_metrics = ort->dst._metrics;
1062 else
1063 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07001064 rt->rt6i_idev = ort->rt6i_idev;
1065 if (rt->rt6i_idev)
1066 in6_dev_hold(rt->rt6i_idev);
David S. Miller14e50e52007-05-24 18:17:54 -07001067
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001068 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001069 rt->rt6i_flags = ort->rt6i_flags;
1070 rt6_clean_expires(rt);
David S. Miller14e50e52007-05-24 18:17:54 -07001071 rt->rt6i_metric = 0;
1072
1073 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1074#ifdef CONFIG_IPV6_SUBTREES
1075 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1076#endif
1077
1078 dst_free(new);
1079 }
1080
David S. Miller69ead7a2011-03-01 14:45:33 -08001081 dst_release(dst_orig);
1082 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07001083}
David S. Miller14e50e52007-05-24 18:17:54 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/*
1086 * Destination cache support functions
1087 */
1088
1089static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1090{
1091 struct rt6_info *rt;
1092
1093 rt = (struct rt6_info *) dst;
1094
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00001095 /* All IPV6 dsts are created with ->obsolete set to the value
1096 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1097 * into this function always.
1098 */
1099 if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
1100 return NULL;
1101
David S. Miller6431cbc2011-02-07 20:38:06 -08001102 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
1103 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
David S. Miller97bab732012-06-09 22:36:36 -07001104 if (!rt6_has_peer(rt))
David S. Miller6431cbc2011-02-07 20:38:06 -08001105 rt6_bind_peer(rt, 0);
1106 rt->rt6i_peer_genid = rt6_peer_genid();
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -08001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return NULL;
1111}
1112
1113static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1114{
1115 struct rt6_info *rt = (struct rt6_info *) dst;
1116
1117 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001118 if (rt->rt6i_flags & RTF_CACHE) {
1119 if (rt6_check_expired(rt)) {
1120 ip6_del_rt(rt);
1121 dst = NULL;
1122 }
1123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001125 dst = NULL;
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001128 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131static void ip6_link_failure(struct sk_buff *skb)
1132{
1133 struct rt6_info *rt;
1134
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001135 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Eric Dumazetadf30902009-06-02 05:19:30 +00001137 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +00001139 if (rt->rt6i_flags & RTF_CACHE)
1140 rt6_update_expires(rt, 0);
1141 else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 rt->rt6i_node->fn_sernum = -1;
1143 }
1144}
1145
David S. Miller6700c272012-07-17 03:29:28 -07001146static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1147 struct sk_buff *skb, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct rt6_info *rt6 = (struct rt6_info*)dst;
1150
David S. Miller81aded22012-06-15 14:54:11 -07001151 dst_confirm(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
David S. Miller81aded22012-06-15 14:54:11 -07001153 struct net *net = dev_net(dst->dev);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rt6->rt6i_flags |= RTF_MODIFIED;
1156 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -08001157 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -08001159 features |= RTAX_FEATURE_ALLFRAG;
1160 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
David S. Millerdefb3512010-12-08 21:16:57 -08001162 dst_metric_set(dst, RTAX_MTU, mtu);
David S. Miller81aded22012-06-15 14:54:11 -07001163 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165}
1166
David S. Miller42ae66c2012-06-15 20:01:57 -07001167void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1168 int oif, u32 mark)
David S. Miller81aded22012-06-15 14:54:11 -07001169{
1170 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1171 struct dst_entry *dst;
1172 struct flowi6 fl6;
1173
1174 memset(&fl6, 0, sizeof(fl6));
1175 fl6.flowi6_oif = oif;
1176 fl6.flowi6_mark = mark;
David S. Miller3e129392012-07-10 04:01:57 -07001177 fl6.flowi6_flags = 0;
David S. Miller81aded22012-06-15 14:54:11 -07001178 fl6.daddr = iph->daddr;
1179 fl6.saddr = iph->saddr;
1180 fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
1181
1182 dst = ip6_route_output(net, NULL, &fl6);
1183 if (!dst->error)
David S. Miller6700c272012-07-17 03:29:28 -07001184 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07001185 dst_release(dst);
1186}
1187EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1188
1189void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1190{
1191 ip6_update_pmtu(skb, sock_net(sk), mtu,
1192 sk->sk_bound_dev_if, sk->sk_mark);
1193}
1194EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1195
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001196void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1197{
1198 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1199 struct dst_entry *dst;
1200 struct flowi6 fl6;
1201
1202 memset(&fl6, 0, sizeof(fl6));
1203 fl6.flowi6_oif = oif;
1204 fl6.flowi6_mark = mark;
1205 fl6.flowi6_flags = 0;
1206 fl6.daddr = iph->daddr;
1207 fl6.saddr = iph->saddr;
1208 fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
1209
1210 dst = ip6_route_output(net, NULL, &fl6);
1211 if (!dst->error)
David S. Miller6700c272012-07-17 03:29:28 -07001212 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07001213 dst_release(dst);
1214}
1215EXPORT_SYMBOL_GPL(ip6_redirect);
1216
1217void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1218{
1219 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1220}
1221EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1222
David S. Miller0dbaee32010-12-13 12:52:14 -08001223static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
David S. Miller0dbaee32010-12-13 12:52:14 -08001225 struct net_device *dev = dst->dev;
1226 unsigned int mtu = dst_mtu(dst);
1227 struct net *net = dev_net(dev);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1230
Daniel Lezcano55786892008-03-04 13:47:47 -08001231 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1232 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001235 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1236 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1237 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * rely only on pmtu discovery"
1239 */
1240 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1241 mtu = IPV6_MAXPLEN;
1242 return mtu;
1243}
1244
Steffen Klassertebb762f2011-11-23 02:12:51 +00001245static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001246{
David S. Millerd33e4552010-12-14 13:01:14 -08001247 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001248 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1249
1250 if (mtu)
1251 return mtu;
1252
1253 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001254
1255 rcu_read_lock();
1256 idev = __in6_dev_get(dst->dev);
1257 if (idev)
1258 mtu = idev->cnf.mtu6;
1259 rcu_read_unlock();
1260
1261 return mtu;
1262}
1263
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001264static struct dst_entry *icmp6_dst_gc_list;
1265static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001266
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001267struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct neighbour *neigh,
David S. Miller87a11572011-12-06 17:04:13 -05001269 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
David S. Miller87a11572011-12-06 17:04:13 -05001271 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct rt6_info *rt;
1273 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001274 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
David S. Miller38308472011-12-03 18:02:47 -05001276 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00001277 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
David S. Miller8b96d222012-06-11 02:01:56 -07001279 rt = ip6_dst_alloc(net, dev, 0, NULL);
David S. Miller38308472011-12-03 18:02:47 -05001280 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001282 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto out;
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (neigh)
1287 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001288 else {
David S. Millerf894cbf2012-07-02 21:52:24 -07001289 neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
David S. Millerb43faac2011-12-13 16:48:21 -05001290 if (IS_ERR(neigh)) {
RongQing.Li252c3d82012-01-12 22:33:46 +00001291 in6_dev_put(idev);
David S. Millerb43faac2011-12-13 16:48:21 -05001292 dst_free(&rt->dst);
1293 return ERR_CAST(neigh);
1294 }
David S. Miller14deae42009-01-04 16:04:39 -08001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001297 rt->dst.flags |= DST_HOST;
1298 rt->dst.output = ip6_output;
David S. Miller97cac082012-07-02 22:43:47 -07001299 rt->n = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001300 atomic_set(&rt->dst.__refcnt, 1);
David S. Miller87a11572011-12-06 17:04:13 -05001301 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001302 rt->rt6i_dst.plen = 128;
1303 rt->rt6i_idev = idev;
Gao feng70116872011-10-28 02:46:57 +00001304 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001306 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001307 rt->dst.next = icmp6_dst_gc_list;
1308 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001309 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Daniel Lezcano55786892008-03-04 13:47:47 -08001311 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
David S. Miller87a11572011-12-06 17:04:13 -05001313 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315out:
David S. Miller87a11572011-12-06 17:04:13 -05001316 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001319int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Hagen Paul Pfeifere9476e952011-02-25 05:45:19 +00001321 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001322 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001324 spin_lock_bh(&icmp6_dst_lock);
1325 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 while ((dst = *pprev) != NULL) {
1328 if (!atomic_read(&dst->__refcnt)) {
1329 *pprev = dst->next;
1330 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 } else {
1332 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001333 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 }
1336
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001337 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001338
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001339 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
David S. Miller1e493d12008-09-10 17:27:15 -07001342static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1343 void *arg)
1344{
1345 struct dst_entry *dst, **pprev;
1346
1347 spin_lock_bh(&icmp6_dst_lock);
1348 pprev = &icmp6_dst_gc_list;
1349 while ((dst = *pprev) != NULL) {
1350 struct rt6_info *rt = (struct rt6_info *) dst;
1351 if (func(rt, arg)) {
1352 *pprev = dst->next;
1353 dst_free(dst);
1354 } else {
1355 pprev = &dst->next;
1356 }
1357 }
1358 spin_unlock_bh(&icmp6_dst_lock);
1359}
1360
Daniel Lezcano569d3642008-01-18 03:56:57 -08001361static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001364 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001365 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1366 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1367 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1368 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1369 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001370 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Eric Dumazetfc66f952010-10-08 06:37:34 +00001372 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001373 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001374 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out;
1376
Benjamin Thery6891a342008-03-04 13:49:47 -08001377 net->ipv6.ip6_rt_gc_expire++;
1378 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1379 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001380 entries = dst_entries_get_slow(ops);
1381 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001382 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001384 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001385 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/* Clean host part of a prefix. Not necessary in radix tree,
1389 but results in cleaner routing tables.
1390
1391 Remove it only when all the things will work!
1392 */
1393
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001394int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
David S. Miller5170ae82010-12-12 21:35:57 -08001396 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001397 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001398 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001399 struct inet6_dev *idev;
1400
1401 rcu_read_lock();
1402 idev = __in6_dev_get(dev);
1403 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001404 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001405 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001406 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 return hoplimit;
1410}
David S. Millerabbf46a2010-12-12 21:14:46 -08001411EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413/*
1414 *
1415 */
1416
Thomas Graf86872cb2006-08-22 00:01:08 -07001417int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001420 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct rt6_info *rt = NULL;
1422 struct net_device *dev = NULL;
1423 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001424 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int addr_type;
1426
Thomas Graf86872cb2006-08-22 00:01:08 -07001427 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EINVAL;
1429#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001430 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EINVAL;
1432#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001433 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001435 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!dev)
1437 goto out;
1438 idev = in6_dev_get(dev);
1439 if (!idev)
1440 goto out;
1441 }
1442
Thomas Graf86872cb2006-08-22 00:01:08 -07001443 if (cfg->fc_metric == 0)
1444 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Matti Vaittinend71314b2011-11-14 00:14:49 +00001446 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001447 if (cfg->fc_nlinfo.nlh &&
1448 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001449 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001450 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00001451 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00001452 table = fib6_new_table(net, cfg->fc_table);
1453 }
1454 } else {
1455 table = fib6_new_table(net, cfg->fc_table);
1456 }
David S. Miller38308472011-12-03 18:02:47 -05001457
1458 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001459 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001460
David S. Miller8b96d222012-06-11 02:01:56 -07001461 rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
David S. Miller38308472011-12-03 18:02:47 -05001463 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 err = -ENOMEM;
1465 goto out;
1466 }
1467
Gao feng1716a962012-04-06 00:13:10 +00001468 if (cfg->fc_flags & RTF_EXPIRES)
1469 rt6_set_expires(rt, jiffies +
1470 clock_t_to_jiffies(cfg->fc_expires));
1471 else
1472 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Thomas Graf86872cb2006-08-22 00:01:08 -07001474 if (cfg->fc_protocol == RTPROT_UNSPEC)
1475 cfg->fc_protocol = RTPROT_BOOT;
1476 rt->rt6i_protocol = cfg->fc_protocol;
1477
1478 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001481 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001482 else if (cfg->fc_flags & RTF_LOCAL)
1483 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001485 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Changli Gaod8d1f302010-06-10 23:31:35 -07001487 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Thomas Graf86872cb2006-08-22 00:01:08 -07001489 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1490 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001492 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001494 if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1495 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1496 if (!metrics) {
1497 err = -ENOMEM;
1498 goto out;
1499 }
1500 dst_init_metrics(&rt->dst, metrics, 0);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001503 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1504 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505#endif
1506
Thomas Graf86872cb2006-08-22 00:01:08 -07001507 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /* We cannot add true routes via loopback here,
1510 they would result in kernel looping; promote them to reject routes
1511 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001512 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001513 (dev && (dev->flags & IFF_LOOPBACK) &&
1514 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1515 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001517 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (dev) {
1519 dev_put(dev);
1520 in6_dev_put(idev);
1521 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001522 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 dev_hold(dev);
1524 idev = in6_dev_get(dev);
1525 if (!idev) {
1526 err = -ENODEV;
1527 goto out;
1528 }
1529 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001530 rt->dst.output = ip6_pkt_discard_out;
1531 rt->dst.input = ip6_pkt_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001533 switch (cfg->fc_type) {
1534 case RTN_BLACKHOLE:
1535 rt->dst.error = -EINVAL;
1536 break;
1537 case RTN_PROHIBIT:
1538 rt->dst.error = -EACCES;
1539 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00001540 case RTN_THROW:
1541 rt->dst.error = -EAGAIN;
1542 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00001543 default:
1544 rt->dst.error = -ENETUNREACH;
1545 break;
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto install_route;
1548 }
1549
Thomas Graf86872cb2006-08-22 00:01:08 -07001550 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001551 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int gwa_type;
1553
Thomas Graf86872cb2006-08-22 00:01:08 -07001554 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001555 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 gwa_type = ipv6_addr_type(gw_addr);
1557
1558 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1559 struct rt6_info *grt;
1560
1561 /* IPv6 strictly inhibits using not link-local
1562 addresses as nexthop address.
1563 Otherwise, router will not able to send redirects.
1564 It is very good, but in some (rare!) circumstances
1565 (SIT, PtP, NBMA NOARP links) it is handy to allow
1566 some exceptions. --ANK
1567 */
1568 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001569 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
1571
Daniel Lezcano55786892008-03-04 13:47:47 -08001572 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001575 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto out;
1577 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001578 if (dev != grt->dst.dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001579 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto out;
1581 }
1582 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001583 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 idev = grt->rt6i_idev;
1585 dev_hold(dev);
1586 in6_dev_hold(grt->rt6i_idev);
1587 }
David S. Miller38308472011-12-03 18:02:47 -05001588 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001590 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (err)
1593 goto out;
1594 }
1595 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001596 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto out;
1598 }
1599
1600 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001601 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto out;
1603
Daniel Walterc3968a82011-04-13 21:10:57 +00001604 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1605 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1606 err = -EINVAL;
1607 goto out;
1608 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001609 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001610 rt->rt6i_prefsrc.plen = 128;
1611 } else
1612 rt->rt6i_prefsrc.plen = 0;
1613
Thomas Graf86872cb2006-08-22 00:01:08 -07001614 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
David S. Miller8ade06c2011-12-29 18:51:57 -05001615 err = rt6_bind_neighbour(rt, dev);
David S. Millerf83c7792011-12-28 15:41:23 -05001616 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619
Thomas Graf86872cb2006-08-22 00:01:08 -07001620 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001623 if (cfg->fc_mx) {
1624 struct nlattr *nla;
1625 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Thomas Graf86872cb2006-08-22 00:01:08 -07001627 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001628 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001629
1630 if (type) {
1631 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 err = -EINVAL;
1633 goto out;
1634 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001635
David S. Millerdefb3512010-12-08 21:16:57 -08001636 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 }
1640
Changli Gaod8d1f302010-06-10 23:31:35 -07001641 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001643 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001644
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001645 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001646
Thomas Graf86872cb2006-08-22 00:01:08 -07001647 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649out:
1650 if (dev)
1651 dev_put(dev);
1652 if (idev)
1653 in6_dev_put(idev);
1654 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001655 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return err;
1657}
1658
Thomas Graf86872cb2006-08-22 00:01:08 -07001659static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001662 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001663 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Gao feng6825a262012-09-19 19:25:34 +00001665 if (rt == net->ipv6.ip6_null_entry) {
1666 err = -ENOENT;
1667 goto out;
1668 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07001669
Thomas Grafc71099a2006-08-04 23:20:06 -07001670 table = rt->rt6i_table;
1671 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07001672 err = fib6_del(rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -07001673 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Gao feng6825a262012-09-19 19:25:34 +00001675out:
1676 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return err;
1678}
1679
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001680int ip6_del_rt(struct rt6_info *rt)
1681{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001682 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001683 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001684 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001685 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001686}
1687
Thomas Graf86872cb2006-08-22 00:01:08 -07001688static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Thomas Grafc71099a2006-08-04 23:20:06 -07001690 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct fib6_node *fn;
1692 struct rt6_info *rt;
1693 int err = -ESRCH;
1694
Daniel Lezcano55786892008-03-04 13:47:47 -08001695 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001696 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Thomas Grafc71099a2006-08-04 23:20:06 -07001699 read_lock_bh(&table->tb6_lock);
1700
1701 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001702 &cfg->fc_dst, cfg->fc_dst_len,
1703 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001706 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001707 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001708 (!rt->dst.dev ||
1709 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001711 if (cfg->fc_flags & RTF_GATEWAY &&
1712 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001714 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001716 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001717 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Thomas Graf86872cb2006-08-22 00:01:08 -07001719 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001722 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 return err;
1725}
1726
David S. Miller6700c272012-07-17 03:29:28 -07001727static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001728{
David S. Millere8599ff2012-07-11 23:43:53 -07001729 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001730 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07001731 struct rt6_info *rt, *nrt = NULL;
1732 const struct in6_addr *target;
David S. Millere8599ff2012-07-11 23:43:53 -07001733 struct ndisc_options ndopts;
David S. Miller6e157b62012-07-12 00:05:02 -07001734 const struct in6_addr *dest;
1735 struct neighbour *old_neigh;
David S. Millere8599ff2012-07-11 23:43:53 -07001736 struct inet6_dev *in6_dev;
1737 struct neighbour *neigh;
1738 struct icmp6hdr *icmph;
David S. Miller6e157b62012-07-12 00:05:02 -07001739 int optlen, on_link;
1740 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07001741
1742 optlen = skb->tail - skb->transport_header;
1743 optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1744
1745 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07001746 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001747 return;
1748 }
1749
1750 icmph = icmp6_hdr(skb);
1751 target = (const struct in6_addr *) (icmph + 1);
1752 dest = target + 1;
1753
1754 if (ipv6_addr_is_multicast(dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001755 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001756 return;
1757 }
1758
David S. Miller6e157b62012-07-12 00:05:02 -07001759 on_link = 0;
David S. Millere8599ff2012-07-11 23:43:53 -07001760 if (ipv6_addr_equal(dest, target)) {
1761 on_link = 1;
1762 } else if (ipv6_addr_type(target) !=
1763 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07001764 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07001765 return;
1766 }
1767
1768 in6_dev = __in6_dev_get(skb->dev);
1769 if (!in6_dev)
1770 return;
1771 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1772 return;
1773
1774 /* RFC2461 8.1:
1775 * The IP source address of the Redirect MUST be the same as the current
1776 * first-hop router for the specified ICMP Destination Address.
1777 */
1778
1779 if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
1780 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1781 return;
1782 }
David S. Miller6e157b62012-07-12 00:05:02 -07001783
1784 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07001785 if (ndopts.nd_opts_tgt_lladdr) {
1786 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1787 skb->dev);
1788 if (!lladdr) {
1789 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1790 return;
1791 }
1792 }
1793
David S. Miller6e157b62012-07-12 00:05:02 -07001794 rt = (struct rt6_info *) dst;
1795 if (rt == net->ipv6.ip6_null_entry) {
1796 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1797 return;
1798 }
1799
1800 /* Redirect received -> path was valid.
1801 * Look, redirects are sent only in response to data packets,
1802 * so that this nexthop apparently is reachable. --ANK
1803 */
1804 dst_confirm(&rt->dst);
1805
David S. Millere8599ff2012-07-11 23:43:53 -07001806 neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
1807 if (!neigh)
1808 return;
1809
David S. Miller6e157b62012-07-12 00:05:02 -07001810 /* Duplicate redirect: silently ignore. */
1811 old_neigh = rt->n;
1812 if (neigh == old_neigh)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * We have finally decided to accept it.
1817 */
1818
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001819 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1821 NEIGH_UPDATE_F_OVERRIDE|
1822 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1823 NEIGH_UPDATE_F_ISROUTER))
1824 );
1825
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001826 nrt = ip6_rt_copy(rt, dest);
David S. Miller38308472011-12-03 18:02:47 -05001827 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto out;
1829
1830 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1831 if (on_link)
1832 nrt->rt6i_flags &= ~RTF_GATEWAY;
1833
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001834 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
David S. Miller97cac082012-07-02 22:43:47 -07001835 nrt->n = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Thomas Graf40e22e82006-08-22 00:00:45 -07001837 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto out;
1839
Changli Gaod8d1f302010-06-10 23:31:35 -07001840 netevent.old = &rt->dst;
David S. Miller1d248b12012-07-03 01:01:51 -07001841 netevent.old_neigh = old_neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001842 netevent.new = &nrt->dst;
David S. Miller1d248b12012-07-03 01:01:51 -07001843 netevent.new_neigh = neigh;
1844 netevent.daddr = dest;
Tom Tucker8d717402006-07-30 20:43:36 -07001845 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1846
David S. Miller38308472011-12-03 18:02:47 -05001847 if (rt->rt6i_flags & RTF_CACHE) {
David S. Miller6e157b62012-07-12 00:05:02 -07001848 rt = (struct rt6_info *) dst_clone(&rt->dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001849 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
1852out:
David S. Millere8599ff2012-07-11 23:43:53 -07001853 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07001854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 * Misc support functions
1858 */
1859
Gao feng1716a962012-04-06 00:13:10 +00001860static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001861 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
David S. Millerd1918542011-12-28 20:19:20 -05001863 struct net *net = dev_net(ort->dst.dev);
David S. Miller8b96d222012-06-11 02:01:56 -07001864 struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1865 ort->rt6i_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001868 rt->dst.input = ort->dst.input;
1869 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001870 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001872 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001873 rt->rt6i_dst.plen = 128;
David S. Millerdefb3512010-12-08 21:16:57 -08001874 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001875 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 rt->rt6i_idev = ort->rt6i_idev;
1877 if (rt->rt6i_idev)
1878 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001879 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001881 rt->rt6i_gateway = ort->rt6i_gateway;
Gao feng1716a962012-04-06 00:13:10 +00001882 rt->rt6i_flags = ort->rt6i_flags;
1883 if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
1884 (RTF_DEFAULT | RTF_ADDRCONF))
1885 rt6_set_from(rt, ort);
1886 else
1887 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 rt->rt6i_metric = 0;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890#ifdef CONFIG_IPV6_SUBTREES
1891 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1892#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001893 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001894 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 return rt;
1897}
1898
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001899#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001900static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001901 const struct in6_addr *prefix, int prefixlen,
1902 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001903{
1904 struct fib6_node *fn;
1905 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001906 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001907
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001908 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05001909 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001910 return NULL;
1911
Li RongQing5744dd92012-09-11 21:59:01 +00001912 read_lock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001913 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001914 if (!fn)
1915 goto out;
1916
Changli Gaod8d1f302010-06-10 23:31:35 -07001917 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001918 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001919 continue;
1920 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1921 continue;
1922 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1923 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001924 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001925 break;
1926 }
1927out:
Li RongQing5744dd92012-09-11 21:59:01 +00001928 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001929 return rt;
1930}
1931
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001932static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001933 const struct in6_addr *prefix, int prefixlen,
1934 const struct in6_addr *gwaddr, int ifindex,
Eric Dumazet95c96172012-04-15 05:58:06 +00001935 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001936{
Thomas Graf86872cb2006-08-22 00:01:08 -07001937 struct fib6_config cfg = {
1938 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001939 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001940 .fc_ifindex = ifindex,
1941 .fc_dst_len = prefixlen,
1942 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1943 RTF_UP | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00001944 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001945 .fc_nlinfo.nlh = NULL,
1946 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001947 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001948
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001949 cfg.fc_dst = *prefix;
1950 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07001951
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001952 /* We should treat it as a default route if prefix length is 0. */
1953 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001954 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001955
Thomas Graf86872cb2006-08-22 00:01:08 -07001956 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001957
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001958 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001959}
1960#endif
1961
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001962struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001965 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001967 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05001968 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001969 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Li RongQing5744dd92012-09-11 21:59:01 +00001971 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001972 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001973 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001974 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1976 break;
1977 }
1978 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001979 dst_hold(&rt->dst);
Li RongQing5744dd92012-09-11 21:59:01 +00001980 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return rt;
1982}
1983
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001984struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001985 struct net_device *dev,
1986 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Thomas Graf86872cb2006-08-22 00:01:08 -07001988 struct fib6_config cfg = {
1989 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001990 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001991 .fc_ifindex = dev->ifindex,
1992 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1993 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Eric W. Biederman15e47302012-09-07 20:12:54 +00001994 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08001995 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001996 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001997 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001999 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Thomas Graf86872cb2006-08-22 00:01:08 -07002001 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return rt6_get_dflt_router(gwaddr, dev);
2004}
2005
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002006void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002009 struct fib6_table *table;
2010
2011 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08002012 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05002013 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07002017 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07002018 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002020 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07002021 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07002022 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto restart;
2024 }
2025 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002026 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Daniel Lezcano55786892008-03-04 13:47:47 -08002029static void rtmsg_to_fib6_config(struct net *net,
2030 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07002031 struct fib6_config *cfg)
2032{
2033 memset(cfg, 0, sizeof(*cfg));
2034
2035 cfg->fc_table = RT6_TABLE_MAIN;
2036 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
2037 cfg->fc_metric = rtmsg->rtmsg_metric;
2038 cfg->fc_expires = rtmsg->rtmsg_info;
2039 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
2040 cfg->fc_src_len = rtmsg->rtmsg_src_len;
2041 cfg->fc_flags = rtmsg->rtmsg_flags;
2042
Daniel Lezcano55786892008-03-04 13:47:47 -08002043 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08002044
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002045 cfg->fc_dst = rtmsg->rtmsg_dst;
2046 cfg->fc_src = rtmsg->rtmsg_src;
2047 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07002048}
2049
Daniel Lezcano55786892008-03-04 13:47:47 -08002050int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Thomas Graf86872cb2006-08-22 00:01:08 -07002052 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct in6_rtmsg rtmsg;
2054 int err;
2055
2056 switch(cmd) {
2057 case SIOCADDRT: /* Add a route */
2058 case SIOCDELRT: /* Delete a route */
2059 if (!capable(CAP_NET_ADMIN))
2060 return -EPERM;
2061 err = copy_from_user(&rtmsg, arg,
2062 sizeof(struct in6_rtmsg));
2063 if (err)
2064 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07002065
Daniel Lezcano55786892008-03-04 13:47:47 -08002066 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 rtnl_lock();
2069 switch (cmd) {
2070 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002071 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 break;
2073 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002074 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 break;
2076 default:
2077 err = -EINVAL;
2078 }
2079 rtnl_unlock();
2080
2081 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 return -EINVAL;
2085}
2086
2087/*
2088 * Drop the packet on the floor
2089 */
2090
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002091static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002093 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002094 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002095 switch (ipstats_mib_noroutes) {
2096 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002097 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002098 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002099 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2100 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002101 break;
2102 }
2103 /* FALLTHROUGH */
2104 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002105 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2106 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002107 break;
2108 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002109 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 kfree_skb(skb);
2111 return 0;
2112}
2113
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002114static int ip6_pkt_discard(struct sk_buff *skb)
2115{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002116 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002117}
2118
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002119static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Eric Dumazetadf30902009-06-02 05:19:30 +00002121 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002122 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
David S. Miller6723ab52006-10-18 21:20:57 -07002125#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2126
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002127static int ip6_pkt_prohibit(struct sk_buff *skb)
2128{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002129 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002130}
2131
2132static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2133{
Eric Dumazetadf30902009-06-02 05:19:30 +00002134 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002135 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002136}
2137
David S. Miller6723ab52006-10-18 21:20:57 -07002138#endif
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/*
2141 * Allocate a dst for local (unicast / anycast) address.
2142 */
2143
2144struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2145 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002146 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002148 struct net *net = dev_net(idev->dev);
David S. Miller8b96d222012-06-11 02:01:56 -07002149 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
David S. Millerf83c7792011-12-28 15:41:23 -05002150 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
David S. Miller38308472011-12-03 18:02:47 -05002152 if (!rt) {
Joe Perchesf3213832012-05-15 14:11:53 +00002153 net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 in6_dev_hold(idev);
2158
David S. Miller11d53b42011-06-24 15:23:34 -07002159 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002160 rt->dst.input = ip6_input;
2161 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002165 if (anycast)
2166 rt->rt6i_flags |= RTF_ANYCAST;
2167 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller8ade06c2011-12-29 18:51:57 -05002169 err = rt6_bind_neighbour(rt, rt->dst.dev);
David S. Millerf83c7792011-12-28 15:41:23 -05002170 if (err) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002171 dst_free(&rt->dst);
David S. Millerf83c7792011-12-28 15:41:23 -05002172 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002175 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002177 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Changli Gaod8d1f302010-06-10 23:31:35 -07002179 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 return rt;
2182}
2183
Daniel Walterc3968a82011-04-13 21:10:57 +00002184int ip6_route_get_saddr(struct net *net,
2185 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002186 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002187 unsigned int prefs,
2188 struct in6_addr *saddr)
2189{
2190 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2191 int err = 0;
2192 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002193 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002194 else
2195 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2196 daddr, prefs, saddr);
2197 return err;
2198}
2199
2200/* remove deleted ip from prefsrc entries */
2201struct arg_dev_net_ip {
2202 struct net_device *dev;
2203 struct net *net;
2204 struct in6_addr *addr;
2205};
2206
2207static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2208{
2209 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2210 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2211 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2212
David S. Millerd1918542011-12-28 20:19:20 -05002213 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002214 rt != net->ipv6.ip6_null_entry &&
2215 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2216 /* remove prefsrc entry */
2217 rt->rt6i_prefsrc.plen = 0;
2218 }
2219 return 0;
2220}
2221
2222void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2223{
2224 struct net *net = dev_net(ifp->idev->dev);
2225 struct arg_dev_net_ip adni = {
2226 .dev = ifp->idev->dev,
2227 .net = net,
2228 .addr = &ifp->addr,
2229 };
2230 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2231}
2232
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002233struct arg_dev_net {
2234 struct net_device *dev;
2235 struct net *net;
2236};
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238static int fib6_ifdown(struct rt6_info *rt, void *arg)
2239{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002240 const struct arg_dev_net *adn = arg;
2241 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002242
David S. Millerd1918542011-12-28 20:19:20 -05002243 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002244 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248}
2249
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002250void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002252 struct arg_dev_net adn = {
2253 .dev = dev,
2254 .net = net,
2255 };
2256
2257 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002258 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Eric Dumazet95c96172012-04-15 05:58:06 +00002261struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00002263 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264};
2265
2266static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2267{
2268 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2269 struct inet6_dev *idev;
2270
2271 /* In IPv6 pmtu discovery is not optional,
2272 so that RTAX_MTU lock cannot disable it.
2273 We still use this lock to block changes
2274 caused by addrconf/ndisc.
2275 */
2276
2277 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002278 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return 0;
2280
2281 /* For administrative MTU increase, there is no way to discover
2282 IPv6 PMTU increase, so PMTU increase should be updated here.
2283 Since RFC 1981 doesn't include administrative MTU increase
2284 update PMTU increase is a MUST. (i.e. jumbo frame)
2285 */
2286 /*
2287 If new MTU is less than route PMTU, this new MTU will be the
2288 lowest MTU in the path, update the route PMTU to reflect PMTU
2289 decreases; if new MTU is greater than route PMTU, and the
2290 old MTU is the lowest MTU in the path, update the route PMTU
2291 to reflect the increase. In this case if the other nodes' MTU
2292 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2293 PMTU discouvery.
2294 */
David S. Millerd1918542011-12-28 20:19:20 -05002295 if (rt->dst.dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002296 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2297 (dst_mtu(&rt->dst) >= arg->mtu ||
2298 (dst_mtu(&rt->dst) < arg->mtu &&
2299 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002300 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return 0;
2303}
2304
Eric Dumazet95c96172012-04-15 05:58:06 +00002305void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Thomas Grafc71099a2006-08-04 23:20:06 -07002307 struct rt6_mtu_change_arg arg = {
2308 .dev = dev,
2309 .mtu = mtu,
2310 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002312 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002315static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002316 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002317 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002318 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002319 [RTA_PRIORITY] = { .type = NLA_U32 },
2320 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002321 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002322};
2323
2324static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2325 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Thomas Graf86872cb2006-08-22 00:01:08 -07002327 struct rtmsg *rtm;
2328 struct nlattr *tb[RTA_MAX+1];
2329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Thomas Graf86872cb2006-08-22 00:01:08 -07002331 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2332 if (err < 0)
2333 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Thomas Graf86872cb2006-08-22 00:01:08 -07002335 err = -EINVAL;
2336 rtm = nlmsg_data(nlh);
2337 memset(cfg, 0, sizeof(*cfg));
2338
2339 cfg->fc_table = rtm->rtm_table;
2340 cfg->fc_dst_len = rtm->rtm_dst_len;
2341 cfg->fc_src_len = rtm->rtm_src_len;
2342 cfg->fc_flags = RTF_UP;
2343 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002344 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07002345
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002346 if (rtm->rtm_type == RTN_UNREACHABLE ||
2347 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002348 rtm->rtm_type == RTN_PROHIBIT ||
2349 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07002350 cfg->fc_flags |= RTF_REJECT;
2351
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002352 if (rtm->rtm_type == RTN_LOCAL)
2353 cfg->fc_flags |= RTF_LOCAL;
2354
Eric W. Biederman15e47302012-09-07 20:12:54 +00002355 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07002356 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002357 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002358
2359 if (tb[RTA_GATEWAY]) {
2360 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2361 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002363
2364 if (tb[RTA_DST]) {
2365 int plen = (rtm->rtm_dst_len + 7) >> 3;
2366
2367 if (nla_len(tb[RTA_DST]) < plen)
2368 goto errout;
2369
2370 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002372
2373 if (tb[RTA_SRC]) {
2374 int plen = (rtm->rtm_src_len + 7) >> 3;
2375
2376 if (nla_len(tb[RTA_SRC]) < plen)
2377 goto errout;
2378
2379 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002381
Daniel Walterc3968a82011-04-13 21:10:57 +00002382 if (tb[RTA_PREFSRC])
2383 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2384
Thomas Graf86872cb2006-08-22 00:01:08 -07002385 if (tb[RTA_OIF])
2386 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2387
2388 if (tb[RTA_PRIORITY])
2389 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2390
2391 if (tb[RTA_METRICS]) {
2392 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2393 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002395
2396 if (tb[RTA_TABLE])
2397 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2398
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002399 if (tb[RTA_MULTIPATH]) {
2400 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2401 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2402 }
2403
Thomas Graf86872cb2006-08-22 00:01:08 -07002404 err = 0;
2405errout:
2406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002409static int ip6_route_multipath(struct fib6_config *cfg, int add)
2410{
2411 struct fib6_config r_cfg;
2412 struct rtnexthop *rtnh;
2413 int remaining;
2414 int attrlen;
2415 int err = 0, last_err = 0;
2416
2417beginning:
2418 rtnh = (struct rtnexthop *)cfg->fc_mp;
2419 remaining = cfg->fc_mp_len;
2420
2421 /* Parse a Multipath Entry */
2422 while (rtnh_ok(rtnh, remaining)) {
2423 memcpy(&r_cfg, cfg, sizeof(*cfg));
2424 if (rtnh->rtnh_ifindex)
2425 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2426
2427 attrlen = rtnh_attrlen(rtnh);
2428 if (attrlen > 0) {
2429 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2430
2431 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2432 if (nla) {
2433 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2434 r_cfg.fc_flags |= RTF_GATEWAY;
2435 }
2436 }
2437 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2438 if (err) {
2439 last_err = err;
2440 /* If we are trying to remove a route, do not stop the
2441 * loop when ip6_route_del() fails (because next hop is
2442 * already gone), we should try to remove all next hops.
2443 */
2444 if (add) {
2445 /* If add fails, we should try to delete all
2446 * next hops that have been already added.
2447 */
2448 add = 0;
2449 goto beginning;
2450 }
2451 }
Nicolas Dichtel1a724182012-11-01 22:58:22 +00002452 /* Because each route is added like a single route we remove
2453 * this flag after the first nexthop (if there is a collision,
2454 * we have already fail to add the first nexthop:
2455 * fib6_add_rt2node() has reject it).
2456 */
2457 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002458 rtnh = rtnh_next(rtnh, &remaining);
2459 }
2460
2461 return last_err;
2462}
2463
Thomas Grafc127ea22007-03-22 11:58:32 -07002464static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Thomas Graf86872cb2006-08-22 00:01:08 -07002466 struct fib6_config cfg;
2467 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Thomas Graf86872cb2006-08-22 00:01:08 -07002469 err = rtm_to_fib6_config(skb, nlh, &cfg);
2470 if (err < 0)
2471 return err;
2472
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002473 if (cfg.fc_mp)
2474 return ip6_route_multipath(&cfg, 0);
2475 else
2476 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Thomas Grafc127ea22007-03-22 11:58:32 -07002479static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Thomas Graf86872cb2006-08-22 00:01:08 -07002481 struct fib6_config cfg;
2482 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Thomas Graf86872cb2006-08-22 00:01:08 -07002484 err = rtm_to_fib6_config(skb, nlh, &cfg);
2485 if (err < 0)
2486 return err;
2487
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00002488 if (cfg.fc_mp)
2489 return ip6_route_multipath(&cfg, 1);
2490 else
2491 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Thomas Graf339bf982006-11-10 14:10:15 -08002494static inline size_t rt6_nlmsg_size(void)
2495{
2496 return NLMSG_ALIGN(sizeof(struct rtmsg))
2497 + nla_total_size(16) /* RTA_SRC */
2498 + nla_total_size(16) /* RTA_DST */
2499 + nla_total_size(16) /* RTA_GATEWAY */
2500 + nla_total_size(16) /* RTA_PREFSRC */
2501 + nla_total_size(4) /* RTA_TABLE */
2502 + nla_total_size(4) /* RTA_IIF */
2503 + nla_total_size(4) /* RTA_OIF */
2504 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002505 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002506 + nla_total_size(sizeof(struct rta_cacheinfo));
2507}
2508
Brian Haley191cd582008-08-14 15:33:21 -07002509static int rt6_fill_node(struct net *net,
2510 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002511 struct in6_addr *dst, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002512 int iif, int type, u32 portid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002513 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002516 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002517 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002518 u32 table;
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002519 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 if (prefix) { /* user wants prefix routes only */
2522 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2523 /* success since this is not a prefix route */
2524 return 1;
2525 }
2526 }
2527
Eric W. Biederman15e47302012-09-07 20:12:54 +00002528 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002529 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002530 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002531
2532 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 rtm->rtm_family = AF_INET6;
2534 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2535 rtm->rtm_src_len = rt->rt6i_src.plen;
2536 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002537 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002538 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002539 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002540 table = RT6_TABLE_UNSPEC;
2541 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04002542 if (nla_put_u32(skb, RTA_TABLE, table))
2543 goto nla_put_failure;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002544 if (rt->rt6i_flags & RTF_REJECT) {
2545 switch (rt->dst.error) {
2546 case -EINVAL:
2547 rtm->rtm_type = RTN_BLACKHOLE;
2548 break;
2549 case -EACCES:
2550 rtm->rtm_type = RTN_PROHIBIT;
2551 break;
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00002552 case -EAGAIN:
2553 rtm->rtm_type = RTN_THROW;
2554 break;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00002555 default:
2556 rtm->rtm_type = RTN_UNREACHABLE;
2557 break;
2558 }
2559 }
David S. Miller38308472011-12-03 18:02:47 -05002560 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002561 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002562 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 rtm->rtm_type = RTN_LOCAL;
2564 else
2565 rtm->rtm_type = RTN_UNICAST;
2566 rtm->rtm_flags = 0;
2567 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2568 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002569 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 rtm->rtm_protocol = RTPROT_REDIRECT;
Denis Ovsienkof0396f602012-07-10 04:45:50 +00002571 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2572 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2573 rtm->rtm_protocol = RTPROT_RA;
2574 else
2575 rtm->rtm_protocol = RTPROT_KERNEL;
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
David S. Miller38308472011-12-03 18:02:47 -05002578 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 rtm->rtm_flags |= RTM_F_CLONED;
2580
2581 if (dst) {
David S. Millerc78679e2012-04-01 20:27:33 -04002582 if (nla_put(skb, RTA_DST, 16, dst))
2583 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002584 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 } else if (rtm->rtm_dst_len)
David S. Millerc78679e2012-04-01 20:27:33 -04002586 if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2587 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588#ifdef CONFIG_IPV6_SUBTREES
2589 if (src) {
David S. Millerc78679e2012-04-01 20:27:33 -04002590 if (nla_put(skb, RTA_SRC, 16, src))
2591 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002592 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04002593 } else if (rtm->rtm_src_len &&
2594 nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2595 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002597 if (iif) {
2598#ifdef CONFIG_IPV6_MROUTE
2599 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002600 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002601 if (err <= 0) {
2602 if (!nowait) {
2603 if (err == 0)
2604 return 0;
2605 goto nla_put_failure;
2606 } else {
2607 if (err == -EMSGSIZE)
2608 goto nla_put_failure;
2609 }
2610 }
2611 } else
2612#endif
David S. Millerc78679e2012-04-01 20:27:33 -04002613 if (nla_put_u32(skb, RTA_IIF, iif))
2614 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002615 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct in6_addr saddr_buf;
David S. Millerc78679e2012-04-01 20:27:33 -04002617 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2618 nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2619 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002621
Daniel Walterc3968a82011-04-13 21:10:57 +00002622 if (rt->rt6i_prefsrc.plen) {
2623 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002624 saddr_buf = rt->rt6i_prefsrc.addr;
David S. Millerc78679e2012-04-01 20:27:33 -04002625 if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2626 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00002627 }
2628
David S. Millerdefb3512010-12-08 21:16:57 -08002629 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002630 goto nla_put_failure;
2631
David S. Miller97cac082012-07-02 22:43:47 -07002632 n = rt->n;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002633 if (n) {
Amerigo Wangfdd66812012-09-10 02:48:44 +00002634 if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0)
Eric Dumazet94f826b2012-03-27 09:53:52 +00002635 goto nla_put_failure;
Eric Dumazet94f826b2012-03-27 09:53:52 +00002636 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002637
David S. Millerc78679e2012-04-01 20:27:33 -04002638 if (rt->dst.dev &&
2639 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2640 goto nla_put_failure;
2641 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2642 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00002643
2644 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002645
David S. Miller87a50692012-07-10 05:06:14 -07002646 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002647 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Thomas Graf2d7202b2006-08-22 00:01:27 -07002649 return nlmsg_end(skb, nlh);
2650
2651nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002652 nlmsg_cancel(skb, nlh);
2653 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Patrick McHardy1b43af52006-08-10 23:11:17 -07002656int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2659 int prefix;
2660
Thomas Graf2d7202b2006-08-22 00:01:27 -07002661 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2662 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2664 } else
2665 prefix = 0;
2666
Brian Haley191cd582008-08-14 15:33:21 -07002667 return rt6_fill_node(arg->net,
2668 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002669 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002670 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Thomas Grafc127ea22007-03-22 11:58:32 -07002673static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002675 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002676 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002678 struct sk_buff *skb;
2679 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002680 struct flowi6 fl6;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002681 int err, iif = 0, oif = 0;
Thomas Grafab364a62006-08-22 00:01:47 -07002682
2683 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2684 if (err < 0)
2685 goto errout;
2686
2687 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002688 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002689
2690 if (tb[RTA_SRC]) {
2691 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2692 goto errout;
2693
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002694 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002695 }
2696
2697 if (tb[RTA_DST]) {
2698 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2699 goto errout;
2700
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002701 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002702 }
2703
2704 if (tb[RTA_IIF])
2705 iif = nla_get_u32(tb[RTA_IIF]);
2706
2707 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002708 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002709
2710 if (iif) {
2711 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002712 int flags = 0;
2713
Daniel Lezcano55786892008-03-04 13:47:47 -08002714 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002715 if (!dev) {
2716 err = -ENODEV;
2717 goto errout;
2718 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002719
2720 fl6.flowi6_iif = iif;
2721
2722 if (!ipv6_addr_any(&fl6.saddr))
2723 flags |= RT6_LOOKUP_F_HAS_SADDR;
2724
2725 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2726 flags);
2727 } else {
2728 fl6.flowi6_oif = oif;
2729
2730 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
Thomas Grafab364a62006-08-22 00:01:47 -07002731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002734 if (!skb) {
Shmulik Ladkani2173bff2012-04-03 23:13:00 +00002735 dst_release(&rt->dst);
Thomas Grafab364a62006-08-22 00:01:47 -07002736 err = -ENOBUFS;
2737 goto errout;
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* Reserve room for dummy headers, this skb can pass
2741 through good chunk of routing engine.
2742 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002743 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2745
Changli Gaod8d1f302010-06-10 23:31:35 -07002746 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
David S. Miller4c9483b2011-03-12 16:22:43 -05002748 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002749 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002750 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002752 kfree_skb(skb);
2753 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755
Eric W. Biederman15e47302012-09-07 20:12:54 +00002756 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07002757errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Thomas Graf86872cb2006-08-22 00:01:08 -07002761void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
2763 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002764 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002765 u32 seq;
2766 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002768 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002769 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002770
Thomas Graf339bf982006-11-10 14:10:15 -08002771 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002772 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002773 goto errout;
2774
Brian Haley191cd582008-08-14 15:33:21 -07002775 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002776 event, info->portid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002777 if (err < 0) {
2778 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2779 WARN_ON(err == -EMSGSIZE);
2780 kfree_skb(skb);
2781 goto errout;
2782 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002783 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002784 info->nlh, gfp_any());
2785 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002786errout:
2787 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002788 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002791static int ip6_route_dev_notify(struct notifier_block *this,
2792 unsigned long event, void *data)
2793{
2794 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002795 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002796
2797 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002798 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002799 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2800#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002801 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002802 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002803 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002804 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2805#endif
2806 }
2807
2808 return NOTIFY_OK;
2809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
2812 * /proc
2813 */
2814
2815#ifdef CONFIG_PROC_FS
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817struct rt6_proc_arg
2818{
2819 char *buffer;
2820 int offset;
2821 int length;
2822 int skip;
2823 int len;
2824};
2825
2826static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2827{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002828 struct seq_file *m = p_arg;
David S. Miller69cce1d2011-07-17 23:09:49 -07002829 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002831 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002834 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002836 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837#endif
David S. Miller97cac082012-07-02 22:43:47 -07002838 n = rt->n;
David S. Miller69cce1d2011-07-17 23:09:49 -07002839 if (n) {
2840 seq_printf(m, "%pi6", n->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002842 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002844 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002845 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2846 rt->dst.__use, rt->rt6i_flags,
David S. Millerd1918542011-12-28 20:19:20 -05002847 rt->dst.dev ? rt->dst.dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
2849}
2850
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002851static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002853 struct net *net = (struct net *)m->private;
Josh Hunt32b293a2011-12-28 13:23:07 +00002854 fib6_clean_all_ro(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002858static int ipv6_route_open(struct inode *inode, struct file *file)
2859{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002860 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002861}
2862
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002863static const struct file_operations ipv6_route_proc_fops = {
2864 .owner = THIS_MODULE,
2865 .open = ipv6_route_open,
2866 .read = seq_read,
2867 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002868 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002869};
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2872{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002873 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002875 net->ipv6.rt6_stats->fib_nodes,
2876 net->ipv6.rt6_stats->fib_route_nodes,
2877 net->ipv6.rt6_stats->fib_rt_alloc,
2878 net->ipv6.rt6_stats->fib_rt_entries,
2879 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002880 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002881 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 return 0;
2884}
2885
2886static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2887{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002888 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002889}
2890
Arjan van de Ven9a321442007-02-12 00:55:35 -08002891static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 .owner = THIS_MODULE,
2893 .open = rt6_stats_seq_open,
2894 .read = seq_read,
2895 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002896 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897};
2898#endif /* CONFIG_PROC_FS */
2899
2900#ifdef CONFIG_SYSCTL
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002903int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 void __user *buffer, size_t *lenp, loff_t *ppos)
2905{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002906 struct net *net;
2907 int delay;
2908 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002910
2911 net = (struct net *)ctl->extra1;
2912 delay = net->ipv6.sysctl.flush_delay;
2913 proc_dointvec(ctl, write, buffer, lenp, ppos);
2914 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002918ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002919 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002921 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002923 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002924 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 },
2926 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002928 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 .maxlen = sizeof(int),
2930 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002931 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 },
2933 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002935 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .maxlen = sizeof(int),
2937 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002938 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 },
2940 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002942 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 .maxlen = sizeof(int),
2944 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002945 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 },
2947 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002949 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .maxlen = sizeof(int),
2951 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002952 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 },
2954 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002956 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 .maxlen = sizeof(int),
2958 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002959 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 },
2961 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002963 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .maxlen = sizeof(int),
2965 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002966 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 },
2968 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002970 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 .maxlen = sizeof(int),
2972 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002973 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 },
2975 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002977 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .maxlen = sizeof(int),
2979 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002980 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 },
2982 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002984 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .maxlen = sizeof(int),
2986 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002987 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002989 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990};
2991
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002992struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002993{
2994 struct ctl_table *table;
2995
2996 table = kmemdup(ipv6_route_table_template,
2997 sizeof(ipv6_route_table_template),
2998 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002999
3000 if (table) {
3001 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00003002 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003003 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003004 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
3005 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3006 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
3007 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
3008 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
3009 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
3010 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08003011 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09003012 }
3013
Daniel Lezcano760f2d02008-01-10 02:53:43 -08003014 return table;
3015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016#endif
3017
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003018static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003019{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07003020 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003021
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003022 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
3023 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003024
Eric Dumazetfc66f952010-10-08 06:37:34 +00003025 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3026 goto out_ip6_dst_ops;
3027
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003028 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
3029 sizeof(*net->ipv6.ip6_null_entry),
3030 GFP_KERNEL);
3031 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00003032 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07003033 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003034 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003035 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003036 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
3037 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003038
3039#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3040 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
3041 sizeof(*net->ipv6.ip6_prohibit_entry),
3042 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003043 if (!net->ipv6.ip6_prohibit_entry)
3044 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003045 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003046 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003047 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003048 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
3049 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003050
3051 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
3052 sizeof(*net->ipv6.ip6_blk_hole_entry),
3053 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003054 if (!net->ipv6.ip6_blk_hole_entry)
3055 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003056 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003057 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07003058 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08003059 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
3060 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003061#endif
3062
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07003063 net->ipv6.sysctl.flush_delay = 0;
3064 net->ipv6.sysctl.ip6_rt_max_size = 4096;
3065 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3066 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3067 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3068 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3069 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3070 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3071
Benjamin Thery6891a342008-03-04 13:49:47 -08003072 net->ipv6.ip6_rt_gc_expire = 30*HZ;
3073
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003074 ret = 0;
3075out:
3076 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003077
Peter Zijlstra68fffc62008-10-07 14:12:10 -07003078#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3079out_ip6_prohibit_entry:
3080 kfree(net->ipv6.ip6_prohibit_entry);
3081out_ip6_null_entry:
3082 kfree(net->ipv6.ip6_null_entry);
3083#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00003084out_ip6_dst_entries:
3085 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003086out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003087 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003088}
3089
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003090static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003091{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003092 kfree(net->ipv6.ip6_null_entry);
3093#ifdef CONFIG_IPV6_MULTIPLE_TABLES
3094 kfree(net->ipv6.ip6_prohibit_entry);
3095 kfree(net->ipv6.ip6_blk_hole_entry);
3096#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003097 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003098}
3099
Thomas Grafd1896342012-06-18 12:08:33 +00003100static int __net_init ip6_route_net_init_late(struct net *net)
3101{
3102#ifdef CONFIG_PROC_FS
3103 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
3104 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
3105#endif
3106 return 0;
3107}
3108
3109static void __net_exit ip6_route_net_exit_late(struct net *net)
3110{
3111#ifdef CONFIG_PROC_FS
3112 proc_net_remove(net, "ipv6_route");
3113 proc_net_remove(net, "rt6_stats");
3114#endif
3115}
3116
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003117static struct pernet_operations ip6_route_net_ops = {
3118 .init = ip6_route_net_init,
3119 .exit = ip6_route_net_exit,
3120};
3121
David S. Millerc3426b42012-06-09 16:27:05 -07003122static int __net_init ipv6_inetpeer_init(struct net *net)
3123{
3124 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3125
3126 if (!bp)
3127 return -ENOMEM;
3128 inet_peer_base_init(bp);
3129 net->ipv6.peers = bp;
3130 return 0;
3131}
3132
3133static void __net_exit ipv6_inetpeer_exit(struct net *net)
3134{
3135 struct inet_peer_base *bp = net->ipv6.peers;
3136
3137 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07003138 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07003139 kfree(bp);
3140}
3141
David S. Miller2b823f72012-06-09 19:00:16 -07003142static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07003143 .init = ipv6_inetpeer_init,
3144 .exit = ipv6_inetpeer_exit,
3145};
3146
Thomas Grafd1896342012-06-18 12:08:33 +00003147static struct pernet_operations ip6_route_net_late_ops = {
3148 .init = ip6_route_net_init_late,
3149 .exit = ip6_route_net_exit_late,
3150};
3151
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003152static struct notifier_block ip6_route_dev_notifier = {
3153 .notifier_call = ip6_route_dev_notify,
3154 .priority = 0,
3155};
3156
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003157int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003159 int ret;
3160
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08003161 ret = -ENOMEM;
3162 ip6_dst_ops_template.kmem_cachep =
3163 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3164 SLAB_HWCACHE_ALIGN, NULL);
3165 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08003166 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07003167
Eric Dumazetfc66f952010-10-08 06:37:34 +00003168 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003169 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003170 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08003171
David S. Millerc3426b42012-06-09 16:27:05 -07003172 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3173 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003174 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00003175
David S. Miller7e52b332012-06-15 15:51:55 -07003176 ret = register_pernet_subsys(&ip6_route_net_ops);
3177 if (ret)
3178 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07003179
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07003180 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3181
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003182 /* Registering of the loopback is done before this portion of code,
3183 * the loopback reference in rt6_info will not be taken, do it
3184 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07003185 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003186 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3187 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07003188 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003189 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07003190 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003191 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3192 #endif
David S. Millere8803b62012-06-16 01:12:19 -07003193 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003194 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003195 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003196
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003197 ret = xfrm6_init();
3198 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07003199 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003200
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003201 ret = fib6_rules_init();
3202 if (ret)
3203 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003204
Thomas Grafd1896342012-06-18 12:08:33 +00003205 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3206 if (ret)
3207 goto fib6_rules_init;
3208
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003209 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003210 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3211 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3212 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Thomas Grafd1896342012-06-18 12:08:33 +00003213 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003214
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003215 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003216 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00003217 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003218
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003219out:
3220 return ret;
3221
Thomas Grafd1896342012-06-18 12:08:33 +00003222out_register_late_subsys:
3223 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003224fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003225 fib6_rules_cleanup();
3226xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003227 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00003228out_fib6_init:
3229 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003230out_register_subsys:
3231 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07003232out_register_inetpeer:
3233 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003234out_dst_entries:
3235 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003236out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003237 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
3241void ip6_route_cleanup(void)
3242{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003243 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00003244 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07003245 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07003248 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003249 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003250 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003251 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}