blob: e8b2bb9060ef1723a526a908dbc1fa4352476678 [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. Miller5c1e6aa2011-04-28 14:13:38 -0700233 return (struct rt6_info *)dst_alloc(ops, dev, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static void ip6_dst_destroy(struct dst_entry *dst)
237{
238 struct rt6_info *rt = (struct rt6_info *)dst;
239 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800240 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (idev != NULL) {
243 rt->rt6i_idev = NULL;
244 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900245 }
David S. Millerb3419362010-11-30 12:27:11 -0800246 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800247 rt->rt6i_peer = NULL;
248 inet_putpeer(peer);
249 }
250}
251
David S. Miller6431cbc2011-02-07 20:38:06 -0800252static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
253
254static u32 rt6_peer_genid(void)
255{
256 return atomic_read(&__rt6_peer_genid);
257}
258
David S. Millerb3419362010-11-30 12:27:11 -0800259void rt6_bind_peer(struct rt6_info *rt, int create)
260{
261 struct inet_peer *peer;
262
David S. Millerb3419362010-11-30 12:27:11 -0800263 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
264 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
265 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800266 else
267 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
271 int how)
272{
273 struct rt6_info *rt = (struct rt6_info *)dst;
274 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800275 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900276 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800278 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
279 struct inet6_dev *loopback_idev =
280 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (loopback_idev != NULL) {
282 rt->rt6i_idev = loopback_idev;
283 in6_dev_put(idev);
284 }
285 }
286}
287
288static __inline__ int rt6_check_expired(const struct rt6_info *rt)
289{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000290 return (rt->rt6i_flags & RTF_EXPIRES) &&
291 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000294static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700295{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000296 return ipv6_addr_type(daddr) &
297 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700301 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800304static inline struct rt6_info *rt6_device_match(struct net *net,
305 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000306 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700308 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct rt6_info *local = NULL;
311 struct rt6_info *sprt;
312
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900313 if (!oif && ipv6_addr_any(saddr))
314 goto out;
315
Changli Gaod8d1f302010-06-10 23:31:35 -0700316 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900317 struct net_device *dev = sprt->rt6i_dev;
318
319 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (dev->ifindex == oif)
321 return sprt;
322 if (dev->flags & IFF_LOOPBACK) {
323 if (sprt->rt6i_idev == NULL ||
324 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700325 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900327 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 local->rt6i_idev->dev->ifindex == oif))
329 continue;
330 }
331 local = sprt;
332 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900333 } else {
334 if (ipv6_chk_addr(net, saddr, dev,
335 flags & RT6_LOOKUP_F_IFACE))
336 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900340 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (local)
342 return local;
343
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700344 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800345 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900347out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return rt;
349}
350
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800351#ifdef CONFIG_IPV6_ROUTER_PREF
352static void rt6_probe(struct rt6_info *rt)
353{
354 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
355 /*
356 * Okay, this does not seem to be appropriate
357 * for now, however, we need to check if it
358 * is really so; aka Router Reachability Probing.
359 *
360 * Router Reachability Probe MUST be rate-limited
361 * to no more than one per minute.
362 */
363 if (!neigh || (neigh->nud_state & NUD_VALID))
364 return;
365 read_lock_bh(&neigh->lock);
366 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800367 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800368 struct in6_addr mcaddr;
369 struct in6_addr *target;
370
371 neigh->updated = jiffies;
372 read_unlock_bh(&neigh->lock);
373
374 target = (struct in6_addr *)&neigh->primary_key;
375 addrconf_addr_solict_mult(target, &mcaddr);
376 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
377 } else
378 read_unlock_bh(&neigh->lock);
379}
380#else
381static inline void rt6_probe(struct rt6_info *rt)
382{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800383}
384#endif
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800387 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700389static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800391 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700392 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800393 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700394 if ((dev->flags & IFF_LOOPBACK) &&
395 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
396 return 1;
397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Dave Jonesb6f99a22007-03-22 12:27:49 -0700400static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800402 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800403 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700404 if (rt->rt6i_flags & RTF_NONEXTHOP ||
405 !(rt->rt6i_flags & RTF_GATEWAY))
406 m = 1;
407 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800408 read_lock_bh(&neigh->lock);
409 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700410 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800411#ifdef CONFIG_IPV6_ROUTER_PREF
412 else if (neigh->nud_state & NUD_FAILED)
413 m = 0;
414#endif
415 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800416 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800417 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800418 } else
419 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800420 return m;
421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800423static int rt6_score_route(struct rt6_info *rt, int oif,
424 int strict)
425{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700426 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900427
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700428 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700429 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800430 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800431#ifdef CONFIG_IPV6_ROUTER_PREF
432 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
433#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700434 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800435 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800436 return -1;
437 return m;
438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David S. Millerf11e6652007-03-24 20:36:25 -0700440static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
441 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800442{
David S. Millerf11e6652007-03-24 20:36:25 -0700443 int m;
444
445 if (rt6_check_expired(rt))
446 goto out;
447
448 m = rt6_score_route(rt, oif, strict);
449 if (m < 0)
450 goto out;
451
452 if (m > *mpri) {
453 if (strict & RT6_LOOKUP_F_REACHABLE)
454 rt6_probe(match);
455 *mpri = m;
456 match = rt;
457 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
458 rt6_probe(rt);
459 }
460
461out:
462 return match;
463}
464
465static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
466 struct rt6_info *rr_head,
467 u32 metric, int oif, int strict)
468{
469 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800470 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
David S. Millerf11e6652007-03-24 20:36:25 -0700472 match = NULL;
473 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700474 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700475 match = find_match(rt, oif, strict, &mpri, match);
476 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700477 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700478 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800479
David S. Millerf11e6652007-03-24 20:36:25 -0700480 return match;
481}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800482
David S. Millerf11e6652007-03-24 20:36:25 -0700483static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
484{
485 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800486 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David S. Millerf11e6652007-03-24 20:36:25 -0700488 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800489 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David S. Millerf11e6652007-03-24 20:36:25 -0700491 rt0 = fn->rr_ptr;
492 if (!rt0)
493 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David S. Millerf11e6652007-03-24 20:36:25 -0700495 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800497 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700498 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700499 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700500
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800501 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700502 if (!next || next->rt6i_metric != rt0->rt6i_metric)
503 next = fn->leaf;
504
505 if (next != rt0)
506 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
David S. Millerf11e6652007-03-24 20:36:25 -0700509 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800510 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900512 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000513 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800516#ifdef CONFIG_IPV6_ROUTE_INFO
517int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000518 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800519{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900520 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800521 struct route_info *rinfo = (struct route_info *) opt;
522 struct in6_addr prefix_buf, *prefix;
523 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900524 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800525 struct rt6_info *rt;
526
527 if (len < sizeof(struct route_info)) {
528 return -EINVAL;
529 }
530
531 /* Sanity check for prefix_len and length */
532 if (rinfo->length > 3) {
533 return -EINVAL;
534 } else if (rinfo->prefix_len > 128) {
535 return -EINVAL;
536 } else if (rinfo->prefix_len > 64) {
537 if (rinfo->length < 2) {
538 return -EINVAL;
539 }
540 } else if (rinfo->prefix_len > 0) {
541 if (rinfo->length < 1) {
542 return -EINVAL;
543 }
544 }
545
546 pref = rinfo->route_pref;
547 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000548 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800549
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900550 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800551
552 if (rinfo->length == 3)
553 prefix = (struct in6_addr *)rinfo->prefix;
554 else {
555 /* this function is safe */
556 ipv6_addr_prefix(&prefix_buf,
557 (struct in6_addr *)rinfo->prefix,
558 rinfo->prefix_len);
559 prefix = &prefix_buf;
560 }
561
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800562 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
563 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800564
565 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700566 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800567 rt = NULL;
568 }
569
570 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800571 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800572 pref);
573 else if (rt)
574 rt->rt6i_flags = RTF_ROUTEINFO |
575 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
576
577 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900578 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800579 rt->rt6i_flags &= ~RTF_EXPIRES;
580 } else {
581 rt->rt6i_expires = jiffies + HZ * lifetime;
582 rt->rt6i_flags |= RTF_EXPIRES;
583 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700584 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800585 }
586 return 0;
587}
588#endif
589
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800590#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700591do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800592 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700593 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700594 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700595 if (fn->fn_flags & RTN_TL_ROOT) \
596 goto out; \
597 pn = fn->parent; \
598 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800599 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700600 else \
601 fn = pn; \
602 if (fn->fn_flags & RTN_RTINFO) \
603 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700604 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700605 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700606} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700607
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800608static struct rt6_info *ip6_pol_route_lookup(struct net *net,
609 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500610 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct fib6_node *fn;
613 struct rt6_info *rt;
614
Thomas Grafc71099a2006-08-04 23:20:06 -0700615 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500616 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700617restart:
618 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500619 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
620 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700621out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700622 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700623 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700624 return rt;
625
626}
627
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900628struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
629 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700630{
David S. Miller4c9483b2011-03-12 16:22:43 -0500631 struct flowi6 fl6 = {
632 .flowi6_oif = oif,
633 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700634 };
635 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700636 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700637
Thomas Grafadaa70b2006-10-13 15:01:03 -0700638 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500639 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700640 flags |= RT6_LOOKUP_F_HAS_SADDR;
641 }
642
David S. Miller4c9483b2011-03-12 16:22:43 -0500643 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700644 if (dst->error == 0)
645 return (struct rt6_info *) dst;
646
647 dst_release(dst);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
650}
651
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900652EXPORT_SYMBOL(rt6_lookup);
653
Thomas Grafc71099a2006-08-04 23:20:06 -0700654/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 It takes new route entry, the addition fails by any reason the
656 route is freed. In any case, if caller does not hold it, it may
657 be destroyed.
658 */
659
Thomas Graf86872cb2006-08-22 00:01:08 -0700660static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700663 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Thomas Grafc71099a2006-08-04 23:20:06 -0700665 table = rt->rt6i_table;
666 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700667 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700668 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return err;
671}
672
Thomas Graf40e22e82006-08-22 00:00:45 -0700673int ip6_ins_rt(struct rt6_info *rt)
674{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800675 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900676 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800677 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800678 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700679}
680
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000681static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
682 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct rt6_info *rt;
685
686 /*
687 * Clone the route.
688 */
689
690 rt = ip6_rt_copy(ort);
691
692 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800693 struct neighbour *neigh;
694 int attempts = !in_softirq();
695
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900696 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
697 if (rt->rt6i_dst.plen != 128 &&
698 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
699 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900703 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rt->rt6i_dst.plen = 128;
705 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700706 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708#ifdef CONFIG_IPV6_SUBTREES
709 if (rt->rt6i_src.plen && saddr) {
710 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
711 rt->rt6i_src.plen = 128;
712 }
713#endif
714
David S. Miller14deae42009-01-04 16:04:39 -0800715 retry:
716 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
717 if (IS_ERR(neigh)) {
718 struct net *net = dev_net(rt->rt6i_dev);
719 int saved_rt_min_interval =
720 net->ipv6.sysctl.ip6_rt_gc_min_interval;
721 int saved_rt_elasticity =
722 net->ipv6.sysctl.ip6_rt_gc_elasticity;
723
724 if (attempts-- > 0) {
725 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
726 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
727
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000728 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800729
730 net->ipv6.sysctl.ip6_rt_gc_elasticity =
731 saved_rt_elasticity;
732 net->ipv6.sysctl.ip6_rt_gc_min_interval =
733 saved_rt_min_interval;
734 goto retry;
735 }
736
737 if (net_ratelimit())
738 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700739 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700740 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800741 return NULL;
742 }
743 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800747 return rt;
748}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000750static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800751{
752 struct rt6_info *rt = ip6_rt_copy(ort);
753 if (rt) {
754 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
755 rt->rt6i_dst.plen = 128;
756 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700757 rt->dst.flags |= DST_HOST;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800758 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
759 }
760 return rt;
761}
762
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800763static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500764 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800767 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700768 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800770 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700771 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700773 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700776 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800778restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500779 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700782 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800783
David S. Miller4c9483b2011-03-12 16:22:43 -0500784 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800785 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800786 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Changli Gaod8d1f302010-06-10 23:31:35 -0700789 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700790 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800791
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800792 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500793 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800794 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500795 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800796 else
797 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800798
Changli Gaod8d1f302010-06-10 23:31:35 -0700799 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800800 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800801
Changli Gaod8d1f302010-06-10 23:31:35 -0700802 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800803 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700804 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800805 if (!err)
806 goto out2;
807 }
808
809 if (--attempts <= 0)
810 goto out2;
811
812 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700813 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800814 * released someone could insert this route. Relookup.
815 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700816 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800817 goto relookup;
818
819out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800820 if (reachable) {
821 reachable = 0;
822 goto restart_2;
823 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700824 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700825 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700827 rt->dst.lastuse = jiffies;
828 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700829
830 return rt;
831}
832
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800833static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500834 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700835{
David S. Miller4c9483b2011-03-12 16:22:43 -0500836 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700837}
838
Thomas Grafc71099a2006-08-04 23:20:06 -0700839void ip6_route_input(struct sk_buff *skb)
840{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000841 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900842 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700843 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500844 struct flowi6 fl6 = {
845 .flowi6_iif = skb->dev->ifindex,
846 .daddr = iph->daddr,
847 .saddr = iph->saddr,
848 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
849 .flowi6_mark = skb->mark,
850 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700851 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700852
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800853 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700854 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700855
David S. Miller4c9483b2011-03-12 16:22:43 -0500856 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700857}
858
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800859static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500860 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700861{
David S. Miller4c9483b2011-03-12 16:22:43 -0500862 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700863}
864
Florian Westphal9c7a4f92011-03-22 19:17:36 -0700865struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500866 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700867{
868 int flags = 0;
869
David S. Miller4c9483b2011-03-12 16:22:43 -0500870 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700871 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700872
David S. Miller4c9483b2011-03-12 16:22:43 -0500873 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700874 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000875 else if (sk)
876 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700877
David S. Miller4c9483b2011-03-12 16:22:43 -0500878 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900881EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
David S. Miller2774c132011-03-01 14:59:04 -0800883struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700884{
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700885 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700886 struct dst_entry *new = NULL;
887
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700888 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
David S. Miller14e50e52007-05-24 18:17:54 -0700889 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700890 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700891
David S. Miller14e50e52007-05-24 18:17:54 -0700892 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800893 new->input = dst_discard;
894 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700895
David S. Millerdefb3512010-12-08 21:16:57 -0800896 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -0700897 rt->rt6i_idev = ort->rt6i_idev;
898 if (rt->rt6i_idev)
899 in6_dev_hold(rt->rt6i_idev);
900 rt->rt6i_expires = 0;
901
902 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
903 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
904 rt->rt6i_metric = 0;
905
906 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
907#ifdef CONFIG_IPV6_SUBTREES
908 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
909#endif
910
911 dst_free(new);
912 }
913
David S. Miller69ead7a2011-03-01 14:45:33 -0800914 dst_release(dst_orig);
915 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700916}
David S. Miller14e50e52007-05-24 18:17:54 -0700917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/*
919 * Destination cache support functions
920 */
921
922static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
923{
924 struct rt6_info *rt;
925
926 rt = (struct rt6_info *) dst;
927
David S. Miller6431cbc2011-02-07 20:38:06 -0800928 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
929 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
930 if (!rt->rt6i_peer)
931 rt6_bind_peer(rt, 0);
932 rt->rt6i_peer_genid = rt6_peer_genid();
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return NULL;
937}
938
939static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
940{
941 struct rt6_info *rt = (struct rt6_info *) dst;
942
943 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000944 if (rt->rt6i_flags & RTF_CACHE) {
945 if (rt6_check_expired(rt)) {
946 ip6_del_rt(rt);
947 dst = NULL;
948 }
949 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000951 dst = NULL;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000954 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static void ip6_link_failure(struct sk_buff *skb)
958{
959 struct rt6_info *rt;
960
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000961 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Eric Dumazetadf30902009-06-02 05:19:30 +0000963 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (rt) {
965 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700966 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 rt->rt6i_flags |= RTF_EXPIRES;
968 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
969 rt->rt6i_node->fn_sernum = -1;
970 }
971}
972
973static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
974{
975 struct rt6_info *rt6 = (struct rt6_info*)dst;
976
977 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
978 rt6->rt6i_flags |= RTF_MODIFIED;
979 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800980 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800982 features |= RTAX_FEATURE_ALLFRAG;
983 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
David S. Millerdefb3512010-12-08 21:16:57 -0800985 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987}
988
David S. Miller0dbaee32010-12-13 12:52:14 -0800989static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
David S. Miller0dbaee32010-12-13 12:52:14 -0800991 struct net_device *dev = dst->dev;
992 unsigned int mtu = dst_mtu(dst);
993 struct net *net = dev_net(dev);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
996
Daniel Lezcano55786892008-03-04 13:47:47 -0800997 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
998 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001001 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1002 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1003 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * rely only on pmtu discovery"
1005 */
1006 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1007 mtu = IPV6_MAXPLEN;
1008 return mtu;
1009}
1010
David S. Millerd33e4552010-12-14 13:01:14 -08001011static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1012{
1013 unsigned int mtu = IPV6_MIN_MTU;
1014 struct inet6_dev *idev;
1015
1016 rcu_read_lock();
1017 idev = __in6_dev_get(dst->dev);
1018 if (idev)
1019 mtu = idev->cnf.mtu6;
1020 rcu_read_unlock();
1021
1022 return mtu;
1023}
1024
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001025static struct dst_entry *icmp6_dst_gc_list;
1026static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001027
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001028struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001030 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct rt6_info *rt;
1033 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001034 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (unlikely(idev == NULL))
1037 return NULL;
1038
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001039 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (unlikely(rt == NULL)) {
1041 in6_dev_put(idev);
1042 goto out;
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (neigh)
1046 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001047 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001049 if (IS_ERR(neigh))
1050 neigh = NULL;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 rt->rt6i_idev = idev;
1054 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001055 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001056 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001057 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001060 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001061 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 : 0;
1063 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1064 rt->rt6i_dst.plen = 128;
1065#endif
1066
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001067 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001068 rt->dst.next = icmp6_dst_gc_list;
1069 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001070 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Daniel Lezcano55786892008-03-04 13:47:47 -08001072 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001075 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001078int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Hagen Paul Pfeifere9476e92011-02-25 05:45:19 +00001080 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001081 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001083 spin_lock_bh(&icmp6_dst_lock);
1084 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 while ((dst = *pprev) != NULL) {
1087 if (!atomic_read(&dst->__refcnt)) {
1088 *pprev = dst->next;
1089 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 } else {
1091 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001092 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 }
1095
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001096 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001097
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001098 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
David S. Miller1e493d12008-09-10 17:27:15 -07001101static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1102 void *arg)
1103{
1104 struct dst_entry *dst, **pprev;
1105
1106 spin_lock_bh(&icmp6_dst_lock);
1107 pprev = &icmp6_dst_gc_list;
1108 while ((dst = *pprev) != NULL) {
1109 struct rt6_info *rt = (struct rt6_info *) dst;
1110 if (func(rt, arg)) {
1111 *pprev = dst->next;
1112 dst_free(dst);
1113 } else {
1114 pprev = &dst->next;
1115 }
1116 }
1117 spin_unlock_bh(&icmp6_dst_lock);
1118}
1119
Daniel Lezcano569d3642008-01-18 03:56:57 -08001120static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001123 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001124 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1125 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1126 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1127 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1128 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001129 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Eric Dumazetfc66f952010-10-08 06:37:34 +00001131 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001132 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001133 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto out;
1135
Benjamin Thery6891a342008-03-04 13:49:47 -08001136 net->ipv6.ip6_rt_gc_expire++;
1137 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1138 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001139 entries = dst_entries_get_slow(ops);
1140 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001141 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001143 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001144 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147/* Clean host part of a prefix. Not necessary in radix tree,
1148 but results in cleaner routing tables.
1149
1150 Remove it only when all the things will work!
1151 */
1152
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001153int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
David S. Miller5170ae82010-12-12 21:35:57 -08001155 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001156 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001157 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001158 struct inet6_dev *idev;
1159
1160 rcu_read_lock();
1161 idev = __in6_dev_get(dev);
1162 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001163 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001164 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001165 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001166 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 return hoplimit;
1169}
David S. Millerabbf46a2010-12-12 21:14:46 -08001170EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172/*
1173 *
1174 */
1175
Thomas Graf86872cb2006-08-22 00:01:08 -07001176int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001179 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct rt6_info *rt = NULL;
1181 struct net_device *dev = NULL;
1182 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001183 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int addr_type;
1185
Thomas Graf86872cb2006-08-22 00:01:08 -07001186 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EINVAL;
1188#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001189 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EINVAL;
1191#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001192 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001194 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!dev)
1196 goto out;
1197 idev = in6_dev_get(dev);
1198 if (!idev)
1199 goto out;
1200 }
1201
Thomas Graf86872cb2006-08-22 00:01:08 -07001202 if (cfg->fc_metric == 0)
1203 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Daniel Lezcano55786892008-03-04 13:47:47 -08001205 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001206 if (table == NULL) {
1207 err = -ENOBUFS;
1208 goto out;
1209 }
1210
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001211 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (rt == NULL) {
1214 err = -ENOMEM;
1215 goto out;
1216 }
1217
Changli Gaod8d1f302010-06-10 23:31:35 -07001218 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001219 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1220 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1221 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Thomas Graf86872cb2006-08-22 00:01:08 -07001223 if (cfg->fc_protocol == RTPROT_UNSPEC)
1224 cfg->fc_protocol = RTPROT_BOOT;
1225 rt->rt6i_protocol = cfg->fc_protocol;
1226
1227 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001230 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001231 else if (cfg->fc_flags & RTF_LOCAL)
1232 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001234 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Changli Gaod8d1f302010-06-10 23:31:35 -07001236 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Graf86872cb2006-08-22 00:01:08 -07001238 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1239 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001241 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001244 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1245 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246#endif
1247
Thomas Graf86872cb2006-08-22 00:01:08 -07001248 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* We cannot add true routes via loopback here,
1251 they would result in kernel looping; promote them to reject routes
1252 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001253 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001254 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1255 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001257 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (dev) {
1259 dev_put(dev);
1260 in6_dev_put(idev);
1261 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001262 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 dev_hold(dev);
1264 idev = in6_dev_get(dev);
1265 if (!idev) {
1266 err = -ENODEV;
1267 goto out;
1268 }
1269 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001270 rt->dst.output = ip6_pkt_discard_out;
1271 rt->dst.input = ip6_pkt_discard;
1272 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1274 goto install_route;
1275 }
1276
Thomas Graf86872cb2006-08-22 00:01:08 -07001277 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001278 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int gwa_type;
1280
Thomas Graf86872cb2006-08-22 00:01:08 -07001281 gw_addr = &cfg->fc_gateway;
1282 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 gwa_type = ipv6_addr_type(gw_addr);
1284
1285 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1286 struct rt6_info *grt;
1287
1288 /* IPv6 strictly inhibits using not link-local
1289 addresses as nexthop address.
1290 Otherwise, router will not able to send redirects.
1291 It is very good, but in some (rare!) circumstances
1292 (SIT, PtP, NBMA NOARP links) it is handy to allow
1293 some exceptions. --ANK
1294 */
1295 err = -EINVAL;
1296 if (!(gwa_type&IPV6_ADDR_UNICAST))
1297 goto out;
1298
Daniel Lezcano55786892008-03-04 13:47:47 -08001299 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 err = -EHOSTUNREACH;
1302 if (grt == NULL)
1303 goto out;
1304 if (dev) {
1305 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001306 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto out;
1308 }
1309 } else {
1310 dev = grt->rt6i_dev;
1311 idev = grt->rt6i_idev;
1312 dev_hold(dev);
1313 in6_dev_hold(grt->rt6i_idev);
1314 }
1315 if (!(grt->rt6i_flags&RTF_GATEWAY))
1316 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001317 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (err)
1320 goto out;
1321 }
1322 err = -EINVAL;
1323 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1324 goto out;
1325 }
1326
1327 err = -ENODEV;
1328 if (dev == NULL)
1329 goto out;
1330
Daniel Walterc3968a82011-04-13 21:10:57 +00001331 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1332 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1333 err = -EINVAL;
1334 goto out;
1335 }
1336 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1337 rt->rt6i_prefsrc.plen = 128;
1338 } else
1339 rt->rt6i_prefsrc.plen = 0;
1340
Thomas Graf86872cb2006-08-22 00:01:08 -07001341 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1343 if (IS_ERR(rt->rt6i_nexthop)) {
1344 err = PTR_ERR(rt->rt6i_nexthop);
1345 rt->rt6i_nexthop = NULL;
1346 goto out;
1347 }
1348 }
1349
Thomas Graf86872cb2006-08-22 00:01:08 -07001350 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001353 if (cfg->fc_mx) {
1354 struct nlattr *nla;
1355 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Thomas Graf86872cb2006-08-22 00:01:08 -07001357 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001358 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001359
1360 if (type) {
1361 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 err = -EINVAL;
1363 goto out;
1364 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001365
David S. Millerdefb3512010-12-08 21:16:57 -08001366 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 }
1370
Changli Gaod8d1f302010-06-10 23:31:35 -07001371 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001373 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001374
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001375 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001376
Thomas Graf86872cb2006-08-22 00:01:08 -07001377 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379out:
1380 if (dev)
1381 dev_put(dev);
1382 if (idev)
1383 in6_dev_put(idev);
1384 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001385 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return err;
1387}
1388
Thomas Graf86872cb2006-08-22 00:01:08 -07001389static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001392 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001393 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001395 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001396 return -ENOENT;
1397
Thomas Grafc71099a2006-08-04 23:20:06 -07001398 table = rt->rt6i_table;
1399 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Thomas Graf86872cb2006-08-22 00:01:08 -07001401 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001402 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Thomas Grafc71099a2006-08-04 23:20:06 -07001404 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 return err;
1407}
1408
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001409int ip6_del_rt(struct rt6_info *rt)
1410{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001411 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001412 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001413 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001414 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001415}
1416
Thomas Graf86872cb2006-08-22 00:01:08 -07001417static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Thomas Grafc71099a2006-08-04 23:20:06 -07001419 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct fib6_node *fn;
1421 struct rt6_info *rt;
1422 int err = -ESRCH;
1423
Daniel Lezcano55786892008-03-04 13:47:47 -08001424 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001425 if (table == NULL)
1426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Thomas Grafc71099a2006-08-04 23:20:06 -07001428 read_lock_bh(&table->tb6_lock);
1429
1430 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001431 &cfg->fc_dst, cfg->fc_dst_len,
1432 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001435 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001436 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001438 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001440 if (cfg->fc_flags & RTF_GATEWAY &&
1441 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001443 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001445 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001446 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Thomas Graf86872cb2006-08-22 00:01:08 -07001448 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001451 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 return err;
1454}
1455
1456/*
1457 * Handle redirects
1458 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001459struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001460 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001461 struct in6_addr gateway;
1462};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001464static struct rt6_info *__ip6_route_redirect(struct net *net,
1465 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001466 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001467 int flags)
1468{
David S. Miller4c9483b2011-03-12 16:22:43 -05001469 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001470 struct rt6_info *rt;
1471 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001474 * Get the "current" route for this destination and
1475 * check if the redirect has come from approriate router.
1476 *
1477 * RFC 2461 specifies that redirects should only be
1478 * accepted if they come from the nexthop to the target.
1479 * Due to the way the routes are chosen, this notion
1480 * is a bit fuzzy and one might need to check all possible
1481 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Thomas Grafc71099a2006-08-04 23:20:06 -07001484 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001485 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001486restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001487 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001488 /*
1489 * Current route is on-link; redirect is always invalid.
1490 *
1491 * Seems, previous statement is not true. It could
1492 * be node, which looks for us as on-link (f.e. proxy ndisc)
1493 * But then router serving it might decide, that we should
1494 * know truth 8)8) --ANK (980726).
1495 */
1496 if (rt6_check_expired(rt))
1497 continue;
1498 if (!(rt->rt6i_flags & RTF_GATEWAY))
1499 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001500 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001501 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001502 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001503 continue;
1504 break;
1505 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001506
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001507 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001508 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001509 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001510out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001511 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001512
1513 read_unlock_bh(&table->tb6_lock);
1514
1515 return rt;
1516};
1517
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001518static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1519 const struct in6_addr *src,
1520 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001521 struct net_device *dev)
1522{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001523 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001524 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001525 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001526 .fl6 = {
1527 .flowi6_oif = dev->ifindex,
1528 .daddr = *dest,
1529 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001530 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001531 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001532
Brian Haley86c36ce2009-10-07 13:58:01 -07001533 ipv6_addr_copy(&rdfl.gateway, gateway);
1534
Thomas Grafadaa70b2006-10-13 15:01:03 -07001535 if (rt6_need_strict(dest))
1536 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001537
David S. Miller4c9483b2011-03-12 16:22:43 -05001538 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001539 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001540}
1541
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001542void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1543 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001544 struct neighbour *neigh, u8 *lladdr, int on_link)
1545{
1546 struct rt6_info *rt, *nrt = NULL;
1547 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001548 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001549
1550 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1551
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001552 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (net_ratelimit())
1554 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1555 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * We have finally decided to accept it.
1561 */
1562
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001563 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1565 NEIGH_UPDATE_F_OVERRIDE|
1566 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1567 NEIGH_UPDATE_F_ISROUTER))
1568 );
1569
1570 /*
1571 * Redirect received -> path was valid.
1572 * Look, redirects are sent only in response to data packets,
1573 * so that this nexthop apparently is reachable. --ANK
1574 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001575 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001578 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 goto out;
1580
1581 nrt = ip6_rt_copy(rt);
1582 if (nrt == NULL)
1583 goto out;
1584
1585 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1586 if (on_link)
1587 nrt->rt6i_flags &= ~RTF_GATEWAY;
1588
1589 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1590 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001591 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1594 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Thomas Graf40e22e82006-08-22 00:00:45 -07001596 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto out;
1598
Changli Gaod8d1f302010-06-10 23:31:35 -07001599 netevent.old = &rt->dst;
1600 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001601 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001604 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return;
1606 }
1607
1608out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001609 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/*
1613 * Handle ICMP "packet too big" messages
1614 * i.e. Path MTU discovery
1615 */
1616
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001617static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001618 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct rt6_info *rt, *nrt;
1621 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001622again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001623 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (rt == NULL)
1625 return;
1626
Andrey Vagind3052b52010-12-11 15:20:11 +00001627 if (rt6_check_expired(rt)) {
1628 ip6_del_rt(rt);
1629 goto again;
1630 }
1631
Changli Gaod8d1f302010-06-10 23:31:35 -07001632 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto out;
1634
1635 if (pmtu < IPV6_MIN_MTU) {
1636 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001637 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * MTU (1280) and a fragment header should always be included
1639 * after a node receiving Too Big message reporting PMTU is
1640 * less than the IPv6 Minimum Link MTU.
1641 */
1642 pmtu = IPV6_MIN_MTU;
1643 allfrag = 1;
1644 }
1645
1646 /* New mtu received -> path was valid.
1647 They are sent only in response to data packets,
1648 so that this nexthop apparently is reachable. --ANK
1649 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001650 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 /* Host route. If it is static, it would be better
1653 not to override it, but add new one, so that
1654 when cache entry will expire old pmtu
1655 would return automatically.
1656 */
1657 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001658 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1659 if (allfrag) {
1660 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1661 features |= RTAX_FEATURE_ALLFRAG;
1662 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1663 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001664 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1666 goto out;
1667 }
1668
1669 /* Network route.
1670 Two cases are possible:
1671 1. It is connected route. Action: COW
1672 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1673 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001674 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001675 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001676 else
1677 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001678
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001679 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001680 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1681 if (allfrag) {
1682 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1683 features |= RTAX_FEATURE_ALLFRAG;
1684 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1685 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001686
1687 /* According to RFC 1981, detecting PMTU increase shouldn't be
1688 * happened within 5 mins, the recommended timer is 10 mins.
1689 * Here this route expiration time is set to ip6_rt_mtu_expires
1690 * which is 10 mins. After 10 mins the decreased pmtu is expired
1691 * and detecting PMTU increase will be automatically happened.
1692 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001693 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001694 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1695
Thomas Graf40e22e82006-08-22 00:00:45 -07001696 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001699 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001702void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001703 struct net_device *dev, u32 pmtu)
1704{
1705 struct net *net = dev_net(dev);
1706
1707 /*
1708 * RFC 1981 states that a node "MUST reduce the size of the packets it
1709 * is sending along the path" that caused the Packet Too Big message.
1710 * Since it's not possible in the general case to determine which
1711 * interface was used to send the original packet, we update the MTU
1712 * on the interface that will be used to send future packets. We also
1713 * update the MTU on the interface that received the Packet Too Big in
1714 * case the original packet was forced out that interface with
1715 * SO_BINDTODEVICE or similar. This is the next best thing to the
1716 * correct behaviour, which would be to update the MTU on all
1717 * interfaces.
1718 */
1719 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1720 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723/*
1724 * Misc support functions
1725 */
1726
1727static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1728{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001729 struct net *net = dev_net(ort->rt6i_dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001730 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1731 ort->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001734 rt->dst.input = ort->dst.input;
1735 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
David S. Millerdefb3512010-12-08 21:16:57 -08001737 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001738 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 rt->rt6i_idev = ort->rt6i_idev;
1740 if (rt->rt6i_idev)
1741 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rt->rt6i_expires = 0;
1744
1745 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1746 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1747 rt->rt6i_metric = 0;
1748
1749 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1750#ifdef CONFIG_IPV6_SUBTREES
1751 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1752#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001753 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 return rt;
1756}
1757
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001758#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001759static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001760 const struct in6_addr *prefix, int prefixlen,
1761 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001762{
1763 struct fib6_node *fn;
1764 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001765 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001766
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001767 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001768 if (table == NULL)
1769 return NULL;
1770
1771 write_lock_bh(&table->tb6_lock);
1772 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001773 if (!fn)
1774 goto out;
1775
Changli Gaod8d1f302010-06-10 23:31:35 -07001776 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001777 if (rt->rt6i_dev->ifindex != ifindex)
1778 continue;
1779 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1780 continue;
1781 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1782 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001783 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001784 break;
1785 }
1786out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001787 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001788 return rt;
1789}
1790
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001791static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001792 const struct in6_addr *prefix, int prefixlen,
1793 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001794 unsigned pref)
1795{
Thomas Graf86872cb2006-08-22 00:01:08 -07001796 struct fib6_config cfg = {
1797 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001798 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001799 .fc_ifindex = ifindex,
1800 .fc_dst_len = prefixlen,
1801 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1802 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001803 .fc_nlinfo.pid = 0,
1804 .fc_nlinfo.nlh = NULL,
1805 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001806 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001807
Thomas Graf86872cb2006-08-22 00:01:08 -07001808 ipv6_addr_copy(&cfg.fc_dst, prefix);
1809 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1810
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001811 /* We should treat it as a default route if prefix length is 0. */
1812 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001813 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001814
Thomas Graf86872cb2006-08-22 00:01:08 -07001815 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001816
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001817 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001818}
1819#endif
1820
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001821struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001824 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001826 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001827 if (table == NULL)
1828 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Thomas Grafc71099a2006-08-04 23:20:06 -07001830 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001831 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001833 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1835 break;
1836 }
1837 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001838 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001839 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return rt;
1841}
1842
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001843struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001844 struct net_device *dev,
1845 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Thomas Graf86872cb2006-08-22 00:01:08 -07001847 struct fib6_config cfg = {
1848 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001849 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001850 .fc_ifindex = dev->ifindex,
1851 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1852 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001853 .fc_nlinfo.pid = 0,
1854 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001855 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001856 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Thomas Graf86872cb2006-08-22 00:01:08 -07001858 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Thomas Graf86872cb2006-08-22 00:01:08 -07001860 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return rt6_get_dflt_router(gwaddr, dev);
1863}
1864
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001865void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001868 struct fib6_table *table;
1869
1870 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001871 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001872 if (table == NULL)
1873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001876 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001877 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001879 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001880 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001881 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto restart;
1883 }
1884 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001885 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Daniel Lezcano55786892008-03-04 13:47:47 -08001888static void rtmsg_to_fib6_config(struct net *net,
1889 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001890 struct fib6_config *cfg)
1891{
1892 memset(cfg, 0, sizeof(*cfg));
1893
1894 cfg->fc_table = RT6_TABLE_MAIN;
1895 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1896 cfg->fc_metric = rtmsg->rtmsg_metric;
1897 cfg->fc_expires = rtmsg->rtmsg_info;
1898 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1899 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1900 cfg->fc_flags = rtmsg->rtmsg_flags;
1901
Daniel Lezcano55786892008-03-04 13:47:47 -08001902 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001903
Thomas Graf86872cb2006-08-22 00:01:08 -07001904 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1905 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1906 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1907}
1908
Daniel Lezcano55786892008-03-04 13:47:47 -08001909int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Thomas Graf86872cb2006-08-22 00:01:08 -07001911 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct in6_rtmsg rtmsg;
1913 int err;
1914
1915 switch(cmd) {
1916 case SIOCADDRT: /* Add a route */
1917 case SIOCDELRT: /* Delete a route */
1918 if (!capable(CAP_NET_ADMIN))
1919 return -EPERM;
1920 err = copy_from_user(&rtmsg, arg,
1921 sizeof(struct in6_rtmsg));
1922 if (err)
1923 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001924
Daniel Lezcano55786892008-03-04 13:47:47 -08001925 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 rtnl_lock();
1928 switch (cmd) {
1929 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001930 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001933 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 break;
1935 default:
1936 err = -EINVAL;
1937 }
1938 rtnl_unlock();
1939
1940 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 return -EINVAL;
1944}
1945
1946/*
1947 * Drop the packet on the floor
1948 */
1949
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001950static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001952 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001953 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001954 switch (ipstats_mib_noroutes) {
1955 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001956 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001957 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001958 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1959 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001960 break;
1961 }
1962 /* FALLTHROUGH */
1963 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001964 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1965 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001966 break;
1967 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001968 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 kfree_skb(skb);
1970 return 0;
1971}
1972
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001973static int ip6_pkt_discard(struct sk_buff *skb)
1974{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001975 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001976}
1977
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001978static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Eric Dumazetadf30902009-06-02 05:19:30 +00001980 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001981 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
David S. Miller6723ab52006-10-18 21:20:57 -07001984#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1985
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001986static int ip6_pkt_prohibit(struct sk_buff *skb)
1987{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001988 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001989}
1990
1991static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1992{
Eric Dumazetadf30902009-06-02 05:19:30 +00001993 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001994 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001995}
1996
David S. Miller6723ab52006-10-18 21:20:57 -07001997#endif
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
2000 * Allocate a dst for local (unicast / anycast) address.
2001 */
2002
2003struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2004 const struct in6_addr *addr,
2005 int anycast)
2006{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002007 struct net *net = dev_net(idev->dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002008 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2009 net->loopback_dev);
David S. Miller14deae42009-01-04 16:04:39 -08002010 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Ben Greear40385652010-11-08 12:33:48 +00002012 if (rt == NULL) {
2013 if (net_ratelimit())
2014 pr_warning("IPv6: Maximum number of routes reached,"
2015 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 in6_dev_hold(idev);
2020
Changli Gaod8d1f302010-06-10 23:31:35 -07002021 rt->dst.flags = DST_HOST;
2022 rt->dst.input = ip6_input;
2023 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002025 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002028 if (anycast)
2029 rt->rt6i_flags |= RTF_ANYCAST;
2030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08002032 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2033 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002034 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08002035
David S. Miller29546a62011-03-03 12:10:37 -08002036 return ERR_CAST(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
David S. Miller14deae42009-01-04 16:04:39 -08002038 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2041 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002042 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Changli Gaod8d1f302010-06-10 23:31:35 -07002044 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 return rt;
2047}
2048
Daniel Walterc3968a82011-04-13 21:10:57 +00002049int ip6_route_get_saddr(struct net *net,
2050 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002051 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002052 unsigned int prefs,
2053 struct in6_addr *saddr)
2054{
2055 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2056 int err = 0;
2057 if (rt->rt6i_prefsrc.plen)
2058 ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2059 else
2060 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2061 daddr, prefs, saddr);
2062 return err;
2063}
2064
2065/* remove deleted ip from prefsrc entries */
2066struct arg_dev_net_ip {
2067 struct net_device *dev;
2068 struct net *net;
2069 struct in6_addr *addr;
2070};
2071
2072static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2073{
2074 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2075 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2076 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2077
2078 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2079 rt != net->ipv6.ip6_null_entry &&
2080 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2081 /* remove prefsrc entry */
2082 rt->rt6i_prefsrc.plen = 0;
2083 }
2084 return 0;
2085}
2086
2087void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2088{
2089 struct net *net = dev_net(ifp->idev->dev);
2090 struct arg_dev_net_ip adni = {
2091 .dev = ifp->idev->dev,
2092 .net = net,
2093 .addr = &ifp->addr,
2094 };
2095 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2096}
2097
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002098struct arg_dev_net {
2099 struct net_device *dev;
2100 struct net *net;
2101};
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103static int fib6_ifdown(struct rt6_info *rt, void *arg)
2104{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002105 const struct arg_dev_net *adn = arg;
2106 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002107
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002108 if ((rt->rt6i_dev == dev || dev == NULL) &&
2109 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 RT6_TRACE("deleted by ifdown %p\n", rt);
2111 return -1;
2112 }
2113 return 0;
2114}
2115
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002116void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002118 struct arg_dev_net adn = {
2119 .dev = dev,
2120 .net = net,
2121 };
2122
2123 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002124 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
2127struct rt6_mtu_change_arg
2128{
2129 struct net_device *dev;
2130 unsigned mtu;
2131};
2132
2133static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2134{
2135 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2136 struct inet6_dev *idev;
2137
2138 /* In IPv6 pmtu discovery is not optional,
2139 so that RTAX_MTU lock cannot disable it.
2140 We still use this lock to block changes
2141 caused by addrconf/ndisc.
2142 */
2143
2144 idev = __in6_dev_get(arg->dev);
2145 if (idev == NULL)
2146 return 0;
2147
2148 /* For administrative MTU increase, there is no way to discover
2149 IPv6 PMTU increase, so PMTU increase should be updated here.
2150 Since RFC 1981 doesn't include administrative MTU increase
2151 update PMTU increase is a MUST. (i.e. jumbo frame)
2152 */
2153 /*
2154 If new MTU is less than route PMTU, this new MTU will be the
2155 lowest MTU in the path, update the route PMTU to reflect PMTU
2156 decreases; if new MTU is greater than route PMTU, and the
2157 old MTU is the lowest MTU in the path, update the route PMTU
2158 to reflect the increase. In this case if the other nodes' MTU
2159 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2160 PMTU discouvery.
2161 */
2162 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002163 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2164 (dst_mtu(&rt->dst) >= arg->mtu ||
2165 (dst_mtu(&rt->dst) < arg->mtu &&
2166 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002167 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
2170}
2171
2172void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2173{
Thomas Grafc71099a2006-08-04 23:20:06 -07002174 struct rt6_mtu_change_arg arg = {
2175 .dev = dev,
2176 .mtu = mtu,
2177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002179 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002182static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002183 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002184 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002185 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002186 [RTA_PRIORITY] = { .type = NLA_U32 },
2187 [RTA_METRICS] = { .type = NLA_NESTED },
2188};
2189
2190static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2191 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Thomas Graf86872cb2006-08-22 00:01:08 -07002193 struct rtmsg *rtm;
2194 struct nlattr *tb[RTA_MAX+1];
2195 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Thomas Graf86872cb2006-08-22 00:01:08 -07002197 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2198 if (err < 0)
2199 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Thomas Graf86872cb2006-08-22 00:01:08 -07002201 err = -EINVAL;
2202 rtm = nlmsg_data(nlh);
2203 memset(cfg, 0, sizeof(*cfg));
2204
2205 cfg->fc_table = rtm->rtm_table;
2206 cfg->fc_dst_len = rtm->rtm_dst_len;
2207 cfg->fc_src_len = rtm->rtm_src_len;
2208 cfg->fc_flags = RTF_UP;
2209 cfg->fc_protocol = rtm->rtm_protocol;
2210
2211 if (rtm->rtm_type == RTN_UNREACHABLE)
2212 cfg->fc_flags |= RTF_REJECT;
2213
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002214 if (rtm->rtm_type == RTN_LOCAL)
2215 cfg->fc_flags |= RTF_LOCAL;
2216
Thomas Graf86872cb2006-08-22 00:01:08 -07002217 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2218 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002219 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002220
2221 if (tb[RTA_GATEWAY]) {
2222 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2223 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002225
2226 if (tb[RTA_DST]) {
2227 int plen = (rtm->rtm_dst_len + 7) >> 3;
2228
2229 if (nla_len(tb[RTA_DST]) < plen)
2230 goto errout;
2231
2232 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002234
2235 if (tb[RTA_SRC]) {
2236 int plen = (rtm->rtm_src_len + 7) >> 3;
2237
2238 if (nla_len(tb[RTA_SRC]) < plen)
2239 goto errout;
2240
2241 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002243
Daniel Walterc3968a82011-04-13 21:10:57 +00002244 if (tb[RTA_PREFSRC])
2245 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2246
Thomas Graf86872cb2006-08-22 00:01:08 -07002247 if (tb[RTA_OIF])
2248 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2249
2250 if (tb[RTA_PRIORITY])
2251 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2252
2253 if (tb[RTA_METRICS]) {
2254 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2255 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002257
2258 if (tb[RTA_TABLE])
2259 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2260
2261 err = 0;
2262errout:
2263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Thomas Grafc127ea22007-03-22 11:58:32 -07002266static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Thomas Graf86872cb2006-08-22 00:01:08 -07002268 struct fib6_config cfg;
2269 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Thomas Graf86872cb2006-08-22 00:01:08 -07002271 err = rtm_to_fib6_config(skb, nlh, &cfg);
2272 if (err < 0)
2273 return err;
2274
2275 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Thomas Grafc127ea22007-03-22 11:58:32 -07002278static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Thomas Graf86872cb2006-08-22 00:01:08 -07002280 struct fib6_config cfg;
2281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Thomas Graf86872cb2006-08-22 00:01:08 -07002283 err = rtm_to_fib6_config(skb, nlh, &cfg);
2284 if (err < 0)
2285 return err;
2286
2287 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Thomas Graf339bf982006-11-10 14:10:15 -08002290static inline size_t rt6_nlmsg_size(void)
2291{
2292 return NLMSG_ALIGN(sizeof(struct rtmsg))
2293 + nla_total_size(16) /* RTA_SRC */
2294 + nla_total_size(16) /* RTA_DST */
2295 + nla_total_size(16) /* RTA_GATEWAY */
2296 + nla_total_size(16) /* RTA_PREFSRC */
2297 + nla_total_size(4) /* RTA_TABLE */
2298 + nla_total_size(4) /* RTA_IIF */
2299 + nla_total_size(4) /* RTA_OIF */
2300 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002301 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002302 + nla_total_size(sizeof(struct rta_cacheinfo));
2303}
2304
Brian Haley191cd582008-08-14 15:33:21 -07002305static int rt6_fill_node(struct net *net,
2306 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002307 struct in6_addr *dst, struct in6_addr *src,
2308 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002309 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002312 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002313 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002314 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (prefix) { /* user wants prefix routes only */
2317 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2318 /* success since this is not a prefix route */
2319 return 1;
2320 }
2321 }
2322
Thomas Graf2d7202b2006-08-22 00:01:27 -07002323 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2324 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002325 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002326
2327 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 rtm->rtm_family = AF_INET6;
2329 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2330 rtm->rtm_src_len = rt->rt6i_src.plen;
2331 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002332 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002333 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002334 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002335 table = RT6_TABLE_UNSPEC;
2336 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002337 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (rt->rt6i_flags&RTF_REJECT)
2339 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002340 else if (rt->rt6i_flags&RTF_LOCAL)
2341 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2343 rtm->rtm_type = RTN_LOCAL;
2344 else
2345 rtm->rtm_type = RTN_UNICAST;
2346 rtm->rtm_flags = 0;
2347 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2348 rtm->rtm_protocol = rt->rt6i_protocol;
2349 if (rt->rt6i_flags&RTF_DYNAMIC)
2350 rtm->rtm_protocol = RTPROT_REDIRECT;
2351 else if (rt->rt6i_flags & RTF_ADDRCONF)
2352 rtm->rtm_protocol = RTPROT_KERNEL;
2353 else if (rt->rt6i_flags&RTF_DEFAULT)
2354 rtm->rtm_protocol = RTPROT_RA;
2355
2356 if (rt->rt6i_flags&RTF_CACHE)
2357 rtm->rtm_flags |= RTM_F_CLONED;
2358
2359 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002360 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002361 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002363 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364#ifdef CONFIG_IPV6_SUBTREES
2365 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002366 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002367 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002369 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002371 if (iif) {
2372#ifdef CONFIG_IPV6_MROUTE
2373 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002374 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002375 if (err <= 0) {
2376 if (!nowait) {
2377 if (err == 0)
2378 return 0;
2379 goto nla_put_failure;
2380 } else {
2381 if (err == -EMSGSIZE)
2382 goto nla_put_failure;
2383 }
2384 }
2385 } else
2386#endif
2387 NLA_PUT_U32(skb, RTA_IIF, iif);
2388 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002390 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002391 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002393
Daniel Walterc3968a82011-04-13 21:10:57 +00002394 if (rt->rt6i_prefsrc.plen) {
2395 struct in6_addr saddr_buf;
2396 ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
2397 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2398 }
2399
David S. Millerdefb3512010-12-08 21:16:57 -08002400 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002401 goto nla_put_failure;
2402
Changli Gaod8d1f302010-06-10 23:31:35 -07002403 if (rt->dst.neighbour)
2404 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002405
Changli Gaod8d1f302010-06-10 23:31:35 -07002406 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002407 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2408
2409 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002410
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002411 if (!(rt->rt6i_flags & RTF_EXPIRES))
2412 expires = 0;
2413 else if (rt->rt6i_expires - jiffies < INT_MAX)
2414 expires = rt->rt6i_expires - jiffies;
2415 else
2416 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002417
Changli Gaod8d1f302010-06-10 23:31:35 -07002418 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2419 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002420 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Thomas Graf2d7202b2006-08-22 00:01:27 -07002422 return nlmsg_end(skb, nlh);
2423
2424nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002425 nlmsg_cancel(skb, nlh);
2426 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Patrick McHardy1b43af52006-08-10 23:11:17 -07002429int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2432 int prefix;
2433
Thomas Graf2d7202b2006-08-22 00:01:27 -07002434 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2435 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2437 } else
2438 prefix = 0;
2439
Brian Haley191cd582008-08-14 15:33:21 -07002440 return rt6_fill_node(arg->net,
2441 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002443 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Thomas Grafc127ea22007-03-22 11:58:32 -07002446static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002448 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002449 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002451 struct sk_buff *skb;
2452 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002453 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002454 int err, iif = 0;
2455
2456 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2457 if (err < 0)
2458 goto errout;
2459
2460 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002461 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002462
2463 if (tb[RTA_SRC]) {
2464 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2465 goto errout;
2466
David S. Miller4c9483b2011-03-12 16:22:43 -05002467 ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
Thomas Grafab364a62006-08-22 00:01:47 -07002468 }
2469
2470 if (tb[RTA_DST]) {
2471 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2472 goto errout;
2473
David S. Miller4c9483b2011-03-12 16:22:43 -05002474 ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
Thomas Grafab364a62006-08-22 00:01:47 -07002475 }
2476
2477 if (tb[RTA_IIF])
2478 iif = nla_get_u32(tb[RTA_IIF]);
2479
2480 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002481 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002482
2483 if (iif) {
2484 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002485 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002486 if (!dev) {
2487 err = -ENODEV;
2488 goto errout;
2489 }
2490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002493 if (skb == NULL) {
2494 err = -ENOBUFS;
2495 goto errout;
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* Reserve room for dummy headers, this skb can pass
2499 through good chunk of routing engine.
2500 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002501 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2503
David S. Miller4c9483b2011-03-12 16:22:43 -05002504 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002505 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
David S. Miller4c9483b2011-03-12 16:22:43 -05002507 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002509 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002511 kfree_skb(skb);
2512 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
Daniel Lezcano55786892008-03-04 13:47:47 -08002515 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002516errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Thomas Graf86872cb2006-08-22 00:01:08 -07002520void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002523 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002524 u32 seq;
2525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002527 err = -ENOBUFS;
2528 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002529
Thomas Graf339bf982006-11-10 14:10:15 -08002530 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002531 if (skb == NULL)
2532 goto errout;
2533
Brian Haley191cd582008-08-14 15:33:21 -07002534 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002535 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002536 if (err < 0) {
2537 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2538 WARN_ON(err == -EMSGSIZE);
2539 kfree_skb(skb);
2540 goto errout;
2541 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002542 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2543 info->nlh, gfp_any());
2544 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002545errout:
2546 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002547 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002550static int ip6_route_dev_notify(struct notifier_block *this,
2551 unsigned long event, void *data)
2552{
2553 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002554 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002555
2556 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002557 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002558 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2559#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002560 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002561 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002562 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002563 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2564#endif
2565 }
2566
2567 return NOTIFY_OK;
2568}
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570/*
2571 * /proc
2572 */
2573
2574#ifdef CONFIG_PROC_FS
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576struct rt6_proc_arg
2577{
2578 char *buffer;
2579 int offset;
2580 int length;
2581 int skip;
2582 int len;
2583};
2584
2585static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2586{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002587 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002589 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002592 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002594 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595#endif
2596
2597 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002598 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002600 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002602 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002603 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2604 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002605 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return 0;
2607}
2608
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002609static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002611 struct net *net = (struct net *)m->private;
2612 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002616static int ipv6_route_open(struct inode *inode, struct file *file)
2617{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002618 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002619}
2620
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002621static const struct file_operations ipv6_route_proc_fops = {
2622 .owner = THIS_MODULE,
2623 .open = ipv6_route_open,
2624 .read = seq_read,
2625 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002626 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002627};
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2630{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002631 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002633 net->ipv6.rt6_stats->fib_nodes,
2634 net->ipv6.rt6_stats->fib_route_nodes,
2635 net->ipv6.rt6_stats->fib_rt_alloc,
2636 net->ipv6.rt6_stats->fib_rt_entries,
2637 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002638 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002639 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 return 0;
2642}
2643
2644static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2645{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002646 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002647}
2648
Arjan van de Ven9a321442007-02-12 00:55:35 -08002649static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .owner = THIS_MODULE,
2651 .open = rt6_stats_seq_open,
2652 .read = seq_read,
2653 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002654 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655};
2656#endif /* CONFIG_PROC_FS */
2657
2658#ifdef CONFIG_SYSCTL
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002661int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 void __user *buffer, size_t *lenp, loff_t *ppos)
2663{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002664 struct net *net;
2665 int delay;
2666 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002668
2669 net = (struct net *)ctl->extra1;
2670 delay = net->ipv6.sysctl.flush_delay;
2671 proc_dointvec(ctl, write, buffer, lenp, ppos);
2672 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002676ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002677 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002679 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002681 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002682 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 },
2684 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002686 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 .maxlen = sizeof(int),
2688 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002689 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 },
2691 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002693 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 .maxlen = sizeof(int),
2695 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002696 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 },
2698 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002700 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 .maxlen = sizeof(int),
2702 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002703 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 },
2705 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002707 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 .maxlen = sizeof(int),
2709 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002710 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 },
2712 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002714 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 .maxlen = sizeof(int),
2716 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002717 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 },
2719 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002721 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .maxlen = sizeof(int),
2723 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002724 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 },
2726 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002728 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .maxlen = sizeof(int),
2730 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002731 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 },
2733 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002735 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 .maxlen = sizeof(int),
2737 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002738 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 },
2740 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002742 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .maxlen = sizeof(int),
2744 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002745 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002747 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748};
2749
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002750struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002751{
2752 struct ctl_table *table;
2753
2754 table = kmemdup(ipv6_route_table_template,
2755 sizeof(ipv6_route_table_template),
2756 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002757
2758 if (table) {
2759 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002760 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002761 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002762 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2763 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2764 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2765 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2766 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2767 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2768 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002769 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002770 }
2771
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002772 return table;
2773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774#endif
2775
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002776static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002777{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002778 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002779
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002780 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2781 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002782
Eric Dumazetfc66f952010-10-08 06:37:34 +00002783 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2784 goto out_ip6_dst_ops;
2785
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002786 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2787 sizeof(*net->ipv6.ip6_null_entry),
2788 GFP_KERNEL);
2789 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002790 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002791 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002792 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002793 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002794 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2795 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002796
2797#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2798 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2799 sizeof(*net->ipv6.ip6_prohibit_entry),
2800 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002801 if (!net->ipv6.ip6_prohibit_entry)
2802 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002803 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002804 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002805 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002806 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2807 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002808
2809 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2810 sizeof(*net->ipv6.ip6_blk_hole_entry),
2811 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002812 if (!net->ipv6.ip6_blk_hole_entry)
2813 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002814 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002815 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002816 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002817 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2818 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002819#endif
2820
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002821 net->ipv6.sysctl.flush_delay = 0;
2822 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2823 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2824 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2825 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2826 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2827 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2828 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2829
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002830#ifdef CONFIG_PROC_FS
2831 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2832 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2833#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002834 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2835
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002836 ret = 0;
2837out:
2838 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002839
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002840#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2841out_ip6_prohibit_entry:
2842 kfree(net->ipv6.ip6_prohibit_entry);
2843out_ip6_null_entry:
2844 kfree(net->ipv6.ip6_null_entry);
2845#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002846out_ip6_dst_entries:
2847 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002848out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002849 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002850}
2851
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002852static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002853{
2854#ifdef CONFIG_PROC_FS
2855 proc_net_remove(net, "ipv6_route");
2856 proc_net_remove(net, "rt6_stats");
2857#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002858 kfree(net->ipv6.ip6_null_entry);
2859#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2860 kfree(net->ipv6.ip6_prohibit_entry);
2861 kfree(net->ipv6.ip6_blk_hole_entry);
2862#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002863 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002864}
2865
2866static struct pernet_operations ip6_route_net_ops = {
2867 .init = ip6_route_net_init,
2868 .exit = ip6_route_net_exit,
2869};
2870
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002871static struct notifier_block ip6_route_dev_notifier = {
2872 .notifier_call = ip6_route_dev_notify,
2873 .priority = 0,
2874};
2875
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002876int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002878 int ret;
2879
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002880 ret = -ENOMEM;
2881 ip6_dst_ops_template.kmem_cachep =
2882 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2883 SLAB_HWCACHE_ALIGN, NULL);
2884 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002885 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002886
Eric Dumazetfc66f952010-10-08 06:37:34 +00002887 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002888 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002889 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002890
Eric Dumazetfc66f952010-10-08 06:37:34 +00002891 ret = register_pernet_subsys(&ip6_route_net_ops);
2892 if (ret)
2893 goto out_dst_entries;
2894
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002895 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2896
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002897 /* Registering of the loopback is done before this portion of code,
2898 * the loopback reference in rt6_info will not be taken, do it
2899 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002900 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002901 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2902 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002903 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002904 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002905 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002906 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2907 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002908 ret = fib6_init();
2909 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002910 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002911
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002912 ret = xfrm6_init();
2913 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002914 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002915
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002916 ret = fib6_rules_init();
2917 if (ret)
2918 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002919
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002920 ret = -ENOBUFS;
2921 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2922 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2923 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2924 goto fib6_rules_init;
2925
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002926 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002927 if (ret)
2928 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002929
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002930out:
2931 return ret;
2932
2933fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002934 fib6_rules_cleanup();
2935xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002936 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002937out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002938 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002939out_register_subsys:
2940 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002941out_dst_entries:
2942 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002943out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002944 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
2948void ip6_route_cleanup(void)
2949{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002950 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002951 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002954 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002955 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002956 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}