blob: f1be5c5c85ef6c0256b6d1d3e0939a606c633722 [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 Hideaki1ddef0442006-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
1065#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001066 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001067 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 : 0;
1069 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1070 rt->rt6i_dst.plen = 128;
1071#endif
1072
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001073 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001074 rt->dst.next = icmp6_dst_gc_list;
1075 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001076 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Daniel Lezcano55786892008-03-04 13:47:47 -08001078 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001081 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001084int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001086 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001087 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001089 spin_lock_bh(&icmp6_dst_lock);
1090 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 while ((dst = *pprev) != NULL) {
1093 if (!atomic_read(&dst->__refcnt)) {
1094 *pprev = dst->next;
1095 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
1097 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001098 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 }
1101
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001102 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001103
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001104 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
David S. Miller1e493d12008-09-10 17:27:15 -07001107static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1108 void *arg)
1109{
1110 struct dst_entry *dst, **pprev;
1111
1112 spin_lock_bh(&icmp6_dst_lock);
1113 pprev = &icmp6_dst_gc_list;
1114 while ((dst = *pprev) != NULL) {
1115 struct rt6_info *rt = (struct rt6_info *) dst;
1116 if (func(rt, arg)) {
1117 *pprev = dst->next;
1118 dst_free(dst);
1119 } else {
1120 pprev = &dst->next;
1121 }
1122 }
1123 spin_unlock_bh(&icmp6_dst_lock);
1124}
1125
Daniel Lezcano569d3642008-01-18 03:56:57 -08001126static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001129 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001130 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1131 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1132 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1133 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1134 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001135 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Eric Dumazetfc66f952010-10-08 06:37:34 +00001137 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001138 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001139 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto out;
1141
Benjamin Thery6891a342008-03-04 13:49:47 -08001142 net->ipv6.ip6_rt_gc_expire++;
1143 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1144 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001145 entries = dst_entries_get_slow(ops);
1146 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001147 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001149 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001150 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153/* Clean host part of a prefix. Not necessary in radix tree,
1154 but results in cleaner routing tables.
1155
1156 Remove it only when all the things will work!
1157 */
1158
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001159int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
David S. Miller5170ae82010-12-12 21:35:57 -08001161 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001162 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001163 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001164 struct inet6_dev *idev;
1165
1166 rcu_read_lock();
1167 idev = __in6_dev_get(dev);
1168 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001169 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001170 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001171 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001172 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 return hoplimit;
1175}
David S. Millerabbf46a2010-12-12 21:14:46 -08001176EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178/*
1179 *
1180 */
1181
Thomas Graf86872cb2006-08-22 00:01:08 -07001182int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001185 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct rt6_info *rt = NULL;
1187 struct net_device *dev = NULL;
1188 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001189 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int addr_type;
1191
Thomas Graf86872cb2006-08-22 00:01:08 -07001192 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return -EINVAL;
1194#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001195 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EINVAL;
1197#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001198 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001200 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!dev)
1202 goto out;
1203 idev = in6_dev_get(dev);
1204 if (!idev)
1205 goto out;
1206 }
1207
Thomas Graf86872cb2006-08-22 00:01:08 -07001208 if (cfg->fc_metric == 0)
1209 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Daniel Lezcano55786892008-03-04 13:47:47 -08001211 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001212 if (table == NULL) {
1213 err = -ENOBUFS;
1214 goto out;
1215 }
1216
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001217 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (rt == NULL) {
1220 err = -ENOMEM;
1221 goto out;
1222 }
1223
Changli Gaod8d1f302010-06-10 23:31:35 -07001224 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001225 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1226 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1227 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Thomas Graf86872cb2006-08-22 00:01:08 -07001229 if (cfg->fc_protocol == RTPROT_UNSPEC)
1230 cfg->fc_protocol = RTPROT_BOOT;
1231 rt->rt6i_protocol = cfg->fc_protocol;
1232
1233 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001236 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001237 else if (cfg->fc_flags & RTF_LOCAL)
1238 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001240 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Changli Gaod8d1f302010-06-10 23:31:35 -07001242 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Thomas Graf86872cb2006-08-22 00:01:08 -07001244 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1245 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001247 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001250 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1251 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#endif
1253
Thomas Graf86872cb2006-08-22 00:01:08 -07001254 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* We cannot add true routes via loopback here,
1257 they would result in kernel looping; promote them to reject routes
1258 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001259 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001260 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1261 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001263 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (dev) {
1265 dev_put(dev);
1266 in6_dev_put(idev);
1267 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001268 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 dev_hold(dev);
1270 idev = in6_dev_get(dev);
1271 if (!idev) {
1272 err = -ENODEV;
1273 goto out;
1274 }
1275 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001276 rt->dst.output = ip6_pkt_discard_out;
1277 rt->dst.input = ip6_pkt_discard;
1278 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1280 goto install_route;
1281 }
1282
Thomas Graf86872cb2006-08-22 00:01:08 -07001283 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001284 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int gwa_type;
1286
Thomas Graf86872cb2006-08-22 00:01:08 -07001287 gw_addr = &cfg->fc_gateway;
1288 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 gwa_type = ipv6_addr_type(gw_addr);
1290
1291 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1292 struct rt6_info *grt;
1293
1294 /* IPv6 strictly inhibits using not link-local
1295 addresses as nexthop address.
1296 Otherwise, router will not able to send redirects.
1297 It is very good, but in some (rare!) circumstances
1298 (SIT, PtP, NBMA NOARP links) it is handy to allow
1299 some exceptions. --ANK
1300 */
1301 err = -EINVAL;
1302 if (!(gwa_type&IPV6_ADDR_UNICAST))
1303 goto out;
1304
Daniel Lezcano55786892008-03-04 13:47:47 -08001305 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 err = -EHOSTUNREACH;
1308 if (grt == NULL)
1309 goto out;
1310 if (dev) {
1311 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001312 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto out;
1314 }
1315 } else {
1316 dev = grt->rt6i_dev;
1317 idev = grt->rt6i_idev;
1318 dev_hold(dev);
1319 in6_dev_hold(grt->rt6i_idev);
1320 }
1321 if (!(grt->rt6i_flags&RTF_GATEWAY))
1322 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001323 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (err)
1326 goto out;
1327 }
1328 err = -EINVAL;
1329 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1330 goto out;
1331 }
1332
1333 err = -ENODEV;
1334 if (dev == NULL)
1335 goto out;
1336
Daniel Walterc3968a82011-04-13 21:10:57 +00001337 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1338 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1339 err = -EINVAL;
1340 goto out;
1341 }
1342 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1343 rt->rt6i_prefsrc.plen = 128;
1344 } else
1345 rt->rt6i_prefsrc.plen = 0;
1346
Thomas Graf86872cb2006-08-22 00:01:08 -07001347 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1349 if (IS_ERR(rt->rt6i_nexthop)) {
1350 err = PTR_ERR(rt->rt6i_nexthop);
1351 rt->rt6i_nexthop = NULL;
1352 goto out;
1353 }
1354 }
1355
Thomas Graf86872cb2006-08-22 00:01:08 -07001356 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001359 if (cfg->fc_mx) {
1360 struct nlattr *nla;
1361 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Thomas Graf86872cb2006-08-22 00:01:08 -07001363 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001364 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001365
1366 if (type) {
1367 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 err = -EINVAL;
1369 goto out;
1370 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001371
David S. Millerdefb3512010-12-08 21:16:57 -08001372 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 }
1376
Changli Gaod8d1f302010-06-10 23:31:35 -07001377 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001379 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001380
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001381 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001382
Thomas Graf86872cb2006-08-22 00:01:08 -07001383 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385out:
1386 if (dev)
1387 dev_put(dev);
1388 if (idev)
1389 in6_dev_put(idev);
1390 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001391 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return err;
1393}
1394
Thomas Graf86872cb2006-08-22 00:01:08 -07001395static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001398 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001399 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001401 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001402 return -ENOENT;
1403
Thomas Grafc71099a2006-08-04 23:20:06 -07001404 table = rt->rt6i_table;
1405 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Thomas Graf86872cb2006-08-22 00:01:08 -07001407 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001408 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Thomas Grafc71099a2006-08-04 23:20:06 -07001410 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return err;
1413}
1414
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001415int ip6_del_rt(struct rt6_info *rt)
1416{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001417 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001418 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001419 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001420 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001421}
1422
Thomas Graf86872cb2006-08-22 00:01:08 -07001423static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Thomas Grafc71099a2006-08-04 23:20:06 -07001425 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct fib6_node *fn;
1427 struct rt6_info *rt;
1428 int err = -ESRCH;
1429
Daniel Lezcano55786892008-03-04 13:47:47 -08001430 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001431 if (table == NULL)
1432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Thomas Grafc71099a2006-08-04 23:20:06 -07001434 read_lock_bh(&table->tb6_lock);
1435
1436 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001437 &cfg->fc_dst, cfg->fc_dst_len,
1438 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001441 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001442 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001444 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001446 if (cfg->fc_flags & RTF_GATEWAY &&
1447 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001449 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001451 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001452 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Thomas Graf86872cb2006-08-22 00:01:08 -07001454 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001457 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 return err;
1460}
1461
1462/*
1463 * Handle redirects
1464 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001465struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001466 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001467 struct in6_addr gateway;
1468};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001470static struct rt6_info *__ip6_route_redirect(struct net *net,
1471 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001472 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001473 int flags)
1474{
David S. Miller4c9483b2011-03-12 16:22:43 -05001475 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001476 struct rt6_info *rt;
1477 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001480 * Get the "current" route for this destination and
1481 * check if the redirect has come from approriate router.
1482 *
1483 * RFC 2461 specifies that redirects should only be
1484 * accepted if they come from the nexthop to the target.
1485 * Due to the way the routes are chosen, this notion
1486 * is a bit fuzzy and one might need to check all possible
1487 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Thomas Grafc71099a2006-08-04 23:20:06 -07001490 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001491 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001492restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001493 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001494 /*
1495 * Current route is on-link; redirect is always invalid.
1496 *
1497 * Seems, previous statement is not true. It could
1498 * be node, which looks for us as on-link (f.e. proxy ndisc)
1499 * But then router serving it might decide, that we should
1500 * know truth 8)8) --ANK (980726).
1501 */
1502 if (rt6_check_expired(rt))
1503 continue;
1504 if (!(rt->rt6i_flags & RTF_GATEWAY))
1505 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001506 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001507 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001508 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001509 continue;
1510 break;
1511 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001512
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001513 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001514 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001515 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001516out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001517 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001518
1519 read_unlock_bh(&table->tb6_lock);
1520
1521 return rt;
1522};
1523
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001524static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1525 const struct in6_addr *src,
1526 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001527 struct net_device *dev)
1528{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001529 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001530 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001531 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001532 .fl6 = {
1533 .flowi6_oif = dev->ifindex,
1534 .daddr = *dest,
1535 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001536 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001537 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001538
Brian Haley86c36ce2009-10-07 13:58:01 -07001539 ipv6_addr_copy(&rdfl.gateway, gateway);
1540
Thomas Grafadaa70b2006-10-13 15:01:03 -07001541 if (rt6_need_strict(dest))
1542 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001543
David S. Miller4c9483b2011-03-12 16:22:43 -05001544 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001545 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001546}
1547
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001548void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1549 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001550 struct neighbour *neigh, u8 *lladdr, int on_link)
1551{
1552 struct rt6_info *rt, *nrt = NULL;
1553 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001554 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001555
1556 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1557
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001558 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (net_ratelimit())
1560 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1561 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /*
1566 * We have finally decided to accept it.
1567 */
1568
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001569 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1571 NEIGH_UPDATE_F_OVERRIDE|
1572 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1573 NEIGH_UPDATE_F_ISROUTER))
1574 );
1575
1576 /*
1577 * Redirect received -> path was valid.
1578 * Look, redirects are sent only in response to data packets,
1579 * so that this nexthop apparently is reachable. --ANK
1580 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001581 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001584 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto out;
1586
1587 nrt = ip6_rt_copy(rt);
1588 if (nrt == NULL)
1589 goto out;
1590
1591 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1592 if (on_link)
1593 nrt->rt6i_flags &= ~RTF_GATEWAY;
1594
1595 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1596 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001597 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1600 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Thomas Graf40e22e82006-08-22 00:00:45 -07001602 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto out;
1604
Changli Gaod8d1f302010-06-10 23:31:35 -07001605 netevent.old = &rt->dst;
1606 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001607 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001610 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return;
1612 }
1613
1614out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001615 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618/*
1619 * Handle ICMP "packet too big" messages
1620 * i.e. Path MTU discovery
1621 */
1622
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001623static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001624 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 struct rt6_info *rt, *nrt;
1627 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001628again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001629 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (rt == NULL)
1631 return;
1632
Andrey Vagind3052b52010-12-11 15:20:11 +00001633 if (rt6_check_expired(rt)) {
1634 ip6_del_rt(rt);
1635 goto again;
1636 }
1637
Changli Gaod8d1f302010-06-10 23:31:35 -07001638 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto out;
1640
1641 if (pmtu < IPV6_MIN_MTU) {
1642 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001643 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * MTU (1280) and a fragment header should always be included
1645 * after a node receiving Too Big message reporting PMTU is
1646 * less than the IPv6 Minimum Link MTU.
1647 */
1648 pmtu = IPV6_MIN_MTU;
1649 allfrag = 1;
1650 }
1651
1652 /* New mtu received -> path was valid.
1653 They are sent only in response to data packets,
1654 so that this nexthop apparently is reachable. --ANK
1655 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001656 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* Host route. If it is static, it would be better
1659 not to override it, but add new one, so that
1660 when cache entry will expire old pmtu
1661 would return automatically.
1662 */
1663 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001664 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1665 if (allfrag) {
1666 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1667 features |= RTAX_FEATURE_ALLFRAG;
1668 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1669 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001670 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1672 goto out;
1673 }
1674
1675 /* Network route.
1676 Two cases are possible:
1677 1. It is connected route. Action: COW
1678 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1679 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001680 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001681 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001682 else
1683 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001684
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001685 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001686 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1687 if (allfrag) {
1688 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1689 features |= RTAX_FEATURE_ALLFRAG;
1690 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1691 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001692
1693 /* According to RFC 1981, detecting PMTU increase shouldn't be
1694 * happened within 5 mins, the recommended timer is 10 mins.
1695 * Here this route expiration time is set to ip6_rt_mtu_expires
1696 * which is 10 mins. After 10 mins the decreased pmtu is expired
1697 * and detecting PMTU increase will be automatically happened.
1698 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001699 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001700 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1701
Thomas Graf40e22e82006-08-22 00:00:45 -07001702 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001705 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001708void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001709 struct net_device *dev, u32 pmtu)
1710{
1711 struct net *net = dev_net(dev);
1712
1713 /*
1714 * RFC 1981 states that a node "MUST reduce the size of the packets it
1715 * is sending along the path" that caused the Packet Too Big message.
1716 * Since it's not possible in the general case to determine which
1717 * interface was used to send the original packet, we update the MTU
1718 * on the interface that will be used to send future packets. We also
1719 * update the MTU on the interface that received the Packet Too Big in
1720 * case the original packet was forced out that interface with
1721 * SO_BINDTODEVICE or similar. This is the next best thing to the
1722 * correct behaviour, which would be to update the MTU on all
1723 * interfaces.
1724 */
1725 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1726 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * Misc support functions
1731 */
1732
1733static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1734{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001735 struct net *net = dev_net(ort->rt6i_dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001736 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1737 ort->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001740 rt->dst.input = ort->dst.input;
1741 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
David S. Millerdefb3512010-12-08 21:16:57 -08001743 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001744 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 rt->rt6i_idev = ort->rt6i_idev;
1746 if (rt->rt6i_idev)
1747 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001748 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rt->rt6i_expires = 0;
1750
1751 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1752 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1753 rt->rt6i_metric = 0;
1754
1755 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1756#ifdef CONFIG_IPV6_SUBTREES
1757 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1758#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001759 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 return rt;
1762}
1763
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001764#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001765static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001766 const struct in6_addr *prefix, int prefixlen,
1767 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001768{
1769 struct fib6_node *fn;
1770 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001771 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001772
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001773 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001774 if (table == NULL)
1775 return NULL;
1776
1777 write_lock_bh(&table->tb6_lock);
1778 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001779 if (!fn)
1780 goto out;
1781
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001783 if (rt->rt6i_dev->ifindex != ifindex)
1784 continue;
1785 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1786 continue;
1787 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1788 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001789 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001790 break;
1791 }
1792out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001793 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001794 return rt;
1795}
1796
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001797static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001798 const struct in6_addr *prefix, int prefixlen,
1799 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001800 unsigned pref)
1801{
Thomas Graf86872cb2006-08-22 00:01:08 -07001802 struct fib6_config cfg = {
1803 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001804 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 .fc_ifindex = ifindex,
1806 .fc_dst_len = prefixlen,
1807 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1808 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001809 .fc_nlinfo.pid = 0,
1810 .fc_nlinfo.nlh = NULL,
1811 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001812 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001813
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 ipv6_addr_copy(&cfg.fc_dst, prefix);
1815 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1816
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001817 /* We should treat it as a default route if prefix length is 0. */
1818 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001819 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001820
Thomas Graf86872cb2006-08-22 00:01:08 -07001821 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001822
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001823 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001824}
1825#endif
1826
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001827struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001830 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001832 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001833 if (table == NULL)
1834 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Thomas Grafc71099a2006-08-04 23:20:06 -07001836 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001837 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001839 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1841 break;
1842 }
1843 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001844 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001845 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return rt;
1847}
1848
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001849struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001850 struct net_device *dev,
1851 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Thomas Graf86872cb2006-08-22 00:01:08 -07001853 struct fib6_config cfg = {
1854 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001855 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001856 .fc_ifindex = dev->ifindex,
1857 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1858 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001859 .fc_nlinfo.pid = 0,
1860 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001861 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001862 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Thomas Graf86872cb2006-08-22 00:01:08 -07001864 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Thomas Graf86872cb2006-08-22 00:01:08 -07001866 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return rt6_get_dflt_router(gwaddr, dev);
1869}
1870
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001871void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001874 struct fib6_table *table;
1875
1876 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001877 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001878 if (table == NULL)
1879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001882 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001883 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001885 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001886 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001887 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto restart;
1889 }
1890 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001891 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Daniel Lezcano55786892008-03-04 13:47:47 -08001894static void rtmsg_to_fib6_config(struct net *net,
1895 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001896 struct fib6_config *cfg)
1897{
1898 memset(cfg, 0, sizeof(*cfg));
1899
1900 cfg->fc_table = RT6_TABLE_MAIN;
1901 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1902 cfg->fc_metric = rtmsg->rtmsg_metric;
1903 cfg->fc_expires = rtmsg->rtmsg_info;
1904 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1905 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1906 cfg->fc_flags = rtmsg->rtmsg_flags;
1907
Daniel Lezcano55786892008-03-04 13:47:47 -08001908 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001909
Thomas Graf86872cb2006-08-22 00:01:08 -07001910 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1911 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1912 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1913}
1914
Daniel Lezcano55786892008-03-04 13:47:47 -08001915int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Thomas Graf86872cb2006-08-22 00:01:08 -07001917 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct in6_rtmsg rtmsg;
1919 int err;
1920
1921 switch(cmd) {
1922 case SIOCADDRT: /* Add a route */
1923 case SIOCDELRT: /* Delete a route */
1924 if (!capable(CAP_NET_ADMIN))
1925 return -EPERM;
1926 err = copy_from_user(&rtmsg, arg,
1927 sizeof(struct in6_rtmsg));
1928 if (err)
1929 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001930
Daniel Lezcano55786892008-03-04 13:47:47 -08001931 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 rtnl_lock();
1934 switch (cmd) {
1935 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001936 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001939 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 default:
1942 err = -EINVAL;
1943 }
1944 rtnl_unlock();
1945
1946 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 return -EINVAL;
1950}
1951
1952/*
1953 * Drop the packet on the floor
1954 */
1955
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001956static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001958 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001959 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001960 switch (ipstats_mib_noroutes) {
1961 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001962 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001963 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001964 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1965 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001966 break;
1967 }
1968 /* FALLTHROUGH */
1969 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001970 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1971 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001972 break;
1973 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001974 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 kfree_skb(skb);
1976 return 0;
1977}
1978
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001979static int ip6_pkt_discard(struct sk_buff *skb)
1980{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001981 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001982}
1983
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001984static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Eric Dumazetadf30902009-06-02 05:19:30 +00001986 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001987 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
David S. Miller6723ab52006-10-18 21:20:57 -07001990#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1991
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001992static int ip6_pkt_prohibit(struct sk_buff *skb)
1993{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001994 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001995}
1996
1997static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1998{
Eric Dumazetadf30902009-06-02 05:19:30 +00001999 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002000 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002001}
2002
David S. Miller6723ab52006-10-18 21:20:57 -07002003#endif
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/*
2006 * Allocate a dst for local (unicast / anycast) address.
2007 */
2008
2009struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2010 const struct in6_addr *addr,
2011 int anycast)
2012{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002013 struct net *net = dev_net(idev->dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002014 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2015 net->loopback_dev);
David S. Miller14deae42009-01-04 16:04:39 -08002016 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Ben Greear40385652010-11-08 12:33:48 +00002018 if (rt == NULL) {
2019 if (net_ratelimit())
2020 pr_warning("IPv6: Maximum number of routes reached,"
2021 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 in6_dev_hold(idev);
2026
Changli Gaod8d1f302010-06-10 23:31:35 -07002027 rt->dst.flags = DST_HOST;
2028 rt->dst.input = ip6_input;
2029 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002031 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002034 if (anycast)
2035 rt->rt6i_flags |= RTF_ANYCAST;
2036 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002038 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2039 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002040 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002041
David S. Miller29546a62011-03-03 12:10:37 -08002042 return ERR_CAST(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
David S. Miller14deae42009-01-04 16:04:39 -08002044 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2047 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002048 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Changli Gaod8d1f302010-06-10 23:31:35 -07002050 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 return rt;
2053}
2054
Daniel Walterc3968a82011-04-13 21:10:57 +00002055int ip6_route_get_saddr(struct net *net,
2056 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002057 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002058 unsigned int prefs,
2059 struct in6_addr *saddr)
2060{
2061 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2062 int err = 0;
2063 if (rt->rt6i_prefsrc.plen)
2064 ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2065 else
2066 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2067 daddr, prefs, saddr);
2068 return err;
2069}
2070
2071/* remove deleted ip from prefsrc entries */
2072struct arg_dev_net_ip {
2073 struct net_device *dev;
2074 struct net *net;
2075 struct in6_addr *addr;
2076};
2077
2078static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2079{
2080 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2081 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2082 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2083
2084 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2085 rt != net->ipv6.ip6_null_entry &&
2086 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2087 /* remove prefsrc entry */
2088 rt->rt6i_prefsrc.plen = 0;
2089 }
2090 return 0;
2091}
2092
2093void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2094{
2095 struct net *net = dev_net(ifp->idev->dev);
2096 struct arg_dev_net_ip adni = {
2097 .dev = ifp->idev->dev,
2098 .net = net,
2099 .addr = &ifp->addr,
2100 };
2101 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2102}
2103
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002104struct arg_dev_net {
2105 struct net_device *dev;
2106 struct net *net;
2107};
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static int fib6_ifdown(struct rt6_info *rt, void *arg)
2110{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002111 const struct arg_dev_net *adn = arg;
2112 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002113
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002114 if ((rt->rt6i_dev == dev || dev == NULL) &&
2115 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 RT6_TRACE("deleted by ifdown %p\n", rt);
2117 return -1;
2118 }
2119 return 0;
2120}
2121
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002122void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002124 struct arg_dev_net adn = {
2125 .dev = dev,
2126 .net = net,
2127 };
2128
2129 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002130 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133struct rt6_mtu_change_arg
2134{
2135 struct net_device *dev;
2136 unsigned mtu;
2137};
2138
2139static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2140{
2141 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2142 struct inet6_dev *idev;
2143
2144 /* In IPv6 pmtu discovery is not optional,
2145 so that RTAX_MTU lock cannot disable it.
2146 We still use this lock to block changes
2147 caused by addrconf/ndisc.
2148 */
2149
2150 idev = __in6_dev_get(arg->dev);
2151 if (idev == NULL)
2152 return 0;
2153
2154 /* For administrative MTU increase, there is no way to discover
2155 IPv6 PMTU increase, so PMTU increase should be updated here.
2156 Since RFC 1981 doesn't include administrative MTU increase
2157 update PMTU increase is a MUST. (i.e. jumbo frame)
2158 */
2159 /*
2160 If new MTU is less than route PMTU, this new MTU will be the
2161 lowest MTU in the path, update the route PMTU to reflect PMTU
2162 decreases; if new MTU is greater than route PMTU, and the
2163 old MTU is the lowest MTU in the path, update the route PMTU
2164 to reflect the increase. In this case if the other nodes' MTU
2165 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2166 PMTU discouvery.
2167 */
2168 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002169 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2170 (dst_mtu(&rt->dst) >= arg->mtu ||
2171 (dst_mtu(&rt->dst) < arg->mtu &&
2172 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002173 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 0;
2176}
2177
2178void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2179{
Thomas Grafc71099a2006-08-04 23:20:06 -07002180 struct rt6_mtu_change_arg arg = {
2181 .dev = dev,
2182 .mtu = mtu,
2183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002185 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002188static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002189 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002190 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002191 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002192 [RTA_PRIORITY] = { .type = NLA_U32 },
2193 [RTA_METRICS] = { .type = NLA_NESTED },
2194};
2195
2196static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2197 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Thomas Graf86872cb2006-08-22 00:01:08 -07002199 struct rtmsg *rtm;
2200 struct nlattr *tb[RTA_MAX+1];
2201 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Thomas Graf86872cb2006-08-22 00:01:08 -07002203 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2204 if (err < 0)
2205 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf86872cb2006-08-22 00:01:08 -07002207 err = -EINVAL;
2208 rtm = nlmsg_data(nlh);
2209 memset(cfg, 0, sizeof(*cfg));
2210
2211 cfg->fc_table = rtm->rtm_table;
2212 cfg->fc_dst_len = rtm->rtm_dst_len;
2213 cfg->fc_src_len = rtm->rtm_src_len;
2214 cfg->fc_flags = RTF_UP;
2215 cfg->fc_protocol = rtm->rtm_protocol;
2216
2217 if (rtm->rtm_type == RTN_UNREACHABLE)
2218 cfg->fc_flags |= RTF_REJECT;
2219
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002220 if (rtm->rtm_type == RTN_LOCAL)
2221 cfg->fc_flags |= RTF_LOCAL;
2222
Thomas Graf86872cb2006-08-22 00:01:08 -07002223 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2224 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002225 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002226
2227 if (tb[RTA_GATEWAY]) {
2228 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2229 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002231
2232 if (tb[RTA_DST]) {
2233 int plen = (rtm->rtm_dst_len + 7) >> 3;
2234
2235 if (nla_len(tb[RTA_DST]) < plen)
2236 goto errout;
2237
2238 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002240
2241 if (tb[RTA_SRC]) {
2242 int plen = (rtm->rtm_src_len + 7) >> 3;
2243
2244 if (nla_len(tb[RTA_SRC]) < plen)
2245 goto errout;
2246
2247 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002249
Daniel Walterc3968a82011-04-13 21:10:57 +00002250 if (tb[RTA_PREFSRC])
2251 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2252
Thomas Graf86872cb2006-08-22 00:01:08 -07002253 if (tb[RTA_OIF])
2254 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2255
2256 if (tb[RTA_PRIORITY])
2257 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2258
2259 if (tb[RTA_METRICS]) {
2260 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2261 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002263
2264 if (tb[RTA_TABLE])
2265 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2266
2267 err = 0;
2268errout:
2269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Thomas Grafc127ea22007-03-22 11:58:32 -07002272static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Thomas Graf86872cb2006-08-22 00:01:08 -07002274 struct fib6_config cfg;
2275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Thomas Graf86872cb2006-08-22 00:01:08 -07002277 err = rtm_to_fib6_config(skb, nlh, &cfg);
2278 if (err < 0)
2279 return err;
2280
2281 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Thomas Grafc127ea22007-03-22 11:58:32 -07002284static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Thomas Graf86872cb2006-08-22 00:01:08 -07002286 struct fib6_config cfg;
2287 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Thomas Graf86872cb2006-08-22 00:01:08 -07002289 err = rtm_to_fib6_config(skb, nlh, &cfg);
2290 if (err < 0)
2291 return err;
2292
2293 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Thomas Graf339bf982006-11-10 14:10:15 -08002296static inline size_t rt6_nlmsg_size(void)
2297{
2298 return NLMSG_ALIGN(sizeof(struct rtmsg))
2299 + nla_total_size(16) /* RTA_SRC */
2300 + nla_total_size(16) /* RTA_DST */
2301 + nla_total_size(16) /* RTA_GATEWAY */
2302 + nla_total_size(16) /* RTA_PREFSRC */
2303 + nla_total_size(4) /* RTA_TABLE */
2304 + nla_total_size(4) /* RTA_IIF */
2305 + nla_total_size(4) /* RTA_OIF */
2306 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002307 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002308 + nla_total_size(sizeof(struct rta_cacheinfo));
2309}
2310
Brian Haley191cd582008-08-14 15:33:21 -07002311static int rt6_fill_node(struct net *net,
2312 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002313 struct in6_addr *dst, struct in6_addr *src,
2314 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002315 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002318 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002319 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002320 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 if (prefix) { /* user wants prefix routes only */
2323 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2324 /* success since this is not a prefix route */
2325 return 1;
2326 }
2327 }
2328
Thomas Graf2d7202b2006-08-22 00:01:27 -07002329 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2330 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002331 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002332
2333 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 rtm->rtm_family = AF_INET6;
2335 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2336 rtm->rtm_src_len = rt->rt6i_src.plen;
2337 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002338 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002339 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002340 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002341 table = RT6_TABLE_UNSPEC;
2342 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002343 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (rt->rt6i_flags&RTF_REJECT)
2345 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002346 else if (rt->rt6i_flags&RTF_LOCAL)
2347 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2349 rtm->rtm_type = RTN_LOCAL;
2350 else
2351 rtm->rtm_type = RTN_UNICAST;
2352 rtm->rtm_flags = 0;
2353 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2354 rtm->rtm_protocol = rt->rt6i_protocol;
2355 if (rt->rt6i_flags&RTF_DYNAMIC)
2356 rtm->rtm_protocol = RTPROT_REDIRECT;
2357 else if (rt->rt6i_flags & RTF_ADDRCONF)
2358 rtm->rtm_protocol = RTPROT_KERNEL;
2359 else if (rt->rt6i_flags&RTF_DEFAULT)
2360 rtm->rtm_protocol = RTPROT_RA;
2361
2362 if (rt->rt6i_flags&RTF_CACHE)
2363 rtm->rtm_flags |= RTM_F_CLONED;
2364
2365 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002366 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002367 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002369 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370#ifdef CONFIG_IPV6_SUBTREES
2371 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002372 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002373 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002375 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002377 if (iif) {
2378#ifdef CONFIG_IPV6_MROUTE
2379 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002380 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002381 if (err <= 0) {
2382 if (!nowait) {
2383 if (err == 0)
2384 return 0;
2385 goto nla_put_failure;
2386 } else {
2387 if (err == -EMSGSIZE)
2388 goto nla_put_failure;
2389 }
2390 }
2391 } else
2392#endif
2393 NLA_PUT_U32(skb, RTA_IIF, iif);
2394 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002396 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002397 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002399
Daniel Walterc3968a82011-04-13 21:10:57 +00002400 if (rt->rt6i_prefsrc.plen) {
2401 struct in6_addr saddr_buf;
2402 ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
2403 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2404 }
2405
David S. Millerdefb3512010-12-08 21:16:57 -08002406 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002407 goto nla_put_failure;
2408
Changli Gaod8d1f302010-06-10 23:31:35 -07002409 if (rt->dst.neighbour)
2410 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002411
Changli Gaod8d1f302010-06-10 23:31:35 -07002412 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002413 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2414
2415 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002416
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002417 if (!(rt->rt6i_flags & RTF_EXPIRES))
2418 expires = 0;
2419 else if (rt->rt6i_expires - jiffies < INT_MAX)
2420 expires = rt->rt6i_expires - jiffies;
2421 else
2422 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002423
Changli Gaod8d1f302010-06-10 23:31:35 -07002424 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2425 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002426 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Thomas Graf2d7202b2006-08-22 00:01:27 -07002428 return nlmsg_end(skb, nlh);
2429
2430nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002431 nlmsg_cancel(skb, nlh);
2432 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Patrick McHardy1b43af52006-08-10 23:11:17 -07002435int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2438 int prefix;
2439
Thomas Graf2d7202b2006-08-22 00:01:27 -07002440 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2441 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2443 } else
2444 prefix = 0;
2445
Brian Haley191cd582008-08-14 15:33:21 -07002446 return rt6_fill_node(arg->net,
2447 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002449 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Thomas Grafc127ea22007-03-22 11:58:32 -07002452static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002454 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002455 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002457 struct sk_buff *skb;
2458 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002459 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002460 int err, iif = 0;
2461
2462 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2463 if (err < 0)
2464 goto errout;
2465
2466 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002467 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002468
2469 if (tb[RTA_SRC]) {
2470 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2471 goto errout;
2472
David S. Miller4c9483b2011-03-12 16:22:43 -05002473 ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
Thomas Grafab364a62006-08-22 00:01:47 -07002474 }
2475
2476 if (tb[RTA_DST]) {
2477 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2478 goto errout;
2479
David S. Miller4c9483b2011-03-12 16:22:43 -05002480 ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
Thomas Grafab364a62006-08-22 00:01:47 -07002481 }
2482
2483 if (tb[RTA_IIF])
2484 iif = nla_get_u32(tb[RTA_IIF]);
2485
2486 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002487 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002488
2489 if (iif) {
2490 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002491 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002492 if (!dev) {
2493 err = -ENODEV;
2494 goto errout;
2495 }
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002499 if (skb == NULL) {
2500 err = -ENOBUFS;
2501 goto errout;
2502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 /* Reserve room for dummy headers, this skb can pass
2505 through good chunk of routing engine.
2506 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002507 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2509
David S. Miller4c9483b2011-03-12 16:22:43 -05002510 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002511 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
David S. Miller4c9483b2011-03-12 16:22:43 -05002513 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002515 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002517 kfree_skb(skb);
2518 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520
Daniel Lezcano55786892008-03-04 13:47:47 -08002521 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002522errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Thomas Graf86872cb2006-08-22 00:01:08 -07002526void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002529 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002530 u32 seq;
2531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002533 err = -ENOBUFS;
2534 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002535
Thomas Graf339bf982006-11-10 14:10:15 -08002536 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002537 if (skb == NULL)
2538 goto errout;
2539
Brian Haley191cd582008-08-14 15:33:21 -07002540 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002541 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002542 if (err < 0) {
2543 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2544 WARN_ON(err == -EMSGSIZE);
2545 kfree_skb(skb);
2546 goto errout;
2547 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002548 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2549 info->nlh, gfp_any());
2550 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002551errout:
2552 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002553 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002556static int ip6_route_dev_notify(struct notifier_block *this,
2557 unsigned long event, void *data)
2558{
2559 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002560 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002561
2562 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002563 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002564 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2565#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002566 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002567 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002568 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002569 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2570#endif
2571 }
2572
2573 return NOTIFY_OK;
2574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576/*
2577 * /proc
2578 */
2579
2580#ifdef CONFIG_PROC_FS
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582struct rt6_proc_arg
2583{
2584 char *buffer;
2585 int offset;
2586 int length;
2587 int skip;
2588 int len;
2589};
2590
2591static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2592{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002593 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002595 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002598 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002600 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601#endif
2602
2603 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002604 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002606 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002608 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002609 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2610 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002611 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
2613}
2614
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002615static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002617 struct net *net = (struct net *)m->private;
2618 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002622static int ipv6_route_open(struct inode *inode, struct file *file)
2623{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002624 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002625}
2626
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002627static const struct file_operations ipv6_route_proc_fops = {
2628 .owner = THIS_MODULE,
2629 .open = ipv6_route_open,
2630 .read = seq_read,
2631 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002632 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002633};
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2636{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002637 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002639 net->ipv6.rt6_stats->fib_nodes,
2640 net->ipv6.rt6_stats->fib_route_nodes,
2641 net->ipv6.rt6_stats->fib_rt_alloc,
2642 net->ipv6.rt6_stats->fib_rt_entries,
2643 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002644 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002645 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 return 0;
2648}
2649
2650static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2651{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002652 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002653}
2654
Arjan van de Ven9a321442007-02-12 00:55:35 -08002655static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 .owner = THIS_MODULE,
2657 .open = rt6_stats_seq_open,
2658 .read = seq_read,
2659 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002660 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661};
2662#endif /* CONFIG_PROC_FS */
2663
2664#ifdef CONFIG_SYSCTL
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002667int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 void __user *buffer, size_t *lenp, loff_t *ppos)
2669{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002670 struct net *net;
2671 int delay;
2672 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002674
2675 net = (struct net *)ctl->extra1;
2676 delay = net->ipv6.sysctl.flush_delay;
2677 proc_dointvec(ctl, write, buffer, lenp, ppos);
2678 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002682ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002683 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002685 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002687 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002688 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 },
2690 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002692 .data = &ip6_dst_ops_template.gc_thresh,
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 = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002699 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 },
2704 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002706 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
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_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002713 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
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_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002720 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .maxlen = sizeof(int),
2722 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002723 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 },
2725 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002727 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .maxlen = sizeof(int),
2729 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002730 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 },
2732 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002734 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .maxlen = sizeof(int),
2736 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002737 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 },
2739 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002741 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .maxlen = sizeof(int),
2743 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002744 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 },
2746 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002748 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 .maxlen = sizeof(int),
2750 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002751 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002753 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754};
2755
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002756struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002757{
2758 struct ctl_table *table;
2759
2760 table = kmemdup(ipv6_route_table_template,
2761 sizeof(ipv6_route_table_template),
2762 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002763
2764 if (table) {
2765 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002766 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002767 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002768 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2769 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2770 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2771 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2772 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2773 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2774 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002775 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002776 }
2777
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002778 return table;
2779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780#endif
2781
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002782static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002783{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002784 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002785
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002786 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2787 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002788
Eric Dumazetfc66f952010-10-08 06:37:34 +00002789 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2790 goto out_ip6_dst_ops;
2791
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002792 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2793 sizeof(*net->ipv6.ip6_null_entry),
2794 GFP_KERNEL);
2795 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002796 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002797 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002798 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002799 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002800 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2801 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002802
2803#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2804 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2805 sizeof(*net->ipv6.ip6_prohibit_entry),
2806 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002807 if (!net->ipv6.ip6_prohibit_entry)
2808 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002809 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002810 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002811 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002812 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2813 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002814
2815 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2816 sizeof(*net->ipv6.ip6_blk_hole_entry),
2817 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002818 if (!net->ipv6.ip6_blk_hole_entry)
2819 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002820 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002821 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002822 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002823 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2824 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002825#endif
2826
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002827 net->ipv6.sysctl.flush_delay = 0;
2828 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2829 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2830 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2831 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2832 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2833 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2834 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2835
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002836#ifdef CONFIG_PROC_FS
2837 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2838 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2839#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002840 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2841
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002842 ret = 0;
2843out:
2844 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002845
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002846#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2847out_ip6_prohibit_entry:
2848 kfree(net->ipv6.ip6_prohibit_entry);
2849out_ip6_null_entry:
2850 kfree(net->ipv6.ip6_null_entry);
2851#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002852out_ip6_dst_entries:
2853 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002854out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002855 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002856}
2857
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002858static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002859{
2860#ifdef CONFIG_PROC_FS
2861 proc_net_remove(net, "ipv6_route");
2862 proc_net_remove(net, "rt6_stats");
2863#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002864 kfree(net->ipv6.ip6_null_entry);
2865#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2866 kfree(net->ipv6.ip6_prohibit_entry);
2867 kfree(net->ipv6.ip6_blk_hole_entry);
2868#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002869 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002870}
2871
2872static struct pernet_operations ip6_route_net_ops = {
2873 .init = ip6_route_net_init,
2874 .exit = ip6_route_net_exit,
2875};
2876
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002877static struct notifier_block ip6_route_dev_notifier = {
2878 .notifier_call = ip6_route_dev_notify,
2879 .priority = 0,
2880};
2881
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002882int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002884 int ret;
2885
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002886 ret = -ENOMEM;
2887 ip6_dst_ops_template.kmem_cachep =
2888 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2889 SLAB_HWCACHE_ALIGN, NULL);
2890 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002891 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002892
Eric Dumazetfc66f952010-10-08 06:37:34 +00002893 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002894 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002895 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002896
Eric Dumazetfc66f952010-10-08 06:37:34 +00002897 ret = register_pernet_subsys(&ip6_route_net_ops);
2898 if (ret)
2899 goto out_dst_entries;
2900
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002901 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2902
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002903 /* Registering of the loopback is done before this portion of code,
2904 * the loopback reference in rt6_info will not be taken, do it
2905 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002906 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002907 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2908 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002909 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002910 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002911 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002912 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2913 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002914 ret = fib6_init();
2915 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002916 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002917
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002918 ret = xfrm6_init();
2919 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002920 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002921
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002922 ret = fib6_rules_init();
2923 if (ret)
2924 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002925
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002926 ret = -ENOBUFS;
2927 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2928 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2929 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2930 goto fib6_rules_init;
2931
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002932 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002933 if (ret)
2934 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002935
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002936out:
2937 return ret;
2938
2939fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002940 fib6_rules_cleanup();
2941xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002942 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002943out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002944 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002945out_register_subsys:
2946 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002947out_dst_entries:
2948 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002949out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002950 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002951 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
2954void ip6_route_cleanup(void)
2955{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002956 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002957 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002960 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002961 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002962 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}