blob: 053a92ebf2d5a942737fe9206b62f83a5659b329 [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>
29#include <linux/types.h>
30#include <linux/times.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
33#include <linux/net.h>
34#include <linux/route.h>
35#include <linux/netdevice.h>
36#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090037#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080042#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020044#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/snmp.h>
46#include <net/ipv6.h>
47#include <net/ip6_fib.h>
48#include <net/ip6_route.h>
49#include <net/ndisc.h>
50#include <net/addrconf.h>
51#include <net/tcp.h>
52#include <linux/rtnetlink.h>
53#include <net/dst.h>
54#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070055#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070056#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <asm/uaccess.h>
59
60#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
63
64/* Set to 3 to get tracing. */
65#define RT6_DEBUG 2
66
67#if RT6_DEBUG >= 3
68#define RDBG(x) printk x
69#define RT6_TRACE(x...) printk(KERN_DEBUG x)
70#else
71#define RDBG(x)
72#define RT6_TRACE(x...) do { ; } while (0)
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
76static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080077static unsigned int ip6_default_advmss(const struct dst_entry *dst);
David S. Millerd33e4552010-12-14 13:01:14 -080078static unsigned int ip6_default_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static struct dst_entry *ip6_negative_advice(struct dst_entry *);
80static void ip6_dst_destroy(struct dst_entry *);
81static void ip6_dst_ifdown(struct dst_entry *,
82 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080083static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int ip6_pkt_discard(struct sk_buff *skb);
86static int ip6_pkt_discard_out(struct sk_buff *skb);
87static void ip6_link_failure(struct sk_buff *skb);
88static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
89
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080090#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080091static struct rt6_info *rt6_add_route_info(struct net *net,
92 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080093 struct in6_addr *gwaddr, int ifindex,
94 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080095static struct rt6_info *rt6_get_route_info(struct net *net,
96 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080097 struct in6_addr *gwaddr, int ifindex);
98#endif
99
David S. Miller06582542011-01-27 14:58:42 -0800100static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
101{
102 struct rt6_info *rt = (struct rt6_info *) dst;
103 struct inet_peer *peer;
104 u32 *p = NULL;
105
106 if (!rt->rt6i_peer)
107 rt6_bind_peer(rt, 1);
108
109 peer = rt->rt6i_peer;
110 if (peer) {
111 u32 *old_p = __DST_METRICS_PTR(old);
112 unsigned long prev, new;
113
114 p = peer->metrics;
115 if (inet_metrics_new(peer))
116 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
117
118 new = (unsigned long) p;
119 prev = cmpxchg(&dst->_metrics, old, new);
120
121 if (prev != old) {
122 p = __DST_METRICS_PTR(prev);
123 if (prev & DST_METRICS_READ_ONLY)
124 p = NULL;
125 }
126 }
127 return p;
128}
129
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800130static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800132 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .gc = ip6_dst_gc,
134 .gc_thresh = 1024,
135 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800136 .default_advmss = ip6_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800137 .default_mtu = ip6_default_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800138 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .destroy = ip6_dst_destroy,
140 .ifdown = ip6_dst_ifdown,
141 .negative_advice = ip6_negative_advice,
142 .link_failure = ip6_link_failure,
143 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700144 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Roland Dreierec831ea2011-01-31 13:16:00 -0800147static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
148{
149 return 0;
150}
151
David S. Miller14e50e52007-05-24 18:17:54 -0700152static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
153{
154}
155
156static struct dst_ops ip6_dst_blackhole_ops = {
157 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800158 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700159 .destroy = ip6_dst_destroy,
160 .check = ip6_dst_check,
Roland Dreierec831ea2011-01-31 13:16:00 -0800161 .default_mtu = ip6_blackhole_default_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800162 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700163 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Miller14e50e52007-05-24 18:17:54 -0700164};
165
David S. Miller62fa8a82011-01-26 20:51:05 -0800166static const u32 ip6_template_metrics[RTAX_MAX] = {
167 [RTAX_HOPLIMIT - 1] = 255,
168};
169
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800170static struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700171 .dst = {
172 .__refcnt = ATOMIC_INIT(1),
173 .__use = 1,
174 .obsolete = -1,
175 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700176 .input = ip6_pkt_discard,
177 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 },
179 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700180 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .rt6i_metric = ~(u32) 0,
182 .rt6i_ref = ATOMIC_INIT(1),
183};
184
Thomas Graf101367c2006-08-04 03:39:02 -0700185#ifdef CONFIG_IPV6_MULTIPLE_TABLES
186
David S. Miller6723ab52006-10-18 21:20:57 -0700187static int ip6_pkt_prohibit(struct sk_buff *skb);
188static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700189
Adrian Bunk280a34c2008-04-21 02:29:32 -0700190static struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700191 .dst = {
192 .__refcnt = ATOMIC_INIT(1),
193 .__use = 1,
194 .obsolete = -1,
195 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700196 .input = ip6_pkt_prohibit,
197 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700198 },
199 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700200 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700201 .rt6i_metric = ~(u32) 0,
202 .rt6i_ref = ATOMIC_INIT(1),
203};
204
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800205static struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700206 .dst = {
207 .__refcnt = ATOMIC_INIT(1),
208 .__use = 1,
209 .obsolete = -1,
210 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700211 .input = dst_discard,
212 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700213 },
214 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700215 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700216 .rt6i_metric = ~(u32) 0,
217 .rt6i_ref = ATOMIC_INIT(1),
218};
219
220#endif
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* allocate dst with ip6_dst_ops */
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800223static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
David S. Miller3c7bd1a2011-02-16 14:08:44 -0800225 return (struct rt6_info *)dst_alloc(ops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static void ip6_dst_destroy(struct dst_entry *dst)
229{
230 struct rt6_info *rt = (struct rt6_info *)dst;
231 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800232 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (idev != NULL) {
235 rt->rt6i_idev = NULL;
236 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900237 }
David S. Millerb3419362010-11-30 12:27:11 -0800238 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800239 rt->rt6i_peer = NULL;
240 inet_putpeer(peer);
241 }
242}
243
David S. Miller6431cbc2011-02-07 20:38:06 -0800244static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
245
246static u32 rt6_peer_genid(void)
247{
248 return atomic_read(&__rt6_peer_genid);
249}
250
David S. Millerb3419362010-11-30 12:27:11 -0800251void rt6_bind_peer(struct rt6_info *rt, int create)
252{
253 struct inet_peer *peer;
254
David S. Millerb3419362010-11-30 12:27:11 -0800255 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
256 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
257 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800258 else
259 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
263 int how)
264{
265 struct rt6_info *rt = (struct rt6_info *)dst;
266 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800267 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900268 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800270 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
271 struct inet6_dev *loopback_idev =
272 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (loopback_idev != NULL) {
274 rt->rt6i_idev = loopback_idev;
275 in6_dev_put(idev);
276 }
277 }
278}
279
280static __inline__ int rt6_check_expired(const struct rt6_info *rt)
281{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000282 return (rt->rt6i_flags & RTF_EXPIRES) &&
283 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Thomas Grafc71099a2006-08-04 23:20:06 -0700286static inline int rt6_need_strict(struct in6_addr *daddr)
287{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000288 return ipv6_addr_type(daddr) &
289 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700293 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800296static inline struct rt6_info *rt6_device_match(struct net *net,
297 struct rt6_info *rt,
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900298 struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700300 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct rt6_info *local = NULL;
303 struct rt6_info *sprt;
304
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900305 if (!oif && ipv6_addr_any(saddr))
306 goto out;
307
Changli Gaod8d1f302010-06-10 23:31:35 -0700308 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900309 struct net_device *dev = sprt->rt6i_dev;
310
311 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (dev->ifindex == oif)
313 return sprt;
314 if (dev->flags & IFF_LOOPBACK) {
315 if (sprt->rt6i_idev == NULL ||
316 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700317 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900319 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 local->rt6i_idev->dev->ifindex == oif))
321 continue;
322 }
323 local = sprt;
324 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900325 } else {
326 if (ipv6_chk_addr(net, saddr, dev,
327 flags & RT6_LOOKUP_F_IFACE))
328 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900332 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (local)
334 return local;
335
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700336 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800337 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return rt;
341}
342
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800343#ifdef CONFIG_IPV6_ROUTER_PREF
344static void rt6_probe(struct rt6_info *rt)
345{
346 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
347 /*
348 * Okay, this does not seem to be appropriate
349 * for now, however, we need to check if it
350 * is really so; aka Router Reachability Probing.
351 *
352 * Router Reachability Probe MUST be rate-limited
353 * to no more than one per minute.
354 */
355 if (!neigh || (neigh->nud_state & NUD_VALID))
356 return;
357 read_lock_bh(&neigh->lock);
358 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800359 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800360 struct in6_addr mcaddr;
361 struct in6_addr *target;
362
363 neigh->updated = jiffies;
364 read_unlock_bh(&neigh->lock);
365
366 target = (struct in6_addr *)&neigh->primary_key;
367 addrconf_addr_solict_mult(target, &mcaddr);
368 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
369 } else
370 read_unlock_bh(&neigh->lock);
371}
372#else
373static inline void rt6_probe(struct rt6_info *rt)
374{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800375}
376#endif
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800379 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700381static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800383 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700384 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800385 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700386 if ((dev->flags & IFF_LOOPBACK) &&
387 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
388 return 1;
389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Dave Jonesb6f99a22007-03-22 12:27:49 -0700392static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800394 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800395 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700396 if (rt->rt6i_flags & RTF_NONEXTHOP ||
397 !(rt->rt6i_flags & RTF_GATEWAY))
398 m = 1;
399 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800400 read_lock_bh(&neigh->lock);
401 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700402 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800403#ifdef CONFIG_IPV6_ROUTER_PREF
404 else if (neigh->nud_state & NUD_FAILED)
405 m = 0;
406#endif
407 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800408 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800409 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800410 } else
411 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800412 return m;
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800415static int rt6_score_route(struct rt6_info *rt, int oif,
416 int strict)
417{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700418 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900419
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700420 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700421 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800422 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800423#ifdef CONFIG_IPV6_ROUTER_PREF
424 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
425#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700426 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800427 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800428 return -1;
429 return m;
430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David S. Millerf11e6652007-03-24 20:36:25 -0700432static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
433 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800434{
David S. Millerf11e6652007-03-24 20:36:25 -0700435 int m;
436
437 if (rt6_check_expired(rt))
438 goto out;
439
440 m = rt6_score_route(rt, oif, strict);
441 if (m < 0)
442 goto out;
443
444 if (m > *mpri) {
445 if (strict & RT6_LOOKUP_F_REACHABLE)
446 rt6_probe(match);
447 *mpri = m;
448 match = rt;
449 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
450 rt6_probe(rt);
451 }
452
453out:
454 return match;
455}
456
457static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
458 struct rt6_info *rr_head,
459 u32 metric, int oif, int strict)
460{
461 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800462 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
David S. Millerf11e6652007-03-24 20:36:25 -0700464 match = NULL;
465 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700466 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700467 match = find_match(rt, oif, strict, &mpri, match);
468 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700469 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700470 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800471
David S. Millerf11e6652007-03-24 20:36:25 -0700472 return match;
473}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800474
David S. Millerf11e6652007-03-24 20:36:25 -0700475static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
476{
477 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800478 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Millerf11e6652007-03-24 20:36:25 -0700480 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800481 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David S. Millerf11e6652007-03-24 20:36:25 -0700483 rt0 = fn->rr_ptr;
484 if (!rt0)
485 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David S. Millerf11e6652007-03-24 20:36:25 -0700487 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800489 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700490 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700491 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700492
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800493 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700494 if (!next || next->rt6i_metric != rt0->rt6i_metric)
495 next = fn->leaf;
496
497 if (next != rt0)
498 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
David S. Millerf11e6652007-03-24 20:36:25 -0700501 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800502 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900504 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000505 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800508#ifdef CONFIG_IPV6_ROUTE_INFO
509int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
510 struct in6_addr *gwaddr)
511{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900512 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800513 struct route_info *rinfo = (struct route_info *) opt;
514 struct in6_addr prefix_buf, *prefix;
515 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900516 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800517 struct rt6_info *rt;
518
519 if (len < sizeof(struct route_info)) {
520 return -EINVAL;
521 }
522
523 /* Sanity check for prefix_len and length */
524 if (rinfo->length > 3) {
525 return -EINVAL;
526 } else if (rinfo->prefix_len > 128) {
527 return -EINVAL;
528 } else if (rinfo->prefix_len > 64) {
529 if (rinfo->length < 2) {
530 return -EINVAL;
531 }
532 } else if (rinfo->prefix_len > 0) {
533 if (rinfo->length < 1) {
534 return -EINVAL;
535 }
536 }
537
538 pref = rinfo->route_pref;
539 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000540 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800541
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900542 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800543
544 if (rinfo->length == 3)
545 prefix = (struct in6_addr *)rinfo->prefix;
546 else {
547 /* this function is safe */
548 ipv6_addr_prefix(&prefix_buf,
549 (struct in6_addr *)rinfo->prefix,
550 rinfo->prefix_len);
551 prefix = &prefix_buf;
552 }
553
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800554 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
555 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800556
557 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700558 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800559 rt = NULL;
560 }
561
562 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800563 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800564 pref);
565 else if (rt)
566 rt->rt6i_flags = RTF_ROUTEINFO |
567 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
568
569 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900570 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800571 rt->rt6i_flags &= ~RTF_EXPIRES;
572 } else {
573 rt->rt6i_expires = jiffies + HZ * lifetime;
574 rt->rt6i_flags |= RTF_EXPIRES;
575 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700576 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800577 }
578 return 0;
579}
580#endif
581
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800582#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700583do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800584 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700585 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700586 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700587 if (fn->fn_flags & RTN_TL_ROOT) \
588 goto out; \
589 pn = fn->parent; \
590 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800591 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700592 else \
593 fn = pn; \
594 if (fn->fn_flags & RTN_RTINFO) \
595 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700596 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700597 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700598} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700599
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800600static struct rt6_info *ip6_pol_route_lookup(struct net *net,
601 struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700602 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct fib6_node *fn;
605 struct rt6_info *rt;
606
Thomas Grafc71099a2006-08-04 23:20:06 -0700607 read_lock_bh(&table->tb6_lock);
608 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
609restart:
610 rt = fn->leaf;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900611 rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800612 BACKTRACK(net, &fl->fl6_src);
Thomas Grafc71099a2006-08-04 23:20:06 -0700613out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700614 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700615 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700616 return rt;
617
618}
619
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900620struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
621 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700622{
623 struct flowi fl = {
624 .oif = oif,
Changli Gao58116622010-11-12 18:43:55 +0000625 .fl6_dst = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700626 };
627 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700628 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700629
Thomas Grafadaa70b2006-10-13 15:01:03 -0700630 if (saddr) {
631 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
632 flags |= RT6_LOOKUP_F_HAS_SADDR;
633 }
634
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800635 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700636 if (dst->error == 0)
637 return (struct rt6_info *) dst;
638
639 dst_release(dst);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return NULL;
642}
643
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900644EXPORT_SYMBOL(rt6_lookup);
645
Thomas Grafc71099a2006-08-04 23:20:06 -0700646/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 It takes new route entry, the addition fails by any reason the
648 route is freed. In any case, if caller does not hold it, it may
649 be destroyed.
650 */
651
Thomas Graf86872cb2006-08-22 00:01:08 -0700652static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700655 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Thomas Grafc71099a2006-08-04 23:20:06 -0700657 table = rt->rt6i_table;
658 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700659 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700660 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return err;
663}
664
Thomas Graf40e22e82006-08-22 00:00:45 -0700665int ip6_ins_rt(struct rt6_info *rt)
666{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800667 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900668 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800669 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800670 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700671}
672
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800673static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
674 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct rt6_info *rt;
677
678 /*
679 * Clone the route.
680 */
681
682 rt = ip6_rt_copy(ort);
683
684 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800685 struct neighbour *neigh;
686 int attempts = !in_softirq();
687
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900688 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
689 if (rt->rt6i_dst.plen != 128 &&
690 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
691 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900695 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rt->rt6i_dst.plen = 128;
697 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700698 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700#ifdef CONFIG_IPV6_SUBTREES
701 if (rt->rt6i_src.plen && saddr) {
702 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
703 rt->rt6i_src.plen = 128;
704 }
705#endif
706
David S. Miller14deae42009-01-04 16:04:39 -0800707 retry:
708 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
709 if (IS_ERR(neigh)) {
710 struct net *net = dev_net(rt->rt6i_dev);
711 int saved_rt_min_interval =
712 net->ipv6.sysctl.ip6_rt_gc_min_interval;
713 int saved_rt_elasticity =
714 net->ipv6.sysctl.ip6_rt_gc_elasticity;
715
716 if (attempts-- > 0) {
717 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
718 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
719
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000720 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800721
722 net->ipv6.sysctl.ip6_rt_gc_elasticity =
723 saved_rt_elasticity;
724 net->ipv6.sysctl.ip6_rt_gc_min_interval =
725 saved_rt_min_interval;
726 goto retry;
727 }
728
729 if (net_ratelimit())
730 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700731 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700732 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800733 return NULL;
734 }
735 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800739 return rt;
740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800742static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
743{
744 struct rt6_info *rt = ip6_rt_copy(ort);
745 if (rt) {
746 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
747 rt->rt6i_dst.plen = 128;
748 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700749 rt->dst.flags |= DST_HOST;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800750 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
751 }
752 return rt;
753}
754
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800755static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
756 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800759 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700760 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800762 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700763 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700765 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700768 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800770restart_2:
Thomas Grafc71099a2006-08-04 23:20:06 -0700771 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700774 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800775
776 BACKTRACK(net, &fl->fl6_src);
777 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800778 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Changli Gaod8d1f302010-06-10 23:31:35 -0700781 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700782 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800783
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800784 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800785 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
David S. Millerd80bc0f2011-01-24 16:01:58 -0800786 else
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800787 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800788
Changli Gaod8d1f302010-06-10 23:31:35 -0700789 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800790 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800791
Changli Gaod8d1f302010-06-10 23:31:35 -0700792 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800793 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700794 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800795 if (!err)
796 goto out2;
797 }
798
799 if (--attempts <= 0)
800 goto out2;
801
802 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700803 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800804 * released someone could insert this route. Relookup.
805 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700806 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800807 goto relookup;
808
809out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800810 if (reachable) {
811 reachable = 0;
812 goto restart_2;
813 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700814 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700815 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700817 rt->dst.lastuse = jiffies;
818 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700819
820 return rt;
821}
822
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800823static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700824 struct flowi *fl, int flags)
825{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800826 return ip6_pol_route(net, table, fl->iif, fl, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700827}
828
Thomas Grafc71099a2006-08-04 23:20:06 -0700829void ip6_route_input(struct sk_buff *skb)
830{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700831 struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900832 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700833 int flags = RT6_LOOKUP_F_HAS_SADDR;
Thomas Grafc71099a2006-08-04 23:20:06 -0700834 struct flowi fl = {
835 .iif = skb->dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000836 .fl6_dst = iph->daddr,
837 .fl6_src = iph->saddr,
838 .fl6_flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900839 .mark = skb->mark,
Thomas Grafc71099a2006-08-04 23:20:06 -0700840 .proto = iph->nexthdr,
841 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700842
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800843 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700844 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700845
Eric Dumazetadf30902009-06-02 05:19:30 +0000846 skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700847}
848
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800849static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700850 struct flowi *fl, int flags)
851{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800852 return ip6_pol_route(net, table, fl->oif, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700853}
854
Daniel Lezcano4591db42008-03-05 10:48:10 -0800855struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
856 struct flowi *fl)
Thomas Grafc71099a2006-08-04 23:20:06 -0700857{
858 int flags = 0;
859
Brian Haley6057fd72010-05-28 23:02:35 -0700860 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700861 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700862
Thomas Grafadaa70b2006-10-13 15:01:03 -0700863 if (!ipv6_addr_any(&fl->fl6_src))
864 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000865 else if (sk)
866 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700867
Daniel Lezcano4591db42008-03-05 10:48:10 -0800868 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900871EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David S. Miller2774c132011-03-01 14:59:04 -0800873struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700874{
David S. Miller69ead7a2011-03-01 14:45:33 -0800875 struct rt6_info *rt = dst_alloc(&ip6_dst_blackhole_ops, 1);
876 struct rt6_info *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700877 struct dst_entry *new = NULL;
878
879 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700880 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700881
David S. Miller14e50e52007-05-24 18:17:54 -0700882 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800883 new->input = dst_discard;
884 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700885
David S. Millerdefb3512010-12-08 21:16:57 -0800886 dst_copy_metrics(new, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -0700887 new->dev = ort->dst.dev;
David S. Miller14e50e52007-05-24 18:17:54 -0700888 if (new->dev)
889 dev_hold(new->dev);
890 rt->rt6i_idev = ort->rt6i_idev;
891 if (rt->rt6i_idev)
892 in6_dev_hold(rt->rt6i_idev);
893 rt->rt6i_expires = 0;
894
895 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
896 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
897 rt->rt6i_metric = 0;
898
899 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
900#ifdef CONFIG_IPV6_SUBTREES
901 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
902#endif
903
904 dst_free(new);
905 }
906
David S. Miller69ead7a2011-03-01 14:45:33 -0800907 dst_release(dst_orig);
908 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700909}
David S. Miller14e50e52007-05-24 18:17:54 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*
912 * Destination cache support functions
913 */
914
915static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
916{
917 struct rt6_info *rt;
918
919 rt = (struct rt6_info *) dst;
920
David S. Miller6431cbc2011-02-07 20:38:06 -0800921 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
922 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
923 if (!rt->rt6i_peer)
924 rt6_bind_peer(rt, 0);
925 rt->rt6i_peer_genid = rt6_peer_genid();
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return NULL;
930}
931
932static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
933{
934 struct rt6_info *rt = (struct rt6_info *) dst;
935
936 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000937 if (rt->rt6i_flags & RTF_CACHE) {
938 if (rt6_check_expired(rt)) {
939 ip6_del_rt(rt);
940 dst = NULL;
941 }
942 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000944 dst = NULL;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000947 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static void ip6_link_failure(struct sk_buff *skb)
951{
952 struct rt6_info *rt;
953
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000954 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Eric Dumazetadf30902009-06-02 05:19:30 +0000956 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (rt) {
958 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700959 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 rt->rt6i_flags |= RTF_EXPIRES;
961 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
962 rt->rt6i_node->fn_sernum = -1;
963 }
964}
965
966static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
967{
968 struct rt6_info *rt6 = (struct rt6_info*)dst;
969
970 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
971 rt6->rt6i_flags |= RTF_MODIFIED;
972 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800973 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800975 features |= RTAX_FEATURE_ALLFRAG;
976 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
David S. Millerdefb3512010-12-08 21:16:57 -0800978 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
David S. Miller0dbaee32010-12-13 12:52:14 -0800982static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
David S. Miller0dbaee32010-12-13 12:52:14 -0800984 struct net_device *dev = dst->dev;
985 unsigned int mtu = dst_mtu(dst);
986 struct net *net = dev_net(dev);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
989
Daniel Lezcano55786892008-03-04 13:47:47 -0800990 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
991 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900994 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
995 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
996 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * rely only on pmtu discovery"
998 */
999 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1000 mtu = IPV6_MAXPLEN;
1001 return mtu;
1002}
1003
David S. Millerd33e4552010-12-14 13:01:14 -08001004static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1005{
1006 unsigned int mtu = IPV6_MIN_MTU;
1007 struct inet6_dev *idev;
1008
1009 rcu_read_lock();
1010 idev = __in6_dev_get(dst->dev);
1011 if (idev)
1012 mtu = idev->cnf.mtu6;
1013 rcu_read_unlock();
1014
1015 return mtu;
1016}
1017
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001018static struct dst_entry *icmp6_dst_gc_list;
1019static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001020
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001021struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001023 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct rt6_info *rt;
1026 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001027 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (unlikely(idev == NULL))
1030 return NULL;
1031
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001032 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (unlikely(rt == NULL)) {
1034 in6_dev_put(idev);
1035 goto out;
1036 }
1037
1038 dev_hold(dev);
1039 if (neigh)
1040 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001041 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001043 if (IS_ERR(neigh))
1044 neigh = NULL;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 rt->rt6i_dev = dev;
1048 rt->rt6i_idev = idev;
1049 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001050 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001051 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001052 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001055 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001056 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 : 0;
1058 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1059 rt->rt6i_dst.plen = 128;
1060#endif
1061
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001062 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001063 rt->dst.next = icmp6_dst_gc_list;
1064 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001065 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Daniel Lezcano55786892008-03-04 13:47:47 -08001067 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001070 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001073int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001075 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001076 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001078 spin_lock_bh(&icmp6_dst_lock);
1079 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 while ((dst = *pprev) != NULL) {
1082 if (!atomic_read(&dst->__refcnt)) {
1083 *pprev = dst->next;
1084 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 } else {
1086 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001087 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
1090
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001091 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001092
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001093 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
David S. Miller1e493d12008-09-10 17:27:15 -07001096static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1097 void *arg)
1098{
1099 struct dst_entry *dst, **pprev;
1100
1101 spin_lock_bh(&icmp6_dst_lock);
1102 pprev = &icmp6_dst_gc_list;
1103 while ((dst = *pprev) != NULL) {
1104 struct rt6_info *rt = (struct rt6_info *) dst;
1105 if (func(rt, arg)) {
1106 *pprev = dst->next;
1107 dst_free(dst);
1108 } else {
1109 pprev = &dst->next;
1110 }
1111 }
1112 spin_unlock_bh(&icmp6_dst_lock);
1113}
1114
Daniel Lezcano569d3642008-01-18 03:56:57 -08001115static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001118 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001119 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1120 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1121 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1122 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1123 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001124 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Eric Dumazetfc66f952010-10-08 06:37:34 +00001126 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001127 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001128 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 goto out;
1130
Benjamin Thery6891a342008-03-04 13:49:47 -08001131 net->ipv6.ip6_rt_gc_expire++;
1132 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1133 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001134 entries = dst_entries_get_slow(ops);
1135 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001136 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001138 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001139 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142/* Clean host part of a prefix. Not necessary in radix tree,
1143 but results in cleaner routing tables.
1144
1145 Remove it only when all the things will work!
1146 */
1147
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001148int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
David S. Miller5170ae82010-12-12 21:35:57 -08001150 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001151 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001152 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001153 struct inet6_dev *idev;
1154
1155 rcu_read_lock();
1156 idev = __in6_dev_get(dev);
1157 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001158 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001159 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001160 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001161 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 return hoplimit;
1164}
David S. Millerabbf46a2010-12-12 21:14:46 -08001165EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167/*
1168 *
1169 */
1170
Thomas Graf86872cb2006-08-22 00:01:08 -07001171int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001174 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct rt6_info *rt = NULL;
1176 struct net_device *dev = NULL;
1177 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001178 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int addr_type;
1180
Thomas Graf86872cb2006-08-22 00:01:08 -07001181 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -EINVAL;
1183#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001184 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EINVAL;
1186#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001187 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001189 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!dev)
1191 goto out;
1192 idev = in6_dev_get(dev);
1193 if (!idev)
1194 goto out;
1195 }
1196
Thomas Graf86872cb2006-08-22 00:01:08 -07001197 if (cfg->fc_metric == 0)
1198 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Daniel Lezcano55786892008-03-04 13:47:47 -08001200 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001201 if (table == NULL) {
1202 err = -ENOBUFS;
1203 goto out;
1204 }
1205
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001206 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (rt == NULL) {
1209 err = -ENOMEM;
1210 goto out;
1211 }
1212
Changli Gaod8d1f302010-06-10 23:31:35 -07001213 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001214 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1215 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1216 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Thomas Graf86872cb2006-08-22 00:01:08 -07001218 if (cfg->fc_protocol == RTPROT_UNSPEC)
1219 cfg->fc_protocol = RTPROT_BOOT;
1220 rt->rt6i_protocol = cfg->fc_protocol;
1221
1222 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001225 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001226 else if (cfg->fc_flags & RTF_LOCAL)
1227 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001229 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Changli Gaod8d1f302010-06-10 23:31:35 -07001231 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Thomas Graf86872cb2006-08-22 00:01:08 -07001233 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1234 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001236 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001239 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1240 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241#endif
1242
Thomas Graf86872cb2006-08-22 00:01:08 -07001243 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* We cannot add true routes via loopback here,
1246 they would result in kernel looping; promote them to reject routes
1247 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001248 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001249 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1250 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001252 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (dev) {
1254 dev_put(dev);
1255 in6_dev_put(idev);
1256 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001257 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 dev_hold(dev);
1259 idev = in6_dev_get(dev);
1260 if (!idev) {
1261 err = -ENODEV;
1262 goto out;
1263 }
1264 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001265 rt->dst.output = ip6_pkt_discard_out;
1266 rt->dst.input = ip6_pkt_discard;
1267 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1269 goto install_route;
1270 }
1271
Thomas Graf86872cb2006-08-22 00:01:08 -07001272 if (cfg->fc_flags & RTF_GATEWAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct in6_addr *gw_addr;
1274 int gwa_type;
1275
Thomas Graf86872cb2006-08-22 00:01:08 -07001276 gw_addr = &cfg->fc_gateway;
1277 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 gwa_type = ipv6_addr_type(gw_addr);
1279
1280 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1281 struct rt6_info *grt;
1282
1283 /* IPv6 strictly inhibits using not link-local
1284 addresses as nexthop address.
1285 Otherwise, router will not able to send redirects.
1286 It is very good, but in some (rare!) circumstances
1287 (SIT, PtP, NBMA NOARP links) it is handy to allow
1288 some exceptions. --ANK
1289 */
1290 err = -EINVAL;
1291 if (!(gwa_type&IPV6_ADDR_UNICAST))
1292 goto out;
1293
Daniel Lezcano55786892008-03-04 13:47:47 -08001294 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 err = -EHOSTUNREACH;
1297 if (grt == NULL)
1298 goto out;
1299 if (dev) {
1300 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001301 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 goto out;
1303 }
1304 } else {
1305 dev = grt->rt6i_dev;
1306 idev = grt->rt6i_idev;
1307 dev_hold(dev);
1308 in6_dev_hold(grt->rt6i_idev);
1309 }
1310 if (!(grt->rt6i_flags&RTF_GATEWAY))
1311 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001312 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 if (err)
1315 goto out;
1316 }
1317 err = -EINVAL;
1318 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1319 goto out;
1320 }
1321
1322 err = -ENODEV;
1323 if (dev == NULL)
1324 goto out;
1325
Thomas Graf86872cb2006-08-22 00:01:08 -07001326 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1328 if (IS_ERR(rt->rt6i_nexthop)) {
1329 err = PTR_ERR(rt->rt6i_nexthop);
1330 rt->rt6i_nexthop = NULL;
1331 goto out;
1332 }
1333 }
1334
Thomas Graf86872cb2006-08-22 00:01:08 -07001335 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001338 if (cfg->fc_mx) {
1339 struct nlattr *nla;
1340 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Thomas Graf86872cb2006-08-22 00:01:08 -07001342 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001343 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001344
1345 if (type) {
1346 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 err = -EINVAL;
1348 goto out;
1349 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001350
David S. Millerdefb3512010-12-08 21:16:57 -08001351 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 }
1355
Changli Gaod8d1f302010-06-10 23:31:35 -07001356 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001358 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001359
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001360 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001361
Thomas Graf86872cb2006-08-22 00:01:08 -07001362 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364out:
1365 if (dev)
1366 dev_put(dev);
1367 if (idev)
1368 in6_dev_put(idev);
1369 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001370 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return err;
1372}
1373
Thomas Graf86872cb2006-08-22 00:01:08 -07001374static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001377 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001378 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001380 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001381 return -ENOENT;
1382
Thomas Grafc71099a2006-08-04 23:20:06 -07001383 table = rt->rt6i_table;
1384 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Thomas Graf86872cb2006-08-22 00:01:08 -07001386 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001387 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Thomas Grafc71099a2006-08-04 23:20:06 -07001389 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 return err;
1392}
1393
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001394int ip6_del_rt(struct rt6_info *rt)
1395{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001396 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001397 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001398 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001399 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001400}
1401
Thomas Graf86872cb2006-08-22 00:01:08 -07001402static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Thomas Grafc71099a2006-08-04 23:20:06 -07001404 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct fib6_node *fn;
1406 struct rt6_info *rt;
1407 int err = -ESRCH;
1408
Daniel Lezcano55786892008-03-04 13:47:47 -08001409 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001410 if (table == NULL)
1411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Thomas Grafc71099a2006-08-04 23:20:06 -07001413 read_lock_bh(&table->tb6_lock);
1414
1415 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001416 &cfg->fc_dst, cfg->fc_dst_len,
1417 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001420 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001421 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001423 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001425 if (cfg->fc_flags & RTF_GATEWAY &&
1426 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001428 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001430 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001431 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Thomas Graf86872cb2006-08-22 00:01:08 -07001433 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001436 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 return err;
1439}
1440
1441/*
1442 * Handle redirects
1443 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001444struct ip6rd_flowi {
1445 struct flowi fl;
1446 struct in6_addr gateway;
1447};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001449static struct rt6_info *__ip6_route_redirect(struct net *net,
1450 struct fib6_table *table,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001451 struct flowi *fl,
1452 int flags)
1453{
1454 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1455 struct rt6_info *rt;
1456 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001459 * Get the "current" route for this destination and
1460 * check if the redirect has come from approriate router.
1461 *
1462 * RFC 2461 specifies that redirects should only be
1463 * accepted if they come from the nexthop to the target.
1464 * Due to the way the routes are chosen, this notion
1465 * is a bit fuzzy and one might need to check all possible
1466 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Thomas Grafc71099a2006-08-04 23:20:06 -07001469 read_lock_bh(&table->tb6_lock);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001470 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001471restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001472 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001473 /*
1474 * Current route is on-link; redirect is always invalid.
1475 *
1476 * Seems, previous statement is not true. It could
1477 * be node, which looks for us as on-link (f.e. proxy ndisc)
1478 * But then router serving it might decide, that we should
1479 * know truth 8)8) --ANK (980726).
1480 */
1481 if (rt6_check_expired(rt))
1482 continue;
1483 if (!(rt->rt6i_flags & RTF_GATEWAY))
1484 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001485 if (fl->oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001486 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001487 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001488 continue;
1489 break;
1490 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001491
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001492 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001493 rt = net->ipv6.ip6_null_entry;
1494 BACKTRACK(net, &fl->fl6_src);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001495out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001496 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001497
1498 read_unlock_bh(&table->tb6_lock);
1499
1500 return rt;
1501};
1502
1503static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1504 struct in6_addr *src,
1505 struct in6_addr *gateway,
1506 struct net_device *dev)
1507{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001508 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001509 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001510 struct ip6rd_flowi rdfl = {
1511 .fl = {
1512 .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +00001513 .fl6_dst = *dest,
1514 .fl6_src = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001515 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001516 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001517
Brian Haley86c36ce2009-10-07 13:58:01 -07001518 ipv6_addr_copy(&rdfl.gateway, gateway);
1519
Thomas Grafadaa70b2006-10-13 15:01:03 -07001520 if (rt6_need_strict(dest))
1521 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001522
Daniel Lezcano55786892008-03-04 13:47:47 -08001523 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001524 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001525}
1526
1527void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1528 struct in6_addr *saddr,
1529 struct neighbour *neigh, u8 *lladdr, int on_link)
1530{
1531 struct rt6_info *rt, *nrt = NULL;
1532 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001533 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001534
1535 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1536
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001537 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (net_ratelimit())
1539 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1540 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /*
1545 * We have finally decided to accept it.
1546 */
1547
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001548 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1550 NEIGH_UPDATE_F_OVERRIDE|
1551 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1552 NEIGH_UPDATE_F_ISROUTER))
1553 );
1554
1555 /*
1556 * Redirect received -> path was valid.
1557 * Look, redirects are sent only in response to data packets,
1558 * so that this nexthop apparently is reachable. --ANK
1559 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001560 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001563 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto out;
1565
1566 nrt = ip6_rt_copy(rt);
1567 if (nrt == NULL)
1568 goto out;
1569
1570 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1571 if (on_link)
1572 nrt->rt6i_flags &= ~RTF_GATEWAY;
1573
1574 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1575 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001576 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1579 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Thomas Graf40e22e82006-08-22 00:00:45 -07001581 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 goto out;
1583
Changli Gaod8d1f302010-06-10 23:31:35 -07001584 netevent.old = &rt->dst;
1585 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001586 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001589 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return;
1591 }
1592
1593out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001594 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597/*
1598 * Handle ICMP "packet too big" messages
1599 * i.e. Path MTU discovery
1600 */
1601
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001602static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
1603 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct rt6_info *rt, *nrt;
1606 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001607again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001608 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (rt == NULL)
1610 return;
1611
Andrey Vagind3052b52010-12-11 15:20:11 +00001612 if (rt6_check_expired(rt)) {
1613 ip6_del_rt(rt);
1614 goto again;
1615 }
1616
Changli Gaod8d1f302010-06-10 23:31:35 -07001617 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto out;
1619
1620 if (pmtu < IPV6_MIN_MTU) {
1621 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001622 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * MTU (1280) and a fragment header should always be included
1624 * after a node receiving Too Big message reporting PMTU is
1625 * less than the IPv6 Minimum Link MTU.
1626 */
1627 pmtu = IPV6_MIN_MTU;
1628 allfrag = 1;
1629 }
1630
1631 /* New mtu received -> path was valid.
1632 They are sent only in response to data packets,
1633 so that this nexthop apparently is reachable. --ANK
1634 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001635 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* Host route. If it is static, it would be better
1638 not to override it, but add new one, so that
1639 when cache entry will expire old pmtu
1640 would return automatically.
1641 */
1642 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001643 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1644 if (allfrag) {
1645 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1646 features |= RTAX_FEATURE_ALLFRAG;
1647 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1648 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001649 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1651 goto out;
1652 }
1653
1654 /* Network route.
1655 Two cases are possible:
1656 1. It is connected route. Action: COW
1657 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1658 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001659 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001660 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001661 else
1662 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001663
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001664 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001665 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1666 if (allfrag) {
1667 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1668 features |= RTAX_FEATURE_ALLFRAG;
1669 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1670 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001671
1672 /* According to RFC 1981, detecting PMTU increase shouldn't be
1673 * happened within 5 mins, the recommended timer is 10 mins.
1674 * Here this route expiration time is set to ip6_rt_mtu_expires
1675 * which is 10 mins. After 10 mins the decreased pmtu is expired
1676 * and detecting PMTU increase will be automatically happened.
1677 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001678 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001679 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1680
Thomas Graf40e22e82006-08-22 00:00:45 -07001681 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001684 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001687void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1688 struct net_device *dev, u32 pmtu)
1689{
1690 struct net *net = dev_net(dev);
1691
1692 /*
1693 * RFC 1981 states that a node "MUST reduce the size of the packets it
1694 * is sending along the path" that caused the Packet Too Big message.
1695 * Since it's not possible in the general case to determine which
1696 * interface was used to send the original packet, we update the MTU
1697 * on the interface that will be used to send future packets. We also
1698 * update the MTU on the interface that received the Packet Too Big in
1699 * case the original packet was forced out that interface with
1700 * SO_BINDTODEVICE or similar. This is the next best thing to the
1701 * correct behaviour, which would be to update the MTU on all
1702 * interfaces.
1703 */
1704 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1705 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
1709 * Misc support functions
1710 */
1711
1712static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1713{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001714 struct net *net = dev_net(ort->rt6i_dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001715 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001718 rt->dst.input = ort->dst.input;
1719 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
David S. Millerdefb3512010-12-08 21:16:57 -08001721 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001722 rt->dst.error = ort->dst.error;
1723 rt->dst.dev = ort->dst.dev;
1724 if (rt->dst.dev)
1725 dev_hold(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 rt->rt6i_idev = ort->rt6i_idev;
1727 if (rt->rt6i_idev)
1728 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001729 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 rt->rt6i_expires = 0;
1731
1732 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1733 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1734 rt->rt6i_metric = 0;
1735
1736 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1737#ifdef CONFIG_IPV6_SUBTREES
1738 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1739#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001740 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 return rt;
1743}
1744
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001745#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001746static struct rt6_info *rt6_get_route_info(struct net *net,
1747 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001748 struct in6_addr *gwaddr, int ifindex)
1749{
1750 struct fib6_node *fn;
1751 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001752 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001753
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001754 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001755 if (table == NULL)
1756 return NULL;
1757
1758 write_lock_bh(&table->tb6_lock);
1759 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001760 if (!fn)
1761 goto out;
1762
Changli Gaod8d1f302010-06-10 23:31:35 -07001763 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001764 if (rt->rt6i_dev->ifindex != ifindex)
1765 continue;
1766 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1767 continue;
1768 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1769 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001770 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001771 break;
1772 }
1773out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001774 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001775 return rt;
1776}
1777
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001778static struct rt6_info *rt6_add_route_info(struct net *net,
1779 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001780 struct in6_addr *gwaddr, int ifindex,
1781 unsigned pref)
1782{
Thomas Graf86872cb2006-08-22 00:01:08 -07001783 struct fib6_config cfg = {
1784 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001785 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001786 .fc_ifindex = ifindex,
1787 .fc_dst_len = prefixlen,
1788 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1789 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001790 .fc_nlinfo.pid = 0,
1791 .fc_nlinfo.nlh = NULL,
1792 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001793 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001794
Thomas Graf86872cb2006-08-22 00:01:08 -07001795 ipv6_addr_copy(&cfg.fc_dst, prefix);
1796 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1797
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001798 /* We should treat it as a default route if prefix length is 0. */
1799 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001800 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001801
Thomas Graf86872cb2006-08-22 00:01:08 -07001802 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001803
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001804 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001805}
1806#endif
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001809{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001811 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001813 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001814 if (table == NULL)
1815 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Thomas Grafc71099a2006-08-04 23:20:06 -07001817 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001818 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001820 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1822 break;
1823 }
1824 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001825 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001826 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return rt;
1828}
1829
1830struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001831 struct net_device *dev,
1832 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Thomas Graf86872cb2006-08-22 00:01:08 -07001834 struct fib6_config cfg = {
1835 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001836 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001837 .fc_ifindex = dev->ifindex,
1838 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1839 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001840 .fc_nlinfo.pid = 0,
1841 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001842 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001843 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Thomas Graf86872cb2006-08-22 00:01:08 -07001845 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Thomas Graf86872cb2006-08-22 00:01:08 -07001847 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return rt6_get_dflt_router(gwaddr, dev);
1850}
1851
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001852void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001855 struct fib6_table *table;
1856
1857 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001858 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001859 if (table == NULL)
1860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001863 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001864 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001866 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001867 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001868 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto restart;
1870 }
1871 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001872 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Daniel Lezcano55786892008-03-04 13:47:47 -08001875static void rtmsg_to_fib6_config(struct net *net,
1876 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001877 struct fib6_config *cfg)
1878{
1879 memset(cfg, 0, sizeof(*cfg));
1880
1881 cfg->fc_table = RT6_TABLE_MAIN;
1882 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1883 cfg->fc_metric = rtmsg->rtmsg_metric;
1884 cfg->fc_expires = rtmsg->rtmsg_info;
1885 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1886 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1887 cfg->fc_flags = rtmsg->rtmsg_flags;
1888
Daniel Lezcano55786892008-03-04 13:47:47 -08001889 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001890
Thomas Graf86872cb2006-08-22 00:01:08 -07001891 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1892 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1893 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1894}
1895
Daniel Lezcano55786892008-03-04 13:47:47 -08001896int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Thomas Graf86872cb2006-08-22 00:01:08 -07001898 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct in6_rtmsg rtmsg;
1900 int err;
1901
1902 switch(cmd) {
1903 case SIOCADDRT: /* Add a route */
1904 case SIOCDELRT: /* Delete a route */
1905 if (!capable(CAP_NET_ADMIN))
1906 return -EPERM;
1907 err = copy_from_user(&rtmsg, arg,
1908 sizeof(struct in6_rtmsg));
1909 if (err)
1910 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001911
Daniel Lezcano55786892008-03-04 13:47:47 -08001912 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 rtnl_lock();
1915 switch (cmd) {
1916 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001917 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
1919 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001920 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 break;
1922 default:
1923 err = -EINVAL;
1924 }
1925 rtnl_unlock();
1926
1927 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 return -EINVAL;
1931}
1932
1933/*
1934 * Drop the packet on the floor
1935 */
1936
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001937static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001939 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001940 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001941 switch (ipstats_mib_noroutes) {
1942 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001943 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001944 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001945 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1946 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001947 break;
1948 }
1949 /* FALLTHROUGH */
1950 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001951 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1952 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001953 break;
1954 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001955 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 kfree_skb(skb);
1957 return 0;
1958}
1959
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001960static int ip6_pkt_discard(struct sk_buff *skb)
1961{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001962 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001963}
1964
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001965static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Eric Dumazetadf30902009-06-02 05:19:30 +00001967 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001968 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
David S. Miller6723ab52006-10-18 21:20:57 -07001971#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1972
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001973static int ip6_pkt_prohibit(struct sk_buff *skb)
1974{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001975 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001976}
1977
1978static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1979{
Eric Dumazetadf30902009-06-02 05:19:30 +00001980 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001981 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001982}
1983
David S. Miller6723ab52006-10-18 21:20:57 -07001984#endif
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986/*
1987 * Allocate a dst for local (unicast / anycast) address.
1988 */
1989
1990struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1991 const struct in6_addr *addr,
1992 int anycast)
1993{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001994 struct net *net = dev_net(idev->dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001995 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -08001996 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Ben Greear40385652010-11-08 12:33:48 +00001998 if (rt == NULL) {
1999 if (net_ratelimit())
2000 pr_warning("IPv6: Maximum number of routes reached,"
2001 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Daniel Lezcano55786892008-03-04 13:47:47 -08002005 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 in6_dev_hold(idev);
2007
Changli Gaod8d1f302010-06-10 23:31:35 -07002008 rt->dst.flags = DST_HOST;
2009 rt->dst.input = ip6_input;
2010 rt->dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08002011 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 rt->rt6i_idev = idev;
David S. Millerdefb3512010-12-08 21:16:57 -08002013 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
Changli Gaod8d1f302010-06-10 23:31:35 -07002014 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002017 if (anycast)
2018 rt->rt6i_flags |= RTF_ANYCAST;
2019 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002021 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2022 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002023 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002024
2025 /* We are casting this because that is the return
2026 * value type. But an errno encoded pointer is the
2027 * same regardless of the underlying pointer type,
2028 * and that's what we are returning. So this is OK.
2029 */
2030 return (struct rt6_info *) neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
David S. Miller14deae42009-01-04 16:04:39 -08002032 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2035 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002036 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Changli Gaod8d1f302010-06-10 23:31:35 -07002038 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 return rt;
2041}
2042
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002043struct arg_dev_net {
2044 struct net_device *dev;
2045 struct net *net;
2046};
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048static int fib6_ifdown(struct rt6_info *rt, void *arg)
2049{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002050 const struct arg_dev_net *adn = arg;
2051 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002052
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002053 if ((rt->rt6i_dev == dev || dev == NULL) &&
2054 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 RT6_TRACE("deleted by ifdown %p\n", rt);
2056 return -1;
2057 }
2058 return 0;
2059}
2060
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002061void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002063 struct arg_dev_net adn = {
2064 .dev = dev,
2065 .net = net,
2066 };
2067
2068 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002069 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072struct rt6_mtu_change_arg
2073{
2074 struct net_device *dev;
2075 unsigned mtu;
2076};
2077
2078static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2079{
2080 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2081 struct inet6_dev *idev;
2082
2083 /* In IPv6 pmtu discovery is not optional,
2084 so that RTAX_MTU lock cannot disable it.
2085 We still use this lock to block changes
2086 caused by addrconf/ndisc.
2087 */
2088
2089 idev = __in6_dev_get(arg->dev);
2090 if (idev == NULL)
2091 return 0;
2092
2093 /* For administrative MTU increase, there is no way to discover
2094 IPv6 PMTU increase, so PMTU increase should be updated here.
2095 Since RFC 1981 doesn't include administrative MTU increase
2096 update PMTU increase is a MUST. (i.e. jumbo frame)
2097 */
2098 /*
2099 If new MTU is less than route PMTU, this new MTU will be the
2100 lowest MTU in the path, update the route PMTU to reflect PMTU
2101 decreases; if new MTU is greater than route PMTU, and the
2102 old MTU is the lowest MTU in the path, update the route PMTU
2103 to reflect the increase. In this case if the other nodes' MTU
2104 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2105 PMTU discouvery.
2106 */
2107 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002108 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2109 (dst_mtu(&rt->dst) >= arg->mtu ||
2110 (dst_mtu(&rt->dst) < arg->mtu &&
2111 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002112 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
2117void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2118{
Thomas Grafc71099a2006-08-04 23:20:06 -07002119 struct rt6_mtu_change_arg arg = {
2120 .dev = dev,
2121 .mtu = mtu,
2122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002124 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002127static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002128 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002129 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002130 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002131 [RTA_PRIORITY] = { .type = NLA_U32 },
2132 [RTA_METRICS] = { .type = NLA_NESTED },
2133};
2134
2135static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2136 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Thomas Graf86872cb2006-08-22 00:01:08 -07002138 struct rtmsg *rtm;
2139 struct nlattr *tb[RTA_MAX+1];
2140 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Thomas Graf86872cb2006-08-22 00:01:08 -07002142 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2143 if (err < 0)
2144 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Thomas Graf86872cb2006-08-22 00:01:08 -07002146 err = -EINVAL;
2147 rtm = nlmsg_data(nlh);
2148 memset(cfg, 0, sizeof(*cfg));
2149
2150 cfg->fc_table = rtm->rtm_table;
2151 cfg->fc_dst_len = rtm->rtm_dst_len;
2152 cfg->fc_src_len = rtm->rtm_src_len;
2153 cfg->fc_flags = RTF_UP;
2154 cfg->fc_protocol = rtm->rtm_protocol;
2155
2156 if (rtm->rtm_type == RTN_UNREACHABLE)
2157 cfg->fc_flags |= RTF_REJECT;
2158
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002159 if (rtm->rtm_type == RTN_LOCAL)
2160 cfg->fc_flags |= RTF_LOCAL;
2161
Thomas Graf86872cb2006-08-22 00:01:08 -07002162 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2163 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002164 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002165
2166 if (tb[RTA_GATEWAY]) {
2167 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2168 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002170
2171 if (tb[RTA_DST]) {
2172 int plen = (rtm->rtm_dst_len + 7) >> 3;
2173
2174 if (nla_len(tb[RTA_DST]) < plen)
2175 goto errout;
2176
2177 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002179
2180 if (tb[RTA_SRC]) {
2181 int plen = (rtm->rtm_src_len + 7) >> 3;
2182
2183 if (nla_len(tb[RTA_SRC]) < plen)
2184 goto errout;
2185
2186 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002188
2189 if (tb[RTA_OIF])
2190 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2191
2192 if (tb[RTA_PRIORITY])
2193 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2194
2195 if (tb[RTA_METRICS]) {
2196 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2197 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002199
2200 if (tb[RTA_TABLE])
2201 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2202
2203 err = 0;
2204errout:
2205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Thomas Grafc127ea22007-03-22 11:58:32 -07002208static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Thomas Graf86872cb2006-08-22 00:01:08 -07002210 struct fib6_config cfg;
2211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Thomas Graf86872cb2006-08-22 00:01:08 -07002213 err = rtm_to_fib6_config(skb, nlh, &cfg);
2214 if (err < 0)
2215 return err;
2216
2217 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Thomas Grafc127ea22007-03-22 11:58:32 -07002220static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Thomas Graf86872cb2006-08-22 00:01:08 -07002222 struct fib6_config cfg;
2223 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Thomas Graf86872cb2006-08-22 00:01:08 -07002225 err = rtm_to_fib6_config(skb, nlh, &cfg);
2226 if (err < 0)
2227 return err;
2228
2229 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Thomas Graf339bf982006-11-10 14:10:15 -08002232static inline size_t rt6_nlmsg_size(void)
2233{
2234 return NLMSG_ALIGN(sizeof(struct rtmsg))
2235 + nla_total_size(16) /* RTA_SRC */
2236 + nla_total_size(16) /* RTA_DST */
2237 + nla_total_size(16) /* RTA_GATEWAY */
2238 + nla_total_size(16) /* RTA_PREFSRC */
2239 + nla_total_size(4) /* RTA_TABLE */
2240 + nla_total_size(4) /* RTA_IIF */
2241 + nla_total_size(4) /* RTA_OIF */
2242 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002243 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002244 + nla_total_size(sizeof(struct rta_cacheinfo));
2245}
2246
Brian Haley191cd582008-08-14 15:33:21 -07002247static int rt6_fill_node(struct net *net,
2248 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002249 struct in6_addr *dst, struct in6_addr *src,
2250 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002251 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002254 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002255 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002256 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (prefix) { /* user wants prefix routes only */
2259 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2260 /* success since this is not a prefix route */
2261 return 1;
2262 }
2263 }
2264
Thomas Graf2d7202b2006-08-22 00:01:27 -07002265 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2266 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002267 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002268
2269 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rtm->rtm_family = AF_INET6;
2271 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2272 rtm->rtm_src_len = rt->rt6i_src.plen;
2273 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002274 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002275 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002276 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002277 table = RT6_TABLE_UNSPEC;
2278 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002279 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (rt->rt6i_flags&RTF_REJECT)
2281 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002282 else if (rt->rt6i_flags&RTF_LOCAL)
2283 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2285 rtm->rtm_type = RTN_LOCAL;
2286 else
2287 rtm->rtm_type = RTN_UNICAST;
2288 rtm->rtm_flags = 0;
2289 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2290 rtm->rtm_protocol = rt->rt6i_protocol;
2291 if (rt->rt6i_flags&RTF_DYNAMIC)
2292 rtm->rtm_protocol = RTPROT_REDIRECT;
2293 else if (rt->rt6i_flags & RTF_ADDRCONF)
2294 rtm->rtm_protocol = RTPROT_KERNEL;
2295 else if (rt->rt6i_flags&RTF_DEFAULT)
2296 rtm->rtm_protocol = RTPROT_RA;
2297
2298 if (rt->rt6i_flags&RTF_CACHE)
2299 rtm->rtm_flags |= RTM_F_CLONED;
2300
2301 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002302 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002303 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002305 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#ifdef CONFIG_IPV6_SUBTREES
2307 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002308 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002309 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002311 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002313 if (iif) {
2314#ifdef CONFIG_IPV6_MROUTE
2315 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002316 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002317 if (err <= 0) {
2318 if (!nowait) {
2319 if (err == 0)
2320 return 0;
2321 goto nla_put_failure;
2322 } else {
2323 if (err == -EMSGSIZE)
2324 goto nla_put_failure;
2325 }
2326 }
2327 } else
2328#endif
2329 NLA_PUT_U32(skb, RTA_IIF, iif);
2330 } else if (dst) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002331 struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 struct in6_addr saddr_buf;
Brian Haley191cd582008-08-14 15:33:21 -07002333 if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09002334 dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002335 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002337
David S. Millerdefb3512010-12-08 21:16:57 -08002338 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002339 goto nla_put_failure;
2340
Changli Gaod8d1f302010-06-10 23:31:35 -07002341 if (rt->dst.neighbour)
2342 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002343
Changli Gaod8d1f302010-06-10 23:31:35 -07002344 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002345 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2346
2347 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002348
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002349 if (!(rt->rt6i_flags & RTF_EXPIRES))
2350 expires = 0;
2351 else if (rt->rt6i_expires - jiffies < INT_MAX)
2352 expires = rt->rt6i_expires - jiffies;
2353 else
2354 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002355
Changli Gaod8d1f302010-06-10 23:31:35 -07002356 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2357 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002358 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Thomas Graf2d7202b2006-08-22 00:01:27 -07002360 return nlmsg_end(skb, nlh);
2361
2362nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002363 nlmsg_cancel(skb, nlh);
2364 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Patrick McHardy1b43af52006-08-10 23:11:17 -07002367int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2370 int prefix;
2371
Thomas Graf2d7202b2006-08-22 00:01:27 -07002372 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2373 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2375 } else
2376 prefix = 0;
2377
Brian Haley191cd582008-08-14 15:33:21 -07002378 return rt6_fill_node(arg->net,
2379 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002381 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Thomas Grafc127ea22007-03-22 11:58:32 -07002384static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002386 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002387 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002389 struct sk_buff *skb;
2390 struct rtmsg *rtm;
2391 struct flowi fl;
2392 int err, iif = 0;
2393
2394 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2395 if (err < 0)
2396 goto errout;
2397
2398 err = -EINVAL;
2399 memset(&fl, 0, sizeof(fl));
2400
2401 if (tb[RTA_SRC]) {
2402 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2403 goto errout;
2404
2405 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2406 }
2407
2408 if (tb[RTA_DST]) {
2409 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2410 goto errout;
2411
2412 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2413 }
2414
2415 if (tb[RTA_IIF])
2416 iif = nla_get_u32(tb[RTA_IIF]);
2417
2418 if (tb[RTA_OIF])
2419 fl.oif = nla_get_u32(tb[RTA_OIF]);
2420
2421 if (iif) {
2422 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002423 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002424 if (!dev) {
2425 err = -ENODEV;
2426 goto errout;
2427 }
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002431 if (skb == NULL) {
2432 err = -ENOBUFS;
2433 goto errout;
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* Reserve room for dummy headers, this skb can pass
2437 through good chunk of routing engine.
2438 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002439 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2441
Daniel Lezcano8a3edd82008-03-07 11:14:16 -08002442 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
Changli Gaod8d1f302010-06-10 23:31:35 -07002443 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Brian Haley191cd582008-08-14 15:33:21 -07002445 err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002447 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002449 kfree_skb(skb);
2450 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452
Daniel Lezcano55786892008-03-04 13:47:47 -08002453 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002454errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Thomas Graf86872cb2006-08-22 00:01:08 -07002458void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002461 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002462 u32 seq;
2463 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002465 err = -ENOBUFS;
2466 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002467
Thomas Graf339bf982006-11-10 14:10:15 -08002468 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002469 if (skb == NULL)
2470 goto errout;
2471
Brian Haley191cd582008-08-14 15:33:21 -07002472 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002473 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002474 if (err < 0) {
2475 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2476 WARN_ON(err == -EMSGSIZE);
2477 kfree_skb(skb);
2478 goto errout;
2479 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002480 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2481 info->nlh, gfp_any());
2482 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002483errout:
2484 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002485 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002488static int ip6_route_dev_notify(struct notifier_block *this,
2489 unsigned long event, void *data)
2490{
2491 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002492 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002493
2494 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002495 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002496 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2497#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002498 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002499 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002500 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002501 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2502#endif
2503 }
2504
2505 return NOTIFY_OK;
2506}
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508/*
2509 * /proc
2510 */
2511
2512#ifdef CONFIG_PROC_FS
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514struct rt6_proc_arg
2515{
2516 char *buffer;
2517 int offset;
2518 int length;
2519 int skip;
2520 int len;
2521};
2522
2523static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2524{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002525 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002527 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002530 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002532 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533#endif
2534
2535 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002536 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002538 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002540 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002541 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2542 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002543 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return 0;
2545}
2546
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002547static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002549 struct net *net = (struct net *)m->private;
2550 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002554static int ipv6_route_open(struct inode *inode, struct file *file)
2555{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002556 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002557}
2558
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002559static const struct file_operations ipv6_route_proc_fops = {
2560 .owner = THIS_MODULE,
2561 .open = ipv6_route_open,
2562 .read = seq_read,
2563 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002564 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002565};
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2568{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002569 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002571 net->ipv6.rt6_stats->fib_nodes,
2572 net->ipv6.rt6_stats->fib_route_nodes,
2573 net->ipv6.rt6_stats->fib_rt_alloc,
2574 net->ipv6.rt6_stats->fib_rt_entries,
2575 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002576 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002577 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 return 0;
2580}
2581
2582static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2583{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002584 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002585}
2586
Arjan van de Ven9a321442007-02-12 00:55:35 -08002587static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 .owner = THIS_MODULE,
2589 .open = rt6_stats_seq_open,
2590 .read = seq_read,
2591 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002592 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593};
2594#endif /* CONFIG_PROC_FS */
2595
2596#ifdef CONFIG_SYSCTL
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002599int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 void __user *buffer, size_t *lenp, loff_t *ppos)
2601{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002602 struct net *net = current->nsproxy->net_ns;
2603 int delay = net->ipv6.sysctl.flush_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (write) {
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002605 proc_dointvec(ctl, write, buffer, lenp, ppos);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002606 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608 } else
2609 return -EINVAL;
2610}
2611
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002612ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002613 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002615 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002617 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002618 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 },
2620 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002622 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .maxlen = sizeof(int),
2624 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002625 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 },
2627 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002629 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 .maxlen = sizeof(int),
2631 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002632 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 },
2634 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002636 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 .maxlen = sizeof(int),
2638 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002639 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 },
2641 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002643 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .maxlen = sizeof(int),
2645 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002646 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 },
2648 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002650 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 .maxlen = sizeof(int),
2652 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002653 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 },
2655 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002657 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 .maxlen = sizeof(int),
2659 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002660 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 },
2662 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002664 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 .maxlen = sizeof(int),
2666 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002667 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 },
2669 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002671 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .maxlen = sizeof(int),
2673 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002674 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 },
2676 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002678 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .maxlen = sizeof(int),
2680 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002681 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002683 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684};
2685
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002686struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002687{
2688 struct ctl_table *table;
2689
2690 table = kmemdup(ipv6_route_table_template,
2691 sizeof(ipv6_route_table_template),
2692 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002693
2694 if (table) {
2695 table[0].data = &net->ipv6.sysctl.flush_delay;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002696 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002697 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2698 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2699 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2700 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2701 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2702 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2703 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002704 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002705 }
2706
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002707 return table;
2708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709#endif
2710
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002711static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002712{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002713 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002714
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002715 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2716 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002717
Eric Dumazetfc66f952010-10-08 06:37:34 +00002718 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2719 goto out_ip6_dst_ops;
2720
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002721 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2722 sizeof(*net->ipv6.ip6_null_entry),
2723 GFP_KERNEL);
2724 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002725 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002726 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002727 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002728 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002729 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2730 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002731
2732#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2733 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2734 sizeof(*net->ipv6.ip6_prohibit_entry),
2735 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002736 if (!net->ipv6.ip6_prohibit_entry)
2737 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002738 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002739 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002740 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002741 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2742 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002743
2744 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2745 sizeof(*net->ipv6.ip6_blk_hole_entry),
2746 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002747 if (!net->ipv6.ip6_blk_hole_entry)
2748 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002749 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002750 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002751 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002752 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2753 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002754#endif
2755
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002756 net->ipv6.sysctl.flush_delay = 0;
2757 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2758 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2759 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2760 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2761 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2762 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2763 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2764
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002765#ifdef CONFIG_PROC_FS
2766 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2767 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2768#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002769 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2770
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002771 ret = 0;
2772out:
2773 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002774
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002775#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2776out_ip6_prohibit_entry:
2777 kfree(net->ipv6.ip6_prohibit_entry);
2778out_ip6_null_entry:
2779 kfree(net->ipv6.ip6_null_entry);
2780#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002781out_ip6_dst_entries:
2782 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002783out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002784 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002785}
2786
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002787static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002788{
2789#ifdef CONFIG_PROC_FS
2790 proc_net_remove(net, "ipv6_route");
2791 proc_net_remove(net, "rt6_stats");
2792#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002793 kfree(net->ipv6.ip6_null_entry);
2794#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2795 kfree(net->ipv6.ip6_prohibit_entry);
2796 kfree(net->ipv6.ip6_blk_hole_entry);
2797#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002798 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002799}
2800
2801static struct pernet_operations ip6_route_net_ops = {
2802 .init = ip6_route_net_init,
2803 .exit = ip6_route_net_exit,
2804};
2805
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002806static struct notifier_block ip6_route_dev_notifier = {
2807 .notifier_call = ip6_route_dev_notify,
2808 .priority = 0,
2809};
2810
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002811int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002813 int ret;
2814
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002815 ret = -ENOMEM;
2816 ip6_dst_ops_template.kmem_cachep =
2817 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2818 SLAB_HWCACHE_ALIGN, NULL);
2819 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002820 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002821
Eric Dumazetfc66f952010-10-08 06:37:34 +00002822 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002823 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002824 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002825
Eric Dumazetfc66f952010-10-08 06:37:34 +00002826 ret = register_pernet_subsys(&ip6_route_net_ops);
2827 if (ret)
2828 goto out_dst_entries;
2829
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002830 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2831
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002832 /* Registering of the loopback is done before this portion of code,
2833 * the loopback reference in rt6_info will not be taken, do it
2834 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002835 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002836 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2837 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002838 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002839 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002840 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002841 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2842 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002843 ret = fib6_init();
2844 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002845 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002846
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002847 ret = xfrm6_init();
2848 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002849 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002850
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002851 ret = fib6_rules_init();
2852 if (ret)
2853 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002854
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002855 ret = -ENOBUFS;
2856 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2857 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2858 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2859 goto fib6_rules_init;
2860
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002861 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002862 if (ret)
2863 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002864
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002865out:
2866 return ret;
2867
2868fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002869 fib6_rules_cleanup();
2870xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002871 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002872out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002873 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002874out_register_subsys:
2875 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002876out_dst_entries:
2877 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002878out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002879 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
2883void ip6_route_cleanup(void)
2884{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002885 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002886 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002889 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002890 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002891 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}