blob: 8c2e3ab58f2af211c04f17c337929bd2802cbe27 [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
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/times.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
34#include <linux/net.h>
35#include <linux/route.h>
36#include <linux/netdevice.h>
37#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090038#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080043#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/snmp.h>
47#include <net/ipv6.h>
48#include <net/ip6_fib.h>
49#include <net/ip6_route.h>
50#include <net/ndisc.h>
51#include <net/addrconf.h>
52#include <net/tcp.h>
53#include <linux/rtnetlink.h>
54#include <net/dst.h>
55#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <asm/uaccess.h>
60
61#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
64
Eric Dumazet21efcfa2011-07-19 20:18:36 +000065static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
66 const struct in6_addr *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080068static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000069static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static struct dst_entry *ip6_negative_advice(struct dst_entry *);
71static void ip6_dst_destroy(struct dst_entry *);
72static void ip6_dst_ifdown(struct dst_entry *,
73 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080074static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static int ip6_pkt_discard(struct sk_buff *skb);
77static int ip6_pkt_discard_out(struct sk_buff *skb);
78static void ip6_link_failure(struct sk_buff *skb);
79static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
80
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080081#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080082static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000083 const struct in6_addr *prefix, int prefixlen,
84 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080085 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080086static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000087 const struct in6_addr *prefix, int prefixlen,
88 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080089#endif
90
David S. Miller06582542011-01-27 14:58:42 -080091static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
92{
93 struct rt6_info *rt = (struct rt6_info *) dst;
94 struct inet_peer *peer;
95 u32 *p = NULL;
96
Yan, Zheng8e2ec632011-09-05 21:34:30 +000097 if (!(rt->dst.flags & DST_HOST))
98 return NULL;
99
David S. Miller06582542011-01-27 14:58:42 -0800100 if (!rt->rt6i_peer)
101 rt6_bind_peer(rt, 1);
102
103 peer = rt->rt6i_peer;
104 if (peer) {
105 u32 *old_p = __DST_METRICS_PTR(old);
106 unsigned long prev, new;
107
108 p = peer->metrics;
109 if (inet_metrics_new(peer))
110 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
111
112 new = (unsigned long) p;
113 prev = cmpxchg(&dst->_metrics, old, new);
114
115 if (prev != old) {
116 p = __DST_METRICS_PTR(prev);
117 if (prev & DST_METRICS_READ_ONLY)
118 p = NULL;
119 }
120 }
121 return p;
122}
123
David S. Millerd3aaeb32011-07-18 00:40:17 -0700124static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
125{
David S. Millerf83c7792011-12-28 15:41:23 -0500126 struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
127 if (n)
128 return n;
129 return neigh_create(&nd_tbl, daddr, dst->dev);
130}
131
David S. Miller8ade06c2011-12-29 18:51:57 -0500132static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
David S. Millerf83c7792011-12-28 15:41:23 -0500133{
David S. Miller8ade06c2011-12-29 18:51:57 -0500134 struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
135 if (!n) {
136 n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
137 if (IS_ERR(n))
138 return PTR_ERR(n);
139 }
David S. Millerf83c7792011-12-28 15:41:23 -0500140 dst_set_neighbour(&rt->dst, n);
141
142 return 0;
David S. Millerd3aaeb32011-07-18 00:40:17 -0700143}
144
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800145static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800147 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .gc = ip6_dst_gc,
149 .gc_thresh = 1024,
150 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800151 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000152 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800153 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .destroy = ip6_dst_destroy,
155 .ifdown = ip6_dst_ifdown,
156 .negative_advice = ip6_negative_advice,
157 .link_failure = ip6_link_failure,
158 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700159 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700160 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Steffen Klassertebb762f2011-11-23 02:12:51 +0000163static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800164{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000165 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
166
167 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800168}
169
David S. Miller14e50e52007-05-24 18:17:54 -0700170static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
171{
172}
173
Held Bernhard0972ddb2011-04-24 22:07:32 +0000174static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
175 unsigned long old)
176{
177 return NULL;
178}
179
David S. Miller14e50e52007-05-24 18:17:54 -0700180static struct dst_ops ip6_dst_blackhole_ops = {
181 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800182 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700183 .destroy = ip6_dst_destroy,
184 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000185 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800186 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700187 .update_pmtu = ip6_rt_blackhole_update_pmtu,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000188 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700189 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700190};
191
David S. Miller62fa8a82011-01-26 20:51:05 -0800192static const u32 ip6_template_metrics[RTAX_MAX] = {
193 [RTAX_HOPLIMIT - 1] = 255,
194};
195
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800196static struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700197 .dst = {
198 .__refcnt = ATOMIC_INIT(1),
199 .__use = 1,
200 .obsolete = -1,
201 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700202 .input = ip6_pkt_discard,
203 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 },
205 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700206 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .rt6i_metric = ~(u32) 0,
208 .rt6i_ref = ATOMIC_INIT(1),
209};
210
Thomas Graf101367c2006-08-04 03:39:02 -0700211#ifdef CONFIG_IPV6_MULTIPLE_TABLES
212
David S. Miller6723ab52006-10-18 21:20:57 -0700213static int ip6_pkt_prohibit(struct sk_buff *skb);
214static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700215
Adrian Bunk280a34c2008-04-21 02:29:32 -0700216static struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700217 .dst = {
218 .__refcnt = ATOMIC_INIT(1),
219 .__use = 1,
220 .obsolete = -1,
221 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700222 .input = ip6_pkt_prohibit,
223 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700224 },
225 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700226 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700227 .rt6i_metric = ~(u32) 0,
228 .rt6i_ref = ATOMIC_INIT(1),
229};
230
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800231static struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700232 .dst = {
233 .__refcnt = ATOMIC_INIT(1),
234 .__use = 1,
235 .obsolete = -1,
236 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700237 .input = dst_discard,
238 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700239 },
240 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700241 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700242 .rt6i_metric = ~(u32) 0,
243 .rt6i_ref = ATOMIC_INIT(1),
244};
245
246#endif
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* allocate dst with ip6_dst_ops */
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700249static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
David S. Miller957c6652011-06-24 15:25:00 -0700250 struct net_device *dev,
251 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
David S. Miller957c6652011-06-24 15:25:00 -0700253 struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
David S. Millercf911662011-04-28 14:31:47 -0700254
David S. Miller38308472011-12-03 18:02:47 -0500255 if (rt)
Madalin Bucurfbe58182011-09-26 07:04:56 +0000256 memset(&rt->rt6i_table, 0,
David S. Miller38308472011-12-03 18:02:47 -0500257 sizeof(*rt) - sizeof(struct dst_entry));
David S. Millercf911662011-04-28 14:31:47 -0700258
259 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static void ip6_dst_destroy(struct dst_entry *dst)
263{
264 struct rt6_info *rt = (struct rt6_info *)dst;
265 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800266 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000268 if (!(rt->dst.flags & DST_HOST))
269 dst_destroy_metrics_generic(dst);
270
David S. Miller38308472011-12-03 18:02:47 -0500271 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 rt->rt6i_idev = NULL;
273 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900274 }
David S. Millerb3419362010-11-30 12:27:11 -0800275 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800276 rt->rt6i_peer = NULL;
277 inet_putpeer(peer);
278 }
279}
280
David S. Miller6431cbc2011-02-07 20:38:06 -0800281static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
282
283static u32 rt6_peer_genid(void)
284{
285 return atomic_read(&__rt6_peer_genid);
286}
287
David S. Millerb3419362010-11-30 12:27:11 -0800288void rt6_bind_peer(struct rt6_info *rt, int create)
289{
290 struct inet_peer *peer;
291
David S. Millerb3419362010-11-30 12:27:11 -0800292 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
293 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
294 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800295 else
296 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
300 int how)
301{
302 struct rt6_info *rt = (struct rt6_info *)dst;
303 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800304 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900305 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David S. Miller38308472011-12-03 18:02:47 -0500307 if (dev != loopback_dev && idev && idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800308 struct inet6_dev *loopback_idev =
309 in6_dev_get(loopback_dev);
David S. Miller38308472011-12-03 18:02:47 -0500310 if (loopback_idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 rt->rt6i_idev = loopback_idev;
312 in6_dev_put(idev);
313 }
314 }
315}
316
317static __inline__ int rt6_check_expired(const struct rt6_info *rt)
318{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000319 return (rt->rt6i_flags & RTF_EXPIRES) &&
David S. Millerd1918542011-12-28 20:19:20 -0500320 time_after(jiffies, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000323static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700324{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000325 return ipv6_addr_type(daddr) &
326 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700330 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
332
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800333static inline struct rt6_info *rt6_device_match(struct net *net,
334 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000335 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700337 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct rt6_info *local = NULL;
340 struct rt6_info *sprt;
341
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900342 if (!oif && ipv6_addr_any(saddr))
343 goto out;
344
Changli Gaod8d1f302010-06-10 23:31:35 -0700345 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500346 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900347
348 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (dev->ifindex == oif)
350 return sprt;
351 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500352 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700354 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900356 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 local->rt6i_idev->dev->ifindex == oif))
358 continue;
359 }
360 local = sprt;
361 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900362 } else {
363 if (ipv6_chk_addr(net, saddr, dev,
364 flags & RT6_LOOKUP_F_IFACE))
365 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900369 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (local)
371 return local;
372
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700373 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800374 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return rt;
378}
379
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800380#ifdef CONFIG_IPV6_ROUTER_PREF
381static void rt6_probe(struct rt6_info *rt)
382{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000383 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800384 /*
385 * Okay, this does not seem to be appropriate
386 * for now, however, we need to check if it
387 * is really so; aka Router Reachability Probing.
388 *
389 * Router Reachability Probe MUST be rate-limited
390 * to no more than one per minute.
391 */
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000392 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000393 neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800394 if (!neigh || (neigh->nud_state & NUD_VALID))
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000395 goto out;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800396 read_lock_bh(&neigh->lock);
397 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800398 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800399 struct in6_addr mcaddr;
400 struct in6_addr *target;
401
402 neigh->updated = jiffies;
403 read_unlock_bh(&neigh->lock);
404
405 target = (struct in6_addr *)&neigh->primary_key;
406 addrconf_addr_solict_mult(target, &mcaddr);
David S. Millerd1918542011-12-28 20:19:20 -0500407 ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000408 } else {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800409 read_unlock_bh(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000410 }
411out:
412 rcu_read_unlock();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800413}
414#else
415static inline void rt6_probe(struct rt6_info *rt)
416{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800417}
418#endif
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800421 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700423static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
David S. Millerd1918542011-12-28 20:19:20 -0500425 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700426 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800427 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700428 if ((dev->flags & IFF_LOOPBACK) &&
429 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
430 return 1;
431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Dave Jonesb6f99a22007-03-22 12:27:49 -0700434static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000436 struct neighbour *neigh;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800437 int m;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000438
439 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000440 neigh = dst_get_neighbour_noref(&rt->dst);
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700441 if (rt->rt6i_flags & RTF_NONEXTHOP ||
442 !(rt->rt6i_flags & RTF_GATEWAY))
443 m = 1;
444 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800445 read_lock_bh(&neigh->lock);
446 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700447 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800448#ifdef CONFIG_IPV6_ROUTER_PREF
449 else if (neigh->nud_state & NUD_FAILED)
450 m = 0;
451#endif
452 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800453 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800454 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800455 } else
456 m = 0;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000457 rcu_read_unlock();
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800458 return m;
459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800461static int rt6_score_route(struct rt6_info *rt, int oif,
462 int strict)
463{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700464 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700466 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700467 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800468 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800469#ifdef CONFIG_IPV6_ROUTER_PREF
470 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
471#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700472 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800473 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800474 return -1;
475 return m;
476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David S. Millerf11e6652007-03-24 20:36:25 -0700478static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
479 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800480{
David S. Millerf11e6652007-03-24 20:36:25 -0700481 int m;
482
483 if (rt6_check_expired(rt))
484 goto out;
485
486 m = rt6_score_route(rt, oif, strict);
487 if (m < 0)
488 goto out;
489
490 if (m > *mpri) {
491 if (strict & RT6_LOOKUP_F_REACHABLE)
492 rt6_probe(match);
493 *mpri = m;
494 match = rt;
495 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
496 rt6_probe(rt);
497 }
498
499out:
500 return match;
501}
502
503static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
504 struct rt6_info *rr_head,
505 u32 metric, int oif, int strict)
506{
507 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800508 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
David S. Millerf11e6652007-03-24 20:36:25 -0700510 match = NULL;
511 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700512 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700513 match = find_match(rt, oif, strict, &mpri, match);
514 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700515 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700516 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800517
David S. Millerf11e6652007-03-24 20:36:25 -0700518 return match;
519}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800520
David S. Millerf11e6652007-03-24 20:36:25 -0700521static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
522{
523 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800524 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
David S. Millerf11e6652007-03-24 20:36:25 -0700526 rt0 = fn->rr_ptr;
527 if (!rt0)
528 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
David S. Millerf11e6652007-03-24 20:36:25 -0700530 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800532 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700533 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700534 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700535
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800536 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700537 if (!next || next->rt6i_metric != rt0->rt6i_metric)
538 next = fn->leaf;
539
540 if (next != rt0)
541 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
David S. Millerd1918542011-12-28 20:19:20 -0500544 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000545 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800548#ifdef CONFIG_IPV6_ROUTE_INFO
549int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000550 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800551{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900552 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800553 struct route_info *rinfo = (struct route_info *) opt;
554 struct in6_addr prefix_buf, *prefix;
555 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900556 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800557 struct rt6_info *rt;
558
559 if (len < sizeof(struct route_info)) {
560 return -EINVAL;
561 }
562
563 /* Sanity check for prefix_len and length */
564 if (rinfo->length > 3) {
565 return -EINVAL;
566 } else if (rinfo->prefix_len > 128) {
567 return -EINVAL;
568 } else if (rinfo->prefix_len > 64) {
569 if (rinfo->length < 2) {
570 return -EINVAL;
571 }
572 } else if (rinfo->prefix_len > 0) {
573 if (rinfo->length < 1) {
574 return -EINVAL;
575 }
576 }
577
578 pref = rinfo->route_pref;
579 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000580 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800581
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900582 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800583
584 if (rinfo->length == 3)
585 prefix = (struct in6_addr *)rinfo->prefix;
586 else {
587 /* this function is safe */
588 ipv6_addr_prefix(&prefix_buf,
589 (struct in6_addr *)rinfo->prefix,
590 rinfo->prefix_len);
591 prefix = &prefix_buf;
592 }
593
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800594 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
595 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800596
597 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700598 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800599 rt = NULL;
600 }
601
602 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800603 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800604 pref);
605 else if (rt)
606 rt->rt6i_flags = RTF_ROUTEINFO |
607 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
608
609 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900610 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800611 rt->rt6i_flags &= ~RTF_EXPIRES;
612 } else {
David S. Millerd1918542011-12-28 20:19:20 -0500613 rt->dst.expires = jiffies + HZ * lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800614 rt->rt6i_flags |= RTF_EXPIRES;
615 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700616 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800617 }
618 return 0;
619}
620#endif
621
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800622#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700623do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800624 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700625 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700626 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700627 if (fn->fn_flags & RTN_TL_ROOT) \
628 goto out; \
629 pn = fn->parent; \
630 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800631 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700632 else \
633 fn = pn; \
634 if (fn->fn_flags & RTN_RTINFO) \
635 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700636 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700637 } \
David S. Miller38308472011-12-03 18:02:47 -0500638} while (0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700639
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800640static struct rt6_info *ip6_pol_route_lookup(struct net *net,
641 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500642 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct fib6_node *fn;
645 struct rt6_info *rt;
646
Thomas Grafc71099a2006-08-04 23:20:06 -0700647 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500648 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700649restart:
650 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500651 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
652 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700653out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700654 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700655 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700656 return rt;
657
658}
659
Florian Westphalea6e5742011-09-05 16:05:44 +0200660struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
661 int flags)
662{
663 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
664}
665EXPORT_SYMBOL_GPL(ip6_route_lookup);
666
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900667struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
668 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700669{
David S. Miller4c9483b2011-03-12 16:22:43 -0500670 struct flowi6 fl6 = {
671 .flowi6_oif = oif,
672 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700673 };
674 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700675 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700676
Thomas Grafadaa70b2006-10-13 15:01:03 -0700677 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500678 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700679 flags |= RT6_LOOKUP_F_HAS_SADDR;
680 }
681
David S. Miller4c9483b2011-03-12 16:22:43 -0500682 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700683 if (dst->error == 0)
684 return (struct rt6_info *) dst;
685
686 dst_release(dst);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return NULL;
689}
690
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900691EXPORT_SYMBOL(rt6_lookup);
692
Thomas Grafc71099a2006-08-04 23:20:06 -0700693/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 It takes new route entry, the addition fails by any reason the
695 route is freed. In any case, if caller does not hold it, it may
696 be destroyed.
697 */
698
Thomas Graf86872cb2006-08-22 00:01:08 -0700699static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700702 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Grafc71099a2006-08-04 23:20:06 -0700704 table = rt->rt6i_table;
705 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700706 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700707 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 return err;
710}
711
Thomas Graf40e22e82006-08-22 00:00:45 -0700712int ip6_ins_rt(struct rt6_info *rt)
713{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800714 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -0500715 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800716 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800717 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700718}
719
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000720static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort,
721 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000722 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct rt6_info *rt;
725
726 /*
727 * Clone the route.
728 */
729
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000730 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800733 int attempts = !in_softirq();
734
David S. Miller38308472011-12-03 18:02:47 -0500735 if (!(rt->rt6i_flags & RTF_GATEWAY)) {
David S. Millerbb3c3682011-12-13 17:35:06 -0500736 if (ort->rt6i_dst.plen != 128 &&
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000737 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900738 rt->rt6i_flags |= RTF_ANYCAST;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000739 rt->rt6i_gateway = *daddr;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744#ifdef CONFIG_IPV6_SUBTREES
745 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000746 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rt->rt6i_src.plen = 128;
748 }
749#endif
750
David S. Miller14deae42009-01-04 16:04:39 -0800751 retry:
David S. Miller8ade06c2011-12-29 18:51:57 -0500752 if (rt6_bind_neighbour(rt, rt->dst.dev)) {
David S. Millerd1918542011-12-28 20:19:20 -0500753 struct net *net = dev_net(rt->dst.dev);
David S. Miller14deae42009-01-04 16:04:39 -0800754 int saved_rt_min_interval =
755 net->ipv6.sysctl.ip6_rt_gc_min_interval;
756 int saved_rt_elasticity =
757 net->ipv6.sysctl.ip6_rt_gc_elasticity;
758
759 if (attempts-- > 0) {
760 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
761 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
762
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000763 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800764
765 net->ipv6.sysctl.ip6_rt_gc_elasticity =
766 saved_rt_elasticity;
767 net->ipv6.sysctl.ip6_rt_gc_min_interval =
768 saved_rt_min_interval;
769 goto retry;
770 }
771
772 if (net_ratelimit())
773 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700774 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700775 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800776 return NULL;
777 }
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800780 return rt;
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000783static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
784 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800785{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000786 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
787
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800788 if (rt) {
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800789 rt->rt6i_flags |= RTF_CACHE;
David Miller27217452011-12-02 16:52:08 +0000790 dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst)));
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800791 }
792 return rt;
793}
794
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800795static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500796 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800799 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700800 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800802 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700803 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700805 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700808 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800810restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500811 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700814 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800815
David S. Miller4c9483b2011-03-12 16:22:43 -0500816 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800817 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800818 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Changli Gaod8d1f302010-06-10 23:31:35 -0700821 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700822 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800823
David Miller27217452011-12-02 16:52:08 +0000824 if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500825 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800826 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500827 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800828 else
829 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800830
Changli Gaod8d1f302010-06-10 23:31:35 -0700831 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800832 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800833
Changli Gaod8d1f302010-06-10 23:31:35 -0700834 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800835 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700836 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800837 if (!err)
838 goto out2;
839 }
840
841 if (--attempts <= 0)
842 goto out2;
843
844 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700845 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800846 * released someone could insert this route. Relookup.
847 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700848 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800849 goto relookup;
850
851out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800852 if (reachable) {
853 reachable = 0;
854 goto restart_2;
855 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700856 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700857 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700859 rt->dst.lastuse = jiffies;
860 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700861
862 return rt;
863}
864
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800865static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500866 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700867{
David S. Miller4c9483b2011-03-12 16:22:43 -0500868 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700869}
870
Thomas Grafc71099a2006-08-04 23:20:06 -0700871void ip6_route_input(struct sk_buff *skb)
872{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000873 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900874 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700875 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500876 struct flowi6 fl6 = {
877 .flowi6_iif = skb->dev->ifindex,
878 .daddr = iph->daddr,
879 .saddr = iph->saddr,
David S. Miller38308472011-12-03 18:02:47 -0500880 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
David S. Miller4c9483b2011-03-12 16:22:43 -0500881 .flowi6_mark = skb->mark,
882 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700883 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700884
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800885 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700886 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700887
David S. Miller4c9483b2011-03-12 16:22:43 -0500888 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700889}
890
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800891static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500892 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700893{
David S. Miller4c9483b2011-03-12 16:22:43 -0500894 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700895}
896
Florian Westphal9c7a4f92011-03-22 19:17:36 -0700897struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500898 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700899{
900 int flags = 0;
901
David S. Miller4c9483b2011-03-12 16:22:43 -0500902 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700903 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700904
David S. Miller4c9483b2011-03-12 16:22:43 -0500905 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700906 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000907 else if (sk)
908 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700909
David S. Miller4c9483b2011-03-12 16:22:43 -0500910 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900913EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
David S. Miller2774c132011-03-01 14:59:04 -0800915struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700916{
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700917 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700918 struct dst_entry *new = NULL;
919
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700920 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
David S. Miller14e50e52007-05-24 18:17:54 -0700921 if (rt) {
David S. Millercf911662011-04-28 14:31:47 -0700922 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
923
Changli Gaod8d1f302010-06-10 23:31:35 -0700924 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700925
David S. Miller14e50e52007-05-24 18:17:54 -0700926 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800927 new->input = dst_discard;
928 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700929
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000930 if (dst_metrics_read_only(&ort->dst))
931 new->_metrics = ort->dst._metrics;
932 else
933 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -0700934 rt->rt6i_idev = ort->rt6i_idev;
935 if (rt->rt6i_idev)
936 in6_dev_hold(rt->rt6i_idev);
David S. Millerd1918542011-12-28 20:19:20 -0500937 rt->dst.expires = 0;
David S. Miller14e50e52007-05-24 18:17:54 -0700938
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000939 rt->rt6i_gateway = ort->rt6i_gateway;
David S. Miller14e50e52007-05-24 18:17:54 -0700940 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
941 rt->rt6i_metric = 0;
942
943 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
944#ifdef CONFIG_IPV6_SUBTREES
945 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
946#endif
947
948 dst_free(new);
949 }
950
David S. Miller69ead7a2011-03-01 14:45:33 -0800951 dst_release(dst_orig);
952 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700953}
David S. Miller14e50e52007-05-24 18:17:54 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/*
956 * Destination cache support functions
957 */
958
959static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
960{
961 struct rt6_info *rt;
962
963 rt = (struct rt6_info *) dst;
964
David S. Miller6431cbc2011-02-07 20:38:06 -0800965 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
966 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
967 if (!rt->rt6i_peer)
968 rt6_bind_peer(rt, 0);
969 rt->rt6i_peer_genid = rt6_peer_genid();
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return NULL;
974}
975
976static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
977{
978 struct rt6_info *rt = (struct rt6_info *) dst;
979
980 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000981 if (rt->rt6i_flags & RTF_CACHE) {
982 if (rt6_check_expired(rt)) {
983 ip6_del_rt(rt);
984 dst = NULL;
985 }
986 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000988 dst = NULL;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000991 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static void ip6_link_failure(struct sk_buff *skb)
995{
996 struct rt6_info *rt;
997
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000998 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Eric Dumazetadf30902009-06-02 05:19:30 +00001000 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (rt) {
David S. Miller38308472011-12-03 18:02:47 -05001002 if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001003 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 rt->rt6i_flags |= RTF_EXPIRES;
1005 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
1006 rt->rt6i_node->fn_sernum = -1;
1007 }
1008}
1009
1010static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
1011{
1012 struct rt6_info *rt6 = (struct rt6_info*)dst;
1013
1014 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
1015 rt6->rt6i_flags |= RTF_MODIFIED;
1016 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -08001017 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -08001019 features |= RTAX_FEATURE_ALLFRAG;
1020 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
David S. Millerdefb3512010-12-08 21:16:57 -08001022 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024}
1025
David S. Miller0dbaee32010-12-13 12:52:14 -08001026static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
David S. Miller0dbaee32010-12-13 12:52:14 -08001028 struct net_device *dev = dst->dev;
1029 unsigned int mtu = dst_mtu(dst);
1030 struct net *net = dev_net(dev);
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1033
Daniel Lezcano55786892008-03-04 13:47:47 -08001034 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1035 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001038 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1039 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1040 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * rely only on pmtu discovery"
1042 */
1043 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1044 mtu = IPV6_MAXPLEN;
1045 return mtu;
1046}
1047
Steffen Klassertebb762f2011-11-23 02:12:51 +00001048static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001049{
David S. Millerd33e4552010-12-14 13:01:14 -08001050 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001051 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1052
1053 if (mtu)
1054 return mtu;
1055
1056 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001057
1058 rcu_read_lock();
1059 idev = __in6_dev_get(dst->dev);
1060 if (idev)
1061 mtu = idev->cnf.mtu6;
1062 rcu_read_unlock();
1063
1064 return mtu;
1065}
1066
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001067static struct dst_entry *icmp6_dst_gc_list;
1068static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001069
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001070struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct neighbour *neigh,
David S. Miller87a11572011-12-06 17:04:13 -05001072 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
David S. Miller87a11572011-12-06 17:04:13 -05001074 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct rt6_info *rt;
1076 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001077 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David S. Miller38308472011-12-03 18:02:47 -05001079 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return NULL;
1081
David S. Miller957c6652011-06-24 15:25:00 -07001082 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05001083 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001085 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto out;
1087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (neigh)
1090 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001091 else {
David S. Millerf83c7792011-12-28 15:41:23 -05001092 neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr);
David S. Millerb43faac2011-12-13 16:48:21 -05001093 if (IS_ERR(neigh)) {
RongQing.Li252c3d82012-01-12 22:33:46 +00001094 in6_dev_put(idev);
David S. Millerb43faac2011-12-13 16:48:21 -05001095 dst_free(&rt->dst);
1096 return ERR_CAST(neigh);
1097 }
David S. Miller14deae42009-01-04 16:04:39 -08001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001100 rt->dst.flags |= DST_HOST;
1101 rt->dst.output = ip6_output;
David S. Miller69cce1d2011-07-17 23:09:49 -07001102 dst_set_neighbour(&rt->dst, neigh);
Changli Gaod8d1f302010-06-10 23:31:35 -07001103 atomic_set(&rt->dst.__refcnt, 1);
David S. Miller87a11572011-12-06 17:04:13 -05001104 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001105 rt->rt6i_dst.plen = 128;
1106 rt->rt6i_idev = idev;
Gao feng70116872011-10-28 02:46:57 +00001107 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001109 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001110 rt->dst.next = icmp6_dst_gc_list;
1111 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001112 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Daniel Lezcano55786892008-03-04 13:47:47 -08001114 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
David S. Miller87a11572011-12-06 17:04:13 -05001116 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118out:
David S. Miller87a11572011-12-06 17:04:13 -05001119 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001122int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001124 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001125 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001127 spin_lock_bh(&icmp6_dst_lock);
1128 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 while ((dst = *pprev) != NULL) {
1131 if (!atomic_read(&dst->__refcnt)) {
1132 *pprev = dst->next;
1133 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 } else {
1135 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001136 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001140 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001141
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001142 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
David S. Miller1e493d12008-09-10 17:27:15 -07001145static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1146 void *arg)
1147{
1148 struct dst_entry *dst, **pprev;
1149
1150 spin_lock_bh(&icmp6_dst_lock);
1151 pprev = &icmp6_dst_gc_list;
1152 while ((dst = *pprev) != NULL) {
1153 struct rt6_info *rt = (struct rt6_info *) dst;
1154 if (func(rt, arg)) {
1155 *pprev = dst->next;
1156 dst_free(dst);
1157 } else {
1158 pprev = &dst->next;
1159 }
1160 }
1161 spin_unlock_bh(&icmp6_dst_lock);
1162}
1163
Daniel Lezcano569d3642008-01-18 03:56:57 -08001164static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001167 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001168 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1169 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1170 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1171 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1172 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001173 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Eric Dumazetfc66f952010-10-08 06:37:34 +00001175 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001176 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001177 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto out;
1179
Benjamin Thery6891a342008-03-04 13:49:47 -08001180 net->ipv6.ip6_rt_gc_expire++;
1181 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1182 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001183 entries = dst_entries_get_slow(ops);
1184 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001185 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001187 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001188 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/* Clean host part of a prefix. Not necessary in radix tree,
1192 but results in cleaner routing tables.
1193
1194 Remove it only when all the things will work!
1195 */
1196
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001197int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
David S. Miller5170ae82010-12-12 21:35:57 -08001199 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001200 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001201 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001202 struct inet6_dev *idev;
1203
1204 rcu_read_lock();
1205 idev = __in6_dev_get(dev);
1206 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001207 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001208 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001209 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001210 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 return hoplimit;
1213}
David S. Millerabbf46a2010-12-12 21:14:46 -08001214EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216/*
1217 *
1218 */
1219
Thomas Graf86872cb2006-08-22 00:01:08 -07001220int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001223 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct rt6_info *rt = NULL;
1225 struct net_device *dev = NULL;
1226 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001227 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int addr_type;
1229
Thomas Graf86872cb2006-08-22 00:01:08 -07001230 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EINVAL;
1232#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001233 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -EINVAL;
1235#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001236 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001238 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (!dev)
1240 goto out;
1241 idev = in6_dev_get(dev);
1242 if (!idev)
1243 goto out;
1244 }
1245
Thomas Graf86872cb2006-08-22 00:01:08 -07001246 if (cfg->fc_metric == 0)
1247 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Matti Vaittinend71314b2011-11-14 00:14:49 +00001249 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001250 if (cfg->fc_nlinfo.nlh &&
1251 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001252 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001253 if (!table) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001254 printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n");
1255 table = fib6_new_table(net, cfg->fc_table);
1256 }
1257 } else {
1258 table = fib6_new_table(net, cfg->fc_table);
1259 }
David S. Miller38308472011-12-03 18:02:47 -05001260
1261 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001262 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001263
David S. Miller957c6652011-06-24 15:25:00 -07001264 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
David S. Miller38308472011-12-03 18:02:47 -05001266 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 err = -ENOMEM;
1268 goto out;
1269 }
1270
Changli Gaod8d1f302010-06-10 23:31:35 -07001271 rt->dst.obsolete = -1;
David S. Millerd1918542011-12-28 20:19:20 -05001272 rt->dst.expires = (cfg->fc_flags & RTF_EXPIRES) ?
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001273 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1274 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Thomas Graf86872cb2006-08-22 00:01:08 -07001276 if (cfg->fc_protocol == RTPROT_UNSPEC)
1277 cfg->fc_protocol = RTPROT_BOOT;
1278 rt->rt6i_protocol = cfg->fc_protocol;
1279
1280 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001283 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001284 else if (cfg->fc_flags & RTF_LOCAL)
1285 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001287 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Changli Gaod8d1f302010-06-10 23:31:35 -07001289 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Thomas Graf86872cb2006-08-22 00:01:08 -07001291 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1292 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001294 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001296 if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1297 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1298 if (!metrics) {
1299 err = -ENOMEM;
1300 goto out;
1301 }
1302 dst_init_metrics(&rt->dst, metrics, 0);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001305 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1306 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307#endif
1308
Thomas Graf86872cb2006-08-22 00:01:08 -07001309 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* We cannot add true routes via loopback here,
1312 they would result in kernel looping; promote them to reject routes
1313 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001314 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001315 (dev && (dev->flags & IFF_LOOPBACK) &&
1316 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1317 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001319 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (dev) {
1321 dev_put(dev);
1322 in6_dev_put(idev);
1323 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001324 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 dev_hold(dev);
1326 idev = in6_dev_get(dev);
1327 if (!idev) {
1328 err = -ENODEV;
1329 goto out;
1330 }
1331 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001332 rt->dst.output = ip6_pkt_discard_out;
1333 rt->dst.input = ip6_pkt_discard;
1334 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1336 goto install_route;
1337 }
1338
Thomas Graf86872cb2006-08-22 00:01:08 -07001339 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001340 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int gwa_type;
1342
Thomas Graf86872cb2006-08-22 00:01:08 -07001343 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001344 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 gwa_type = ipv6_addr_type(gw_addr);
1346
1347 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1348 struct rt6_info *grt;
1349
1350 /* IPv6 strictly inhibits using not link-local
1351 addresses as nexthop address.
1352 Otherwise, router will not able to send redirects.
1353 It is very good, but in some (rare!) circumstances
1354 (SIT, PtP, NBMA NOARP links) it is handy to allow
1355 some exceptions. --ANK
1356 */
1357 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001358 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto out;
1360
Daniel Lezcano55786892008-03-04 13:47:47 -08001361 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001364 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto out;
1366 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001367 if (dev != grt->dst.dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001368 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto out;
1370 }
1371 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001372 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 idev = grt->rt6i_idev;
1374 dev_hold(dev);
1375 in6_dev_hold(grt->rt6i_idev);
1376 }
David S. Miller38308472011-12-03 18:02:47 -05001377 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001379 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if (err)
1382 goto out;
1383 }
1384 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001385 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 goto out;
1387 }
1388
1389 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001390 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 goto out;
1392
Daniel Walterc3968a82011-04-13 21:10:57 +00001393 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1394 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1395 err = -EINVAL;
1396 goto out;
1397 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001398 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001399 rt->rt6i_prefsrc.plen = 128;
1400 } else
1401 rt->rt6i_prefsrc.plen = 0;
1402
Thomas Graf86872cb2006-08-22 00:01:08 -07001403 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
David S. Miller8ade06c2011-12-29 18:51:57 -05001404 err = rt6_bind_neighbour(rt, dev);
David S. Millerf83c7792011-12-28 15:41:23 -05001405 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Thomas Graf86872cb2006-08-22 00:01:08 -07001409 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001412 if (cfg->fc_mx) {
1413 struct nlattr *nla;
1414 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Thomas Graf86872cb2006-08-22 00:01:08 -07001416 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001417 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001418
1419 if (type) {
1420 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 err = -EINVAL;
1422 goto out;
1423 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001424
David S. Millerdefb3512010-12-08 21:16:57 -08001425 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
1429
Changli Gaod8d1f302010-06-10 23:31:35 -07001430 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001432 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001433
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001434 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001435
Thomas Graf86872cb2006-08-22 00:01:08 -07001436 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438out:
1439 if (dev)
1440 dev_put(dev);
1441 if (idev)
1442 in6_dev_put(idev);
1443 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001444 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return err;
1446}
1447
Thomas Graf86872cb2006-08-22 00:01:08 -07001448static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001451 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001452 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001454 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001455 return -ENOENT;
1456
Thomas Grafc71099a2006-08-04 23:20:06 -07001457 table = rt->rt6i_table;
1458 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Thomas Graf86872cb2006-08-22 00:01:08 -07001460 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001461 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Grafc71099a2006-08-04 23:20:06 -07001463 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return err;
1466}
1467
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001468int ip6_del_rt(struct rt6_info *rt)
1469{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001470 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001471 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001472 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001473 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001474}
1475
Thomas Graf86872cb2006-08-22 00:01:08 -07001476static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Thomas Grafc71099a2006-08-04 23:20:06 -07001478 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct fib6_node *fn;
1480 struct rt6_info *rt;
1481 int err = -ESRCH;
1482
Daniel Lezcano55786892008-03-04 13:47:47 -08001483 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001484 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Thomas Grafc71099a2006-08-04 23:20:06 -07001487 read_lock_bh(&table->tb6_lock);
1488
1489 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001490 &cfg->fc_dst, cfg->fc_dst_len,
1491 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001494 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001495 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001496 (!rt->dst.dev ||
1497 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001499 if (cfg->fc_flags & RTF_GATEWAY &&
1500 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001502 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001504 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001505 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Thomas Graf86872cb2006-08-22 00:01:08 -07001507 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001510 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 return err;
1513}
1514
1515/*
1516 * Handle redirects
1517 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001518struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001519 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001520 struct in6_addr gateway;
1521};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001523static struct rt6_info *__ip6_route_redirect(struct net *net,
1524 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001525 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001526 int flags)
1527{
David S. Miller4c9483b2011-03-12 16:22:43 -05001528 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001529 struct rt6_info *rt;
1530 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001533 * Get the "current" route for this destination and
1534 * check if the redirect has come from approriate router.
1535 *
1536 * RFC 2461 specifies that redirects should only be
1537 * accepted if they come from the nexthop to the target.
1538 * Due to the way the routes are chosen, this notion
1539 * is a bit fuzzy and one might need to check all possible
1540 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Grafc71099a2006-08-04 23:20:06 -07001543 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001544 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001545restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001546 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001547 /*
1548 * Current route is on-link; redirect is always invalid.
1549 *
1550 * Seems, previous statement is not true. It could
1551 * be node, which looks for us as on-link (f.e. proxy ndisc)
1552 * But then router serving it might decide, that we should
1553 * know truth 8)8) --ANK (980726).
1554 */
1555 if (rt6_check_expired(rt))
1556 continue;
1557 if (!(rt->rt6i_flags & RTF_GATEWAY))
1558 continue;
David S. Millerd1918542011-12-28 20:19:20 -05001559 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001560 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001561 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001562 continue;
1563 break;
1564 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001565
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001566 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001567 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001568 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001569out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001570 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001571
1572 read_unlock_bh(&table->tb6_lock);
1573
1574 return rt;
1575};
1576
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001577static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1578 const struct in6_addr *src,
1579 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001580 struct net_device *dev)
1581{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001582 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001583 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001584 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001585 .fl6 = {
1586 .flowi6_oif = dev->ifindex,
1587 .daddr = *dest,
1588 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001589 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001590 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001591
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001592 rdfl.gateway = *gateway;
Brian Haley86c36ce2009-10-07 13:58:01 -07001593
Thomas Grafadaa70b2006-10-13 15:01:03 -07001594 if (rt6_need_strict(dest))
1595 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001596
David S. Miller4c9483b2011-03-12 16:22:43 -05001597 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001598 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001599}
1600
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001601void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1602 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001603 struct neighbour *neigh, u8 *lladdr, int on_link)
1604{
1605 struct rt6_info *rt, *nrt = NULL;
1606 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001607 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001608
1609 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1610
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001611 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (net_ratelimit())
1613 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1614 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * We have finally decided to accept it.
1620 */
1621
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001622 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1624 NEIGH_UPDATE_F_OVERRIDE|
1625 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1626 NEIGH_UPDATE_F_ISROUTER))
1627 );
1628
1629 /*
1630 * Redirect received -> path was valid.
1631 * Look, redirects are sent only in response to data packets,
1632 * so that this nexthop apparently is reachable. --ANK
1633 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001634 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* Duplicate redirect: silently ignore. */
David Miller27217452011-12-02 16:52:08 +00001637 if (neigh == dst_get_neighbour_noref_raw(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out;
1639
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001640 nrt = ip6_rt_copy(rt, dest);
David S. Miller38308472011-12-03 18:02:47 -05001641 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 goto out;
1643
1644 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1645 if (on_link)
1646 nrt->rt6i_flags &= ~RTF_GATEWAY;
1647
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001648 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
David S. Miller69cce1d2011-07-17 23:09:49 -07001649 dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Thomas Graf40e22e82006-08-22 00:00:45 -07001651 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto out;
1653
Changli Gaod8d1f302010-06-10 23:31:35 -07001654 netevent.old = &rt->dst;
1655 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001656 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1657
David S. Miller38308472011-12-03 18:02:47 -05001658 if (rt->rt6i_flags & RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001659 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return;
1661 }
1662
1663out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001664 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667/*
1668 * Handle ICMP "packet too big" messages
1669 * i.e. Path MTU discovery
1670 */
1671
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001672static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001673 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 struct rt6_info *rt, *nrt;
1676 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001677again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001678 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
David S. Miller38308472011-12-03 18:02:47 -05001679 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681
Andrey Vagind3052b52010-12-11 15:20:11 +00001682 if (rt6_check_expired(rt)) {
1683 ip6_del_rt(rt);
1684 goto again;
1685 }
1686
Changli Gaod8d1f302010-06-10 23:31:35 -07001687 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto out;
1689
1690 if (pmtu < IPV6_MIN_MTU) {
1691 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001692 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 * MTU (1280) and a fragment header should always be included
1694 * after a node receiving Too Big message reporting PMTU is
1695 * less than the IPv6 Minimum Link MTU.
1696 */
1697 pmtu = IPV6_MIN_MTU;
1698 allfrag = 1;
1699 }
1700
1701 /* New mtu received -> path was valid.
1702 They are sent only in response to data packets,
1703 so that this nexthop apparently is reachable. --ANK
1704 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001705 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 /* Host route. If it is static, it would be better
1708 not to override it, but add new one, so that
1709 when cache entry will expire old pmtu
1710 would return automatically.
1711 */
1712 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001713 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1714 if (allfrag) {
1715 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1716 features |= RTAX_FEATURE_ALLFRAG;
1717 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1718 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1721 goto out;
1722 }
1723
1724 /* Network route.
1725 Two cases are possible:
1726 1. It is connected route. Action: COW
1727 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1728 */
David Miller27217452011-12-02 16:52:08 +00001729 if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001730 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001731 else
1732 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001733
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001734 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001735 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1736 if (allfrag) {
1737 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1738 features |= RTAX_FEATURE_ALLFRAG;
1739 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1740 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001741
1742 /* According to RFC 1981, detecting PMTU increase shouldn't be
1743 * happened within 5 mins, the recommended timer is 10 mins.
1744 * Here this route expiration time is set to ip6_rt_mtu_expires
1745 * which is 10 mins. After 10 mins the decreased pmtu is expired
1746 * and detecting PMTU increase will be automatically happened.
1747 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001748 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001749 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1750
Thomas Graf40e22e82006-08-22 00:00:45 -07001751 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001754 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001757void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001758 struct net_device *dev, u32 pmtu)
1759{
1760 struct net *net = dev_net(dev);
1761
1762 /*
1763 * RFC 1981 states that a node "MUST reduce the size of the packets it
1764 * is sending along the path" that caused the Packet Too Big message.
1765 * Since it's not possible in the general case to determine which
1766 * interface was used to send the original packet, we update the MTU
1767 * on the interface that will be used to send future packets. We also
1768 * update the MTU on the interface that received the Packet Too Big in
1769 * case the original packet was forced out that interface with
1770 * SO_BINDTODEVICE or similar. This is the next best thing to the
1771 * correct behaviour, which would be to update the MTU on all
1772 * interfaces.
1773 */
1774 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1775 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/*
1779 * Misc support functions
1780 */
1781
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001782static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
1783 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
David S. Millerd1918542011-12-28 20:19:20 -05001785 struct net *net = dev_net(ort->dst.dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001786 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
David S. Miller957c6652011-06-24 15:25:00 -07001787 ort->dst.dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001790 rt->dst.input = ort->dst.input;
1791 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001792 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001794 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001795 rt->rt6i_dst.plen = 128;
David S. Millerdefb3512010-12-08 21:16:57 -08001796 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001797 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 rt->rt6i_idev = ort->rt6i_idev;
1799 if (rt->rt6i_idev)
1800 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001801 rt->dst.lastuse = jiffies;
David S. Millerd1918542011-12-28 20:19:20 -05001802 rt->dst.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001804 rt->rt6i_gateway = ort->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1806 rt->rt6i_metric = 0;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808#ifdef CONFIG_IPV6_SUBTREES
1809 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1810#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001811 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001812 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 return rt;
1815}
1816
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001817#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001818static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001819 const struct in6_addr *prefix, int prefixlen,
1820 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001821{
1822 struct fib6_node *fn;
1823 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001824 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001825
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001826 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05001827 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001828 return NULL;
1829
1830 write_lock_bh(&table->tb6_lock);
1831 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001832 if (!fn)
1833 goto out;
1834
Changli Gaod8d1f302010-06-10 23:31:35 -07001835 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001836 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001837 continue;
1838 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1839 continue;
1840 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1841 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001842 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001843 break;
1844 }
1845out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001846 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001847 return rt;
1848}
1849
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001850static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001851 const struct in6_addr *prefix, int prefixlen,
1852 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001853 unsigned pref)
1854{
Thomas Graf86872cb2006-08-22 00:01:08 -07001855 struct fib6_config cfg = {
1856 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001857 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001858 .fc_ifindex = ifindex,
1859 .fc_dst_len = prefixlen,
1860 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1861 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001862 .fc_nlinfo.pid = 0,
1863 .fc_nlinfo.nlh = NULL,
1864 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001865 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001866
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001867 cfg.fc_dst = *prefix;
1868 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07001869
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001870 /* We should treat it as a default route if prefix length is 0. */
1871 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001872 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001873
Thomas Graf86872cb2006-08-22 00:01:08 -07001874 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001875
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001876 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001877}
1878#endif
1879
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001880struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001883 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001885 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05001886 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001887 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Thomas Grafc71099a2006-08-04 23:20:06 -07001889 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001890 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001891 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001892 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1894 break;
1895 }
1896 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001897 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001898 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return rt;
1900}
1901
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001902struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001903 struct net_device *dev,
1904 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Thomas Graf86872cb2006-08-22 00:01:08 -07001906 struct fib6_config cfg = {
1907 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001908 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001909 .fc_ifindex = dev->ifindex,
1910 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1911 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001912 .fc_nlinfo.pid = 0,
1913 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001914 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001917 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Thomas Graf86872cb2006-08-22 00:01:08 -07001919 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return rt6_get_dflt_router(gwaddr, dev);
1922}
1923
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001924void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001927 struct fib6_table *table;
1928
1929 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001930 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05001931 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001932 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001935 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001936 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001938 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001939 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001940 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto restart;
1942 }
1943 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001944 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Daniel Lezcano55786892008-03-04 13:47:47 -08001947static void rtmsg_to_fib6_config(struct net *net,
1948 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001949 struct fib6_config *cfg)
1950{
1951 memset(cfg, 0, sizeof(*cfg));
1952
1953 cfg->fc_table = RT6_TABLE_MAIN;
1954 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1955 cfg->fc_metric = rtmsg->rtmsg_metric;
1956 cfg->fc_expires = rtmsg->rtmsg_info;
1957 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1958 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1959 cfg->fc_flags = rtmsg->rtmsg_flags;
1960
Daniel Lezcano55786892008-03-04 13:47:47 -08001961 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001962
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001963 cfg->fc_dst = rtmsg->rtmsg_dst;
1964 cfg->fc_src = rtmsg->rtmsg_src;
1965 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07001966}
1967
Daniel Lezcano55786892008-03-04 13:47:47 -08001968int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Thomas Graf86872cb2006-08-22 00:01:08 -07001970 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 struct in6_rtmsg rtmsg;
1972 int err;
1973
1974 switch(cmd) {
1975 case SIOCADDRT: /* Add a route */
1976 case SIOCDELRT: /* Delete a route */
1977 if (!capable(CAP_NET_ADMIN))
1978 return -EPERM;
1979 err = copy_from_user(&rtmsg, arg,
1980 sizeof(struct in6_rtmsg));
1981 if (err)
1982 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001983
Daniel Lezcano55786892008-03-04 13:47:47 -08001984 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 rtnl_lock();
1987 switch (cmd) {
1988 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001989 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 break;
1991 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001992 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994 default:
1995 err = -EINVAL;
1996 }
1997 rtnl_unlock();
1998
1999 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 return -EINVAL;
2003}
2004
2005/*
2006 * Drop the packet on the floor
2007 */
2008
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002009static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002011 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002012 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002013 switch (ipstats_mib_noroutes) {
2014 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002015 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002016 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002017 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2018 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002019 break;
2020 }
2021 /* FALLTHROUGH */
2022 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002023 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2024 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002025 break;
2026 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002027 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 kfree_skb(skb);
2029 return 0;
2030}
2031
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002032static int ip6_pkt_discard(struct sk_buff *skb)
2033{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002034 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002035}
2036
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002037static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Eric Dumazetadf30902009-06-02 05:19:30 +00002039 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002040 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
David S. Miller6723ab52006-10-18 21:20:57 -07002043#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2044
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002045static int ip6_pkt_prohibit(struct sk_buff *skb)
2046{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002047 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002048}
2049
2050static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2051{
Eric Dumazetadf30902009-06-02 05:19:30 +00002052 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002053 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002054}
2055
David S. Miller6723ab52006-10-18 21:20:57 -07002056#endif
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058/*
2059 * Allocate a dst for local (unicast / anycast) address.
2060 */
2061
2062struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2063 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002064 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002066 struct net *net = dev_net(idev->dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002067 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
David S. Miller957c6652011-06-24 15:25:00 -07002068 net->loopback_dev, 0);
David S. Millerf83c7792011-12-28 15:41:23 -05002069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
David S. Miller38308472011-12-03 18:02:47 -05002071 if (!rt) {
Ben Greear40385652010-11-08 12:33:48 +00002072 if (net_ratelimit())
2073 pr_warning("IPv6: Maximum number of routes reached,"
2074 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 in6_dev_hold(idev);
2079
David S. Miller11d53b42011-06-24 15:23:34 -07002080 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002081 rt->dst.input = ip6_input;
2082 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002084 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002087 if (anycast)
2088 rt->rt6i_flags |= RTF_ANYCAST;
2089 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller8ade06c2011-12-29 18:51:57 -05002091 err = rt6_bind_neighbour(rt, rt->dst.dev);
David S. Millerf83c7792011-12-28 15:41:23 -05002092 if (err) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002093 dst_free(&rt->dst);
David S. Millerf83c7792011-12-28 15:41:23 -05002094 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002097 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002099 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Changli Gaod8d1f302010-06-10 23:31:35 -07002101 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 return rt;
2104}
2105
Daniel Walterc3968a82011-04-13 21:10:57 +00002106int ip6_route_get_saddr(struct net *net,
2107 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002108 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002109 unsigned int prefs,
2110 struct in6_addr *saddr)
2111{
2112 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2113 int err = 0;
2114 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002115 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002116 else
2117 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2118 daddr, prefs, saddr);
2119 return err;
2120}
2121
2122/* remove deleted ip from prefsrc entries */
2123struct arg_dev_net_ip {
2124 struct net_device *dev;
2125 struct net *net;
2126 struct in6_addr *addr;
2127};
2128
2129static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2130{
2131 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2132 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2133 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2134
David S. Millerd1918542011-12-28 20:19:20 -05002135 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002136 rt != net->ipv6.ip6_null_entry &&
2137 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2138 /* remove prefsrc entry */
2139 rt->rt6i_prefsrc.plen = 0;
2140 }
2141 return 0;
2142}
2143
2144void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2145{
2146 struct net *net = dev_net(ifp->idev->dev);
2147 struct arg_dev_net_ip adni = {
2148 .dev = ifp->idev->dev,
2149 .net = net,
2150 .addr = &ifp->addr,
2151 };
2152 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2153}
2154
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002155struct arg_dev_net {
2156 struct net_device *dev;
2157 struct net *net;
2158};
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160static int fib6_ifdown(struct rt6_info *rt, void *arg)
2161{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002162 const struct arg_dev_net *adn = arg;
2163 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002164
David S. Millerd1918542011-12-28 20:19:20 -05002165 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002166 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
2170}
2171
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002172void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002174 struct arg_dev_net adn = {
2175 .dev = dev,
2176 .net = net,
2177 };
2178
2179 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002180 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183struct rt6_mtu_change_arg
2184{
2185 struct net_device *dev;
2186 unsigned mtu;
2187};
2188
2189static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2190{
2191 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2192 struct inet6_dev *idev;
2193
2194 /* In IPv6 pmtu discovery is not optional,
2195 so that RTAX_MTU lock cannot disable it.
2196 We still use this lock to block changes
2197 caused by addrconf/ndisc.
2198 */
2199
2200 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002201 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203
2204 /* For administrative MTU increase, there is no way to discover
2205 IPv6 PMTU increase, so PMTU increase should be updated here.
2206 Since RFC 1981 doesn't include administrative MTU increase
2207 update PMTU increase is a MUST. (i.e. jumbo frame)
2208 */
2209 /*
2210 If new MTU is less than route PMTU, this new MTU will be the
2211 lowest MTU in the path, update the route PMTU to reflect PMTU
2212 decreases; if new MTU is greater than route PMTU, and the
2213 old MTU is the lowest MTU in the path, update the route PMTU
2214 to reflect the increase. In this case if the other nodes' MTU
2215 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2216 PMTU discouvery.
2217 */
David S. Millerd1918542011-12-28 20:19:20 -05002218 if (rt->dst.dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002219 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2220 (dst_mtu(&rt->dst) >= arg->mtu ||
2221 (dst_mtu(&rt->dst) < arg->mtu &&
2222 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002223 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 0;
2226}
2227
2228void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2229{
Thomas Grafc71099a2006-08-04 23:20:06 -07002230 struct rt6_mtu_change_arg arg = {
2231 .dev = dev,
2232 .mtu = mtu,
2233 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002235 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002238static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002239 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002240 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002241 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002242 [RTA_PRIORITY] = { .type = NLA_U32 },
2243 [RTA_METRICS] = { .type = NLA_NESTED },
2244};
2245
2246static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2247 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Thomas Graf86872cb2006-08-22 00:01:08 -07002249 struct rtmsg *rtm;
2250 struct nlattr *tb[RTA_MAX+1];
2251 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Thomas Graf86872cb2006-08-22 00:01:08 -07002253 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2254 if (err < 0)
2255 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Thomas Graf86872cb2006-08-22 00:01:08 -07002257 err = -EINVAL;
2258 rtm = nlmsg_data(nlh);
2259 memset(cfg, 0, sizeof(*cfg));
2260
2261 cfg->fc_table = rtm->rtm_table;
2262 cfg->fc_dst_len = rtm->rtm_dst_len;
2263 cfg->fc_src_len = rtm->rtm_src_len;
2264 cfg->fc_flags = RTF_UP;
2265 cfg->fc_protocol = rtm->rtm_protocol;
2266
2267 if (rtm->rtm_type == RTN_UNREACHABLE)
2268 cfg->fc_flags |= RTF_REJECT;
2269
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002270 if (rtm->rtm_type == RTN_LOCAL)
2271 cfg->fc_flags |= RTF_LOCAL;
2272
Thomas Graf86872cb2006-08-22 00:01:08 -07002273 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2274 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002275 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002276
2277 if (tb[RTA_GATEWAY]) {
2278 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2279 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002281
2282 if (tb[RTA_DST]) {
2283 int plen = (rtm->rtm_dst_len + 7) >> 3;
2284
2285 if (nla_len(tb[RTA_DST]) < plen)
2286 goto errout;
2287
2288 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002290
2291 if (tb[RTA_SRC]) {
2292 int plen = (rtm->rtm_src_len + 7) >> 3;
2293
2294 if (nla_len(tb[RTA_SRC]) < plen)
2295 goto errout;
2296
2297 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002299
Daniel Walterc3968a82011-04-13 21:10:57 +00002300 if (tb[RTA_PREFSRC])
2301 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2302
Thomas Graf86872cb2006-08-22 00:01:08 -07002303 if (tb[RTA_OIF])
2304 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2305
2306 if (tb[RTA_PRIORITY])
2307 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2308
2309 if (tb[RTA_METRICS]) {
2310 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2311 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002313
2314 if (tb[RTA_TABLE])
2315 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2316
2317 err = 0;
2318errout:
2319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Thomas Grafc127ea22007-03-22 11:58:32 -07002322static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Thomas Graf86872cb2006-08-22 00:01:08 -07002324 struct fib6_config cfg;
2325 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Thomas Graf86872cb2006-08-22 00:01:08 -07002327 err = rtm_to_fib6_config(skb, nlh, &cfg);
2328 if (err < 0)
2329 return err;
2330
2331 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Thomas Grafc127ea22007-03-22 11:58:32 -07002334static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Thomas Graf86872cb2006-08-22 00:01:08 -07002336 struct fib6_config cfg;
2337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Thomas Graf86872cb2006-08-22 00:01:08 -07002339 err = rtm_to_fib6_config(skb, nlh, &cfg);
2340 if (err < 0)
2341 return err;
2342
2343 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Thomas Graf339bf982006-11-10 14:10:15 -08002346static inline size_t rt6_nlmsg_size(void)
2347{
2348 return NLMSG_ALIGN(sizeof(struct rtmsg))
2349 + nla_total_size(16) /* RTA_SRC */
2350 + nla_total_size(16) /* RTA_DST */
2351 + nla_total_size(16) /* RTA_GATEWAY */
2352 + nla_total_size(16) /* RTA_PREFSRC */
2353 + nla_total_size(4) /* RTA_TABLE */
2354 + nla_total_size(4) /* RTA_IIF */
2355 + nla_total_size(4) /* RTA_OIF */
2356 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002357 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002358 + nla_total_size(sizeof(struct rta_cacheinfo));
2359}
2360
Brian Haley191cd582008-08-14 15:33:21 -07002361static int rt6_fill_node(struct net *net,
2362 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002363 struct in6_addr *dst, struct in6_addr *src,
2364 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002365 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
David S. Miller346f8702011-12-29 15:22:33 -05002367 const struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002369 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002370 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002371 u32 table;
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002372 struct neighbour *n;
David S. Miller346f8702011-12-29 15:22:33 -05002373 u32 ts, tsage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (prefix) { /* user wants prefix routes only */
2376 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2377 /* success since this is not a prefix route */
2378 return 1;
2379 }
2380 }
2381
Thomas Graf2d7202b2006-08-22 00:01:27 -07002382 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002383 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002384 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002385
2386 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 rtm->rtm_family = AF_INET6;
2388 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2389 rtm->rtm_src_len = rt->rt6i_src.plen;
2390 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002391 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002392 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002393 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002394 table = RT6_TABLE_UNSPEC;
2395 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002396 NLA_PUT_U32(skb, RTA_TABLE, table);
David S. Miller38308472011-12-03 18:02:47 -05002397 if (rt->rt6i_flags & RTF_REJECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 rtm->rtm_type = RTN_UNREACHABLE;
David S. Miller38308472011-12-03 18:02:47 -05002399 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002400 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002401 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 rtm->rtm_type = RTN_LOCAL;
2403 else
2404 rtm->rtm_type = RTN_UNICAST;
2405 rtm->rtm_flags = 0;
2406 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2407 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002408 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 rtm->rtm_protocol = RTPROT_REDIRECT;
2410 else if (rt->rt6i_flags & RTF_ADDRCONF)
2411 rtm->rtm_protocol = RTPROT_KERNEL;
David S. Miller38308472011-12-03 18:02:47 -05002412 else if (rt->rt6i_flags & RTF_DEFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 rtm->rtm_protocol = RTPROT_RA;
2414
David S. Miller38308472011-12-03 18:02:47 -05002415 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 rtm->rtm_flags |= RTM_F_CLONED;
2417
2418 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002419 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002420 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002422 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423#ifdef CONFIG_IPV6_SUBTREES
2424 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002425 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002426 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002428 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002430 if (iif) {
2431#ifdef CONFIG_IPV6_MROUTE
2432 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002433 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002434 if (err <= 0) {
2435 if (!nowait) {
2436 if (err == 0)
2437 return 0;
2438 goto nla_put_failure;
2439 } else {
2440 if (err == -EMSGSIZE)
2441 goto nla_put_failure;
2442 }
2443 }
2444 } else
2445#endif
2446 NLA_PUT_U32(skb, RTA_IIF, iif);
2447 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002449 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002450 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002452
Daniel Walterc3968a82011-04-13 21:10:57 +00002453 if (rt->rt6i_prefsrc.plen) {
2454 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002455 saddr_buf = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002456 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2457 }
2458
David S. Millerdefb3512010-12-08 21:16:57 -08002459 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002460 goto nla_put_failure;
2461
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002462 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +00002463 n = dst_get_neighbour_noref(&rt->dst);
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002464 if (n)
2465 NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2466 rcu_read_unlock();
Thomas Graf2d7202b2006-08-22 00:01:27 -07002467
Changli Gaod8d1f302010-06-10 23:31:35 -07002468 if (rt->dst.dev)
David S. Millerd1918542011-12-28 20:19:20 -05002469 NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002470
2471 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002472
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002473 if (!(rt->rt6i_flags & RTF_EXPIRES))
2474 expires = 0;
David S. Millerd1918542011-12-28 20:19:20 -05002475 else if (rt->dst.expires - jiffies < INT_MAX)
2476 expires = rt->dst.expires - jiffies;
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002477 else
2478 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002479
David S. Miller346f8702011-12-29 15:22:33 -05002480 peer = rt->rt6i_peer;
2481 ts = tsage = 0;
2482 if (peer && peer->tcp_ts_stamp) {
2483 ts = peer->tcp_ts;
2484 tsage = get_seconds() - peer->tcp_ts_stamp;
2485 }
2486
2487 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
Changli Gaod8d1f302010-06-10 23:31:35 -07002488 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002489 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Thomas Graf2d7202b2006-08-22 00:01:27 -07002491 return nlmsg_end(skb, nlh);
2492
2493nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002494 nlmsg_cancel(skb, nlh);
2495 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Patrick McHardy1b43af52006-08-10 23:11:17 -07002498int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2501 int prefix;
2502
Thomas Graf2d7202b2006-08-22 00:01:27 -07002503 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2504 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2506 } else
2507 prefix = 0;
2508
Brian Haley191cd582008-08-14 15:33:21 -07002509 return rt6_fill_node(arg->net,
2510 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002512 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Thomas Grafc127ea22007-03-22 11:58:32 -07002515static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002517 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002518 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002520 struct sk_buff *skb;
2521 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002522 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002523 int err, iif = 0;
2524
2525 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2526 if (err < 0)
2527 goto errout;
2528
2529 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002530 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002531
2532 if (tb[RTA_SRC]) {
2533 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2534 goto errout;
2535
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002536 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002537 }
2538
2539 if (tb[RTA_DST]) {
2540 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2541 goto errout;
2542
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002543 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002544 }
2545
2546 if (tb[RTA_IIF])
2547 iif = nla_get_u32(tb[RTA_IIF]);
2548
2549 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002550 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002551
2552 if (iif) {
2553 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002554 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002555 if (!dev) {
2556 err = -ENODEV;
2557 goto errout;
2558 }
2559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002562 if (!skb) {
Thomas Grafab364a62006-08-22 00:01:47 -07002563 err = -ENOBUFS;
2564 goto errout;
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 /* Reserve room for dummy headers, this skb can pass
2568 through good chunk of routing engine.
2569 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002570 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2572
David S. Miller4c9483b2011-03-12 16:22:43 -05002573 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002574 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David S. Miller4c9483b2011-03-12 16:22:43 -05002576 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002578 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002580 kfree_skb(skb);
2581 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
Daniel Lezcano55786892008-03-04 13:47:47 -08002584 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002585errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Thomas Graf86872cb2006-08-22 00:01:08 -07002589void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002592 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002593 u32 seq;
2594 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002596 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002597 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002598
Thomas Graf339bf982006-11-10 14:10:15 -08002599 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002600 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002601 goto errout;
2602
Brian Haley191cd582008-08-14 15:33:21 -07002603 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002604 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002605 if (err < 0) {
2606 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2607 WARN_ON(err == -EMSGSIZE);
2608 kfree_skb(skb);
2609 goto errout;
2610 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002611 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2612 info->nlh, gfp_any());
2613 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002614errout:
2615 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002616 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002619static int ip6_route_dev_notify(struct notifier_block *this,
2620 unsigned long event, void *data)
2621{
2622 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002623 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002624
2625 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002626 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002627 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2628#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002629 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002630 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002631 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002632 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2633#endif
2634 }
2635
2636 return NOTIFY_OK;
2637}
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639/*
2640 * /proc
2641 */
2642
2643#ifdef CONFIG_PROC_FS
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645struct rt6_proc_arg
2646{
2647 char *buffer;
2648 int offset;
2649 int length;
2650 int skip;
2651 int len;
2652};
2653
2654static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2655{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002656 struct seq_file *m = p_arg;
David S. Miller69cce1d2011-07-17 23:09:49 -07002657 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002659 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002662 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002664 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665#endif
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002666 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +00002667 n = dst_get_neighbour_noref(&rt->dst);
David S. Miller69cce1d2011-07-17 23:09:49 -07002668 if (n) {
2669 seq_printf(m, "%pi6", n->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002671 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002673 rcu_read_unlock();
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002674 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002675 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2676 rt->dst.__use, rt->rt6i_flags,
David S. Millerd1918542011-12-28 20:19:20 -05002677 rt->dst.dev ? rt->dst.dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
2679}
2680
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002681static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002683 struct net *net = (struct net *)m->private;
Josh Hunt32b293a2011-12-28 13:23:07 +00002684 fib6_clean_all_ro(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002688static int ipv6_route_open(struct inode *inode, struct file *file)
2689{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002690 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002691}
2692
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002693static const struct file_operations ipv6_route_proc_fops = {
2694 .owner = THIS_MODULE,
2695 .open = ipv6_route_open,
2696 .read = seq_read,
2697 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002698 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002699};
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2702{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002703 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002705 net->ipv6.rt6_stats->fib_nodes,
2706 net->ipv6.rt6_stats->fib_route_nodes,
2707 net->ipv6.rt6_stats->fib_rt_alloc,
2708 net->ipv6.rt6_stats->fib_rt_entries,
2709 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002710 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002711 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 return 0;
2714}
2715
2716static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2717{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002718 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002719}
2720
Arjan van de Ven9a321442007-02-12 00:55:35 -08002721static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .owner = THIS_MODULE,
2723 .open = rt6_stats_seq_open,
2724 .read = seq_read,
2725 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002726 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727};
2728#endif /* CONFIG_PROC_FS */
2729
2730#ifdef CONFIG_SYSCTL
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002733int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 void __user *buffer, size_t *lenp, loff_t *ppos)
2735{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002736 struct net *net;
2737 int delay;
2738 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002740
2741 net = (struct net *)ctl->extra1;
2742 delay = net->ipv6.sysctl.flush_delay;
2743 proc_dointvec(ctl, write, buffer, lenp, ppos);
2744 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002748ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002749 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002751 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002753 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002754 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 },
2756 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002758 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 .maxlen = sizeof(int),
2760 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002761 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 },
2763 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002765 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 .maxlen = sizeof(int),
2767 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002768 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 },
2770 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002772 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 .maxlen = sizeof(int),
2774 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002775 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 },
2777 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002779 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 .maxlen = sizeof(int),
2781 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002782 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 },
2784 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002786 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 .maxlen = sizeof(int),
2788 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002789 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 },
2791 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002793 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 .maxlen = sizeof(int),
2795 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002796 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 },
2798 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002800 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 .maxlen = sizeof(int),
2802 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002803 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 },
2805 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002807 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .maxlen = sizeof(int),
2809 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002810 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 },
2812 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002814 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 .maxlen = sizeof(int),
2816 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002817 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002819 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820};
2821
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002822struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002823{
2824 struct ctl_table *table;
2825
2826 table = kmemdup(ipv6_route_table_template,
2827 sizeof(ipv6_route_table_template),
2828 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002829
2830 if (table) {
2831 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002832 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002833 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002834 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2835 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2836 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2837 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2838 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2839 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2840 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002841 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002842 }
2843
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002844 return table;
2845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846#endif
2847
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002848static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002849{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002850 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002851
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002852 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2853 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002854
Eric Dumazetfc66f952010-10-08 06:37:34 +00002855 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2856 goto out_ip6_dst_ops;
2857
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002858 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2859 sizeof(*net->ipv6.ip6_null_entry),
2860 GFP_KERNEL);
2861 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002862 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002863 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002864 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002865 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002866 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2867 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002868
2869#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2870 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2871 sizeof(*net->ipv6.ip6_prohibit_entry),
2872 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002873 if (!net->ipv6.ip6_prohibit_entry)
2874 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002875 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002876 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002877 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002878 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2879 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002880
2881 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2882 sizeof(*net->ipv6.ip6_blk_hole_entry),
2883 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002884 if (!net->ipv6.ip6_blk_hole_entry)
2885 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002886 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002887 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002888 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002889 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2890 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002891#endif
2892
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002893 net->ipv6.sysctl.flush_delay = 0;
2894 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2895 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2896 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2897 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2898 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2899 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2900 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2901
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002902#ifdef CONFIG_PROC_FS
2903 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2904 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2905#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002906 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2907
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002908 ret = 0;
2909out:
2910 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002911
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002912#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2913out_ip6_prohibit_entry:
2914 kfree(net->ipv6.ip6_prohibit_entry);
2915out_ip6_null_entry:
2916 kfree(net->ipv6.ip6_null_entry);
2917#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002918out_ip6_dst_entries:
2919 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002920out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002921 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002922}
2923
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002924static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002925{
2926#ifdef CONFIG_PROC_FS
2927 proc_net_remove(net, "ipv6_route");
2928 proc_net_remove(net, "rt6_stats");
2929#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002930 kfree(net->ipv6.ip6_null_entry);
2931#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2932 kfree(net->ipv6.ip6_prohibit_entry);
2933 kfree(net->ipv6.ip6_blk_hole_entry);
2934#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002935 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002936}
2937
2938static struct pernet_operations ip6_route_net_ops = {
2939 .init = ip6_route_net_init,
2940 .exit = ip6_route_net_exit,
2941};
2942
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002943static struct notifier_block ip6_route_dev_notifier = {
2944 .notifier_call = ip6_route_dev_notify,
2945 .priority = 0,
2946};
2947
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002948int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002950 int ret;
2951
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002952 ret = -ENOMEM;
2953 ip6_dst_ops_template.kmem_cachep =
2954 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2955 SLAB_HWCACHE_ALIGN, NULL);
2956 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002957 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002958
Eric Dumazetfc66f952010-10-08 06:37:34 +00002959 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002960 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002961 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002962
Eric Dumazetfc66f952010-10-08 06:37:34 +00002963 ret = register_pernet_subsys(&ip6_route_net_ops);
2964 if (ret)
2965 goto out_dst_entries;
2966
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002967 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2968
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002969 /* Registering of the loopback is done before this portion of code,
2970 * the loopback reference in rt6_info will not be taken, do it
2971 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002972 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002973 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2974 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002975 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002976 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002977 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002978 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2979 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002980 ret = fib6_init();
2981 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002982 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002983
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002984 ret = xfrm6_init();
2985 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002986 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002987
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002988 ret = fib6_rules_init();
2989 if (ret)
2990 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002991
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002992 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002993 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2994 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2995 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002996 goto fib6_rules_init;
2997
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002998 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002999 if (ret)
3000 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003001
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003002out:
3003 return ret;
3004
3005fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003006 fib6_rules_cleanup();
3007xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003008 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003009out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003010 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003011out_register_subsys:
3012 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003013out_dst_entries:
3014 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003015out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003016 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020void ip6_route_cleanup(void)
3021{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003022 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07003023 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003026 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003027 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003028 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}