blob: 852fc28ca81808087c187f7a676d5be884f4ef2b [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,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000092 const struct in6_addr *prefix, int prefixlen,
93 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080094 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080095static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096 const struct in6_addr *prefix, int prefixlen,
97 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080098#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 Harrison09640e632009-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 Harrison09640e632009-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
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000286static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700287{
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,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000298 const 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 Hideaki52e163562006-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,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000510 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800511{
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,
David S. Miller4c9483b2011-03-12 16:22:43 -0500602 struct flowi6 *fl6, 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);
David S. Miller4c9483b2011-03-12 16:22:43 -0500608 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700609restart:
610 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500611 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
612 BACKTRACK(net, &fl6->saddr);
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{
David S. Miller4c9483b2011-03-12 16:22:43 -0500623 struct flowi6 fl6 = {
624 .flowi6_oif = oif,
625 .daddr = *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) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500631 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700632 flags |= RT6_LOOKUP_F_HAS_SADDR;
633 }
634
David S. Miller4c9483b2011-03-12 16:22:43 -0500635 dst = fib6_rule_lookup(net, &fl6, 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
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000673static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
674 const 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
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000742static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800743{
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,
David S. Miller4c9483b2011-03-12 16:22:43 -0500756 struct flowi6 *fl6, 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:
David S. Miller4c9483b2011-03-12 16:22:43 -0500771 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
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
David S. Miller4c9483b2011-03-12 16:22:43 -0500776 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800777 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))
David S. Miller4c9483b2011-03-12 16:22:43 -0500785 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800786 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500787 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800788 else
789 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800790
Changli Gaod8d1f302010-06-10 23:31:35 -0700791 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800792 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800793
Changli Gaod8d1f302010-06-10 23:31:35 -0700794 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800795 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700796 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800797 if (!err)
798 goto out2;
799 }
800
801 if (--attempts <= 0)
802 goto out2;
803
804 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700805 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800806 * released someone could insert this route. Relookup.
807 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700808 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800809 goto relookup;
810
811out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800812 if (reachable) {
813 reachable = 0;
814 goto restart_2;
815 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700816 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700817 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700819 rt->dst.lastuse = jiffies;
820 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700821
822 return rt;
823}
824
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800825static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500826 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700827{
David S. Miller4c9483b2011-03-12 16:22:43 -0500828 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700829}
830
Thomas Grafc71099a2006-08-04 23:20:06 -0700831void ip6_route_input(struct sk_buff *skb)
832{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000833 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900834 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700835 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500836 struct flowi6 fl6 = {
837 .flowi6_iif = skb->dev->ifindex,
838 .daddr = iph->daddr,
839 .saddr = iph->saddr,
840 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
841 .flowi6_mark = skb->mark,
842 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700843 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700844
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800845 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700846 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700847
David S. Miller4c9483b2011-03-12 16:22:43 -0500848 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700849}
850
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800851static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500852 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700853{
David S. Miller4c9483b2011-03-12 16:22:43 -0500854 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700855}
856
Florian Westphal9c7a4f9c2011-03-22 19:17:36 -0700857struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500858 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700859{
860 int flags = 0;
861
David S. Miller4c9483b2011-03-12 16:22:43 -0500862 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700863 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700864
David S. Miller4c9483b2011-03-12 16:22:43 -0500865 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700866 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000867 else if (sk)
868 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700869
David S. Miller4c9483b2011-03-12 16:22:43 -0500870 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900873EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
David S. Miller2774c132011-03-01 14:59:04 -0800875struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700876{
David S. Miller69ead7a2011-03-01 14:45:33 -0800877 struct rt6_info *rt = dst_alloc(&ip6_dst_blackhole_ops, 1);
878 struct rt6_info *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700879 struct dst_entry *new = NULL;
880
881 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700882 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700883
David S. Miller14e50e52007-05-24 18:17:54 -0700884 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800885 new->input = dst_discard;
886 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700887
David S. Millerdefb3512010-12-08 21:16:57 -0800888 dst_copy_metrics(new, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 new->dev = ort->dst.dev;
David S. Miller14e50e52007-05-24 18:17:54 -0700890 if (new->dev)
891 dev_hold(new->dev);
892 rt->rt6i_idev = ort->rt6i_idev;
893 if (rt->rt6i_idev)
894 in6_dev_hold(rt->rt6i_idev);
895 rt->rt6i_expires = 0;
896
897 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
898 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
899 rt->rt6i_metric = 0;
900
901 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
902#ifdef CONFIG_IPV6_SUBTREES
903 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
904#endif
905
906 dst_free(new);
907 }
908
David S. Miller69ead7a2011-03-01 14:45:33 -0800909 dst_release(dst_orig);
910 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700911}
David S. Miller14e50e52007-05-24 18:17:54 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/*
914 * Destination cache support functions
915 */
916
917static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
918{
919 struct rt6_info *rt;
920
921 rt = (struct rt6_info *) dst;
922
David S. Miller6431cbc2011-02-07 20:38:06 -0800923 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
924 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
925 if (!rt->rt6i_peer)
926 rt6_bind_peer(rt, 0);
927 rt->rt6i_peer_genid = rt6_peer_genid();
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return NULL;
932}
933
934static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
935{
936 struct rt6_info *rt = (struct rt6_info *) dst;
937
938 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000939 if (rt->rt6i_flags & RTF_CACHE) {
940 if (rt6_check_expired(rt)) {
941 ip6_del_rt(rt);
942 dst = NULL;
943 }
944 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000946 dst = NULL;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000949 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static void ip6_link_failure(struct sk_buff *skb)
953{
954 struct rt6_info *rt;
955
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000956 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Eric Dumazetadf30902009-06-02 05:19:30 +0000958 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (rt) {
960 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700961 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rt->rt6i_flags |= RTF_EXPIRES;
963 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
964 rt->rt6i_node->fn_sernum = -1;
965 }
966}
967
968static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
969{
970 struct rt6_info *rt6 = (struct rt6_info*)dst;
971
972 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
973 rt6->rt6i_flags |= RTF_MODIFIED;
974 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800975 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800977 features |= RTAX_FEATURE_ALLFRAG;
978 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
David S. Millerdefb3512010-12-08 21:16:57 -0800980 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982}
983
David S. Miller0dbaee32010-12-13 12:52:14 -0800984static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
David S. Miller0dbaee32010-12-13 12:52:14 -0800986 struct net_device *dev = dst->dev;
987 unsigned int mtu = dst_mtu(dst);
988 struct net *net = dev_net(dev);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
991
Daniel Lezcano55786892008-03-04 13:47:47 -0800992 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
993 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900996 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
997 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
998 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * rely only on pmtu discovery"
1000 */
1001 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1002 mtu = IPV6_MAXPLEN;
1003 return mtu;
1004}
1005
David S. Millerd33e4552010-12-14 13:01:14 -08001006static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1007{
1008 unsigned int mtu = IPV6_MIN_MTU;
1009 struct inet6_dev *idev;
1010
1011 rcu_read_lock();
1012 idev = __in6_dev_get(dst->dev);
1013 if (idev)
1014 mtu = idev->cnf.mtu6;
1015 rcu_read_unlock();
1016
1017 return mtu;
1018}
1019
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001020static struct dst_entry *icmp6_dst_gc_list;
1021static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001022
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001023struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001025 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct rt6_info *rt;
1028 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001029 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (unlikely(idev == NULL))
1032 return NULL;
1033
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001034 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (unlikely(rt == NULL)) {
1036 in6_dev_put(idev);
1037 goto out;
1038 }
1039
1040 dev_hold(dev);
1041 if (neigh)
1042 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001043 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001045 if (IS_ERR(neigh))
1046 neigh = NULL;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 rt->rt6i_dev = dev;
1050 rt->rt6i_idev = idev;
1051 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001052 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001053 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001054 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001057 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001058 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 : 0;
1060 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1061 rt->rt6i_dst.plen = 128;
1062#endif
1063
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001064 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001065 rt->dst.next = icmp6_dst_gc_list;
1066 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001067 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Daniel Lezcano55786892008-03-04 13:47:47 -08001069 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001072 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001075int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Hagen Paul Pfeifere9476e952011-02-25 05:45:19 +00001077 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001078 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001080 spin_lock_bh(&icmp6_dst_lock);
1081 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 while ((dst = *pprev) != NULL) {
1084 if (!atomic_read(&dst->__refcnt)) {
1085 *pprev = dst->next;
1086 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 } else {
1088 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001089 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 }
1092
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001093 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001094
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001095 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
David S. Miller1e493d12008-09-10 17:27:15 -07001098static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1099 void *arg)
1100{
1101 struct dst_entry *dst, **pprev;
1102
1103 spin_lock_bh(&icmp6_dst_lock);
1104 pprev = &icmp6_dst_gc_list;
1105 while ((dst = *pprev) != NULL) {
1106 struct rt6_info *rt = (struct rt6_info *) dst;
1107 if (func(rt, arg)) {
1108 *pprev = dst->next;
1109 dst_free(dst);
1110 } else {
1111 pprev = &dst->next;
1112 }
1113 }
1114 spin_unlock_bh(&icmp6_dst_lock);
1115}
1116
Daniel Lezcano569d3642008-01-18 03:56:57 -08001117static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001120 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001121 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1122 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1123 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1124 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1125 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001126 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Eric Dumazetfc66f952010-10-08 06:37:34 +00001128 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001129 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001130 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto out;
1132
Benjamin Thery6891a342008-03-04 13:49:47 -08001133 net->ipv6.ip6_rt_gc_expire++;
1134 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1135 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001136 entries = dst_entries_get_slow(ops);
1137 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001138 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001140 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001141 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144/* Clean host part of a prefix. Not necessary in radix tree,
1145 but results in cleaner routing tables.
1146
1147 Remove it only when all the things will work!
1148 */
1149
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001150int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
David S. Miller5170ae82010-12-12 21:35:57 -08001152 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001153 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001154 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001155 struct inet6_dev *idev;
1156
1157 rcu_read_lock();
1158 idev = __in6_dev_get(dev);
1159 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001160 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001161 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001162 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001163 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return hoplimit;
1166}
David S. Millerabbf46a2010-12-12 21:14:46 -08001167EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169/*
1170 *
1171 */
1172
Thomas Graf86872cb2006-08-22 00:01:08 -07001173int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001176 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct rt6_info *rt = NULL;
1178 struct net_device *dev = NULL;
1179 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001180 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int addr_type;
1182
Thomas Graf86872cb2006-08-22 00:01:08 -07001183 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EINVAL;
1185#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001186 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EINVAL;
1188#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001189 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001191 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!dev)
1193 goto out;
1194 idev = in6_dev_get(dev);
1195 if (!idev)
1196 goto out;
1197 }
1198
Thomas Graf86872cb2006-08-22 00:01:08 -07001199 if (cfg->fc_metric == 0)
1200 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Daniel Lezcano55786892008-03-04 13:47:47 -08001202 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001203 if (table == NULL) {
1204 err = -ENOBUFS;
1205 goto out;
1206 }
1207
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001208 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (rt == NULL) {
1211 err = -ENOMEM;
1212 goto out;
1213 }
1214
Changli Gaod8d1f302010-06-10 23:31:35 -07001215 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001216 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1217 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1218 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Thomas Graf86872cb2006-08-22 00:01:08 -07001220 if (cfg->fc_protocol == RTPROT_UNSPEC)
1221 cfg->fc_protocol = RTPROT_BOOT;
1222 rt->rt6i_protocol = cfg->fc_protocol;
1223
1224 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001227 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001228 else if (cfg->fc_flags & RTF_LOCAL)
1229 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001231 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Changli Gaod8d1f302010-06-10 23:31:35 -07001233 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Thomas Graf86872cb2006-08-22 00:01:08 -07001235 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1236 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001238 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001241 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1242 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#endif
1244
Thomas Graf86872cb2006-08-22 00:01:08 -07001245 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* We cannot add true routes via loopback here,
1248 they would result in kernel looping; promote them to reject routes
1249 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001250 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001251 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1252 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001254 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (dev) {
1256 dev_put(dev);
1257 in6_dev_put(idev);
1258 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001259 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dev_hold(dev);
1261 idev = in6_dev_get(dev);
1262 if (!idev) {
1263 err = -ENODEV;
1264 goto out;
1265 }
1266 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001267 rt->dst.output = ip6_pkt_discard_out;
1268 rt->dst.input = ip6_pkt_discard;
1269 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1271 goto install_route;
1272 }
1273
Thomas Graf86872cb2006-08-22 00:01:08 -07001274 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001275 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int gwa_type;
1277
Thomas Graf86872cb2006-08-22 00:01:08 -07001278 gw_addr = &cfg->fc_gateway;
1279 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 gwa_type = ipv6_addr_type(gw_addr);
1281
1282 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1283 struct rt6_info *grt;
1284
1285 /* IPv6 strictly inhibits using not link-local
1286 addresses as nexthop address.
1287 Otherwise, router will not able to send redirects.
1288 It is very good, but in some (rare!) circumstances
1289 (SIT, PtP, NBMA NOARP links) it is handy to allow
1290 some exceptions. --ANK
1291 */
1292 err = -EINVAL;
1293 if (!(gwa_type&IPV6_ADDR_UNICAST))
1294 goto out;
1295
Daniel Lezcano55786892008-03-04 13:47:47 -08001296 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 err = -EHOSTUNREACH;
1299 if (grt == NULL)
1300 goto out;
1301 if (dev) {
1302 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001303 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto out;
1305 }
1306 } else {
1307 dev = grt->rt6i_dev;
1308 idev = grt->rt6i_idev;
1309 dev_hold(dev);
1310 in6_dev_hold(grt->rt6i_idev);
1311 }
1312 if (!(grt->rt6i_flags&RTF_GATEWAY))
1313 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001314 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (err)
1317 goto out;
1318 }
1319 err = -EINVAL;
1320 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1321 goto out;
1322 }
1323
1324 err = -ENODEV;
1325 if (dev == NULL)
1326 goto out;
1327
Daniel Walterc3968a82011-04-13 21:10:57 +00001328 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1329 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1330 err = -EINVAL;
1331 goto out;
1332 }
1333 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1334 rt->rt6i_prefsrc.plen = 128;
1335 } else
1336 rt->rt6i_prefsrc.plen = 0;
1337
Thomas Graf86872cb2006-08-22 00:01:08 -07001338 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1340 if (IS_ERR(rt->rt6i_nexthop)) {
1341 err = PTR_ERR(rt->rt6i_nexthop);
1342 rt->rt6i_nexthop = NULL;
1343 goto out;
1344 }
1345 }
1346
Thomas Graf86872cb2006-08-22 00:01:08 -07001347 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001350 if (cfg->fc_mx) {
1351 struct nlattr *nla;
1352 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Thomas Graf86872cb2006-08-22 00:01:08 -07001354 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001355 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001356
1357 if (type) {
1358 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 err = -EINVAL;
1360 goto out;
1361 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001362
David S. Millerdefb3512010-12-08 21:16:57 -08001363 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 }
1367
Changli Gaod8d1f302010-06-10 23:31:35 -07001368 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001370 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001371
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001372 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001373
Thomas Graf86872cb2006-08-22 00:01:08 -07001374 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376out:
1377 if (dev)
1378 dev_put(dev);
1379 if (idev)
1380 in6_dev_put(idev);
1381 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001382 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return err;
1384}
1385
Thomas Graf86872cb2006-08-22 00:01:08 -07001386static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001389 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001390 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001392 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001393 return -ENOENT;
1394
Thomas Grafc71099a2006-08-04 23:20:06 -07001395 table = rt->rt6i_table;
1396 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Thomas Graf86872cb2006-08-22 00:01:08 -07001398 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001399 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Thomas Grafc71099a2006-08-04 23:20:06 -07001401 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 return err;
1404}
1405
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001406int ip6_del_rt(struct rt6_info *rt)
1407{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001408 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001409 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001410 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001411 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001412}
1413
Thomas Graf86872cb2006-08-22 00:01:08 -07001414static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Thomas Grafc71099a2006-08-04 23:20:06 -07001416 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct fib6_node *fn;
1418 struct rt6_info *rt;
1419 int err = -ESRCH;
1420
Daniel Lezcano55786892008-03-04 13:47:47 -08001421 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001422 if (table == NULL)
1423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thomas Grafc71099a2006-08-04 23:20:06 -07001425 read_lock_bh(&table->tb6_lock);
1426
1427 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001428 &cfg->fc_dst, cfg->fc_dst_len,
1429 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001432 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001433 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001435 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001437 if (cfg->fc_flags & RTF_GATEWAY &&
1438 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001440 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001442 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001443 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Thomas Graf86872cb2006-08-22 00:01:08 -07001445 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001448 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 return err;
1451}
1452
1453/*
1454 * Handle redirects
1455 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001456struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001457 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001458 struct in6_addr gateway;
1459};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001461static struct rt6_info *__ip6_route_redirect(struct net *net,
1462 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001463 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001464 int flags)
1465{
David S. Miller4c9483b2011-03-12 16:22:43 -05001466 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001467 struct rt6_info *rt;
1468 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001471 * Get the "current" route for this destination and
1472 * check if the redirect has come from approriate router.
1473 *
1474 * RFC 2461 specifies that redirects should only be
1475 * accepted if they come from the nexthop to the target.
1476 * Due to the way the routes are chosen, this notion
1477 * is a bit fuzzy and one might need to check all possible
1478 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Thomas Grafc71099a2006-08-04 23:20:06 -07001481 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001482 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001483restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001484 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001485 /*
1486 * Current route is on-link; redirect is always invalid.
1487 *
1488 * Seems, previous statement is not true. It could
1489 * be node, which looks for us as on-link (f.e. proxy ndisc)
1490 * But then router serving it might decide, that we should
1491 * know truth 8)8) --ANK (980726).
1492 */
1493 if (rt6_check_expired(rt))
1494 continue;
1495 if (!(rt->rt6i_flags & RTF_GATEWAY))
1496 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001497 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001498 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001499 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001500 continue;
1501 break;
1502 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001503
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001504 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001505 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001506 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001507out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001508 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001509
1510 read_unlock_bh(&table->tb6_lock);
1511
1512 return rt;
1513};
1514
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001515static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1516 const struct in6_addr *src,
1517 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001518 struct net_device *dev)
1519{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001520 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001521 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001522 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001523 .fl6 = {
1524 .flowi6_oif = dev->ifindex,
1525 .daddr = *dest,
1526 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001527 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001528 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001529
Brian Haley86c36ce2009-10-07 13:58:01 -07001530 ipv6_addr_copy(&rdfl.gateway, gateway);
1531
Thomas Grafadaa70b2006-10-13 15:01:03 -07001532 if (rt6_need_strict(dest))
1533 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001534
David S. Miller4c9483b2011-03-12 16:22:43 -05001535 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001536 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001537}
1538
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001539void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1540 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001541 struct neighbour *neigh, u8 *lladdr, int on_link)
1542{
1543 struct rt6_info *rt, *nrt = NULL;
1544 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001545 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001546
1547 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1548
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001549 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (net_ratelimit())
1551 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1552 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * We have finally decided to accept it.
1558 */
1559
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001560 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1562 NEIGH_UPDATE_F_OVERRIDE|
1563 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1564 NEIGH_UPDATE_F_ISROUTER))
1565 );
1566
1567 /*
1568 * Redirect received -> path was valid.
1569 * Look, redirects are sent only in response to data packets,
1570 * so that this nexthop apparently is reachable. --ANK
1571 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001572 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001575 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto out;
1577
1578 nrt = ip6_rt_copy(rt);
1579 if (nrt == NULL)
1580 goto out;
1581
1582 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1583 if (on_link)
1584 nrt->rt6i_flags &= ~RTF_GATEWAY;
1585
1586 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1587 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001588 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1591 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Thomas Graf40e22e82006-08-22 00:00:45 -07001593 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto out;
1595
Changli Gaod8d1f302010-06-10 23:31:35 -07001596 netevent.old = &rt->dst;
1597 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001598 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001601 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return;
1603 }
1604
1605out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001606 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/*
1610 * Handle ICMP "packet too big" messages
1611 * i.e. Path MTU discovery
1612 */
1613
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001614static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001615 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct rt6_info *rt, *nrt;
1618 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001619again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001620 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (rt == NULL)
1622 return;
1623
Andrey Vagind3052b52010-12-11 15:20:11 +00001624 if (rt6_check_expired(rt)) {
1625 ip6_del_rt(rt);
1626 goto again;
1627 }
1628
Changli Gaod8d1f302010-06-10 23:31:35 -07001629 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto out;
1631
1632 if (pmtu < IPV6_MIN_MTU) {
1633 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001634 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * MTU (1280) and a fragment header should always be included
1636 * after a node receiving Too Big message reporting PMTU is
1637 * less than the IPv6 Minimum Link MTU.
1638 */
1639 pmtu = IPV6_MIN_MTU;
1640 allfrag = 1;
1641 }
1642
1643 /* New mtu received -> path was valid.
1644 They are sent only in response to data packets,
1645 so that this nexthop apparently is reachable. --ANK
1646 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001647 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* Host route. If it is static, it would be better
1650 not to override it, but add new one, so that
1651 when cache entry will expire old pmtu
1652 would return automatically.
1653 */
1654 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001655 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1656 if (allfrag) {
1657 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1658 features |= RTAX_FEATURE_ALLFRAG;
1659 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1660 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001661 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1663 goto out;
1664 }
1665
1666 /* Network route.
1667 Two cases are possible:
1668 1. It is connected route. Action: COW
1669 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1670 */
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001671 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001672 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001673 else
1674 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001675
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001676 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001677 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1678 if (allfrag) {
1679 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1680 features |= RTAX_FEATURE_ALLFRAG;
1681 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1682 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001683
1684 /* According to RFC 1981, detecting PMTU increase shouldn't be
1685 * happened within 5 mins, the recommended timer is 10 mins.
1686 * Here this route expiration time is set to ip6_rt_mtu_expires
1687 * which is 10 mins. After 10 mins the decreased pmtu is expired
1688 * and detecting PMTU increase will be automatically happened.
1689 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001690 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001691 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1692
Thomas Graf40e22e82006-08-22 00:00:45 -07001693 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001696 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001699void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001700 struct net_device *dev, u32 pmtu)
1701{
1702 struct net *net = dev_net(dev);
1703
1704 /*
1705 * RFC 1981 states that a node "MUST reduce the size of the packets it
1706 * is sending along the path" that caused the Packet Too Big message.
1707 * Since it's not possible in the general case to determine which
1708 * interface was used to send the original packet, we update the MTU
1709 * on the interface that will be used to send future packets. We also
1710 * update the MTU on the interface that received the Packet Too Big in
1711 * case the original packet was forced out that interface with
1712 * SO_BINDTODEVICE or similar. This is the next best thing to the
1713 * correct behaviour, which would be to update the MTU on all
1714 * interfaces.
1715 */
1716 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1717 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720/*
1721 * Misc support functions
1722 */
1723
1724static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1725{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001726 struct net *net = dev_net(ort->rt6i_dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001727 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001730 rt->dst.input = ort->dst.input;
1731 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
David S. Millerdefb3512010-12-08 21:16:57 -08001733 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001734 rt->dst.error = ort->dst.error;
1735 rt->dst.dev = ort->dst.dev;
1736 if (rt->dst.dev)
1737 dev_hold(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rt->rt6i_idev = ort->rt6i_idev;
1739 if (rt->rt6i_idev)
1740 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001741 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 rt->rt6i_expires = 0;
1743
1744 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1745 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1746 rt->rt6i_metric = 0;
1747
1748 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1749#ifdef CONFIG_IPV6_SUBTREES
1750 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1751#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001752 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 return rt;
1755}
1756
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001757#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001758static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001759 const struct in6_addr *prefix, int prefixlen,
1760 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001761{
1762 struct fib6_node *fn;
1763 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001764 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001765
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001766 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001767 if (table == NULL)
1768 return NULL;
1769
1770 write_lock_bh(&table->tb6_lock);
1771 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001772 if (!fn)
1773 goto out;
1774
Changli Gaod8d1f302010-06-10 23:31:35 -07001775 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001776 if (rt->rt6i_dev->ifindex != ifindex)
1777 continue;
1778 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1779 continue;
1780 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1781 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001783 break;
1784 }
1785out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001786 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001787 return rt;
1788}
1789
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001790static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001791 const struct in6_addr *prefix, int prefixlen,
1792 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001793 unsigned pref)
1794{
Thomas Graf86872cb2006-08-22 00:01:08 -07001795 struct fib6_config cfg = {
1796 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001797 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 .fc_ifindex = ifindex,
1799 .fc_dst_len = prefixlen,
1800 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1801 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001802 .fc_nlinfo.pid = 0,
1803 .fc_nlinfo.nlh = NULL,
1804 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001806
Thomas Graf86872cb2006-08-22 00:01:08 -07001807 ipv6_addr_copy(&cfg.fc_dst, prefix);
1808 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1809
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001810 /* We should treat it as a default route if prefix length is 0. */
1811 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001812 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001813
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001815
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001816 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001817}
1818#endif
1819
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001820struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001823 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001825 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001826 if (table == NULL)
1827 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Thomas Grafc71099a2006-08-04 23:20:06 -07001829 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001830 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001832 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1834 break;
1835 }
1836 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001837 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001838 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return rt;
1840}
1841
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001842struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001843 struct net_device *dev,
1844 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Thomas Graf86872cb2006-08-22 00:01:08 -07001846 struct fib6_config cfg = {
1847 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001848 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001849 .fc_ifindex = dev->ifindex,
1850 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1851 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001852 .fc_nlinfo.pid = 0,
1853 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001854 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Thomas Graf86872cb2006-08-22 00:01:08 -07001857 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Thomas Graf86872cb2006-08-22 00:01:08 -07001859 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return rt6_get_dflt_router(gwaddr, dev);
1862}
1863
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001864void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001867 struct fib6_table *table;
1868
1869 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001870 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001871 if (table == NULL)
1872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001875 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001876 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001878 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001879 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001880 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto restart;
1882 }
1883 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001884 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Daniel Lezcano55786892008-03-04 13:47:47 -08001887static void rtmsg_to_fib6_config(struct net *net,
1888 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001889 struct fib6_config *cfg)
1890{
1891 memset(cfg, 0, sizeof(*cfg));
1892
1893 cfg->fc_table = RT6_TABLE_MAIN;
1894 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1895 cfg->fc_metric = rtmsg->rtmsg_metric;
1896 cfg->fc_expires = rtmsg->rtmsg_info;
1897 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1898 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1899 cfg->fc_flags = rtmsg->rtmsg_flags;
1900
Daniel Lezcano55786892008-03-04 13:47:47 -08001901 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001902
Thomas Graf86872cb2006-08-22 00:01:08 -07001903 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1904 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1905 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1906}
1907
Daniel Lezcano55786892008-03-04 13:47:47 -08001908int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Thomas Graf86872cb2006-08-22 00:01:08 -07001910 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct in6_rtmsg rtmsg;
1912 int err;
1913
1914 switch(cmd) {
1915 case SIOCADDRT: /* Add a route */
1916 case SIOCDELRT: /* Delete a route */
1917 if (!capable(CAP_NET_ADMIN))
1918 return -EPERM;
1919 err = copy_from_user(&rtmsg, arg,
1920 sizeof(struct in6_rtmsg));
1921 if (err)
1922 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001923
Daniel Lezcano55786892008-03-04 13:47:47 -08001924 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 rtnl_lock();
1927 switch (cmd) {
1928 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001929 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001932 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
1934 default:
1935 err = -EINVAL;
1936 }
1937 rtnl_unlock();
1938
1939 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return -EINVAL;
1943}
1944
1945/*
1946 * Drop the packet on the floor
1947 */
1948
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001949static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001951 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001952 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001953 switch (ipstats_mib_noroutes) {
1954 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001955 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001956 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001957 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1958 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001959 break;
1960 }
1961 /* FALLTHROUGH */
1962 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001963 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1964 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001965 break;
1966 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001967 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 kfree_skb(skb);
1969 return 0;
1970}
1971
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001972static int ip6_pkt_discard(struct sk_buff *skb)
1973{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001974 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001975}
1976
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001977static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Eric Dumazetadf30902009-06-02 05:19:30 +00001979 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001980 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
David S. Miller6723ab52006-10-18 21:20:57 -07001983#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1984
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001985static int ip6_pkt_prohibit(struct sk_buff *skb)
1986{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001987 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001988}
1989
1990static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1991{
Eric Dumazetadf30902009-06-02 05:19:30 +00001992 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001993 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001994}
1995
David S. Miller6723ab52006-10-18 21:20:57 -07001996#endif
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998/*
1999 * Allocate a dst for local (unicast / anycast) address.
2000 */
2001
2002struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2003 const struct in6_addr *addr,
2004 int anycast)
2005{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002006 struct net *net = dev_net(idev->dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002007 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -08002008 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Ben Greear40385652010-11-08 12:33:48 +00002010 if (rt == NULL) {
2011 if (net_ratelimit())
2012 pr_warning("IPv6: Maximum number of routes reached,"
2013 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Daniel Lezcano55786892008-03-04 13:47:47 -08002017 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 in6_dev_hold(idev);
2019
Changli Gaod8d1f302010-06-10 23:31:35 -07002020 rt->dst.flags = DST_HOST;
2021 rt->dst.input = ip6_input;
2022 rt->dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08002023 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 rt->rt6i_idev = idev;
David S. Millerdefb3512010-12-08 21:16:57 -08002025 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
Changli Gaod8d1f302010-06-10 23:31:35 -07002026 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002029 if (anycast)
2030 rt->rt6i_flags |= RTF_ANYCAST;
2031 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002033 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2034 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002035 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002036
David S. Miller29546a62011-03-03 12:10:37 -08002037 return ERR_CAST(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
David S. Miller14deae42009-01-04 16:04:39 -08002039 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2042 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002043 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Changli Gaod8d1f302010-06-10 23:31:35 -07002045 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 return rt;
2048}
2049
Daniel Walterc3968a82011-04-13 21:10:57 +00002050int ip6_route_get_saddr(struct net *net,
2051 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002052 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002053 unsigned int prefs,
2054 struct in6_addr *saddr)
2055{
2056 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2057 int err = 0;
2058 if (rt->rt6i_prefsrc.plen)
2059 ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2060 else
2061 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2062 daddr, prefs, saddr);
2063 return err;
2064}
2065
2066/* remove deleted ip from prefsrc entries */
2067struct arg_dev_net_ip {
2068 struct net_device *dev;
2069 struct net *net;
2070 struct in6_addr *addr;
2071};
2072
2073static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2074{
2075 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2076 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2077 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2078
2079 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2080 rt != net->ipv6.ip6_null_entry &&
2081 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2082 /* remove prefsrc entry */
2083 rt->rt6i_prefsrc.plen = 0;
2084 }
2085 return 0;
2086}
2087
2088void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2089{
2090 struct net *net = dev_net(ifp->idev->dev);
2091 struct arg_dev_net_ip adni = {
2092 .dev = ifp->idev->dev,
2093 .net = net,
2094 .addr = &ifp->addr,
2095 };
2096 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2097}
2098
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002099struct arg_dev_net {
2100 struct net_device *dev;
2101 struct net *net;
2102};
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104static int fib6_ifdown(struct rt6_info *rt, void *arg)
2105{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002106 const struct arg_dev_net *adn = arg;
2107 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002108
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002109 if ((rt->rt6i_dev == dev || dev == NULL) &&
2110 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 RT6_TRACE("deleted by ifdown %p\n", rt);
2112 return -1;
2113 }
2114 return 0;
2115}
2116
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002117void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002119 struct arg_dev_net adn = {
2120 .dev = dev,
2121 .net = net,
2122 };
2123
2124 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002125 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128struct rt6_mtu_change_arg
2129{
2130 struct net_device *dev;
2131 unsigned mtu;
2132};
2133
2134static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2135{
2136 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2137 struct inet6_dev *idev;
2138
2139 /* In IPv6 pmtu discovery is not optional,
2140 so that RTAX_MTU lock cannot disable it.
2141 We still use this lock to block changes
2142 caused by addrconf/ndisc.
2143 */
2144
2145 idev = __in6_dev_get(arg->dev);
2146 if (idev == NULL)
2147 return 0;
2148
2149 /* For administrative MTU increase, there is no way to discover
2150 IPv6 PMTU increase, so PMTU increase should be updated here.
2151 Since RFC 1981 doesn't include administrative MTU increase
2152 update PMTU increase is a MUST. (i.e. jumbo frame)
2153 */
2154 /*
2155 If new MTU is less than route PMTU, this new MTU will be the
2156 lowest MTU in the path, update the route PMTU to reflect PMTU
2157 decreases; if new MTU is greater than route PMTU, and the
2158 old MTU is the lowest MTU in the path, update the route PMTU
2159 to reflect the increase. In this case if the other nodes' MTU
2160 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2161 PMTU discouvery.
2162 */
2163 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002164 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2165 (dst_mtu(&rt->dst) >= arg->mtu ||
2166 (dst_mtu(&rt->dst) < arg->mtu &&
2167 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002168 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171}
2172
2173void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2174{
Thomas Grafc71099a2006-08-04 23:20:06 -07002175 struct rt6_mtu_change_arg arg = {
2176 .dev = dev,
2177 .mtu = mtu,
2178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002180 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002183static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002184 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002185 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002186 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002187 [RTA_PRIORITY] = { .type = NLA_U32 },
2188 [RTA_METRICS] = { .type = NLA_NESTED },
2189};
2190
2191static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2192 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Thomas Graf86872cb2006-08-22 00:01:08 -07002194 struct rtmsg *rtm;
2195 struct nlattr *tb[RTA_MAX+1];
2196 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Thomas Graf86872cb2006-08-22 00:01:08 -07002198 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2199 if (err < 0)
2200 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Thomas Graf86872cb2006-08-22 00:01:08 -07002202 err = -EINVAL;
2203 rtm = nlmsg_data(nlh);
2204 memset(cfg, 0, sizeof(*cfg));
2205
2206 cfg->fc_table = rtm->rtm_table;
2207 cfg->fc_dst_len = rtm->rtm_dst_len;
2208 cfg->fc_src_len = rtm->rtm_src_len;
2209 cfg->fc_flags = RTF_UP;
2210 cfg->fc_protocol = rtm->rtm_protocol;
2211
2212 if (rtm->rtm_type == RTN_UNREACHABLE)
2213 cfg->fc_flags |= RTF_REJECT;
2214
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002215 if (rtm->rtm_type == RTN_LOCAL)
2216 cfg->fc_flags |= RTF_LOCAL;
2217
Thomas Graf86872cb2006-08-22 00:01:08 -07002218 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2219 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002220 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002221
2222 if (tb[RTA_GATEWAY]) {
2223 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2224 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002226
2227 if (tb[RTA_DST]) {
2228 int plen = (rtm->rtm_dst_len + 7) >> 3;
2229
2230 if (nla_len(tb[RTA_DST]) < plen)
2231 goto errout;
2232
2233 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002235
2236 if (tb[RTA_SRC]) {
2237 int plen = (rtm->rtm_src_len + 7) >> 3;
2238
2239 if (nla_len(tb[RTA_SRC]) < plen)
2240 goto errout;
2241
2242 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002244
Daniel Walterc3968a82011-04-13 21:10:57 +00002245 if (tb[RTA_PREFSRC])
2246 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2247
Thomas Graf86872cb2006-08-22 00:01:08 -07002248 if (tb[RTA_OIF])
2249 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2250
2251 if (tb[RTA_PRIORITY])
2252 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2253
2254 if (tb[RTA_METRICS]) {
2255 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2256 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002258
2259 if (tb[RTA_TABLE])
2260 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2261
2262 err = 0;
2263errout:
2264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Thomas Grafc127ea22007-03-22 11:58:32 -07002267static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Thomas Graf86872cb2006-08-22 00:01:08 -07002269 struct fib6_config cfg;
2270 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Thomas Graf86872cb2006-08-22 00:01:08 -07002272 err = rtm_to_fib6_config(skb, nlh, &cfg);
2273 if (err < 0)
2274 return err;
2275
2276 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Thomas Grafc127ea22007-03-22 11:58:32 -07002279static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Thomas Graf86872cb2006-08-22 00:01:08 -07002281 struct fib6_config cfg;
2282 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Thomas Graf86872cb2006-08-22 00:01:08 -07002284 err = rtm_to_fib6_config(skb, nlh, &cfg);
2285 if (err < 0)
2286 return err;
2287
2288 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Thomas Graf339bf982006-11-10 14:10:15 -08002291static inline size_t rt6_nlmsg_size(void)
2292{
2293 return NLMSG_ALIGN(sizeof(struct rtmsg))
2294 + nla_total_size(16) /* RTA_SRC */
2295 + nla_total_size(16) /* RTA_DST */
2296 + nla_total_size(16) /* RTA_GATEWAY */
2297 + nla_total_size(16) /* RTA_PREFSRC */
2298 + nla_total_size(4) /* RTA_TABLE */
2299 + nla_total_size(4) /* RTA_IIF */
2300 + nla_total_size(4) /* RTA_OIF */
2301 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002302 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002303 + nla_total_size(sizeof(struct rta_cacheinfo));
2304}
2305
Brian Haley191cd582008-08-14 15:33:21 -07002306static int rt6_fill_node(struct net *net,
2307 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002308 struct in6_addr *dst, struct in6_addr *src,
2309 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002310 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002313 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002314 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002315 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 if (prefix) { /* user wants prefix routes only */
2318 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2319 /* success since this is not a prefix route */
2320 return 1;
2321 }
2322 }
2323
Thomas Graf2d7202b2006-08-22 00:01:27 -07002324 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2325 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002326 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002327
2328 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 rtm->rtm_family = AF_INET6;
2330 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2331 rtm->rtm_src_len = rt->rt6i_src.plen;
2332 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002333 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002334 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002335 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002336 table = RT6_TABLE_UNSPEC;
2337 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002338 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (rt->rt6i_flags&RTF_REJECT)
2340 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002341 else if (rt->rt6i_flags&RTF_LOCAL)
2342 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2344 rtm->rtm_type = RTN_LOCAL;
2345 else
2346 rtm->rtm_type = RTN_UNICAST;
2347 rtm->rtm_flags = 0;
2348 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2349 rtm->rtm_protocol = rt->rt6i_protocol;
2350 if (rt->rt6i_flags&RTF_DYNAMIC)
2351 rtm->rtm_protocol = RTPROT_REDIRECT;
2352 else if (rt->rt6i_flags & RTF_ADDRCONF)
2353 rtm->rtm_protocol = RTPROT_KERNEL;
2354 else if (rt->rt6i_flags&RTF_DEFAULT)
2355 rtm->rtm_protocol = RTPROT_RA;
2356
2357 if (rt->rt6i_flags&RTF_CACHE)
2358 rtm->rtm_flags |= RTM_F_CLONED;
2359
2360 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002361 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002362 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002364 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#ifdef CONFIG_IPV6_SUBTREES
2366 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002367 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002368 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002370 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002372 if (iif) {
2373#ifdef CONFIG_IPV6_MROUTE
2374 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002375 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002376 if (err <= 0) {
2377 if (!nowait) {
2378 if (err == 0)
2379 return 0;
2380 goto nla_put_failure;
2381 } else {
2382 if (err == -EMSGSIZE)
2383 goto nla_put_failure;
2384 }
2385 }
2386 } else
2387#endif
2388 NLA_PUT_U32(skb, RTA_IIF, iif);
2389 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002391 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002392 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002394
Daniel Walterc3968a82011-04-13 21:10:57 +00002395 if (rt->rt6i_prefsrc.plen) {
2396 struct in6_addr saddr_buf;
2397 ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
2398 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2399 }
2400
David S. Millerdefb3512010-12-08 21:16:57 -08002401 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002402 goto nla_put_failure;
2403
Changli Gaod8d1f302010-06-10 23:31:35 -07002404 if (rt->dst.neighbour)
2405 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002406
Changli Gaod8d1f302010-06-10 23:31:35 -07002407 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002408 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2409
2410 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002411
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002412 if (!(rt->rt6i_flags & RTF_EXPIRES))
2413 expires = 0;
2414 else if (rt->rt6i_expires - jiffies < INT_MAX)
2415 expires = rt->rt6i_expires - jiffies;
2416 else
2417 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002418
Changli Gaod8d1f302010-06-10 23:31:35 -07002419 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2420 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002421 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Thomas Graf2d7202b2006-08-22 00:01:27 -07002423 return nlmsg_end(skb, nlh);
2424
2425nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002426 nlmsg_cancel(skb, nlh);
2427 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Patrick McHardy1b43af52006-08-10 23:11:17 -07002430int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2433 int prefix;
2434
Thomas Graf2d7202b2006-08-22 00:01:27 -07002435 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2436 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2438 } else
2439 prefix = 0;
2440
Brian Haley191cd582008-08-14 15:33:21 -07002441 return rt6_fill_node(arg->net,
2442 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002444 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Thomas Grafc127ea22007-03-22 11:58:32 -07002447static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002449 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002450 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002452 struct sk_buff *skb;
2453 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002454 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002455 int err, iif = 0;
2456
2457 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2458 if (err < 0)
2459 goto errout;
2460
2461 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002462 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002463
2464 if (tb[RTA_SRC]) {
2465 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2466 goto errout;
2467
David S. Miller4c9483b2011-03-12 16:22:43 -05002468 ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
Thomas Grafab364a62006-08-22 00:01:47 -07002469 }
2470
2471 if (tb[RTA_DST]) {
2472 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2473 goto errout;
2474
David S. Miller4c9483b2011-03-12 16:22:43 -05002475 ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
Thomas Grafab364a62006-08-22 00:01:47 -07002476 }
2477
2478 if (tb[RTA_IIF])
2479 iif = nla_get_u32(tb[RTA_IIF]);
2480
2481 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002482 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002483
2484 if (iif) {
2485 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002486 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002487 if (!dev) {
2488 err = -ENODEV;
2489 goto errout;
2490 }
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002494 if (skb == NULL) {
2495 err = -ENOBUFS;
2496 goto errout;
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* Reserve room for dummy headers, this skb can pass
2500 through good chunk of routing engine.
2501 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002502 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2504
David S. Miller4c9483b2011-03-12 16:22:43 -05002505 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002506 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
David S. Miller4c9483b2011-03-12 16:22:43 -05002508 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002510 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002512 kfree_skb(skb);
2513 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515
Daniel Lezcano55786892008-03-04 13:47:47 -08002516 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002517errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Thomas Graf86872cb2006-08-22 00:01:08 -07002521void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002524 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002525 u32 seq;
2526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002528 err = -ENOBUFS;
2529 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002530
Thomas Graf339bf982006-11-10 14:10:15 -08002531 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002532 if (skb == NULL)
2533 goto errout;
2534
Brian Haley191cd582008-08-14 15:33:21 -07002535 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002536 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002537 if (err < 0) {
2538 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2539 WARN_ON(err == -EMSGSIZE);
2540 kfree_skb(skb);
2541 goto errout;
2542 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002543 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2544 info->nlh, gfp_any());
2545 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002546errout:
2547 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002548 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002551static int ip6_route_dev_notify(struct notifier_block *this,
2552 unsigned long event, void *data)
2553{
2554 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002555 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002556
2557 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002558 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002559 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2560#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002561 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002562 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002563 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002564 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2565#endif
2566 }
2567
2568 return NOTIFY_OK;
2569}
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/*
2572 * /proc
2573 */
2574
2575#ifdef CONFIG_PROC_FS
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577struct rt6_proc_arg
2578{
2579 char *buffer;
2580 int offset;
2581 int length;
2582 int skip;
2583 int len;
2584};
2585
2586static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2587{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002588 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002590 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002593 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002595 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
2597
2598 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002599 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002601 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002603 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002604 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2605 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002606 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608}
2609
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002610static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002612 struct net *net = (struct net *)m->private;
2613 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002617static int ipv6_route_open(struct inode *inode, struct file *file)
2618{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002619 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002620}
2621
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002622static const struct file_operations ipv6_route_proc_fops = {
2623 .owner = THIS_MODULE,
2624 .open = ipv6_route_open,
2625 .read = seq_read,
2626 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002627 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002628};
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2631{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002632 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002634 net->ipv6.rt6_stats->fib_nodes,
2635 net->ipv6.rt6_stats->fib_route_nodes,
2636 net->ipv6.rt6_stats->fib_rt_alloc,
2637 net->ipv6.rt6_stats->fib_rt_entries,
2638 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002639 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002640 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 return 0;
2643}
2644
2645static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2646{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002647 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002648}
2649
Arjan van de Ven9a321442007-02-12 00:55:35 -08002650static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 .owner = THIS_MODULE,
2652 .open = rt6_stats_seq_open,
2653 .read = seq_read,
2654 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002655 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656};
2657#endif /* CONFIG_PROC_FS */
2658
2659#ifdef CONFIG_SYSCTL
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002662int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 void __user *buffer, size_t *lenp, loff_t *ppos)
2664{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002665 struct net *net;
2666 int delay;
2667 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002669
2670 net = (struct net *)ctl->extra1;
2671 delay = net->ipv6.sysctl.flush_delay;
2672 proc_dointvec(ctl, write, buffer, lenp, ppos);
2673 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002677ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002678 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002680 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002682 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002683 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 },
2685 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002687 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 .maxlen = sizeof(int),
2689 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002690 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 },
2692 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002694 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .maxlen = sizeof(int),
2696 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002697 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 },
2699 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002701 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 .maxlen = sizeof(int),
2703 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002704 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 },
2706 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002708 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 .maxlen = sizeof(int),
2710 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002711 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 },
2713 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002715 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .maxlen = sizeof(int),
2717 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002718 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 },
2720 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002722 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 .maxlen = sizeof(int),
2724 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002725 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 },
2727 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002729 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 .maxlen = sizeof(int),
2731 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002732 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 },
2734 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002736 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .maxlen = sizeof(int),
2738 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002739 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 },
2741 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002743 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 .maxlen = sizeof(int),
2745 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002746 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002748 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749};
2750
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002751struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002752{
2753 struct ctl_table *table;
2754
2755 table = kmemdup(ipv6_route_table_template,
2756 sizeof(ipv6_route_table_template),
2757 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002758
2759 if (table) {
2760 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002761 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002762 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002763 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2764 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2765 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2766 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2767 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2768 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2769 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002770 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002771 }
2772
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002773 return table;
2774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775#endif
2776
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002777static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002778{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002779 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002780
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002781 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2782 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002783
Eric Dumazetfc66f952010-10-08 06:37:34 +00002784 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2785 goto out_ip6_dst_ops;
2786
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002787 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2788 sizeof(*net->ipv6.ip6_null_entry),
2789 GFP_KERNEL);
2790 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002791 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002792 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002793 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002794 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002795 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2796 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002797
2798#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2799 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2800 sizeof(*net->ipv6.ip6_prohibit_entry),
2801 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002802 if (!net->ipv6.ip6_prohibit_entry)
2803 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002804 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002805 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002806 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002807 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2808 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002809
2810 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2811 sizeof(*net->ipv6.ip6_blk_hole_entry),
2812 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002813 if (!net->ipv6.ip6_blk_hole_entry)
2814 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002815 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002816 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002817 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002818 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2819 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002820#endif
2821
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002822 net->ipv6.sysctl.flush_delay = 0;
2823 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2824 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2825 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2826 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2827 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2828 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2829 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2830
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002831#ifdef CONFIG_PROC_FS
2832 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2833 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2834#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002835 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2836
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002837 ret = 0;
2838out:
2839 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002840
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002841#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2842out_ip6_prohibit_entry:
2843 kfree(net->ipv6.ip6_prohibit_entry);
2844out_ip6_null_entry:
2845 kfree(net->ipv6.ip6_null_entry);
2846#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002847out_ip6_dst_entries:
2848 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002849out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002850 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002851}
2852
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002853static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002854{
2855#ifdef CONFIG_PROC_FS
2856 proc_net_remove(net, "ipv6_route");
2857 proc_net_remove(net, "rt6_stats");
2858#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002859 kfree(net->ipv6.ip6_null_entry);
2860#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2861 kfree(net->ipv6.ip6_prohibit_entry);
2862 kfree(net->ipv6.ip6_blk_hole_entry);
2863#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002864 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002865}
2866
2867static struct pernet_operations ip6_route_net_ops = {
2868 .init = ip6_route_net_init,
2869 .exit = ip6_route_net_exit,
2870};
2871
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002872static struct notifier_block ip6_route_dev_notifier = {
2873 .notifier_call = ip6_route_dev_notify,
2874 .priority = 0,
2875};
2876
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002877int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002879 int ret;
2880
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002881 ret = -ENOMEM;
2882 ip6_dst_ops_template.kmem_cachep =
2883 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2884 SLAB_HWCACHE_ALIGN, NULL);
2885 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002886 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002887
Eric Dumazetfc66f952010-10-08 06:37:34 +00002888 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002889 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002890 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002891
Eric Dumazetfc66f952010-10-08 06:37:34 +00002892 ret = register_pernet_subsys(&ip6_route_net_ops);
2893 if (ret)
2894 goto out_dst_entries;
2895
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002896 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2897
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002898 /* Registering of the loopback is done before this portion of code,
2899 * the loopback reference in rt6_info will not be taken, do it
2900 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002901 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002902 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2903 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002904 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002905 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002906 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002907 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2908 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002909 ret = fib6_init();
2910 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002911 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002912
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002913 ret = xfrm6_init();
2914 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002915 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002916
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002917 ret = fib6_rules_init();
2918 if (ret)
2919 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002920
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002921 ret = -ENOBUFS;
2922 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2923 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2924 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2925 goto fib6_rules_init;
2926
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002927 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002928 if (ret)
2929 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002930
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002931out:
2932 return ret;
2933
2934fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002935 fib6_rules_cleanup();
2936xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002937 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002938out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002939 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002940out_register_subsys:
2941 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002942out_dst_entries:
2943 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002944out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002945 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
2949void ip6_route_cleanup(void)
2950{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002951 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002952 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002955 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002956 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002957 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}