blob: c2af4da074b033818de5040a61621fe42d50e951 [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 */
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700230static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
231 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
David S. Millercf911662011-04-28 14:31:47 -0700233 struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, 0);
234
235 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
236
237 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static void ip6_dst_destroy(struct dst_entry *dst)
241{
242 struct rt6_info *rt = (struct rt6_info *)dst;
243 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800244 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (idev != NULL) {
247 rt->rt6i_idev = NULL;
248 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900249 }
David S. Millerb3419362010-11-30 12:27:11 -0800250 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800251 rt->rt6i_peer = NULL;
252 inet_putpeer(peer);
253 }
254}
255
David S. Miller6431cbc2011-02-07 20:38:06 -0800256static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
257
258static u32 rt6_peer_genid(void)
259{
260 return atomic_read(&__rt6_peer_genid);
261}
262
David S. Millerb3419362010-11-30 12:27:11 -0800263void rt6_bind_peer(struct rt6_info *rt, int create)
264{
265 struct inet_peer *peer;
266
David S. Millerb3419362010-11-30 12:27:11 -0800267 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
268 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
269 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800270 else
271 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
275 int how)
276{
277 struct rt6_info *rt = (struct rt6_info *)dst;
278 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800279 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900280 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800282 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
283 struct inet6_dev *loopback_idev =
284 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (loopback_idev != NULL) {
286 rt->rt6i_idev = loopback_idev;
287 in6_dev_put(idev);
288 }
289 }
290}
291
292static __inline__ int rt6_check_expired(const struct rt6_info *rt)
293{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000294 return (rt->rt6i_flags & RTF_EXPIRES) &&
295 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000298static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700299{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000300 return ipv6_addr_type(daddr) &
301 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700305 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800308static inline struct rt6_info *rt6_device_match(struct net *net,
309 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000310 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700312 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct rt6_info *local = NULL;
315 struct rt6_info *sprt;
316
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900317 if (!oif && ipv6_addr_any(saddr))
318 goto out;
319
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900321 struct net_device *dev = sprt->rt6i_dev;
322
323 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (dev->ifindex == oif)
325 return sprt;
326 if (dev->flags & IFF_LOOPBACK) {
327 if (sprt->rt6i_idev == NULL ||
328 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700329 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900331 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 local->rt6i_idev->dev->ifindex == oif))
333 continue;
334 }
335 local = sprt;
336 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900337 } else {
338 if (ipv6_chk_addr(net, saddr, dev,
339 flags & RT6_LOOKUP_F_IFACE))
340 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900344 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (local)
346 return local;
347
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700348 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800349 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return rt;
353}
354
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800355#ifdef CONFIG_IPV6_ROUTER_PREF
356static void rt6_probe(struct rt6_info *rt)
357{
358 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
359 /*
360 * Okay, this does not seem to be appropriate
361 * for now, however, we need to check if it
362 * is really so; aka Router Reachability Probing.
363 *
364 * Router Reachability Probe MUST be rate-limited
365 * to no more than one per minute.
366 */
367 if (!neigh || (neigh->nud_state & NUD_VALID))
368 return;
369 read_lock_bh(&neigh->lock);
370 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800371 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800372 struct in6_addr mcaddr;
373 struct in6_addr *target;
374
375 neigh->updated = jiffies;
376 read_unlock_bh(&neigh->lock);
377
378 target = (struct in6_addr *)&neigh->primary_key;
379 addrconf_addr_solict_mult(target, &mcaddr);
380 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
381 } else
382 read_unlock_bh(&neigh->lock);
383}
384#else
385static inline void rt6_probe(struct rt6_info *rt)
386{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800387}
388#endif
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800391 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700393static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800395 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700396 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800397 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700398 if ((dev->flags & IFF_LOOPBACK) &&
399 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
400 return 1;
401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Dave Jonesb6f99a22007-03-22 12:27:49 -0700404static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800406 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800407 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700408 if (rt->rt6i_flags & RTF_NONEXTHOP ||
409 !(rt->rt6i_flags & RTF_GATEWAY))
410 m = 1;
411 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800412 read_lock_bh(&neigh->lock);
413 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700414 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800415#ifdef CONFIG_IPV6_ROUTER_PREF
416 else if (neigh->nud_state & NUD_FAILED)
417 m = 0;
418#endif
419 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800420 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800421 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800422 } else
423 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800424 return m;
425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800427static int rt6_score_route(struct rt6_info *rt, int oif,
428 int strict)
429{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700430 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900431
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700432 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700433 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800434 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800435#ifdef CONFIG_IPV6_ROUTER_PREF
436 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
437#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700438 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800439 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800440 return -1;
441 return m;
442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David S. Millerf11e6652007-03-24 20:36:25 -0700444static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
445 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800446{
David S. Millerf11e6652007-03-24 20:36:25 -0700447 int m;
448
449 if (rt6_check_expired(rt))
450 goto out;
451
452 m = rt6_score_route(rt, oif, strict);
453 if (m < 0)
454 goto out;
455
456 if (m > *mpri) {
457 if (strict & RT6_LOOKUP_F_REACHABLE)
458 rt6_probe(match);
459 *mpri = m;
460 match = rt;
461 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
462 rt6_probe(rt);
463 }
464
465out:
466 return match;
467}
468
469static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
470 struct rt6_info *rr_head,
471 u32 metric, int oif, int strict)
472{
473 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800474 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
David S. Millerf11e6652007-03-24 20:36:25 -0700476 match = NULL;
477 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700478 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700479 match = find_match(rt, oif, strict, &mpri, match);
480 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700481 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700482 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800483
David S. Millerf11e6652007-03-24 20:36:25 -0700484 return match;
485}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800486
David S. Millerf11e6652007-03-24 20:36:25 -0700487static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
488{
489 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800490 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David S. Millerf11e6652007-03-24 20:36:25 -0700492 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800493 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David S. Millerf11e6652007-03-24 20:36:25 -0700495 rt0 = fn->rr_ptr;
496 if (!rt0)
497 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David S. Millerf11e6652007-03-24 20:36:25 -0700499 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800501 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700502 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700503 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700504
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800505 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700506 if (!next || next->rt6i_metric != rt0->rt6i_metric)
507 next = fn->leaf;
508
509 if (next != rt0)
510 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
David S. Millerf11e6652007-03-24 20:36:25 -0700513 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800514 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900516 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000517 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800520#ifdef CONFIG_IPV6_ROUTE_INFO
521int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000522 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800523{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900524 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800525 struct route_info *rinfo = (struct route_info *) opt;
526 struct in6_addr prefix_buf, *prefix;
527 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900528 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800529 struct rt6_info *rt;
530
531 if (len < sizeof(struct route_info)) {
532 return -EINVAL;
533 }
534
535 /* Sanity check for prefix_len and length */
536 if (rinfo->length > 3) {
537 return -EINVAL;
538 } else if (rinfo->prefix_len > 128) {
539 return -EINVAL;
540 } else if (rinfo->prefix_len > 64) {
541 if (rinfo->length < 2) {
542 return -EINVAL;
543 }
544 } else if (rinfo->prefix_len > 0) {
545 if (rinfo->length < 1) {
546 return -EINVAL;
547 }
548 }
549
550 pref = rinfo->route_pref;
551 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000552 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800553
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900554 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800555
556 if (rinfo->length == 3)
557 prefix = (struct in6_addr *)rinfo->prefix;
558 else {
559 /* this function is safe */
560 ipv6_addr_prefix(&prefix_buf,
561 (struct in6_addr *)rinfo->prefix,
562 rinfo->prefix_len);
563 prefix = &prefix_buf;
564 }
565
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800566 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
567 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800568
569 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700570 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800571 rt = NULL;
572 }
573
574 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800575 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800576 pref);
577 else if (rt)
578 rt->rt6i_flags = RTF_ROUTEINFO |
579 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
580
581 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900582 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800583 rt->rt6i_flags &= ~RTF_EXPIRES;
584 } else {
585 rt->rt6i_expires = jiffies + HZ * lifetime;
586 rt->rt6i_flags |= RTF_EXPIRES;
587 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700588 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800589 }
590 return 0;
591}
592#endif
593
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800594#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700595do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800596 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700597 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700598 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700599 if (fn->fn_flags & RTN_TL_ROOT) \
600 goto out; \
601 pn = fn->parent; \
602 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800603 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700604 else \
605 fn = pn; \
606 if (fn->fn_flags & RTN_RTINFO) \
607 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700608 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700609 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700610} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700611
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800612static struct rt6_info *ip6_pol_route_lookup(struct net *net,
613 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500614 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct fib6_node *fn;
617 struct rt6_info *rt;
618
Thomas Grafc71099a2006-08-04 23:20:06 -0700619 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500620 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700621restart:
622 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500623 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
624 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700625out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700626 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700627 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700628 return rt;
629
630}
631
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900632struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
633 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700634{
David S. Miller4c9483b2011-03-12 16:22:43 -0500635 struct flowi6 fl6 = {
636 .flowi6_oif = oif,
637 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700638 };
639 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700640 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700641
Thomas Grafadaa70b2006-10-13 15:01:03 -0700642 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500643 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700644 flags |= RT6_LOOKUP_F_HAS_SADDR;
645 }
646
David S. Miller4c9483b2011-03-12 16:22:43 -0500647 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700648 if (dst->error == 0)
649 return (struct rt6_info *) dst;
650
651 dst_release(dst);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return NULL;
654}
655
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900656EXPORT_SYMBOL(rt6_lookup);
657
Thomas Grafc71099a2006-08-04 23:20:06 -0700658/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 It takes new route entry, the addition fails by any reason the
660 route is freed. In any case, if caller does not hold it, it may
661 be destroyed.
662 */
663
Thomas Graf86872cb2006-08-22 00:01:08 -0700664static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700667 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Thomas Grafc71099a2006-08-04 23:20:06 -0700669 table = rt->rt6i_table;
670 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700671 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700672 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return err;
675}
676
Thomas Graf40e22e82006-08-22 00:00:45 -0700677int ip6_ins_rt(struct rt6_info *rt)
678{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800679 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900680 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800681 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800682 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700683}
684
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000685static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
686 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct rt6_info *rt;
689
690 /*
691 * Clone the route.
692 */
693
694 rt = ip6_rt_copy(ort);
695
696 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800697 struct neighbour *neigh;
698 int attempts = !in_softirq();
699
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900700 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
701 if (rt->rt6i_dst.plen != 128 &&
702 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
703 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900707 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 rt->rt6i_dst.plen = 128;
709 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700710 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712#ifdef CONFIG_IPV6_SUBTREES
713 if (rt->rt6i_src.plen && saddr) {
714 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
715 rt->rt6i_src.plen = 128;
716 }
717#endif
718
David S. Miller14deae42009-01-04 16:04:39 -0800719 retry:
720 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
721 if (IS_ERR(neigh)) {
722 struct net *net = dev_net(rt->rt6i_dev);
723 int saved_rt_min_interval =
724 net->ipv6.sysctl.ip6_rt_gc_min_interval;
725 int saved_rt_elasticity =
726 net->ipv6.sysctl.ip6_rt_gc_elasticity;
727
728 if (attempts-- > 0) {
729 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
730 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
731
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000732 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800733
734 net->ipv6.sysctl.ip6_rt_gc_elasticity =
735 saved_rt_elasticity;
736 net->ipv6.sysctl.ip6_rt_gc_min_interval =
737 saved_rt_min_interval;
738 goto retry;
739 }
740
741 if (net_ratelimit())
742 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700743 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700744 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800745 return NULL;
746 }
747 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800751 return rt;
752}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000754static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800755{
756 struct rt6_info *rt = ip6_rt_copy(ort);
757 if (rt) {
758 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
759 rt->rt6i_dst.plen = 128;
760 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700761 rt->dst.flags |= DST_HOST;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800762 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
763 }
764 return rt;
765}
766
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800767static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500768 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800771 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700772 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800774 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700775 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700777 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700780 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800782restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500783 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700786 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800787
David S. Miller4c9483b2011-03-12 16:22:43 -0500788 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800789 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800790 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef042006-03-20 17:01:24 -0800791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Changli Gaod8d1f302010-06-10 23:31:35 -0700793 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700794 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800795
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800796 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500797 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800798 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500799 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800800 else
801 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800802
Changli Gaod8d1f302010-06-10 23:31:35 -0700803 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800804 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800805
Changli Gaod8d1f302010-06-10 23:31:35 -0700806 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800807 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700808 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800809 if (!err)
810 goto out2;
811 }
812
813 if (--attempts <= 0)
814 goto out2;
815
816 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700817 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800818 * released someone could insert this route. Relookup.
819 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700820 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800821 goto relookup;
822
823out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800824 if (reachable) {
825 reachable = 0;
826 goto restart_2;
827 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700828 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700829 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700831 rt->dst.lastuse = jiffies;
832 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700833
834 return rt;
835}
836
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800837static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500838 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700839{
David S. Miller4c9483b2011-03-12 16:22:43 -0500840 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700841}
842
Thomas Grafc71099a2006-08-04 23:20:06 -0700843void ip6_route_input(struct sk_buff *skb)
844{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000845 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900846 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700847 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500848 struct flowi6 fl6 = {
849 .flowi6_iif = skb->dev->ifindex,
850 .daddr = iph->daddr,
851 .saddr = iph->saddr,
852 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
853 .flowi6_mark = skb->mark,
854 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700855 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700856
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800857 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700858 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700859
David S. Miller4c9483b2011-03-12 16:22:43 -0500860 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700861}
862
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800863static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500864 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700865{
David S. Miller4c9483b2011-03-12 16:22:43 -0500866 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700867}
868
Florian Westphal9c7a4f92011-03-22 19:17:36 -0700869struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500870 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700871{
872 int flags = 0;
873
David S. Miller4c9483b2011-03-12 16:22:43 -0500874 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700875 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700876
David S. Miller4c9483b2011-03-12 16:22:43 -0500877 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700878 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000879 else if (sk)
880 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700881
David S. Miller4c9483b2011-03-12 16:22:43 -0500882 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900885EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David S. Miller2774c132011-03-01 14:59:04 -0800887struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700888{
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700889 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700890 struct dst_entry *new = NULL;
891
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700892 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
David S. Miller14e50e52007-05-24 18:17:54 -0700893 if (rt) {
David S. Millercf911662011-04-28 14:31:47 -0700894 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
895
Changli Gaod8d1f302010-06-10 23:31:35 -0700896 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700897
David S. Miller14e50e52007-05-24 18:17:54 -0700898 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800899 new->input = dst_discard;
900 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700901
David S. Millerdefb3512010-12-08 21:16:57 -0800902 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -0700903 rt->rt6i_idev = ort->rt6i_idev;
904 if (rt->rt6i_idev)
905 in6_dev_hold(rt->rt6i_idev);
906 rt->rt6i_expires = 0;
907
908 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
909 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
910 rt->rt6i_metric = 0;
911
912 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
913#ifdef CONFIG_IPV6_SUBTREES
914 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
915#endif
916
917 dst_free(new);
918 }
919
David S. Miller69ead7a2011-03-01 14:45:33 -0800920 dst_release(dst_orig);
921 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700922}
David S. Miller14e50e52007-05-24 18:17:54 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*
925 * Destination cache support functions
926 */
927
928static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
929{
930 struct rt6_info *rt;
931
932 rt = (struct rt6_info *) dst;
933
David S. Miller6431cbc2011-02-07 20:38:06 -0800934 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
935 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
936 if (!rt->rt6i_peer)
937 rt6_bind_peer(rt, 0);
938 rt->rt6i_peer_genid = rt6_peer_genid();
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return NULL;
943}
944
945static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
946{
947 struct rt6_info *rt = (struct rt6_info *) dst;
948
949 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000950 if (rt->rt6i_flags & RTF_CACHE) {
951 if (rt6_check_expired(rt)) {
952 ip6_del_rt(rt);
953 dst = NULL;
954 }
955 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000957 dst = NULL;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000960 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static void ip6_link_failure(struct sk_buff *skb)
964{
965 struct rt6_info *rt;
966
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000967 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Eric Dumazetadf30902009-06-02 05:19:30 +0000969 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (rt) {
971 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700972 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 rt->rt6i_flags |= RTF_EXPIRES;
974 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
975 rt->rt6i_node->fn_sernum = -1;
976 }
977}
978
979static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
980{
981 struct rt6_info *rt6 = (struct rt6_info*)dst;
982
983 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
984 rt6->rt6i_flags |= RTF_MODIFIED;
985 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800986 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800988 features |= RTAX_FEATURE_ALLFRAG;
989 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
David S. Millerdefb3512010-12-08 21:16:57 -0800991 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993}
994
David S. Miller0dbaee32010-12-13 12:52:14 -0800995static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
David S. Miller0dbaee32010-12-13 12:52:14 -0800997 struct net_device *dev = dst->dev;
998 unsigned int mtu = dst_mtu(dst);
999 struct net *net = dev_net(dev);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1002
Daniel Lezcano55786892008-03-04 13:47:47 -08001003 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1004 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001007 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1008 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1009 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * rely only on pmtu discovery"
1011 */
1012 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1013 mtu = IPV6_MAXPLEN;
1014 return mtu;
1015}
1016
David S. Millerd33e4552010-12-14 13:01:14 -08001017static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1018{
1019 unsigned int mtu = IPV6_MIN_MTU;
1020 struct inet6_dev *idev;
1021
1022 rcu_read_lock();
1023 idev = __in6_dev_get(dst->dev);
1024 if (idev)
1025 mtu = idev->cnf.mtu6;
1026 rcu_read_unlock();
1027
1028 return mtu;
1029}
1030
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001031static struct dst_entry *icmp6_dst_gc_list;
1032static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001033
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001034struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001036 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct rt6_info *rt;
1039 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001040 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (unlikely(idev == NULL))
1043 return NULL;
1044
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001045 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (unlikely(rt == NULL)) {
1047 in6_dev_put(idev);
1048 goto out;
1049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (neigh)
1052 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001053 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001055 if (IS_ERR(neigh))
1056 neigh = NULL;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 rt->rt6i_idev = idev;
1060 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001061 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001062 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001063 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001065 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001066 rt->dst.next = icmp6_dst_gc_list;
1067 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001068 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Daniel Lezcano55786892008-03-04 13:47:47 -08001070 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001073 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001076int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001078 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001079 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001081 spin_lock_bh(&icmp6_dst_lock);
1082 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 while ((dst = *pprev) != NULL) {
1085 if (!atomic_read(&dst->__refcnt)) {
1086 *pprev = dst->next;
1087 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 } else {
1089 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001090 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 }
1093
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001094 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001095
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001096 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
David S. Miller1e493d12008-09-10 17:27:15 -07001099static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1100 void *arg)
1101{
1102 struct dst_entry *dst, **pprev;
1103
1104 spin_lock_bh(&icmp6_dst_lock);
1105 pprev = &icmp6_dst_gc_list;
1106 while ((dst = *pprev) != NULL) {
1107 struct rt6_info *rt = (struct rt6_info *) dst;
1108 if (func(rt, arg)) {
1109 *pprev = dst->next;
1110 dst_free(dst);
1111 } else {
1112 pprev = &dst->next;
1113 }
1114 }
1115 spin_unlock_bh(&icmp6_dst_lock);
1116}
1117
Daniel Lezcano569d3642008-01-18 03:56:57 -08001118static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001121 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001122 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1123 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1124 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1125 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1126 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001127 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Eric Dumazetfc66f952010-10-08 06:37:34 +00001129 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001130 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001131 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto out;
1133
Benjamin Thery6891a342008-03-04 13:49:47 -08001134 net->ipv6.ip6_rt_gc_expire++;
1135 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1136 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001137 entries = dst_entries_get_slow(ops);
1138 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001139 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001141 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001142 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145/* Clean host part of a prefix. Not necessary in radix tree,
1146 but results in cleaner routing tables.
1147
1148 Remove it only when all the things will work!
1149 */
1150
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001151int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
David S. Miller5170ae82010-12-12 21:35:57 -08001153 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001154 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001155 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001156 struct inet6_dev *idev;
1157
1158 rcu_read_lock();
1159 idev = __in6_dev_get(dev);
1160 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001161 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001162 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001163 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 return hoplimit;
1167}
David S. Millerabbf46a2010-12-12 21:14:46 -08001168EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170/*
1171 *
1172 */
1173
Thomas Graf86872cb2006-08-22 00:01:08 -07001174int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001177 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct rt6_info *rt = NULL;
1179 struct net_device *dev = NULL;
1180 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001181 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int addr_type;
1183
Thomas Graf86872cb2006-08-22 00:01:08 -07001184 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EINVAL;
1186#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001187 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EINVAL;
1189#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001190 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001192 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!dev)
1194 goto out;
1195 idev = in6_dev_get(dev);
1196 if (!idev)
1197 goto out;
1198 }
1199
Thomas Graf86872cb2006-08-22 00:01:08 -07001200 if (cfg->fc_metric == 0)
1201 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Daniel Lezcano55786892008-03-04 13:47:47 -08001203 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001204 if (table == NULL) {
1205 err = -ENOBUFS;
1206 goto out;
1207 }
1208
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001209 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (rt == NULL) {
1212 err = -ENOMEM;
1213 goto out;
1214 }
1215
Changli Gaod8d1f302010-06-10 23:31:35 -07001216 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001217 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1218 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1219 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Thomas Graf86872cb2006-08-22 00:01:08 -07001221 if (cfg->fc_protocol == RTPROT_UNSPEC)
1222 cfg->fc_protocol = RTPROT_BOOT;
1223 rt->rt6i_protocol = cfg->fc_protocol;
1224
1225 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001228 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001229 else if (cfg->fc_flags & RTF_LOCAL)
1230 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001232 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Changli Gaod8d1f302010-06-10 23:31:35 -07001234 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Thomas Graf86872cb2006-08-22 00:01:08 -07001236 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1237 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001239 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001242 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1243 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#endif
1245
Thomas Graf86872cb2006-08-22 00:01:08 -07001246 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* We cannot add true routes via loopback here,
1249 they would result in kernel looping; promote them to reject routes
1250 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001251 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001252 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1253 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001255 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (dev) {
1257 dev_put(dev);
1258 in6_dev_put(idev);
1259 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001260 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 dev_hold(dev);
1262 idev = in6_dev_get(dev);
1263 if (!idev) {
1264 err = -ENODEV;
1265 goto out;
1266 }
1267 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001268 rt->dst.output = ip6_pkt_discard_out;
1269 rt->dst.input = ip6_pkt_discard;
1270 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1272 goto install_route;
1273 }
1274
Thomas Graf86872cb2006-08-22 00:01:08 -07001275 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001276 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int gwa_type;
1278
Thomas Graf86872cb2006-08-22 00:01:08 -07001279 gw_addr = &cfg->fc_gateway;
1280 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 gwa_type = ipv6_addr_type(gw_addr);
1282
1283 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1284 struct rt6_info *grt;
1285
1286 /* IPv6 strictly inhibits using not link-local
1287 addresses as nexthop address.
1288 Otherwise, router will not able to send redirects.
1289 It is very good, but in some (rare!) circumstances
1290 (SIT, PtP, NBMA NOARP links) it is handy to allow
1291 some exceptions. --ANK
1292 */
1293 err = -EINVAL;
1294 if (!(gwa_type&IPV6_ADDR_UNICAST))
1295 goto out;
1296
Daniel Lezcano55786892008-03-04 13:47:47 -08001297 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 err = -EHOSTUNREACH;
1300 if (grt == NULL)
1301 goto out;
1302 if (dev) {
1303 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001304 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto out;
1306 }
1307 } else {
1308 dev = grt->rt6i_dev;
1309 idev = grt->rt6i_idev;
1310 dev_hold(dev);
1311 in6_dev_hold(grt->rt6i_idev);
1312 }
1313 if (!(grt->rt6i_flags&RTF_GATEWAY))
1314 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001315 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (err)
1318 goto out;
1319 }
1320 err = -EINVAL;
1321 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1322 goto out;
1323 }
1324
1325 err = -ENODEV;
1326 if (dev == NULL)
1327 goto out;
1328
Daniel Walterc3968a82011-04-13 21:10:57 +00001329 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1330 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1331 err = -EINVAL;
1332 goto out;
1333 }
1334 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1335 rt->rt6i_prefsrc.plen = 128;
1336 } else
1337 rt->rt6i_prefsrc.plen = 0;
1338
Thomas Graf86872cb2006-08-22 00:01:08 -07001339 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1341 if (IS_ERR(rt->rt6i_nexthop)) {
1342 err = PTR_ERR(rt->rt6i_nexthop);
1343 rt->rt6i_nexthop = NULL;
1344 goto out;
1345 }
1346 }
1347
Thomas Graf86872cb2006-08-22 00:01:08 -07001348 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001351 if (cfg->fc_mx) {
1352 struct nlattr *nla;
1353 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Thomas Graf86872cb2006-08-22 00:01:08 -07001355 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001356 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001357
1358 if (type) {
1359 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 err = -EINVAL;
1361 goto out;
1362 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001363
David S. Millerdefb3512010-12-08 21:16:57 -08001364 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 }
1368
Changli Gaod8d1f302010-06-10 23:31:35 -07001369 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001371 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001372
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001373 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001374
Thomas Graf86872cb2006-08-22 00:01:08 -07001375 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377out:
1378 if (dev)
1379 dev_put(dev);
1380 if (idev)
1381 in6_dev_put(idev);
1382 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001383 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return err;
1385}
1386
Thomas Graf86872cb2006-08-22 00:01:08 -07001387static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001390 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001391 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001393 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001394 return -ENOENT;
1395
Thomas Grafc71099a2006-08-04 23:20:06 -07001396 table = rt->rt6i_table;
1397 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Thomas Graf86872cb2006-08-22 00:01:08 -07001399 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001400 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Thomas Grafc71099a2006-08-04 23:20:06 -07001402 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return err;
1405}
1406
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001407int ip6_del_rt(struct rt6_info *rt)
1408{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001409 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001410 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001411 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001412 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001413}
1414
Thomas Graf86872cb2006-08-22 00:01:08 -07001415static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Thomas Grafc71099a2006-08-04 23:20:06 -07001417 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct fib6_node *fn;
1419 struct rt6_info *rt;
1420 int err = -ESRCH;
1421
Daniel Lezcano55786892008-03-04 13:47:47 -08001422 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001423 if (table == NULL)
1424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Thomas Grafc71099a2006-08-04 23:20:06 -07001426 read_lock_bh(&table->tb6_lock);
1427
1428 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001429 &cfg->fc_dst, cfg->fc_dst_len,
1430 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001433 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001434 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001436 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001438 if (cfg->fc_flags & RTF_GATEWAY &&
1439 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001441 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001443 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001444 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Thomas Graf86872cb2006-08-22 00:01:08 -07001446 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001449 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 return err;
1452}
1453
1454/*
1455 * Handle redirects
1456 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001457struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001458 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001459 struct in6_addr gateway;
1460};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001462static struct rt6_info *__ip6_route_redirect(struct net *net,
1463 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001464 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001465 int flags)
1466{
David S. Miller4c9483b2011-03-12 16:22:43 -05001467 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001468 struct rt6_info *rt;
1469 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001472 * Get the "current" route for this destination and
1473 * check if the redirect has come from approriate router.
1474 *
1475 * RFC 2461 specifies that redirects should only be
1476 * accepted if they come from the nexthop to the target.
1477 * Due to the way the routes are chosen, this notion
1478 * is a bit fuzzy and one might need to check all possible
1479 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Thomas Grafc71099a2006-08-04 23:20:06 -07001482 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001483 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001484restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001485 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001486 /*
1487 * Current route is on-link; redirect is always invalid.
1488 *
1489 * Seems, previous statement is not true. It could
1490 * be node, which looks for us as on-link (f.e. proxy ndisc)
1491 * But then router serving it might decide, that we should
1492 * know truth 8)8) --ANK (980726).
1493 */
1494 if (rt6_check_expired(rt))
1495 continue;
1496 if (!(rt->rt6i_flags & RTF_GATEWAY))
1497 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001498 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001499 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001500 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001501 continue;
1502 break;
1503 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001504
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001505 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001506 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001507 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001508out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001509 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001510
1511 read_unlock_bh(&table->tb6_lock);
1512
1513 return rt;
1514};
1515
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001516static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1517 const struct in6_addr *src,
1518 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001519 struct net_device *dev)
1520{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001521 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001522 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001523 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001524 .fl6 = {
1525 .flowi6_oif = dev->ifindex,
1526 .daddr = *dest,
1527 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001528 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001529 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001530
Brian Haley86c36ce2009-10-07 13:58:01 -07001531 ipv6_addr_copy(&rdfl.gateway, gateway);
1532
Thomas Grafadaa70b2006-10-13 15:01:03 -07001533 if (rt6_need_strict(dest))
1534 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001535
David S. Miller4c9483b2011-03-12 16:22:43 -05001536 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001537 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001538}
1539
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001540void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1541 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001542 struct neighbour *neigh, u8 *lladdr, int on_link)
1543{
1544 struct rt6_info *rt, *nrt = NULL;
1545 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001546 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001547
1548 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1549
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001550 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (net_ratelimit())
1552 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1553 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /*
1558 * We have finally decided to accept it.
1559 */
1560
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001561 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1563 NEIGH_UPDATE_F_OVERRIDE|
1564 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1565 NEIGH_UPDATE_F_ISROUTER))
1566 );
1567
1568 /*
1569 * Redirect received -> path was valid.
1570 * Look, redirects are sent only in response to data packets,
1571 * so that this nexthop apparently is reachable. --ANK
1572 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001573 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001576 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
1578
1579 nrt = ip6_rt_copy(rt);
1580 if (nrt == NULL)
1581 goto out;
1582
1583 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1584 if (on_link)
1585 nrt->rt6i_flags &= ~RTF_GATEWAY;
1586
1587 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1588 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001589 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1592 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Thomas Graf40e22e82006-08-22 00:00:45 -07001594 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto out;
1596
Changli Gaod8d1f302010-06-10 23:31:35 -07001597 netevent.old = &rt->dst;
1598 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001599 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001602 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return;
1604 }
1605
1606out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001607 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
1611 * Handle ICMP "packet too big" messages
1612 * i.e. Path MTU discovery
1613 */
1614
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001615static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001616 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct rt6_info *rt, *nrt;
1619 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001620again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001621 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (rt == NULL)
1623 return;
1624
Andrey Vagind3052b52010-12-11 15:20:11 +00001625 if (rt6_check_expired(rt)) {
1626 ip6_del_rt(rt);
1627 goto again;
1628 }
1629
Changli Gaod8d1f302010-06-10 23:31:35 -07001630 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto out;
1632
1633 if (pmtu < IPV6_MIN_MTU) {
1634 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001635 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * MTU (1280) and a fragment header should always be included
1637 * after a node receiving Too Big message reporting PMTU is
1638 * less than the IPv6 Minimum Link MTU.
1639 */
1640 pmtu = IPV6_MIN_MTU;
1641 allfrag = 1;
1642 }
1643
1644 /* New mtu received -> path was valid.
1645 They are sent only in response to data packets,
1646 so that this nexthop apparently is reachable. --ANK
1647 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001648 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* Host route. If it is static, it would be better
1651 not to override it, but add new one, so that
1652 when cache entry will expire old pmtu
1653 would return automatically.
1654 */
1655 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001656 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1657 if (allfrag) {
1658 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1659 features |= RTAX_FEATURE_ALLFRAG;
1660 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1661 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001662 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1664 goto out;
1665 }
1666
1667 /* Network route.
1668 Two cases are possible:
1669 1. It is connected route. Action: COW
1670 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1671 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001672 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001673 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001674 else
1675 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001676
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001677 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001678 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1679 if (allfrag) {
1680 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1681 features |= RTAX_FEATURE_ALLFRAG;
1682 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1683 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001684
1685 /* According to RFC 1981, detecting PMTU increase shouldn't be
1686 * happened within 5 mins, the recommended timer is 10 mins.
1687 * Here this route expiration time is set to ip6_rt_mtu_expires
1688 * which is 10 mins. After 10 mins the decreased pmtu is expired
1689 * and detecting PMTU increase will be automatically happened.
1690 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001691 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001692 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1693
Thomas Graf40e22e82006-08-22 00:00:45 -07001694 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001697 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001700void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001701 struct net_device *dev, u32 pmtu)
1702{
1703 struct net *net = dev_net(dev);
1704
1705 /*
1706 * RFC 1981 states that a node "MUST reduce the size of the packets it
1707 * is sending along the path" that caused the Packet Too Big message.
1708 * Since it's not possible in the general case to determine which
1709 * interface was used to send the original packet, we update the MTU
1710 * on the interface that will be used to send future packets. We also
1711 * update the MTU on the interface that received the Packet Too Big in
1712 * case the original packet was forced out that interface with
1713 * SO_BINDTODEVICE or similar. This is the next best thing to the
1714 * correct behaviour, which would be to update the MTU on all
1715 * interfaces.
1716 */
1717 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1718 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721/*
1722 * Misc support functions
1723 */
1724
1725static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1726{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001727 struct net *net = dev_net(ort->rt6i_dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001728 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1729 ort->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001732 rt->dst.input = ort->dst.input;
1733 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David S. Millerdefb3512010-12-08 21:16:57 -08001735 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001736 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 rt->rt6i_idev = ort->rt6i_idev;
1738 if (rt->rt6i_idev)
1739 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001740 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 rt->rt6i_expires = 0;
1742
1743 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1744 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1745 rt->rt6i_metric = 0;
1746
1747 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1748#ifdef CONFIG_IPV6_SUBTREES
1749 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1750#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001751 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001752 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 return rt;
1755}
1756
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001757#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001758static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001759 const struct in6_addr *prefix, int prefixlen,
1760 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001761{
1762 struct fib6_node *fn;
1763 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001764 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001765
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001766 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001767 if (table == NULL)
1768 return NULL;
1769
1770 write_lock_bh(&table->tb6_lock);
1771 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001772 if (!fn)
1773 goto out;
1774
Changli Gaod8d1f302010-06-10 23:31:35 -07001775 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001776 if (rt->rt6i_dev->ifindex != ifindex)
1777 continue;
1778 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1779 continue;
1780 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1781 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001783 break;
1784 }
1785out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001786 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001787 return rt;
1788}
1789
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001790static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001791 const struct in6_addr *prefix, int prefixlen,
1792 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001793 unsigned pref)
1794{
Thomas Graf86872cb2006-08-22 00:01:08 -07001795 struct fib6_config cfg = {
1796 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001797 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 .fc_ifindex = ifindex,
1799 .fc_dst_len = prefixlen,
1800 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1801 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001802 .fc_nlinfo.pid = 0,
1803 .fc_nlinfo.nlh = NULL,
1804 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001806
Thomas Graf86872cb2006-08-22 00:01:08 -07001807 ipv6_addr_copy(&cfg.fc_dst, prefix);
1808 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1809
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001810 /* We should treat it as a default route if prefix length is 0. */
1811 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001812 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001813
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001815
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001816 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001817}
1818#endif
1819
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001820struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001823 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001825 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001826 if (table == NULL)
1827 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Thomas Grafc71099a2006-08-04 23:20:06 -07001829 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001830 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001832 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1834 break;
1835 }
1836 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001837 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001838 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return rt;
1840}
1841
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001842struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001843 struct net_device *dev,
1844 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Thomas Graf86872cb2006-08-22 00:01:08 -07001846 struct fib6_config cfg = {
1847 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001848 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001849 .fc_ifindex = dev->ifindex,
1850 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1851 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001852 .fc_nlinfo.pid = 0,
1853 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001854 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Thomas Graf86872cb2006-08-22 00:01:08 -07001857 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Thomas Graf86872cb2006-08-22 00:01:08 -07001859 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return rt6_get_dflt_router(gwaddr, dev);
1862}
1863
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001864void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001867 struct fib6_table *table;
1868
1869 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001870 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001871 if (table == NULL)
1872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001875 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001876 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001878 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001879 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001880 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto restart;
1882 }
1883 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001884 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Daniel Lezcano55786892008-03-04 13:47:47 -08001887static void rtmsg_to_fib6_config(struct net *net,
1888 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001889 struct fib6_config *cfg)
1890{
1891 memset(cfg, 0, sizeof(*cfg));
1892
1893 cfg->fc_table = RT6_TABLE_MAIN;
1894 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1895 cfg->fc_metric = rtmsg->rtmsg_metric;
1896 cfg->fc_expires = rtmsg->rtmsg_info;
1897 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1898 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1899 cfg->fc_flags = rtmsg->rtmsg_flags;
1900
Daniel Lezcano55786892008-03-04 13:47:47 -08001901 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001902
Thomas Graf86872cb2006-08-22 00:01:08 -07001903 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1904 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1905 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1906}
1907
Daniel Lezcano55786892008-03-04 13:47:47 -08001908int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Thomas Graf86872cb2006-08-22 00:01:08 -07001910 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct in6_rtmsg rtmsg;
1912 int err;
1913
1914 switch(cmd) {
1915 case SIOCADDRT: /* Add a route */
1916 case SIOCDELRT: /* Delete a route */
1917 if (!capable(CAP_NET_ADMIN))
1918 return -EPERM;
1919 err = copy_from_user(&rtmsg, arg,
1920 sizeof(struct in6_rtmsg));
1921 if (err)
1922 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001923
Daniel Lezcano55786892008-03-04 13:47:47 -08001924 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 rtnl_lock();
1927 switch (cmd) {
1928 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001929 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001932 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
1934 default:
1935 err = -EINVAL;
1936 }
1937 rtnl_unlock();
1938
1939 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return -EINVAL;
1943}
1944
1945/*
1946 * Drop the packet on the floor
1947 */
1948
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001949static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001951 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001952 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001953 switch (ipstats_mib_noroutes) {
1954 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001955 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001956 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001957 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1958 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001959 break;
1960 }
1961 /* FALLTHROUGH */
1962 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001963 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1964 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001965 break;
1966 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001967 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 kfree_skb(skb);
1969 return 0;
1970}
1971
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001972static int ip6_pkt_discard(struct sk_buff *skb)
1973{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001974 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001975}
1976
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001977static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Eric Dumazetadf30902009-06-02 05:19:30 +00001979 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001980 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
David S. Miller6723ab52006-10-18 21:20:57 -07001983#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1984
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001985static int ip6_pkt_prohibit(struct sk_buff *skb)
1986{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001987 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001988}
1989
1990static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1991{
Eric Dumazetadf30902009-06-02 05:19:30 +00001992 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001993 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001994}
1995
David S. Miller6723ab52006-10-18 21:20:57 -07001996#endif
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998/*
1999 * Allocate a dst for local (unicast / anycast) address.
2000 */
2001
2002struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2003 const struct in6_addr *addr,
2004 int anycast)
2005{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002006 struct net *net = dev_net(idev->dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002007 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2008 net->loopback_dev);
David S. Miller14deae42009-01-04 16:04:39 -08002009 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Ben Greear40385652010-11-08 12:33:48 +00002011 if (rt == NULL) {
2012 if (net_ratelimit())
2013 pr_warning("IPv6: Maximum number of routes reached,"
2014 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 in6_dev_hold(idev);
2019
David S. Miller11d53b42011-06-24 15:23:34 -07002020 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002021 rt->dst.input = ip6_input;
2022 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002024 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002027 if (anycast)
2028 rt->rt6i_flags |= RTF_ANYCAST;
2029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002031 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2032 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002033 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002034
David S. Miller29546a62011-03-03 12:10:37 -08002035 return ERR_CAST(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
David S. Miller14deae42009-01-04 16:04:39 -08002037 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2040 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002041 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Changli Gaod8d1f302010-06-10 23:31:35 -07002043 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 return rt;
2046}
2047
Daniel Walterc3968a82011-04-13 21:10:57 +00002048int ip6_route_get_saddr(struct net *net,
2049 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002050 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002051 unsigned int prefs,
2052 struct in6_addr *saddr)
2053{
2054 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2055 int err = 0;
2056 if (rt->rt6i_prefsrc.plen)
2057 ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2058 else
2059 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2060 daddr, prefs, saddr);
2061 return err;
2062}
2063
2064/* remove deleted ip from prefsrc entries */
2065struct arg_dev_net_ip {
2066 struct net_device *dev;
2067 struct net *net;
2068 struct in6_addr *addr;
2069};
2070
2071static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2072{
2073 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2074 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2075 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2076
2077 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2078 rt != net->ipv6.ip6_null_entry &&
2079 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2080 /* remove prefsrc entry */
2081 rt->rt6i_prefsrc.plen = 0;
2082 }
2083 return 0;
2084}
2085
2086void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2087{
2088 struct net *net = dev_net(ifp->idev->dev);
2089 struct arg_dev_net_ip adni = {
2090 .dev = ifp->idev->dev,
2091 .net = net,
2092 .addr = &ifp->addr,
2093 };
2094 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2095}
2096
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002097struct arg_dev_net {
2098 struct net_device *dev;
2099 struct net *net;
2100};
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102static int fib6_ifdown(struct rt6_info *rt, void *arg)
2103{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002104 const struct arg_dev_net *adn = arg;
2105 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002106
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002107 if ((rt->rt6i_dev == dev || dev == NULL) &&
2108 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 RT6_TRACE("deleted by ifdown %p\n", rt);
2110 return -1;
2111 }
2112 return 0;
2113}
2114
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002115void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002117 struct arg_dev_net adn = {
2118 .dev = dev,
2119 .net = net,
2120 };
2121
2122 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002123 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126struct rt6_mtu_change_arg
2127{
2128 struct net_device *dev;
2129 unsigned mtu;
2130};
2131
2132static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2133{
2134 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2135 struct inet6_dev *idev;
2136
2137 /* In IPv6 pmtu discovery is not optional,
2138 so that RTAX_MTU lock cannot disable it.
2139 We still use this lock to block changes
2140 caused by addrconf/ndisc.
2141 */
2142
2143 idev = __in6_dev_get(arg->dev);
2144 if (idev == NULL)
2145 return 0;
2146
2147 /* For administrative MTU increase, there is no way to discover
2148 IPv6 PMTU increase, so PMTU increase should be updated here.
2149 Since RFC 1981 doesn't include administrative MTU increase
2150 update PMTU increase is a MUST. (i.e. jumbo frame)
2151 */
2152 /*
2153 If new MTU is less than route PMTU, this new MTU will be the
2154 lowest MTU in the path, update the route PMTU to reflect PMTU
2155 decreases; if new MTU is greater than route PMTU, and the
2156 old MTU is the lowest MTU in the path, update the route PMTU
2157 to reflect the increase. In this case if the other nodes' MTU
2158 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2159 PMTU discouvery.
2160 */
2161 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002162 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2163 (dst_mtu(&rt->dst) >= arg->mtu ||
2164 (dst_mtu(&rt->dst) < arg->mtu &&
2165 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002166 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169}
2170
2171void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2172{
Thomas Grafc71099a2006-08-04 23:20:06 -07002173 struct rt6_mtu_change_arg arg = {
2174 .dev = dev,
2175 .mtu = mtu,
2176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002178 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002181static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002182 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002183 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002184 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002185 [RTA_PRIORITY] = { .type = NLA_U32 },
2186 [RTA_METRICS] = { .type = NLA_NESTED },
2187};
2188
2189static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2190 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Thomas Graf86872cb2006-08-22 00:01:08 -07002192 struct rtmsg *rtm;
2193 struct nlattr *tb[RTA_MAX+1];
2194 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Thomas Graf86872cb2006-08-22 00:01:08 -07002196 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2197 if (err < 0)
2198 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Thomas Graf86872cb2006-08-22 00:01:08 -07002200 err = -EINVAL;
2201 rtm = nlmsg_data(nlh);
2202 memset(cfg, 0, sizeof(*cfg));
2203
2204 cfg->fc_table = rtm->rtm_table;
2205 cfg->fc_dst_len = rtm->rtm_dst_len;
2206 cfg->fc_src_len = rtm->rtm_src_len;
2207 cfg->fc_flags = RTF_UP;
2208 cfg->fc_protocol = rtm->rtm_protocol;
2209
2210 if (rtm->rtm_type == RTN_UNREACHABLE)
2211 cfg->fc_flags |= RTF_REJECT;
2212
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002213 if (rtm->rtm_type == RTN_LOCAL)
2214 cfg->fc_flags |= RTF_LOCAL;
2215
Thomas Graf86872cb2006-08-22 00:01:08 -07002216 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2217 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002218 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002219
2220 if (tb[RTA_GATEWAY]) {
2221 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2222 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002224
2225 if (tb[RTA_DST]) {
2226 int plen = (rtm->rtm_dst_len + 7) >> 3;
2227
2228 if (nla_len(tb[RTA_DST]) < plen)
2229 goto errout;
2230
2231 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002233
2234 if (tb[RTA_SRC]) {
2235 int plen = (rtm->rtm_src_len + 7) >> 3;
2236
2237 if (nla_len(tb[RTA_SRC]) < plen)
2238 goto errout;
2239
2240 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002242
Daniel Walterc3968a82011-04-13 21:10:57 +00002243 if (tb[RTA_PREFSRC])
2244 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2245
Thomas Graf86872cb2006-08-22 00:01:08 -07002246 if (tb[RTA_OIF])
2247 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2248
2249 if (tb[RTA_PRIORITY])
2250 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2251
2252 if (tb[RTA_METRICS]) {
2253 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2254 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002256
2257 if (tb[RTA_TABLE])
2258 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2259
2260 err = 0;
2261errout:
2262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Thomas Grafc127ea22007-03-22 11:58:32 -07002265static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Thomas Graf86872cb2006-08-22 00:01:08 -07002267 struct fib6_config cfg;
2268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Thomas Graf86872cb2006-08-22 00:01:08 -07002270 err = rtm_to_fib6_config(skb, nlh, &cfg);
2271 if (err < 0)
2272 return err;
2273
2274 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Thomas Grafc127ea22007-03-22 11:58:32 -07002277static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Thomas Graf86872cb2006-08-22 00:01:08 -07002279 struct fib6_config cfg;
2280 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Thomas Graf86872cb2006-08-22 00:01:08 -07002282 err = rtm_to_fib6_config(skb, nlh, &cfg);
2283 if (err < 0)
2284 return err;
2285
2286 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Thomas Graf339bf982006-11-10 14:10:15 -08002289static inline size_t rt6_nlmsg_size(void)
2290{
2291 return NLMSG_ALIGN(sizeof(struct rtmsg))
2292 + nla_total_size(16) /* RTA_SRC */
2293 + nla_total_size(16) /* RTA_DST */
2294 + nla_total_size(16) /* RTA_GATEWAY */
2295 + nla_total_size(16) /* RTA_PREFSRC */
2296 + nla_total_size(4) /* RTA_TABLE */
2297 + nla_total_size(4) /* RTA_IIF */
2298 + nla_total_size(4) /* RTA_OIF */
2299 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002300 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002301 + nla_total_size(sizeof(struct rta_cacheinfo));
2302}
2303
Brian Haley191cd582008-08-14 15:33:21 -07002304static int rt6_fill_node(struct net *net,
2305 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002306 struct in6_addr *dst, struct in6_addr *src,
2307 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002308 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002311 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002312 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002313 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (prefix) { /* user wants prefix routes only */
2316 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2317 /* success since this is not a prefix route */
2318 return 1;
2319 }
2320 }
2321
Thomas Graf2d7202b2006-08-22 00:01:27 -07002322 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2323 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002324 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002325
2326 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 rtm->rtm_family = AF_INET6;
2328 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2329 rtm->rtm_src_len = rt->rt6i_src.plen;
2330 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002331 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002332 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002333 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002334 table = RT6_TABLE_UNSPEC;
2335 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002336 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (rt->rt6i_flags&RTF_REJECT)
2338 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002339 else if (rt->rt6i_flags&RTF_LOCAL)
2340 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2342 rtm->rtm_type = RTN_LOCAL;
2343 else
2344 rtm->rtm_type = RTN_UNICAST;
2345 rtm->rtm_flags = 0;
2346 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2347 rtm->rtm_protocol = rt->rt6i_protocol;
2348 if (rt->rt6i_flags&RTF_DYNAMIC)
2349 rtm->rtm_protocol = RTPROT_REDIRECT;
2350 else if (rt->rt6i_flags & RTF_ADDRCONF)
2351 rtm->rtm_protocol = RTPROT_KERNEL;
2352 else if (rt->rt6i_flags&RTF_DEFAULT)
2353 rtm->rtm_protocol = RTPROT_RA;
2354
2355 if (rt->rt6i_flags&RTF_CACHE)
2356 rtm->rtm_flags |= RTM_F_CLONED;
2357
2358 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002359 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002360 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002362 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363#ifdef CONFIG_IPV6_SUBTREES
2364 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002365 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002366 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002368 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002370 if (iif) {
2371#ifdef CONFIG_IPV6_MROUTE
2372 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002373 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002374 if (err <= 0) {
2375 if (!nowait) {
2376 if (err == 0)
2377 return 0;
2378 goto nla_put_failure;
2379 } else {
2380 if (err == -EMSGSIZE)
2381 goto nla_put_failure;
2382 }
2383 }
2384 } else
2385#endif
2386 NLA_PUT_U32(skb, RTA_IIF, iif);
2387 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002389 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002390 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002392
Daniel Walterc3968a82011-04-13 21:10:57 +00002393 if (rt->rt6i_prefsrc.plen) {
2394 struct in6_addr saddr_buf;
2395 ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
2396 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2397 }
2398
David S. Millerdefb3512010-12-08 21:16:57 -08002399 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002400 goto nla_put_failure;
2401
Changli Gaod8d1f302010-06-10 23:31:35 -07002402 if (rt->dst.neighbour)
2403 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002404
Changli Gaod8d1f302010-06-10 23:31:35 -07002405 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002406 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2407
2408 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002409
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002410 if (!(rt->rt6i_flags & RTF_EXPIRES))
2411 expires = 0;
2412 else if (rt->rt6i_expires - jiffies < INT_MAX)
2413 expires = rt->rt6i_expires - jiffies;
2414 else
2415 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002416
Changli Gaod8d1f302010-06-10 23:31:35 -07002417 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2418 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002419 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Thomas Graf2d7202b2006-08-22 00:01:27 -07002421 return nlmsg_end(skb, nlh);
2422
2423nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002424 nlmsg_cancel(skb, nlh);
2425 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Patrick McHardy1b43af52006-08-10 23:11:17 -07002428int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2431 int prefix;
2432
Thomas Graf2d7202b2006-08-22 00:01:27 -07002433 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2434 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2436 } else
2437 prefix = 0;
2438
Brian Haley191cd582008-08-14 15:33:21 -07002439 return rt6_fill_node(arg->net,
2440 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002442 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Thomas Grafc127ea22007-03-22 11:58:32 -07002445static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002447 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002448 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002450 struct sk_buff *skb;
2451 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002452 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002453 int err, iif = 0;
2454
2455 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2456 if (err < 0)
2457 goto errout;
2458
2459 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002460 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002461
2462 if (tb[RTA_SRC]) {
2463 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2464 goto errout;
2465
David S. Miller4c9483b2011-03-12 16:22:43 -05002466 ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
Thomas Grafab364a62006-08-22 00:01:47 -07002467 }
2468
2469 if (tb[RTA_DST]) {
2470 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2471 goto errout;
2472
David S. Miller4c9483b2011-03-12 16:22:43 -05002473 ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
Thomas Grafab364a62006-08-22 00:01:47 -07002474 }
2475
2476 if (tb[RTA_IIF])
2477 iif = nla_get_u32(tb[RTA_IIF]);
2478
2479 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002480 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002481
2482 if (iif) {
2483 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002484 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002485 if (!dev) {
2486 err = -ENODEV;
2487 goto errout;
2488 }
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002492 if (skb == NULL) {
2493 err = -ENOBUFS;
2494 goto errout;
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 /* Reserve room for dummy headers, this skb can pass
2498 through good chunk of routing engine.
2499 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002500 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2502
David S. Miller4c9483b2011-03-12 16:22:43 -05002503 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002504 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
David S. Miller4c9483b2011-03-12 16:22:43 -05002506 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002508 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002510 kfree_skb(skb);
2511 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
2513
Daniel Lezcano55786892008-03-04 13:47:47 -08002514 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002515errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Thomas Graf86872cb2006-08-22 00:01:08 -07002519void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002522 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002523 u32 seq;
2524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002526 err = -ENOBUFS;
2527 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002528
Thomas Graf339bf982006-11-10 14:10:15 -08002529 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002530 if (skb == NULL)
2531 goto errout;
2532
Brian Haley191cd582008-08-14 15:33:21 -07002533 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002534 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002535 if (err < 0) {
2536 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2537 WARN_ON(err == -EMSGSIZE);
2538 kfree_skb(skb);
2539 goto errout;
2540 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002541 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2542 info->nlh, gfp_any());
2543 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002544errout:
2545 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002546 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002549static int ip6_route_dev_notify(struct notifier_block *this,
2550 unsigned long event, void *data)
2551{
2552 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002553 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002554
2555 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002556 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002557 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2558#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002559 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002560 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002561 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002562 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2563#endif
2564 }
2565
2566 return NOTIFY_OK;
2567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/*
2570 * /proc
2571 */
2572
2573#ifdef CONFIG_PROC_FS
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575struct rt6_proc_arg
2576{
2577 char *buffer;
2578 int offset;
2579 int length;
2580 int skip;
2581 int len;
2582};
2583
2584static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2585{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002586 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002588 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002591 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002593 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594#endif
2595
2596 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002597 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002599 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002601 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002602 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2603 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002604 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return 0;
2606}
2607
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002608static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002610 struct net *net = (struct net *)m->private;
2611 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002615static int ipv6_route_open(struct inode *inode, struct file *file)
2616{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002617 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002618}
2619
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002620static const struct file_operations ipv6_route_proc_fops = {
2621 .owner = THIS_MODULE,
2622 .open = ipv6_route_open,
2623 .read = seq_read,
2624 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002625 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002626};
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2629{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002630 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002632 net->ipv6.rt6_stats->fib_nodes,
2633 net->ipv6.rt6_stats->fib_route_nodes,
2634 net->ipv6.rt6_stats->fib_rt_alloc,
2635 net->ipv6.rt6_stats->fib_rt_entries,
2636 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002637 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002638 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 return 0;
2641}
2642
2643static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2644{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002645 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002646}
2647
Arjan van de Ven9a321442007-02-12 00:55:35 -08002648static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 .owner = THIS_MODULE,
2650 .open = rt6_stats_seq_open,
2651 .read = seq_read,
2652 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002653 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654};
2655#endif /* CONFIG_PROC_FS */
2656
2657#ifdef CONFIG_SYSCTL
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002660int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 void __user *buffer, size_t *lenp, loff_t *ppos)
2662{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002663 struct net *net;
2664 int delay;
2665 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002667
2668 net = (struct net *)ctl->extra1;
2669 delay = net->ipv6.sysctl.flush_delay;
2670 proc_dointvec(ctl, write, buffer, lenp, ppos);
2671 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002675ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002676 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002678 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .maxlen = sizeof(int),
Dave Jones89c8b3a2005-04-28 12:11:49 -07002680 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002681 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 },
2683 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002685 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .maxlen = sizeof(int),
2687 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002688 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 },
2690 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002692 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 .maxlen = sizeof(int),
2694 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002695 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 },
2697 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002699 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 .maxlen = sizeof(int),
2701 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002702 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 },
2704 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002706 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 .maxlen = sizeof(int),
2708 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002709 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 },
2711 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002713 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .maxlen = sizeof(int),
2715 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002716 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 },
2718 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002720 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .maxlen = sizeof(int),
2722 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002723 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 },
2725 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002727 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .maxlen = sizeof(int),
2729 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002730 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 },
2732 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002734 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .maxlen = sizeof(int),
2736 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002737 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 },
2739 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002741 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .maxlen = sizeof(int),
2743 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002744 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002746 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747};
2748
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002749struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002750{
2751 struct ctl_table *table;
2752
2753 table = kmemdup(ipv6_route_table_template,
2754 sizeof(ipv6_route_table_template),
2755 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002756
2757 if (table) {
2758 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002759 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002760 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002761 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2762 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2763 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2764 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2765 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2766 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2767 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002768 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002769 }
2770
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002771 return table;
2772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773#endif
2774
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002775static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002776{
Pavel Emelyanov633d4242008-04-21 14:25:23 -07002777 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002778
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002779 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2780 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002781
Eric Dumazetfc66f952010-10-08 06:37:34 +00002782 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2783 goto out_ip6_dst_ops;
2784
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002785 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2786 sizeof(*net->ipv6.ip6_null_entry),
2787 GFP_KERNEL);
2788 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002789 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002790 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002791 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002792 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002793 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2794 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002795
2796#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2797 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2798 sizeof(*net->ipv6.ip6_prohibit_entry),
2799 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002800 if (!net->ipv6.ip6_prohibit_entry)
2801 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002802 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002803 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002804 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002805 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2806 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002807
2808 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2809 sizeof(*net->ipv6.ip6_blk_hole_entry),
2810 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002811 if (!net->ipv6.ip6_blk_hole_entry)
2812 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002813 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002814 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002815 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002816 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2817 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002818#endif
2819
Peter Zijlstrab339a472008-10-07 14:15:00 -07002820 net->ipv6.sysctl.flush_delay = 0;
2821 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2822 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2823 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2824 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2825 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2826 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2827 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2828
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002829#ifdef CONFIG_PROC_FS
2830 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2831 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2832#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002833 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2834
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002835 ret = 0;
2836out:
2837 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002838
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002839#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2840out_ip6_prohibit_entry:
2841 kfree(net->ipv6.ip6_prohibit_entry);
2842out_ip6_null_entry:
2843 kfree(net->ipv6.ip6_null_entry);
2844#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002845out_ip6_dst_entries:
2846 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002847out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002848 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002849}
2850
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002851static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002852{
2853#ifdef CONFIG_PROC_FS
2854 proc_net_remove(net, "ipv6_route");
2855 proc_net_remove(net, "rt6_stats");
2856#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002857 kfree(net->ipv6.ip6_null_entry);
2858#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2859 kfree(net->ipv6.ip6_prohibit_entry);
2860 kfree(net->ipv6.ip6_blk_hole_entry);
2861#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002862 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002863}
2864
2865static struct pernet_operations ip6_route_net_ops = {
2866 .init = ip6_route_net_init,
2867 .exit = ip6_route_net_exit,
2868};
2869
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002870static struct notifier_block ip6_route_dev_notifier = {
2871 .notifier_call = ip6_route_dev_notify,
2872 .priority = 0,
2873};
2874
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002875int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002877 int ret;
2878
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002879 ret = -ENOMEM;
2880 ip6_dst_ops_template.kmem_cachep =
2881 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2882 SLAB_HWCACHE_ALIGN, NULL);
2883 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002884 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002885
Eric Dumazetfc66f952010-10-08 06:37:34 +00002886 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002887 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002888 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002889
Eric Dumazetfc66f952010-10-08 06:37:34 +00002890 ret = register_pernet_subsys(&ip6_route_net_ops);
2891 if (ret)
2892 goto out_dst_entries;
2893
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002894 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2895
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002896 /* Registering of the loopback is done before this portion of code,
2897 * the loopback reference in rt6_info will not be taken, do it
2898 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002899 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002900 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2901 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002902 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002903 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002904 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002905 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2906 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002907 ret = fib6_init();
2908 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002909 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002910
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002911 ret = xfrm6_init();
2912 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002913 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002914
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002915 ret = fib6_rules_init();
2916 if (ret)
2917 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002918
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002919 ret = -ENOBUFS;
2920 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2921 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2922 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2923 goto fib6_rules_init;
2924
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002925 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002926 if (ret)
2927 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002928
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002929out:
2930 return ret;
2931
2932fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002933 fib6_rules_cleanup();
2934xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002935 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002936out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002937 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002938out_register_subsys:
2939 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002940out_dst_entries:
2941 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002942out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002943 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
2947void ip6_route_cleanup(void)
2948{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002949 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002950 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002953 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002954 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002955 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}