blob: 19a77d0e03080cd3c185a4a7593fd5fefda2896e [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 Harrison09640e62009-02-01 00:45:17 -0800132 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .gc = ip6_dst_gc,
134 .gc_thresh = 1024,
135 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800136 .default_advmss = ip6_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800137 .default_mtu = ip6_default_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800138 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .destroy = ip6_dst_destroy,
140 .ifdown = ip6_dst_ifdown,
141 .negative_advice = ip6_negative_advice,
142 .link_failure = ip6_link_failure,
143 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700144 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Roland Dreierec831ea2011-01-31 13:16:00 -0800147static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
148{
149 return 0;
150}
151
David S. Miller14e50e52007-05-24 18:17:54 -0700152static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
153{
154}
155
Held Bernhard0972ddb2011-04-24 22:07:32 +0000156static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
157 unsigned long old)
158{
159 return NULL;
160}
161
David S. Miller14e50e52007-05-24 18:17:54 -0700162static struct dst_ops ip6_dst_blackhole_ops = {
163 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800164 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700165 .destroy = ip6_dst_destroy,
166 .check = ip6_dst_check,
Roland Dreierec831ea2011-01-31 13:16:00 -0800167 .default_mtu = ip6_blackhole_default_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800168 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700169 .update_pmtu = ip6_rt_blackhole_update_pmtu,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000170 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Miller14e50e52007-05-24 18:17:54 -0700171};
172
David S. Miller62fa8a82011-01-26 20:51:05 -0800173static const u32 ip6_template_metrics[RTAX_MAX] = {
174 [RTAX_HOPLIMIT - 1] = 255,
175};
176
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800177static struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700178 .dst = {
179 .__refcnt = ATOMIC_INIT(1),
180 .__use = 1,
181 .obsolete = -1,
182 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700183 .input = ip6_pkt_discard,
184 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 },
186 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700187 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .rt6i_metric = ~(u32) 0,
189 .rt6i_ref = ATOMIC_INIT(1),
190};
191
Thomas Graf101367c2006-08-04 03:39:02 -0700192#ifdef CONFIG_IPV6_MULTIPLE_TABLES
193
David S. Miller6723ab52006-10-18 21:20:57 -0700194static int ip6_pkt_prohibit(struct sk_buff *skb);
195static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700196
Adrian Bunk280a34c2008-04-21 02:29:32 -0700197static struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700198 .dst = {
199 .__refcnt = ATOMIC_INIT(1),
200 .__use = 1,
201 .obsolete = -1,
202 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700203 .input = ip6_pkt_prohibit,
204 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700205 },
206 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700207 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700208 .rt6i_metric = ~(u32) 0,
209 .rt6i_ref = ATOMIC_INIT(1),
210};
211
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800212static struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700213 .dst = {
214 .__refcnt = ATOMIC_INIT(1),
215 .__use = 1,
216 .obsolete = -1,
217 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700218 .input = dst_discard,
219 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700220 },
221 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700222 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700223 .rt6i_metric = ~(u32) 0,
224 .rt6i_ref = ATOMIC_INIT(1),
225};
226
227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* allocate dst with ip6_dst_ops */
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800230static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
David S. Miller3c7bd1a2011-02-16 14:08:44 -0800232 return (struct rt6_info *)dst_alloc(ops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static void ip6_dst_destroy(struct dst_entry *dst)
236{
237 struct rt6_info *rt = (struct rt6_info *)dst;
238 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800239 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (idev != NULL) {
242 rt->rt6i_idev = NULL;
243 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900244 }
David S. Millerb3419362010-11-30 12:27:11 -0800245 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800246 rt->rt6i_peer = NULL;
247 inet_putpeer(peer);
248 }
249}
250
David S. Miller6431cbc2011-02-07 20:38:06 -0800251static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
252
253static u32 rt6_peer_genid(void)
254{
255 return atomic_read(&__rt6_peer_genid);
256}
257
David S. Millerb3419362010-11-30 12:27:11 -0800258void rt6_bind_peer(struct rt6_info *rt, int create)
259{
260 struct inet_peer *peer;
261
David S. Millerb3419362010-11-30 12:27:11 -0800262 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
263 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
264 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800265 else
266 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
270 int how)
271{
272 struct rt6_info *rt = (struct rt6_info *)dst;
273 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800274 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900275 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800277 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
278 struct inet6_dev *loopback_idev =
279 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (loopback_idev != NULL) {
281 rt->rt6i_idev = loopback_idev;
282 in6_dev_put(idev);
283 }
284 }
285}
286
287static __inline__ int rt6_check_expired(const struct rt6_info *rt)
288{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000289 return (rt->rt6i_flags & RTF_EXPIRES) &&
290 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000293static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700294{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000295 return ipv6_addr_type(daddr) &
296 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700300 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
302
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800303static inline struct rt6_info *rt6_device_match(struct net *net,
304 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000305 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700307 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct rt6_info *local = NULL;
310 struct rt6_info *sprt;
311
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900312 if (!oif && ipv6_addr_any(saddr))
313 goto out;
314
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900316 struct net_device *dev = sprt->rt6i_dev;
317
318 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (dev->ifindex == oif)
320 return sprt;
321 if (dev->flags & IFF_LOOPBACK) {
322 if (sprt->rt6i_idev == NULL ||
323 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700324 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900326 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 local->rt6i_idev->dev->ifindex == oif))
328 continue;
329 }
330 local = sprt;
331 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900332 } else {
333 if (ipv6_chk_addr(net, saddr, dev,
334 flags & RT6_LOOKUP_F_IFACE))
335 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900339 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (local)
341 return local;
342
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700343 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800344 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900346out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return rt;
348}
349
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800350#ifdef CONFIG_IPV6_ROUTER_PREF
351static void rt6_probe(struct rt6_info *rt)
352{
353 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
354 /*
355 * Okay, this does not seem to be appropriate
356 * for now, however, we need to check if it
357 * is really so; aka Router Reachability Probing.
358 *
359 * Router Reachability Probe MUST be rate-limited
360 * to no more than one per minute.
361 */
362 if (!neigh || (neigh->nud_state & NUD_VALID))
363 return;
364 read_lock_bh(&neigh->lock);
365 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800366 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800367 struct in6_addr mcaddr;
368 struct in6_addr *target;
369
370 neigh->updated = jiffies;
371 read_unlock_bh(&neigh->lock);
372
373 target = (struct in6_addr *)&neigh->primary_key;
374 addrconf_addr_solict_mult(target, &mcaddr);
375 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
376 } else
377 read_unlock_bh(&neigh->lock);
378}
379#else
380static inline void rt6_probe(struct rt6_info *rt)
381{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800382}
383#endif
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800386 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700388static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800390 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700391 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800392 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700393 if ((dev->flags & IFF_LOOPBACK) &&
394 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
395 return 1;
396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Dave Jonesb6f99a22007-03-22 12:27:49 -0700399static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800401 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800402 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700403 if (rt->rt6i_flags & RTF_NONEXTHOP ||
404 !(rt->rt6i_flags & RTF_GATEWAY))
405 m = 1;
406 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800407 read_lock_bh(&neigh->lock);
408 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700409 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800410#ifdef CONFIG_IPV6_ROUTER_PREF
411 else if (neigh->nud_state & NUD_FAILED)
412 m = 0;
413#endif
414 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800415 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800416 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800417 } else
418 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800419 return m;
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800422static int rt6_score_route(struct rt6_info *rt, int oif,
423 int strict)
424{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700425 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900426
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700427 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700428 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800429 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800430#ifdef CONFIG_IPV6_ROUTER_PREF
431 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
432#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700433 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800434 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800435 return -1;
436 return m;
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David S. Millerf11e6652007-03-24 20:36:25 -0700439static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
440 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800441{
David S. Millerf11e6652007-03-24 20:36:25 -0700442 int m;
443
444 if (rt6_check_expired(rt))
445 goto out;
446
447 m = rt6_score_route(rt, oif, strict);
448 if (m < 0)
449 goto out;
450
451 if (m > *mpri) {
452 if (strict & RT6_LOOKUP_F_REACHABLE)
453 rt6_probe(match);
454 *mpri = m;
455 match = rt;
456 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
457 rt6_probe(rt);
458 }
459
460out:
461 return match;
462}
463
464static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
465 struct rt6_info *rr_head,
466 u32 metric, int oif, int strict)
467{
468 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800469 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David S. Millerf11e6652007-03-24 20:36:25 -0700471 match = NULL;
472 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700473 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700474 match = find_match(rt, oif, strict, &mpri, match);
475 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700476 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700477 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800478
David S. Millerf11e6652007-03-24 20:36:25 -0700479 return match;
480}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800481
David S. Millerf11e6652007-03-24 20:36:25 -0700482static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
483{
484 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800485 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David S. Millerf11e6652007-03-24 20:36:25 -0700487 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800488 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David S. Millerf11e6652007-03-24 20:36:25 -0700490 rt0 = fn->rr_ptr;
491 if (!rt0)
492 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David S. Millerf11e6652007-03-24 20:36:25 -0700494 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800496 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700497 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700498 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700499
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800500 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700501 if (!next || next->rt6i_metric != rt0->rt6i_metric)
502 next = fn->leaf;
503
504 if (next != rt0)
505 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
David S. Millerf11e6652007-03-24 20:36:25 -0700508 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800509 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900511 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000512 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800515#ifdef CONFIG_IPV6_ROUTE_INFO
516int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000517 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800518{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900519 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800520 struct route_info *rinfo = (struct route_info *) opt;
521 struct in6_addr prefix_buf, *prefix;
522 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900523 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800524 struct rt6_info *rt;
525
526 if (len < sizeof(struct route_info)) {
527 return -EINVAL;
528 }
529
530 /* Sanity check for prefix_len and length */
531 if (rinfo->length > 3) {
532 return -EINVAL;
533 } else if (rinfo->prefix_len > 128) {
534 return -EINVAL;
535 } else if (rinfo->prefix_len > 64) {
536 if (rinfo->length < 2) {
537 return -EINVAL;
538 }
539 } else if (rinfo->prefix_len > 0) {
540 if (rinfo->length < 1) {
541 return -EINVAL;
542 }
543 }
544
545 pref = rinfo->route_pref;
546 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000547 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800548
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900549 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800550
551 if (rinfo->length == 3)
552 prefix = (struct in6_addr *)rinfo->prefix;
553 else {
554 /* this function is safe */
555 ipv6_addr_prefix(&prefix_buf,
556 (struct in6_addr *)rinfo->prefix,
557 rinfo->prefix_len);
558 prefix = &prefix_buf;
559 }
560
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800561 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
562 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800563
564 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700565 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800566 rt = NULL;
567 }
568
569 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800570 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800571 pref);
572 else if (rt)
573 rt->rt6i_flags = RTF_ROUTEINFO |
574 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
575
576 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900577 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800578 rt->rt6i_flags &= ~RTF_EXPIRES;
579 } else {
580 rt->rt6i_expires = jiffies + HZ * lifetime;
581 rt->rt6i_flags |= RTF_EXPIRES;
582 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700583 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800584 }
585 return 0;
586}
587#endif
588
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800589#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700590do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800591 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700592 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700593 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700594 if (fn->fn_flags & RTN_TL_ROOT) \
595 goto out; \
596 pn = fn->parent; \
597 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800598 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700599 else \
600 fn = pn; \
601 if (fn->fn_flags & RTN_RTINFO) \
602 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700603 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700604 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700605} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700606
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800607static struct rt6_info *ip6_pol_route_lookup(struct net *net,
608 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500609 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct fib6_node *fn;
612 struct rt6_info *rt;
613
Thomas Grafc71099a2006-08-04 23:20:06 -0700614 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500615 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700616restart:
617 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500618 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
619 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700620out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700621 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700622 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700623 return rt;
624
625}
626
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900627struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
628 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700629{
David S. Miller4c9483b2011-03-12 16:22:43 -0500630 struct flowi6 fl6 = {
631 .flowi6_oif = oif,
632 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700633 };
634 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700635 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700636
Thomas Grafadaa70b2006-10-13 15:01:03 -0700637 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500638 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700639 flags |= RT6_LOOKUP_F_HAS_SADDR;
640 }
641
David S. Miller4c9483b2011-03-12 16:22:43 -0500642 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700643 if (dst->error == 0)
644 return (struct rt6_info *) dst;
645
646 dst_release(dst);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return NULL;
649}
650
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900651EXPORT_SYMBOL(rt6_lookup);
652
Thomas Grafc71099a2006-08-04 23:20:06 -0700653/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 It takes new route entry, the addition fails by any reason the
655 route is freed. In any case, if caller does not hold it, it may
656 be destroyed.
657 */
658
Thomas Graf86872cb2006-08-22 00:01:08 -0700659static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700662 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Thomas Grafc71099a2006-08-04 23:20:06 -0700664 table = rt->rt6i_table;
665 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700666 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700667 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return err;
670}
671
Thomas Graf40e22e82006-08-22 00:00:45 -0700672int ip6_ins_rt(struct rt6_info *rt)
673{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800674 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900675 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800676 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800677 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700678}
679
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000680static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
681 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct rt6_info *rt;
684
685 /*
686 * Clone the route.
687 */
688
689 rt = ip6_rt_copy(ort);
690
691 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800692 struct neighbour *neigh;
693 int attempts = !in_softirq();
694
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900695 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
696 if (rt->rt6i_dst.plen != 128 &&
697 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
698 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900702 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rt->rt6i_dst.plen = 128;
704 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700705 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#ifdef CONFIG_IPV6_SUBTREES
708 if (rt->rt6i_src.plen && saddr) {
709 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
710 rt->rt6i_src.plen = 128;
711 }
712#endif
713
David S. Miller14deae42009-01-04 16:04:39 -0800714 retry:
715 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
716 if (IS_ERR(neigh)) {
717 struct net *net = dev_net(rt->rt6i_dev);
718 int saved_rt_min_interval =
719 net->ipv6.sysctl.ip6_rt_gc_min_interval;
720 int saved_rt_elasticity =
721 net->ipv6.sysctl.ip6_rt_gc_elasticity;
722
723 if (attempts-- > 0) {
724 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
725 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
726
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000727 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800728
729 net->ipv6.sysctl.ip6_rt_gc_elasticity =
730 saved_rt_elasticity;
731 net->ipv6.sysctl.ip6_rt_gc_min_interval =
732 saved_rt_min_interval;
733 goto retry;
734 }
735
736 if (net_ratelimit())
737 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700738 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700739 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800740 return NULL;
741 }
742 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800746 return rt;
747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000749static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800750{
751 struct rt6_info *rt = ip6_rt_copy(ort);
752 if (rt) {
753 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
754 rt->rt6i_dst.plen = 128;
755 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700756 rt->dst.flags |= DST_HOST;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800757 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
758 }
759 return rt;
760}
761
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800762static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500763 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800766 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700767 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800769 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700770 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700772 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700775 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800777restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500778 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700781 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800782
David S. Miller4c9483b2011-03-12 16:22:43 -0500783 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800784 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800785 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef042006-03-20 17:01:24 -0800786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Changli Gaod8d1f302010-06-10 23:31:35 -0700788 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700789 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800790
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800791 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500792 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800793 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500794 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800795 else
796 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800797
Changli Gaod8d1f302010-06-10 23:31:35 -0700798 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800799 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800800
Changli Gaod8d1f302010-06-10 23:31:35 -0700801 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800802 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700803 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800804 if (!err)
805 goto out2;
806 }
807
808 if (--attempts <= 0)
809 goto out2;
810
811 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700812 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800813 * released someone could insert this route. Relookup.
814 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700815 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800816 goto relookup;
817
818out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800819 if (reachable) {
820 reachable = 0;
821 goto restart_2;
822 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700823 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700824 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700826 rt->dst.lastuse = jiffies;
827 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700828
829 return rt;
830}
831
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800832static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500833 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700834{
David S. Miller4c9483b2011-03-12 16:22:43 -0500835 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700836}
837
Thomas Grafc71099a2006-08-04 23:20:06 -0700838void ip6_route_input(struct sk_buff *skb)
839{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000840 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900841 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700842 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500843 struct flowi6 fl6 = {
844 .flowi6_iif = skb->dev->ifindex,
845 .daddr = iph->daddr,
846 .saddr = iph->saddr,
847 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
848 .flowi6_mark = skb->mark,
849 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700850 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700851
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800852 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700853 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700854
David S. Miller4c9483b2011-03-12 16:22:43 -0500855 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700856}
857
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800858static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500859 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700860{
David S. Miller4c9483b2011-03-12 16:22:43 -0500861 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700862}
863
Florian Westphal9c7a4f92011-03-22 19:17:36 -0700864struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500865 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700866{
867 int flags = 0;
868
David S. Miller4c9483b2011-03-12 16:22:43 -0500869 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700870 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700871
David S. Miller4c9483b2011-03-12 16:22:43 -0500872 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700873 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000874 else if (sk)
875 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700876
David S. Miller4c9483b2011-03-12 16:22:43 -0500877 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900880EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
David S. Miller2774c132011-03-01 14:59:04 -0800882struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700883{
David S. Miller69ead7a2011-03-01 14:45:33 -0800884 struct rt6_info *rt = dst_alloc(&ip6_dst_blackhole_ops, 1);
885 struct rt6_info *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700886 struct dst_entry *new = NULL;
887
888 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700890
David S. Miller14e50e52007-05-24 18:17:54 -0700891 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800892 new->input = dst_discard;
893 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700894
David S. Millerdefb3512010-12-08 21:16:57 -0800895 dst_copy_metrics(new, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -0700896 new->dev = ort->dst.dev;
David S. Miller14e50e52007-05-24 18:17:54 -0700897 if (new->dev)
898 dev_hold(new->dev);
899 rt->rt6i_idev = ort->rt6i_idev;
900 if (rt->rt6i_idev)
901 in6_dev_hold(rt->rt6i_idev);
902 rt->rt6i_expires = 0;
903
904 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
905 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
906 rt->rt6i_metric = 0;
907
908 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
909#ifdef CONFIG_IPV6_SUBTREES
910 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
911#endif
912
913 dst_free(new);
914 }
915
David S. Miller69ead7a2011-03-01 14:45:33 -0800916 dst_release(dst_orig);
917 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700918}
David S. Miller14e50e52007-05-24 18:17:54 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * Destination cache support functions
922 */
923
924static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
925{
926 struct rt6_info *rt;
927
928 rt = (struct rt6_info *) dst;
929
David S. Miller6431cbc2011-02-07 20:38:06 -0800930 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
931 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
932 if (!rt->rt6i_peer)
933 rt6_bind_peer(rt, 0);
934 rt->rt6i_peer_genid = rt6_peer_genid();
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return NULL;
939}
940
941static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
942{
943 struct rt6_info *rt = (struct rt6_info *) dst;
944
945 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000946 if (rt->rt6i_flags & RTF_CACHE) {
947 if (rt6_check_expired(rt)) {
948 ip6_del_rt(rt);
949 dst = NULL;
950 }
951 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000953 dst = NULL;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000956 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959static void ip6_link_failure(struct sk_buff *skb)
960{
961 struct rt6_info *rt;
962
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000963 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Eric Dumazetadf30902009-06-02 05:19:30 +0000965 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (rt) {
967 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700968 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 rt->rt6i_flags |= RTF_EXPIRES;
970 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
971 rt->rt6i_node->fn_sernum = -1;
972 }
973}
974
975static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
976{
977 struct rt6_info *rt6 = (struct rt6_info*)dst;
978
979 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
980 rt6->rt6i_flags |= RTF_MODIFIED;
981 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800982 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800984 features |= RTAX_FEATURE_ALLFRAG;
985 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
David S. Millerdefb3512010-12-08 21:16:57 -0800987 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
David S. Miller0dbaee32010-12-13 12:52:14 -0800991static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
David S. Miller0dbaee32010-12-13 12:52:14 -0800993 struct net_device *dev = dst->dev;
994 unsigned int mtu = dst_mtu(dst);
995 struct net *net = dev_net(dev);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
998
Daniel Lezcano55786892008-03-04 13:47:47 -0800999 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1000 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001003 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1004 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1005 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * rely only on pmtu discovery"
1007 */
1008 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1009 mtu = IPV6_MAXPLEN;
1010 return mtu;
1011}
1012
David S. Millerd33e4552010-12-14 13:01:14 -08001013static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1014{
1015 unsigned int mtu = IPV6_MIN_MTU;
1016 struct inet6_dev *idev;
1017
1018 rcu_read_lock();
1019 idev = __in6_dev_get(dst->dev);
1020 if (idev)
1021 mtu = idev->cnf.mtu6;
1022 rcu_read_unlock();
1023
1024 return mtu;
1025}
1026
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001027static struct dst_entry *icmp6_dst_gc_list;
1028static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001029
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001030struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001032 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct rt6_info *rt;
1035 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001036 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (unlikely(idev == NULL))
1039 return NULL;
1040
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001041 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (unlikely(rt == NULL)) {
1043 in6_dev_put(idev);
1044 goto out;
1045 }
1046
1047 dev_hold(dev);
1048 if (neigh)
1049 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001050 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001052 if (IS_ERR(neigh))
1053 neigh = NULL;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 rt->rt6i_dev = dev;
1057 rt->rt6i_idev = idev;
1058 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001059 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001060 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001061 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001064 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001065 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 : 0;
1067 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1068 rt->rt6i_dst.plen = 128;
1069#endif
1070
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001071 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001072 rt->dst.next = icmp6_dst_gc_list;
1073 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001074 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Daniel Lezcano55786892008-03-04 13:47:47 -08001076 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001079 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001082int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001084 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001085 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001087 spin_lock_bh(&icmp6_dst_lock);
1088 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 while ((dst = *pprev) != NULL) {
1091 if (!atomic_read(&dst->__refcnt)) {
1092 *pprev = dst->next;
1093 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 } else {
1095 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001096 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 }
1099
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001100 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001101
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001102 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
David S. Miller1e493d12008-09-10 17:27:15 -07001105static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1106 void *arg)
1107{
1108 struct dst_entry *dst, **pprev;
1109
1110 spin_lock_bh(&icmp6_dst_lock);
1111 pprev = &icmp6_dst_gc_list;
1112 while ((dst = *pprev) != NULL) {
1113 struct rt6_info *rt = (struct rt6_info *) dst;
1114 if (func(rt, arg)) {
1115 *pprev = dst->next;
1116 dst_free(dst);
1117 } else {
1118 pprev = &dst->next;
1119 }
1120 }
1121 spin_unlock_bh(&icmp6_dst_lock);
1122}
1123
Daniel Lezcano569d3642008-01-18 03:56:57 -08001124static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001127 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001128 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1129 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1130 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1131 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1132 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001133 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Eric Dumazetfc66f952010-10-08 06:37:34 +00001135 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001136 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001137 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto out;
1139
Benjamin Thery6891a342008-03-04 13:49:47 -08001140 net->ipv6.ip6_rt_gc_expire++;
1141 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1142 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001143 entries = dst_entries_get_slow(ops);
1144 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001145 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001147 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001148 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151/* Clean host part of a prefix. Not necessary in radix tree,
1152 but results in cleaner routing tables.
1153
1154 Remove it only when all the things will work!
1155 */
1156
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001157int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
David S. Miller5170ae82010-12-12 21:35:57 -08001159 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001160 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001161 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001162 struct inet6_dev *idev;
1163
1164 rcu_read_lock();
1165 idev = __in6_dev_get(dev);
1166 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001167 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001168 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001169 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001170 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 return hoplimit;
1173}
David S. Millerabbf46a2010-12-12 21:14:46 -08001174EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176/*
1177 *
1178 */
1179
Thomas Graf86872cb2006-08-22 00:01:08 -07001180int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001183 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct rt6_info *rt = NULL;
1185 struct net_device *dev = NULL;
1186 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001187 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int addr_type;
1189
Thomas Graf86872cb2006-08-22 00:01:08 -07001190 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EINVAL;
1192#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001193 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -EINVAL;
1195#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001196 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001198 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!dev)
1200 goto out;
1201 idev = in6_dev_get(dev);
1202 if (!idev)
1203 goto out;
1204 }
1205
Thomas Graf86872cb2006-08-22 00:01:08 -07001206 if (cfg->fc_metric == 0)
1207 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Daniel Lezcano55786892008-03-04 13:47:47 -08001209 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001210 if (table == NULL) {
1211 err = -ENOBUFS;
1212 goto out;
1213 }
1214
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001215 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (rt == NULL) {
1218 err = -ENOMEM;
1219 goto out;
1220 }
1221
Changli Gaod8d1f302010-06-10 23:31:35 -07001222 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001223 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1224 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1225 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Thomas Graf86872cb2006-08-22 00:01:08 -07001227 if (cfg->fc_protocol == RTPROT_UNSPEC)
1228 cfg->fc_protocol = RTPROT_BOOT;
1229 rt->rt6i_protocol = cfg->fc_protocol;
1230
1231 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001234 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001235 else if (cfg->fc_flags & RTF_LOCAL)
1236 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001238 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Changli Gaod8d1f302010-06-10 23:31:35 -07001240 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Thomas Graf86872cb2006-08-22 00:01:08 -07001242 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1243 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001245 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001248 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1249 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#endif
1251
Thomas Graf86872cb2006-08-22 00:01:08 -07001252 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* We cannot add true routes via loopback here,
1255 they would result in kernel looping; promote them to reject routes
1256 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001257 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001258 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1259 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001261 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (dev) {
1263 dev_put(dev);
1264 in6_dev_put(idev);
1265 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001266 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dev_hold(dev);
1268 idev = in6_dev_get(dev);
1269 if (!idev) {
1270 err = -ENODEV;
1271 goto out;
1272 }
1273 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001274 rt->dst.output = ip6_pkt_discard_out;
1275 rt->dst.input = ip6_pkt_discard;
1276 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1278 goto install_route;
1279 }
1280
Thomas Graf86872cb2006-08-22 00:01:08 -07001281 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001282 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int gwa_type;
1284
Thomas Graf86872cb2006-08-22 00:01:08 -07001285 gw_addr = &cfg->fc_gateway;
1286 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 gwa_type = ipv6_addr_type(gw_addr);
1288
1289 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1290 struct rt6_info *grt;
1291
1292 /* IPv6 strictly inhibits using not link-local
1293 addresses as nexthop address.
1294 Otherwise, router will not able to send redirects.
1295 It is very good, but in some (rare!) circumstances
1296 (SIT, PtP, NBMA NOARP links) it is handy to allow
1297 some exceptions. --ANK
1298 */
1299 err = -EINVAL;
1300 if (!(gwa_type&IPV6_ADDR_UNICAST))
1301 goto out;
1302
Daniel Lezcano55786892008-03-04 13:47:47 -08001303 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 err = -EHOSTUNREACH;
1306 if (grt == NULL)
1307 goto out;
1308 if (dev) {
1309 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001310 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto out;
1312 }
1313 } else {
1314 dev = grt->rt6i_dev;
1315 idev = grt->rt6i_idev;
1316 dev_hold(dev);
1317 in6_dev_hold(grt->rt6i_idev);
1318 }
1319 if (!(grt->rt6i_flags&RTF_GATEWAY))
1320 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001321 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (err)
1324 goto out;
1325 }
1326 err = -EINVAL;
1327 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1328 goto out;
1329 }
1330
1331 err = -ENODEV;
1332 if (dev == NULL)
1333 goto out;
1334
Daniel Walterc3968a82011-04-13 21:10:57 +00001335 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1336 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1337 err = -EINVAL;
1338 goto out;
1339 }
1340 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1341 rt->rt6i_prefsrc.plen = 128;
1342 } else
1343 rt->rt6i_prefsrc.plen = 0;
1344
Thomas Graf86872cb2006-08-22 00:01:08 -07001345 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1347 if (IS_ERR(rt->rt6i_nexthop)) {
1348 err = PTR_ERR(rt->rt6i_nexthop);
1349 rt->rt6i_nexthop = NULL;
1350 goto out;
1351 }
1352 }
1353
Thomas Graf86872cb2006-08-22 00:01:08 -07001354 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001357 if (cfg->fc_mx) {
1358 struct nlattr *nla;
1359 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Thomas Graf86872cb2006-08-22 00:01:08 -07001361 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001362 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001363
1364 if (type) {
1365 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 err = -EINVAL;
1367 goto out;
1368 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001369
David S. Millerdefb3512010-12-08 21:16:57 -08001370 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 }
1374
Changli Gaod8d1f302010-06-10 23:31:35 -07001375 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001377 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001378
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001379 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001380
Thomas Graf86872cb2006-08-22 00:01:08 -07001381 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383out:
1384 if (dev)
1385 dev_put(dev);
1386 if (idev)
1387 in6_dev_put(idev);
1388 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001389 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
1391}
1392
Thomas Graf86872cb2006-08-22 00:01:08 -07001393static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001396 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001397 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001399 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001400 return -ENOENT;
1401
Thomas Grafc71099a2006-08-04 23:20:06 -07001402 table = rt->rt6i_table;
1403 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Thomas Graf86872cb2006-08-22 00:01:08 -07001405 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001406 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Thomas Grafc71099a2006-08-04 23:20:06 -07001408 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 return err;
1411}
1412
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001413int ip6_del_rt(struct rt6_info *rt)
1414{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001415 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001416 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001417 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001418 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001419}
1420
Thomas Graf86872cb2006-08-22 00:01:08 -07001421static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Thomas Grafc71099a2006-08-04 23:20:06 -07001423 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct fib6_node *fn;
1425 struct rt6_info *rt;
1426 int err = -ESRCH;
1427
Daniel Lezcano55786892008-03-04 13:47:47 -08001428 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001429 if (table == NULL)
1430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Thomas Grafc71099a2006-08-04 23:20:06 -07001432 read_lock_bh(&table->tb6_lock);
1433
1434 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001435 &cfg->fc_dst, cfg->fc_dst_len,
1436 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001439 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001440 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001442 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001444 if (cfg->fc_flags & RTF_GATEWAY &&
1445 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001447 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001449 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001450 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Thomas Graf86872cb2006-08-22 00:01:08 -07001452 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001455 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 return err;
1458}
1459
1460/*
1461 * Handle redirects
1462 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001463struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001464 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001465 struct in6_addr gateway;
1466};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001468static struct rt6_info *__ip6_route_redirect(struct net *net,
1469 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001470 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001471 int flags)
1472{
David S. Miller4c9483b2011-03-12 16:22:43 -05001473 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001474 struct rt6_info *rt;
1475 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001478 * Get the "current" route for this destination and
1479 * check if the redirect has come from approriate router.
1480 *
1481 * RFC 2461 specifies that redirects should only be
1482 * accepted if they come from the nexthop to the target.
1483 * Due to the way the routes are chosen, this notion
1484 * is a bit fuzzy and one might need to check all possible
1485 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Thomas Grafc71099a2006-08-04 23:20:06 -07001488 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001489 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001490restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001491 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001492 /*
1493 * Current route is on-link; redirect is always invalid.
1494 *
1495 * Seems, previous statement is not true. It could
1496 * be node, which looks for us as on-link (f.e. proxy ndisc)
1497 * But then router serving it might decide, that we should
1498 * know truth 8)8) --ANK (980726).
1499 */
1500 if (rt6_check_expired(rt))
1501 continue;
1502 if (!(rt->rt6i_flags & RTF_GATEWAY))
1503 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001504 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001505 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001506 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001507 continue;
1508 break;
1509 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001510
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001511 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001512 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001513 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001514out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001515 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001516
1517 read_unlock_bh(&table->tb6_lock);
1518
1519 return rt;
1520};
1521
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001522static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1523 const struct in6_addr *src,
1524 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001525 struct net_device *dev)
1526{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001527 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001528 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001529 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001530 .fl6 = {
1531 .flowi6_oif = dev->ifindex,
1532 .daddr = *dest,
1533 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001534 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001535 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001536
Brian Haley86c36ce2009-10-07 13:58:01 -07001537 ipv6_addr_copy(&rdfl.gateway, gateway);
1538
Thomas Grafadaa70b2006-10-13 15:01:03 -07001539 if (rt6_need_strict(dest))
1540 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001541
David S. Miller4c9483b2011-03-12 16:22:43 -05001542 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001543 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001544}
1545
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001546void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1547 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001548 struct neighbour *neigh, u8 *lladdr, int on_link)
1549{
1550 struct rt6_info *rt, *nrt = NULL;
1551 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001552 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001553
1554 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1555
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001556 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (net_ratelimit())
1558 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1559 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * We have finally decided to accept it.
1565 */
1566
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001567 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1569 NEIGH_UPDATE_F_OVERRIDE|
1570 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1571 NEIGH_UPDATE_F_ISROUTER))
1572 );
1573
1574 /*
1575 * Redirect received -> path was valid.
1576 * Look, redirects are sent only in response to data packets,
1577 * so that this nexthop apparently is reachable. --ANK
1578 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001579 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001582 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto out;
1584
1585 nrt = ip6_rt_copy(rt);
1586 if (nrt == NULL)
1587 goto out;
1588
1589 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1590 if (on_link)
1591 nrt->rt6i_flags &= ~RTF_GATEWAY;
1592
1593 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1594 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001595 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1598 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Thomas Graf40e22e82006-08-22 00:00:45 -07001600 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto out;
1602
Changli Gaod8d1f302010-06-10 23:31:35 -07001603 netevent.old = &rt->dst;
1604 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001605 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001608 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return;
1610 }
1611
1612out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001613 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
1617 * Handle ICMP "packet too big" messages
1618 * i.e. Path MTU discovery
1619 */
1620
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001621static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001622 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct rt6_info *rt, *nrt;
1625 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001626again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001627 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (rt == NULL)
1629 return;
1630
Andrey Vagind3052b52010-12-11 15:20:11 +00001631 if (rt6_check_expired(rt)) {
1632 ip6_del_rt(rt);
1633 goto again;
1634 }
1635
Changli Gaod8d1f302010-06-10 23:31:35 -07001636 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto out;
1638
1639 if (pmtu < IPV6_MIN_MTU) {
1640 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001641 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * MTU (1280) and a fragment header should always be included
1643 * after a node receiving Too Big message reporting PMTU is
1644 * less than the IPv6 Minimum Link MTU.
1645 */
1646 pmtu = IPV6_MIN_MTU;
1647 allfrag = 1;
1648 }
1649
1650 /* New mtu received -> path was valid.
1651 They are sent only in response to data packets,
1652 so that this nexthop apparently is reachable. --ANK
1653 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001654 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /* Host route. If it is static, it would be better
1657 not to override it, but add new one, so that
1658 when cache entry will expire old pmtu
1659 would return automatically.
1660 */
1661 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001662 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1663 if (allfrag) {
1664 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1665 features |= RTAX_FEATURE_ALLFRAG;
1666 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1667 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001668 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1670 goto out;
1671 }
1672
1673 /* Network route.
1674 Two cases are possible:
1675 1. It is connected route. Action: COW
1676 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1677 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001678 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001679 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001680 else
1681 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001682
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001683 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001684 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1685 if (allfrag) {
1686 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1687 features |= RTAX_FEATURE_ALLFRAG;
1688 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1689 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001690
1691 /* According to RFC 1981, detecting PMTU increase shouldn't be
1692 * happened within 5 mins, the recommended timer is 10 mins.
1693 * Here this route expiration time is set to ip6_rt_mtu_expires
1694 * which is 10 mins. After 10 mins the decreased pmtu is expired
1695 * and detecting PMTU increase will be automatically happened.
1696 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001697 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001698 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1699
Thomas Graf40e22e82006-08-22 00:00:45 -07001700 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001703 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001706void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001707 struct net_device *dev, u32 pmtu)
1708{
1709 struct net *net = dev_net(dev);
1710
1711 /*
1712 * RFC 1981 states that a node "MUST reduce the size of the packets it
1713 * is sending along the path" that caused the Packet Too Big message.
1714 * Since it's not possible in the general case to determine which
1715 * interface was used to send the original packet, we update the MTU
1716 * on the interface that will be used to send future packets. We also
1717 * update the MTU on the interface that received the Packet Too Big in
1718 * case the original packet was forced out that interface with
1719 * SO_BINDTODEVICE or similar. This is the next best thing to the
1720 * correct behaviour, which would be to update the MTU on all
1721 * interfaces.
1722 */
1723 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1724 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727/*
1728 * Misc support functions
1729 */
1730
1731static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1732{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001733 struct net *net = dev_net(ort->rt6i_dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001734 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001737 rt->dst.input = ort->dst.input;
1738 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David S. Millerdefb3512010-12-08 21:16:57 -08001740 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001741 rt->dst.error = ort->dst.error;
1742 rt->dst.dev = ort->dst.dev;
1743 if (rt->dst.dev)
1744 dev_hold(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 rt->rt6i_idev = ort->rt6i_idev;
1746 if (rt->rt6i_idev)
1747 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001748 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rt->rt6i_expires = 0;
1750
1751 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1752 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1753 rt->rt6i_metric = 0;
1754
1755 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1756#ifdef CONFIG_IPV6_SUBTREES
1757 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1758#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001759 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 return rt;
1762}
1763
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001764#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001765static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001766 const struct in6_addr *prefix, int prefixlen,
1767 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001768{
1769 struct fib6_node *fn;
1770 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001771 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001772
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001773 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001774 if (table == NULL)
1775 return NULL;
1776
1777 write_lock_bh(&table->tb6_lock);
1778 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001779 if (!fn)
1780 goto out;
1781
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001783 if (rt->rt6i_dev->ifindex != ifindex)
1784 continue;
1785 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1786 continue;
1787 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1788 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001789 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001790 break;
1791 }
1792out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001793 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001794 return rt;
1795}
1796
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001797static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001798 const struct in6_addr *prefix, int prefixlen,
1799 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001800 unsigned pref)
1801{
Thomas Graf86872cb2006-08-22 00:01:08 -07001802 struct fib6_config cfg = {
1803 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001804 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 .fc_ifindex = ifindex,
1806 .fc_dst_len = prefixlen,
1807 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1808 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001809 .fc_nlinfo.pid = 0,
1810 .fc_nlinfo.nlh = NULL,
1811 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001812 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001813
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 ipv6_addr_copy(&cfg.fc_dst, prefix);
1815 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1816
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001817 /* We should treat it as a default route if prefix length is 0. */
1818 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001819 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001820
Thomas Graf86872cb2006-08-22 00:01:08 -07001821 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001822
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001823 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001824}
1825#endif
1826
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001827struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001830 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001832 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001833 if (table == NULL)
1834 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Thomas Grafc71099a2006-08-04 23:20:06 -07001836 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001837 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001839 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1841 break;
1842 }
1843 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001844 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001845 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return rt;
1847}
1848
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001849struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001850 struct net_device *dev,
1851 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Thomas Graf86872cb2006-08-22 00:01:08 -07001853 struct fib6_config cfg = {
1854 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001855 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001856 .fc_ifindex = dev->ifindex,
1857 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1858 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001859 .fc_nlinfo.pid = 0,
1860 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001861 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001862 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Thomas Graf86872cb2006-08-22 00:01:08 -07001864 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Thomas Graf86872cb2006-08-22 00:01:08 -07001866 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return rt6_get_dflt_router(gwaddr, dev);
1869}
1870
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001871void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001874 struct fib6_table *table;
1875
1876 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001877 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001878 if (table == NULL)
1879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001882 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001883 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001885 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001886 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001887 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto restart;
1889 }
1890 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001891 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Daniel Lezcano55786892008-03-04 13:47:47 -08001894static void rtmsg_to_fib6_config(struct net *net,
1895 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001896 struct fib6_config *cfg)
1897{
1898 memset(cfg, 0, sizeof(*cfg));
1899
1900 cfg->fc_table = RT6_TABLE_MAIN;
1901 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1902 cfg->fc_metric = rtmsg->rtmsg_metric;
1903 cfg->fc_expires = rtmsg->rtmsg_info;
1904 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1905 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1906 cfg->fc_flags = rtmsg->rtmsg_flags;
1907
Daniel Lezcano55786892008-03-04 13:47:47 -08001908 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001909
Thomas Graf86872cb2006-08-22 00:01:08 -07001910 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1911 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1912 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1913}
1914
Daniel Lezcano55786892008-03-04 13:47:47 -08001915int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Thomas Graf86872cb2006-08-22 00:01:08 -07001917 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct in6_rtmsg rtmsg;
1919 int err;
1920
1921 switch(cmd) {
1922 case SIOCADDRT: /* Add a route */
1923 case SIOCDELRT: /* Delete a route */
1924 if (!capable(CAP_NET_ADMIN))
1925 return -EPERM;
1926 err = copy_from_user(&rtmsg, arg,
1927 sizeof(struct in6_rtmsg));
1928 if (err)
1929 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001930
Daniel Lezcano55786892008-03-04 13:47:47 -08001931 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 rtnl_lock();
1934 switch (cmd) {
1935 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001936 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001939 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 default:
1942 err = -EINVAL;
1943 }
1944 rtnl_unlock();
1945
1946 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 return -EINVAL;
1950}
1951
1952/*
1953 * Drop the packet on the floor
1954 */
1955
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001956static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001958 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001959 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001960 switch (ipstats_mib_noroutes) {
1961 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001962 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001963 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001964 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1965 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001966 break;
1967 }
1968 /* FALLTHROUGH */
1969 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001970 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1971 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001972 break;
1973 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001974 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 kfree_skb(skb);
1976 return 0;
1977}
1978
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001979static int ip6_pkt_discard(struct sk_buff *skb)
1980{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001981 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001982}
1983
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001984static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Eric Dumazetadf30902009-06-02 05:19:30 +00001986 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001987 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
David S. Miller6723ab52006-10-18 21:20:57 -07001990#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1991
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001992static int ip6_pkt_prohibit(struct sk_buff *skb)
1993{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001994 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001995}
1996
1997static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1998{
Eric Dumazetadf30902009-06-02 05:19:30 +00001999 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002000 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002001}
2002
David S. Miller6723ab52006-10-18 21:20:57 -07002003#endif
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/*
2006 * Allocate a dst for local (unicast / anycast) address.
2007 */
2008
2009struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2010 const struct in6_addr *addr,
2011 int anycast)
2012{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002013 struct net *net = dev_net(idev->dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002014 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -08002015 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Ben Greear40385652010-11-08 12:33:48 +00002017 if (rt == NULL) {
2018 if (net_ratelimit())
2019 pr_warning("IPv6: Maximum number of routes reached,"
2020 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Daniel Lezcano55786892008-03-04 13:47:47 -08002024 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 in6_dev_hold(idev);
2026
Changli Gaod8d1f302010-06-10 23:31:35 -07002027 rt->dst.flags = DST_HOST;
2028 rt->dst.input = ip6_input;
2029 rt->dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08002030 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002032 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002035 if (anycast)
2036 rt->rt6i_flags |= RTF_ANYCAST;
2037 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002039 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2040 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002041 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002042
David S. Miller29546a62011-03-03 12:10:37 -08002043 return ERR_CAST(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
David S. Miller14deae42009-01-04 16:04:39 -08002045 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2048 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002049 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Changli Gaod8d1f302010-06-10 23:31:35 -07002051 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 return rt;
2054}
2055
Daniel Walterc3968a82011-04-13 21:10:57 +00002056int ip6_route_get_saddr(struct net *net,
2057 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002058 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002059 unsigned int prefs,
2060 struct in6_addr *saddr)
2061{
2062 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2063 int err = 0;
2064 if (rt->rt6i_prefsrc.plen)
2065 ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2066 else
2067 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2068 daddr, prefs, saddr);
2069 return err;
2070}
2071
2072/* remove deleted ip from prefsrc entries */
2073struct arg_dev_net_ip {
2074 struct net_device *dev;
2075 struct net *net;
2076 struct in6_addr *addr;
2077};
2078
2079static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2080{
2081 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2082 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2083 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2084
2085 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2086 rt != net->ipv6.ip6_null_entry &&
2087 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2088 /* remove prefsrc entry */
2089 rt->rt6i_prefsrc.plen = 0;
2090 }
2091 return 0;
2092}
2093
2094void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2095{
2096 struct net *net = dev_net(ifp->idev->dev);
2097 struct arg_dev_net_ip adni = {
2098 .dev = ifp->idev->dev,
2099 .net = net,
2100 .addr = &ifp->addr,
2101 };
2102 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2103}
2104
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002105struct arg_dev_net {
2106 struct net_device *dev;
2107 struct net *net;
2108};
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110static int fib6_ifdown(struct rt6_info *rt, void *arg)
2111{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002112 const struct arg_dev_net *adn = arg;
2113 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002114
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002115 if ((rt->rt6i_dev == dev || dev == NULL) &&
2116 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 RT6_TRACE("deleted by ifdown %p\n", rt);
2118 return -1;
2119 }
2120 return 0;
2121}
2122
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002123void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002125 struct arg_dev_net adn = {
2126 .dev = dev,
2127 .net = net,
2128 };
2129
2130 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002131 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134struct rt6_mtu_change_arg
2135{
2136 struct net_device *dev;
2137 unsigned mtu;
2138};
2139
2140static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2141{
2142 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2143 struct inet6_dev *idev;
2144
2145 /* In IPv6 pmtu discovery is not optional,
2146 so that RTAX_MTU lock cannot disable it.
2147 We still use this lock to block changes
2148 caused by addrconf/ndisc.
2149 */
2150
2151 idev = __in6_dev_get(arg->dev);
2152 if (idev == NULL)
2153 return 0;
2154
2155 /* For administrative MTU increase, there is no way to discover
2156 IPv6 PMTU increase, so PMTU increase should be updated here.
2157 Since RFC 1981 doesn't include administrative MTU increase
2158 update PMTU increase is a MUST. (i.e. jumbo frame)
2159 */
2160 /*
2161 If new MTU is less than route PMTU, this new MTU will be the
2162 lowest MTU in the path, update the route PMTU to reflect PMTU
2163 decreases; if new MTU is greater than route PMTU, and the
2164 old MTU is the lowest MTU in the path, update the route PMTU
2165 to reflect the increase. In this case if the other nodes' MTU
2166 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2167 PMTU discouvery.
2168 */
2169 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002170 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2171 (dst_mtu(&rt->dst) >= arg->mtu ||
2172 (dst_mtu(&rt->dst) < arg->mtu &&
2173 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002174 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177}
2178
2179void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2180{
Thomas Grafc71099a2006-08-04 23:20:06 -07002181 struct rt6_mtu_change_arg arg = {
2182 .dev = dev,
2183 .mtu = mtu,
2184 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002186 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002189static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002190 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002191 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002192 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002193 [RTA_PRIORITY] = { .type = NLA_U32 },
2194 [RTA_METRICS] = { .type = NLA_NESTED },
2195};
2196
2197static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2198 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Thomas Graf86872cb2006-08-22 00:01:08 -07002200 struct rtmsg *rtm;
2201 struct nlattr *tb[RTA_MAX+1];
2202 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Thomas Graf86872cb2006-08-22 00:01:08 -07002204 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2205 if (err < 0)
2206 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Thomas Graf86872cb2006-08-22 00:01:08 -07002208 err = -EINVAL;
2209 rtm = nlmsg_data(nlh);
2210 memset(cfg, 0, sizeof(*cfg));
2211
2212 cfg->fc_table = rtm->rtm_table;
2213 cfg->fc_dst_len = rtm->rtm_dst_len;
2214 cfg->fc_src_len = rtm->rtm_src_len;
2215 cfg->fc_flags = RTF_UP;
2216 cfg->fc_protocol = rtm->rtm_protocol;
2217
2218 if (rtm->rtm_type == RTN_UNREACHABLE)
2219 cfg->fc_flags |= RTF_REJECT;
2220
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002221 if (rtm->rtm_type == RTN_LOCAL)
2222 cfg->fc_flags |= RTF_LOCAL;
2223
Thomas Graf86872cb2006-08-22 00:01:08 -07002224 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2225 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002226 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002227
2228 if (tb[RTA_GATEWAY]) {
2229 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2230 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002232
2233 if (tb[RTA_DST]) {
2234 int plen = (rtm->rtm_dst_len + 7) >> 3;
2235
2236 if (nla_len(tb[RTA_DST]) < plen)
2237 goto errout;
2238
2239 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002241
2242 if (tb[RTA_SRC]) {
2243 int plen = (rtm->rtm_src_len + 7) >> 3;
2244
2245 if (nla_len(tb[RTA_SRC]) < plen)
2246 goto errout;
2247
2248 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002250
Daniel Walterc3968a82011-04-13 21:10:57 +00002251 if (tb[RTA_PREFSRC])
2252 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2253
Thomas Graf86872cb2006-08-22 00:01:08 -07002254 if (tb[RTA_OIF])
2255 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2256
2257 if (tb[RTA_PRIORITY])
2258 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2259
2260 if (tb[RTA_METRICS]) {
2261 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2262 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002264
2265 if (tb[RTA_TABLE])
2266 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2267
2268 err = 0;
2269errout:
2270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Thomas Grafc127ea22007-03-22 11:58:32 -07002273static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Thomas Graf86872cb2006-08-22 00:01:08 -07002275 struct fib6_config cfg;
2276 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Thomas Graf86872cb2006-08-22 00:01:08 -07002278 err = rtm_to_fib6_config(skb, nlh, &cfg);
2279 if (err < 0)
2280 return err;
2281
2282 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Thomas Grafc127ea22007-03-22 11:58:32 -07002285static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Thomas Graf86872cb2006-08-22 00:01:08 -07002287 struct fib6_config cfg;
2288 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Thomas Graf86872cb2006-08-22 00:01:08 -07002290 err = rtm_to_fib6_config(skb, nlh, &cfg);
2291 if (err < 0)
2292 return err;
2293
2294 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Thomas Graf339bf982006-11-10 14:10:15 -08002297static inline size_t rt6_nlmsg_size(void)
2298{
2299 return NLMSG_ALIGN(sizeof(struct rtmsg))
2300 + nla_total_size(16) /* RTA_SRC */
2301 + nla_total_size(16) /* RTA_DST */
2302 + nla_total_size(16) /* RTA_GATEWAY */
2303 + nla_total_size(16) /* RTA_PREFSRC */
2304 + nla_total_size(4) /* RTA_TABLE */
2305 + nla_total_size(4) /* RTA_IIF */
2306 + nla_total_size(4) /* RTA_OIF */
2307 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002308 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002309 + nla_total_size(sizeof(struct rta_cacheinfo));
2310}
2311
Brian Haley191cd582008-08-14 15:33:21 -07002312static int rt6_fill_node(struct net *net,
2313 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002314 struct in6_addr *dst, struct in6_addr *src,
2315 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002316 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002319 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002320 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002321 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (prefix) { /* user wants prefix routes only */
2324 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2325 /* success since this is not a prefix route */
2326 return 1;
2327 }
2328 }
2329
Thomas Graf2d7202b2006-08-22 00:01:27 -07002330 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2331 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002332 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002333
2334 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 rtm->rtm_family = AF_INET6;
2336 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2337 rtm->rtm_src_len = rt->rt6i_src.plen;
2338 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002339 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002340 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002341 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002342 table = RT6_TABLE_UNSPEC;
2343 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002344 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (rt->rt6i_flags&RTF_REJECT)
2346 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002347 else if (rt->rt6i_flags&RTF_LOCAL)
2348 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2350 rtm->rtm_type = RTN_LOCAL;
2351 else
2352 rtm->rtm_type = RTN_UNICAST;
2353 rtm->rtm_flags = 0;
2354 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2355 rtm->rtm_protocol = rt->rt6i_protocol;
2356 if (rt->rt6i_flags&RTF_DYNAMIC)
2357 rtm->rtm_protocol = RTPROT_REDIRECT;
2358 else if (rt->rt6i_flags & RTF_ADDRCONF)
2359 rtm->rtm_protocol = RTPROT_KERNEL;
2360 else if (rt->rt6i_flags&RTF_DEFAULT)
2361 rtm->rtm_protocol = RTPROT_RA;
2362
2363 if (rt->rt6i_flags&RTF_CACHE)
2364 rtm->rtm_flags |= RTM_F_CLONED;
2365
2366 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002367 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002368 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002370 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371#ifdef CONFIG_IPV6_SUBTREES
2372 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002373 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002374 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002376 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002378 if (iif) {
2379#ifdef CONFIG_IPV6_MROUTE
2380 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002381 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002382 if (err <= 0) {
2383 if (!nowait) {
2384 if (err == 0)
2385 return 0;
2386 goto nla_put_failure;
2387 } else {
2388 if (err == -EMSGSIZE)
2389 goto nla_put_failure;
2390 }
2391 }
2392 } else
2393#endif
2394 NLA_PUT_U32(skb, RTA_IIF, iif);
2395 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002397 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002398 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002400
Daniel Walterc3968a82011-04-13 21:10:57 +00002401 if (rt->rt6i_prefsrc.plen) {
2402 struct in6_addr saddr_buf;
2403 ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
2404 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2405 }
2406
David S. Millerdefb3512010-12-08 21:16:57 -08002407 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002408 goto nla_put_failure;
2409
Changli Gaod8d1f302010-06-10 23:31:35 -07002410 if (rt->dst.neighbour)
2411 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002412
Changli Gaod8d1f302010-06-10 23:31:35 -07002413 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002414 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2415
2416 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002417
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002418 if (!(rt->rt6i_flags & RTF_EXPIRES))
2419 expires = 0;
2420 else if (rt->rt6i_expires - jiffies < INT_MAX)
2421 expires = rt->rt6i_expires - jiffies;
2422 else
2423 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002424
Changli Gaod8d1f302010-06-10 23:31:35 -07002425 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2426 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002427 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Thomas Graf2d7202b2006-08-22 00:01:27 -07002429 return nlmsg_end(skb, nlh);
2430
2431nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002432 nlmsg_cancel(skb, nlh);
2433 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Patrick McHardy1b43af52006-08-10 23:11:17 -07002436int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2439 int prefix;
2440
Thomas Graf2d7202b2006-08-22 00:01:27 -07002441 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2442 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2444 } else
2445 prefix = 0;
2446
Brian Haley191cd582008-08-14 15:33:21 -07002447 return rt6_fill_node(arg->net,
2448 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002450 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Thomas Grafc127ea22007-03-22 11:58:32 -07002453static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002455 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002456 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002458 struct sk_buff *skb;
2459 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002460 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002461 int err, iif = 0;
2462
2463 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2464 if (err < 0)
2465 goto errout;
2466
2467 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002468 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002469
2470 if (tb[RTA_SRC]) {
2471 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2472 goto errout;
2473
David S. Miller4c9483b2011-03-12 16:22:43 -05002474 ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
Thomas Grafab364a62006-08-22 00:01:47 -07002475 }
2476
2477 if (tb[RTA_DST]) {
2478 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2479 goto errout;
2480
David S. Miller4c9483b2011-03-12 16:22:43 -05002481 ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
Thomas Grafab364a62006-08-22 00:01:47 -07002482 }
2483
2484 if (tb[RTA_IIF])
2485 iif = nla_get_u32(tb[RTA_IIF]);
2486
2487 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002488 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002489
2490 if (iif) {
2491 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002492 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002493 if (!dev) {
2494 err = -ENODEV;
2495 goto errout;
2496 }
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002500 if (skb == NULL) {
2501 err = -ENOBUFS;
2502 goto errout;
2503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 /* Reserve room for dummy headers, this skb can pass
2506 through good chunk of routing engine.
2507 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002508 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2510
David S. Miller4c9483b2011-03-12 16:22:43 -05002511 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002512 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
David S. Miller4c9483b2011-03-12 16:22:43 -05002514 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002516 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002518 kfree_skb(skb);
2519 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521
Daniel Lezcano55786892008-03-04 13:47:47 -08002522 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002523errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Thomas Graf86872cb2006-08-22 00:01:08 -07002527void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002530 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002531 u32 seq;
2532 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002534 err = -ENOBUFS;
2535 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002536
Thomas Graf339bf982006-11-10 14:10:15 -08002537 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002538 if (skb == NULL)
2539 goto errout;
2540
Brian Haley191cd582008-08-14 15:33:21 -07002541 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002542 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002543 if (err < 0) {
2544 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2545 WARN_ON(err == -EMSGSIZE);
2546 kfree_skb(skb);
2547 goto errout;
2548 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002549 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2550 info->nlh, gfp_any());
2551 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002552errout:
2553 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002554 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002557static int ip6_route_dev_notify(struct notifier_block *this,
2558 unsigned long event, void *data)
2559{
2560 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002561 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002562
2563 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002564 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002565 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2566#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002567 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002568 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002569 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002570 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2571#endif
2572 }
2573
2574 return NOTIFY_OK;
2575}
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577/*
2578 * /proc
2579 */
2580
2581#ifdef CONFIG_PROC_FS
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583struct rt6_proc_arg
2584{
2585 char *buffer;
2586 int offset;
2587 int length;
2588 int skip;
2589 int len;
2590};
2591
2592static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2593{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002594 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002596 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002599 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002601 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602#endif
2603
2604 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002605 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002607 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002609 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002610 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2611 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002612 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return 0;
2614}
2615
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002616static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002618 struct net *net = (struct net *)m->private;
2619 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002623static int ipv6_route_open(struct inode *inode, struct file *file)
2624{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002625 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002626}
2627
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002628static const struct file_operations ipv6_route_proc_fops = {
2629 .owner = THIS_MODULE,
2630 .open = ipv6_route_open,
2631 .read = seq_read,
2632 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002633 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002634};
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2637{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002638 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002640 net->ipv6.rt6_stats->fib_nodes,
2641 net->ipv6.rt6_stats->fib_route_nodes,
2642 net->ipv6.rt6_stats->fib_rt_alloc,
2643 net->ipv6.rt6_stats->fib_rt_entries,
2644 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002645 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002646 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 return 0;
2649}
2650
2651static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2652{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002653 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002654}
2655
Arjan van de Ven9a321442007-02-12 00:55:35 -08002656static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 .owner = THIS_MODULE,
2658 .open = rt6_stats_seq_open,
2659 .read = seq_read,
2660 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002661 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662};
2663#endif /* CONFIG_PROC_FS */
2664
2665#ifdef CONFIG_SYSCTL
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002668int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 void __user *buffer, size_t *lenp, loff_t *ppos)
2670{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002671 struct net *net;
2672 int delay;
2673 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002675
2676 net = (struct net *)ctl->extra1;
2677 delay = net->ipv6.sysctl.flush_delay;
2678 proc_dointvec(ctl, write, buffer, lenp, ppos);
2679 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002683ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002684 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002686 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 .maxlen = sizeof(int),
Dave Jones89c8b3a2005-04-28 12:11:49 -07002688 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002689 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 },
2691 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002693 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 .maxlen = sizeof(int),
2695 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002696 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 },
2698 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002700 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 .maxlen = sizeof(int),
2702 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002703 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 },
2705 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002707 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 .maxlen = sizeof(int),
2709 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002710 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 },
2712 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002714 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 .maxlen = sizeof(int),
2716 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002717 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 },
2719 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002721 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .maxlen = sizeof(int),
2723 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002724 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 },
2726 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002728 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .maxlen = sizeof(int),
2730 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002731 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 },
2733 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002735 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 .maxlen = sizeof(int),
2737 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002738 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 },
2740 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002742 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .maxlen = sizeof(int),
2744 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002745 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 },
2747 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002749 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .maxlen = sizeof(int),
2751 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002752 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002754 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755};
2756
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002757struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002758{
2759 struct ctl_table *table;
2760
2761 table = kmemdup(ipv6_route_table_template,
2762 sizeof(ipv6_route_table_template),
2763 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002764
2765 if (table) {
2766 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002767 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002768 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002769 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2770 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2771 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2772 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2773 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2774 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2775 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002776 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002777 }
2778
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002779 return table;
2780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781#endif
2782
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002783static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002784{
Pavel Emelyanov633d4242008-04-21 14:25:23 -07002785 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002786
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002787 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2788 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002789
Eric Dumazetfc66f952010-10-08 06:37:34 +00002790 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2791 goto out_ip6_dst_ops;
2792
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002793 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2794 sizeof(*net->ipv6.ip6_null_entry),
2795 GFP_KERNEL);
2796 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002797 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002798 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002799 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002800 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002801 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2802 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002803
2804#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2805 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2806 sizeof(*net->ipv6.ip6_prohibit_entry),
2807 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002808 if (!net->ipv6.ip6_prohibit_entry)
2809 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002810 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002811 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002812 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002813 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2814 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002815
2816 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2817 sizeof(*net->ipv6.ip6_blk_hole_entry),
2818 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002819 if (!net->ipv6.ip6_blk_hole_entry)
2820 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002821 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002822 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002823 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002824 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2825 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002826#endif
2827
Peter Zijlstrab339a472008-10-07 14:15:00 -07002828 net->ipv6.sysctl.flush_delay = 0;
2829 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2830 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2831 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2832 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2833 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2834 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2835 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2836
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002837#ifdef CONFIG_PROC_FS
2838 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2839 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2840#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002841 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2842
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002843 ret = 0;
2844out:
2845 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002846
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002847#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2848out_ip6_prohibit_entry:
2849 kfree(net->ipv6.ip6_prohibit_entry);
2850out_ip6_null_entry:
2851 kfree(net->ipv6.ip6_null_entry);
2852#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002853out_ip6_dst_entries:
2854 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002855out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002856 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002857}
2858
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002859static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002860{
2861#ifdef CONFIG_PROC_FS
2862 proc_net_remove(net, "ipv6_route");
2863 proc_net_remove(net, "rt6_stats");
2864#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002865 kfree(net->ipv6.ip6_null_entry);
2866#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2867 kfree(net->ipv6.ip6_prohibit_entry);
2868 kfree(net->ipv6.ip6_blk_hole_entry);
2869#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002870 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002871}
2872
2873static struct pernet_operations ip6_route_net_ops = {
2874 .init = ip6_route_net_init,
2875 .exit = ip6_route_net_exit,
2876};
2877
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002878static struct notifier_block ip6_route_dev_notifier = {
2879 .notifier_call = ip6_route_dev_notify,
2880 .priority = 0,
2881};
2882
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002883int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002885 int ret;
2886
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002887 ret = -ENOMEM;
2888 ip6_dst_ops_template.kmem_cachep =
2889 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2890 SLAB_HWCACHE_ALIGN, NULL);
2891 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002892 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002893
Eric Dumazetfc66f952010-10-08 06:37:34 +00002894 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002895 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002896 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002897
Eric Dumazetfc66f952010-10-08 06:37:34 +00002898 ret = register_pernet_subsys(&ip6_route_net_ops);
2899 if (ret)
2900 goto out_dst_entries;
2901
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002902 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2903
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002904 /* Registering of the loopback is done before this portion of code,
2905 * the loopback reference in rt6_info will not be taken, do it
2906 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002907 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002908 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2909 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002910 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002911 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002912 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002913 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2914 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002915 ret = fib6_init();
2916 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002917 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002918
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002919 ret = xfrm6_init();
2920 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002921 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002922
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002923 ret = fib6_rules_init();
2924 if (ret)
2925 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002926
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002927 ret = -ENOBUFS;
2928 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2929 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2930 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2931 goto fib6_rules_init;
2932
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002933 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002934 if (ret)
2935 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002936
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002937out:
2938 return ret;
2939
2940fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002941 fib6_rules_cleanup();
2942xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002943 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002944out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002945 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002946out_register_subsys:
2947 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002948out_dst_entries:
2949 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002950out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002951 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
2955void ip6_route_cleanup(void)
2956{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002957 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002958 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002961 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002962 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002963 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}