blob: 2998cb5be90b7631c4d9129b8a60e9844526a04e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
29#include <linux/types.h>
30#include <linux/times.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
33#include <linux/net.h>
34#include <linux/route.h>
35#include <linux/netdevice.h>
36#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090037#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080042#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020044#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/snmp.h>
46#include <net/ipv6.h>
47#include <net/ip6_fib.h>
48#include <net/ip6_route.h>
49#include <net/ndisc.h>
50#include <net/addrconf.h>
51#include <net/tcp.h>
52#include <linux/rtnetlink.h>
53#include <net/dst.h>
54#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070055#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070056#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <asm/uaccess.h>
59
60#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
63
64/* Set to 3 to get tracing. */
65#define RT6_DEBUG 2
66
67#if RT6_DEBUG >= 3
68#define RDBG(x) printk x
69#define RT6_TRACE(x...) printk(KERN_DEBUG x)
70#else
71#define RDBG(x)
72#define RT6_TRACE(x...) do { ; } while (0)
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
76static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080077static unsigned int ip6_default_advmss(const struct dst_entry *dst);
David S. Millerd33e4552010-12-14 13:01:14 -080078static unsigned int ip6_default_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static struct dst_entry *ip6_negative_advice(struct dst_entry *);
80static void ip6_dst_destroy(struct dst_entry *);
81static void ip6_dst_ifdown(struct dst_entry *,
82 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080083static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int ip6_pkt_discard(struct sk_buff *skb);
86static int ip6_pkt_discard_out(struct sk_buff *skb);
87static void ip6_link_failure(struct sk_buff *skb);
88static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
89
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080090#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080091static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000092 const struct in6_addr *prefix, int prefixlen,
93 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080094 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080095static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096 const struct in6_addr *prefix, int prefixlen,
97 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080098#endif
99
David S. Miller06582542011-01-27 14:58:42 -0800100static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
101{
102 struct rt6_info *rt = (struct rt6_info *) dst;
103 struct inet_peer *peer;
104 u32 *p = NULL;
105
106 if (!rt->rt6i_peer)
107 rt6_bind_peer(rt, 1);
108
109 peer = rt->rt6i_peer;
110 if (peer) {
111 u32 *old_p = __DST_METRICS_PTR(old);
112 unsigned long prev, new;
113
114 p = peer->metrics;
115 if (inet_metrics_new(peer))
116 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
117
118 new = (unsigned long) p;
119 prev = cmpxchg(&dst->_metrics, old, new);
120
121 if (prev != old) {
122 p = __DST_METRICS_PTR(prev);
123 if (prev & DST_METRICS_READ_ONLY)
124 p = NULL;
125 }
126 }
127 return p;
128}
129
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800130static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800132 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .gc = ip6_dst_gc,
134 .gc_thresh = 1024,
135 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800136 .default_advmss = ip6_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800137 .default_mtu = ip6_default_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800138 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .destroy = ip6_dst_destroy,
140 .ifdown = ip6_dst_ifdown,
141 .negative_advice = ip6_negative_advice,
142 .link_failure = ip6_link_failure,
143 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700144 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Roland Dreierec831ea2011-01-31 13:16:00 -0800147static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
148{
149 return 0;
150}
151
David S. Miller14e50e52007-05-24 18:17:54 -0700152static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
153{
154}
155
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 Harrison09640e632009-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,
David S. Miller957c6652011-06-24 15:25:00 -0700231 struct net_device *dev,
232 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David S. Miller957c6652011-06-24 15:25:00 -0700234 struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
David S. Millercf911662011-04-28 14:31:47 -0700235
236 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
237
238 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static void ip6_dst_destroy(struct dst_entry *dst)
242{
243 struct rt6_info *rt = (struct rt6_info *)dst;
244 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800245 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (idev != NULL) {
248 rt->rt6i_idev = NULL;
249 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900250 }
David S. Millerb3419362010-11-30 12:27:11 -0800251 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800252 rt->rt6i_peer = NULL;
253 inet_putpeer(peer);
254 }
255}
256
David S. Miller6431cbc2011-02-07 20:38:06 -0800257static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
258
259static u32 rt6_peer_genid(void)
260{
261 return atomic_read(&__rt6_peer_genid);
262}
263
David S. Millerb3419362010-11-30 12:27:11 -0800264void rt6_bind_peer(struct rt6_info *rt, int create)
265{
266 struct inet_peer *peer;
267
David S. Millerb3419362010-11-30 12:27:11 -0800268 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
269 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
270 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800271 else
272 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
276 int how)
277{
278 struct rt6_info *rt = (struct rt6_info *)dst;
279 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800280 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900281 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800283 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
284 struct inet6_dev *loopback_idev =
285 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (loopback_idev != NULL) {
287 rt->rt6i_idev = loopback_idev;
288 in6_dev_put(idev);
289 }
290 }
291}
292
293static __inline__ int rt6_check_expired(const struct rt6_info *rt)
294{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000295 return (rt->rt6i_flags & RTF_EXPIRES) &&
296 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000299static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700300{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000301 return ipv6_addr_type(daddr) &
302 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700306 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800309static inline struct rt6_info *rt6_device_match(struct net *net,
310 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000311 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700313 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct rt6_info *local = NULL;
316 struct rt6_info *sprt;
317
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900318 if (!oif && ipv6_addr_any(saddr))
319 goto out;
320
Changli Gaod8d1f302010-06-10 23:31:35 -0700321 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900322 struct net_device *dev = sprt->rt6i_dev;
323
324 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (dev->ifindex == oif)
326 return sprt;
327 if (dev->flags & IFF_LOOPBACK) {
328 if (sprt->rt6i_idev == NULL ||
329 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700330 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900332 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 local->rt6i_idev->dev->ifindex == oif))
334 continue;
335 }
336 local = sprt;
337 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900338 } else {
339 if (ipv6_chk_addr(net, saddr, dev,
340 flags & RT6_LOOKUP_F_IFACE))
341 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900345 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (local)
347 return local;
348
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700349 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800350 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return rt;
354}
355
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800356#ifdef CONFIG_IPV6_ROUTER_PREF
357static void rt6_probe(struct rt6_info *rt)
358{
David S. Miller69cce1d2011-07-17 23:09:49 -0700359 struct neighbour *neigh = rt ? dst_get_neighbour(&rt->dst) : NULL;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800360 /*
361 * Okay, this does not seem to be appropriate
362 * for now, however, we need to check if it
363 * is really so; aka Router Reachability Probing.
364 *
365 * Router Reachability Probe MUST be rate-limited
366 * to no more than one per minute.
367 */
368 if (!neigh || (neigh->nud_state & NUD_VALID))
369 return;
370 read_lock_bh(&neigh->lock);
371 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800372 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800373 struct in6_addr mcaddr;
374 struct in6_addr *target;
375
376 neigh->updated = jiffies;
377 read_unlock_bh(&neigh->lock);
378
379 target = (struct in6_addr *)&neigh->primary_key;
380 addrconf_addr_solict_mult(target, &mcaddr);
381 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
382 } else
383 read_unlock_bh(&neigh->lock);
384}
385#else
386static inline void rt6_probe(struct rt6_info *rt)
387{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800388}
389#endif
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800392 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700394static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800396 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700397 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800398 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700399 if ((dev->flags & IFF_LOOPBACK) &&
400 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
401 return 1;
402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Dave Jonesb6f99a22007-03-22 12:27:49 -0700405static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
David S. Miller69cce1d2011-07-17 23:09:49 -0700407 struct neighbour *neigh = dst_get_neighbour(&rt->dst);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800408 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700409 if (rt->rt6i_flags & RTF_NONEXTHOP ||
410 !(rt->rt6i_flags & RTF_GATEWAY))
411 m = 1;
412 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800413 read_lock_bh(&neigh->lock);
414 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700415 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800416#ifdef CONFIG_IPV6_ROUTER_PREF
417 else if (neigh->nud_state & NUD_FAILED)
418 m = 0;
419#endif
420 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800421 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800422 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800423 } else
424 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800425 return m;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800428static int rt6_score_route(struct rt6_info *rt, int oif,
429 int strict)
430{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700431 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900432
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700433 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700434 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800435 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800436#ifdef CONFIG_IPV6_ROUTER_PREF
437 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
438#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700439 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800440 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800441 return -1;
442 return m;
443}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David S. Millerf11e6652007-03-24 20:36:25 -0700445static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
446 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800447{
David S. Millerf11e6652007-03-24 20:36:25 -0700448 int m;
449
450 if (rt6_check_expired(rt))
451 goto out;
452
453 m = rt6_score_route(rt, oif, strict);
454 if (m < 0)
455 goto out;
456
457 if (m > *mpri) {
458 if (strict & RT6_LOOKUP_F_REACHABLE)
459 rt6_probe(match);
460 *mpri = m;
461 match = rt;
462 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
463 rt6_probe(rt);
464 }
465
466out:
467 return match;
468}
469
470static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
471 struct rt6_info *rr_head,
472 u32 metric, int oif, int strict)
473{
474 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800475 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David S. Millerf11e6652007-03-24 20:36:25 -0700477 match = NULL;
478 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700479 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700480 match = find_match(rt, oif, strict, &mpri, match);
481 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700482 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700483 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800484
David S. Millerf11e6652007-03-24 20:36:25 -0700485 return match;
486}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800487
David S. Millerf11e6652007-03-24 20:36:25 -0700488static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
489{
490 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800491 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David S. Millerf11e6652007-03-24 20:36:25 -0700493 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800494 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
David S. Millerf11e6652007-03-24 20:36:25 -0700496 rt0 = fn->rr_ptr;
497 if (!rt0)
498 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David S. Millerf11e6652007-03-24 20:36:25 -0700500 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800502 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700503 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700504 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700505
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800506 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700507 if (!next || next->rt6i_metric != rt0->rt6i_metric)
508 next = fn->leaf;
509
510 if (next != rt0)
511 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
David S. Millerf11e6652007-03-24 20:36:25 -0700514 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800515 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900517 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000518 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800521#ifdef CONFIG_IPV6_ROUTE_INFO
522int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000523 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800524{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900525 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800526 struct route_info *rinfo = (struct route_info *) opt;
527 struct in6_addr prefix_buf, *prefix;
528 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900529 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800530 struct rt6_info *rt;
531
532 if (len < sizeof(struct route_info)) {
533 return -EINVAL;
534 }
535
536 /* Sanity check for prefix_len and length */
537 if (rinfo->length > 3) {
538 return -EINVAL;
539 } else if (rinfo->prefix_len > 128) {
540 return -EINVAL;
541 } else if (rinfo->prefix_len > 64) {
542 if (rinfo->length < 2) {
543 return -EINVAL;
544 }
545 } else if (rinfo->prefix_len > 0) {
546 if (rinfo->length < 1) {
547 return -EINVAL;
548 }
549 }
550
551 pref = rinfo->route_pref;
552 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000553 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800554
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900555 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800556
557 if (rinfo->length == 3)
558 prefix = (struct in6_addr *)rinfo->prefix;
559 else {
560 /* this function is safe */
561 ipv6_addr_prefix(&prefix_buf,
562 (struct in6_addr *)rinfo->prefix,
563 rinfo->prefix_len);
564 prefix = &prefix_buf;
565 }
566
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800567 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
568 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800569
570 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700571 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800572 rt = NULL;
573 }
574
575 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800576 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800577 pref);
578 else if (rt)
579 rt->rt6i_flags = RTF_ROUTEINFO |
580 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
581
582 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900583 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800584 rt->rt6i_flags &= ~RTF_EXPIRES;
585 } else {
586 rt->rt6i_expires = jiffies + HZ * lifetime;
587 rt->rt6i_flags |= RTF_EXPIRES;
588 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700589 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800590 }
591 return 0;
592}
593#endif
594
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800595#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700596do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800597 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700598 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700599 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700600 if (fn->fn_flags & RTN_TL_ROOT) \
601 goto out; \
602 pn = fn->parent; \
603 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800604 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700605 else \
606 fn = pn; \
607 if (fn->fn_flags & RTN_RTINFO) \
608 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700609 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700610 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700611} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700612
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800613static struct rt6_info *ip6_pol_route_lookup(struct net *net,
614 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500615 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct fib6_node *fn;
618 struct rt6_info *rt;
619
Thomas Grafc71099a2006-08-04 23:20:06 -0700620 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500621 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700622restart:
623 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500624 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
625 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700626out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700627 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700628 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700629 return rt;
630
631}
632
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900633struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
634 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700635{
David S. Miller4c9483b2011-03-12 16:22:43 -0500636 struct flowi6 fl6 = {
637 .flowi6_oif = oif,
638 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700639 };
640 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700641 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700642
Thomas Grafadaa70b2006-10-13 15:01:03 -0700643 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500644 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700645 flags |= RT6_LOOKUP_F_HAS_SADDR;
646 }
647
David S. Miller4c9483b2011-03-12 16:22:43 -0500648 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700649 if (dst->error == 0)
650 return (struct rt6_info *) dst;
651
652 dst_release(dst);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NULL;
655}
656
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900657EXPORT_SYMBOL(rt6_lookup);
658
Thomas Grafc71099a2006-08-04 23:20:06 -0700659/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 It takes new route entry, the addition fails by any reason the
661 route is freed. In any case, if caller does not hold it, it may
662 be destroyed.
663 */
664
Thomas Graf86872cb2006-08-22 00:01:08 -0700665static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700668 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Grafc71099a2006-08-04 23:20:06 -0700670 table = rt->rt6i_table;
671 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700672 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700673 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 return err;
676}
677
Thomas Graf40e22e82006-08-22 00:00:45 -0700678int ip6_ins_rt(struct rt6_info *rt)
679{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800680 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900681 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800682 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800683 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700684}
685
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000686static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
687 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct rt6_info *rt;
690
691 /*
692 * Clone the route.
693 */
694
695 rt = ip6_rt_copy(ort);
696
697 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800698 struct neighbour *neigh;
699 int attempts = !in_softirq();
700
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900701 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
702 if (rt->rt6i_dst.plen != 128 &&
703 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
704 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900708 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 rt->rt6i_dst.plen = 128;
710 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700711 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713#ifdef CONFIG_IPV6_SUBTREES
714 if (rt->rt6i_src.plen && saddr) {
715 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
716 rt->rt6i_src.plen = 128;
717 }
718#endif
719
David S. Miller14deae42009-01-04 16:04:39 -0800720 retry:
721 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
722 if (IS_ERR(neigh)) {
723 struct net *net = dev_net(rt->rt6i_dev);
724 int saved_rt_min_interval =
725 net->ipv6.sysctl.ip6_rt_gc_min_interval;
726 int saved_rt_elasticity =
727 net->ipv6.sysctl.ip6_rt_gc_elasticity;
728
729 if (attempts-- > 0) {
730 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
731 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
732
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000733 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800734
735 net->ipv6.sysctl.ip6_rt_gc_elasticity =
736 saved_rt_elasticity;
737 net->ipv6.sysctl.ip6_rt_gc_min_interval =
738 saved_rt_min_interval;
739 goto retry;
740 }
741
742 if (net_ratelimit())
743 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700744 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700745 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800746 return NULL;
747 }
David S. Miller69cce1d2011-07-17 23:09:49 -0700748 dst_set_neighbour(&rt->dst, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800752 return rt;
753}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000755static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800756{
757 struct rt6_info *rt = ip6_rt_copy(ort);
758 if (rt) {
759 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
760 rt->rt6i_dst.plen = 128;
761 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700762 rt->dst.flags |= DST_HOST;
David S. Miller69cce1d2011-07-17 23:09:49 -0700763 dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour(&ort->dst)));
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800764 }
765 return rt;
766}
767
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800768static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500769 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800772 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700773 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800775 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700776 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700778 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700781 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800783restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500784 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700787 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800788
David S. Miller4c9483b2011-03-12 16:22:43 -0500789 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800790 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800791 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Changli Gaod8d1f302010-06-10 23:31:35 -0700794 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700795 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800796
David S. Miller69cce1d2011-07-17 23:09:49 -0700797 if (!dst_get_neighbour(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500798 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800799 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500800 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800801 else
802 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800803
Changli Gaod8d1f302010-06-10 23:31:35 -0700804 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800805 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800806
Changli Gaod8d1f302010-06-10 23:31:35 -0700807 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800808 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700809 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800810 if (!err)
811 goto out2;
812 }
813
814 if (--attempts <= 0)
815 goto out2;
816
817 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700818 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800819 * released someone could insert this route. Relookup.
820 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700821 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800822 goto relookup;
823
824out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800825 if (reachable) {
826 reachable = 0;
827 goto restart_2;
828 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700829 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700830 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700832 rt->dst.lastuse = jiffies;
833 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700834
835 return rt;
836}
837
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800838static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500839 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700840{
David S. Miller4c9483b2011-03-12 16:22:43 -0500841 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700842}
843
Thomas Grafc71099a2006-08-04 23:20:06 -0700844void ip6_route_input(struct sk_buff *skb)
845{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000846 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900847 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700848 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500849 struct flowi6 fl6 = {
850 .flowi6_iif = skb->dev->ifindex,
851 .daddr = iph->daddr,
852 .saddr = iph->saddr,
853 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
854 .flowi6_mark = skb->mark,
855 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700856 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700857
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800858 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700859 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700860
David S. Miller4c9483b2011-03-12 16:22:43 -0500861 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700862}
863
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800864static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500865 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700866{
David S. Miller4c9483b2011-03-12 16:22:43 -0500867 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700868}
869
Florian Westphal9c7a4f9c2011-03-22 19:17:36 -0700870struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500871 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700872{
873 int flags = 0;
874
David S. Miller4c9483b2011-03-12 16:22:43 -0500875 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700876 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700877
David S. Miller4c9483b2011-03-12 16:22:43 -0500878 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700879 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000880 else if (sk)
881 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700882
David S. Miller4c9483b2011-03-12 16:22:43 -0500883 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900886EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
David S. Miller2774c132011-03-01 14:59:04 -0800888struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700889{
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700890 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700891 struct dst_entry *new = NULL;
892
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700893 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
David S. Miller14e50e52007-05-24 18:17:54 -0700894 if (rt) {
David S. Millercf911662011-04-28 14:31:47 -0700895 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
896
Changli Gaod8d1f302010-06-10 23:31:35 -0700897 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700898
David S. Miller14e50e52007-05-24 18:17:54 -0700899 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800900 new->input = dst_discard;
901 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700902
David S. Millerdefb3512010-12-08 21:16:57 -0800903 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -0700904 rt->rt6i_idev = ort->rt6i_idev;
905 if (rt->rt6i_idev)
906 in6_dev_hold(rt->rt6i_idev);
907 rt->rt6i_expires = 0;
908
909 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
910 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
911 rt->rt6i_metric = 0;
912
913 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
914#ifdef CONFIG_IPV6_SUBTREES
915 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
916#endif
917
918 dst_free(new);
919 }
920
David S. Miller69ead7a2011-03-01 14:45:33 -0800921 dst_release(dst_orig);
922 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700923}
David S. Miller14e50e52007-05-24 18:17:54 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Destination cache support functions
927 */
928
929static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
930{
931 struct rt6_info *rt;
932
933 rt = (struct rt6_info *) dst;
934
David S. Miller6431cbc2011-02-07 20:38:06 -0800935 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
936 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
937 if (!rt->rt6i_peer)
938 rt6_bind_peer(rt, 0);
939 rt->rt6i_peer_genid = rt6_peer_genid();
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return NULL;
944}
945
946static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
947{
948 struct rt6_info *rt = (struct rt6_info *) dst;
949
950 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000951 if (rt->rt6i_flags & RTF_CACHE) {
952 if (rt6_check_expired(rt)) {
953 ip6_del_rt(rt);
954 dst = NULL;
955 }
956 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000958 dst = NULL;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000961 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964static void ip6_link_failure(struct sk_buff *skb)
965{
966 struct rt6_info *rt;
967
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000968 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Eric Dumazetadf30902009-06-02 05:19:30 +0000970 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (rt) {
972 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700973 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 rt->rt6i_flags |= RTF_EXPIRES;
975 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
976 rt->rt6i_node->fn_sernum = -1;
977 }
978}
979
980static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
981{
982 struct rt6_info *rt6 = (struct rt6_info*)dst;
983
984 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
985 rt6->rt6i_flags |= RTF_MODIFIED;
986 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800987 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800989 features |= RTAX_FEATURE_ALLFRAG;
990 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
David S. Millerdefb3512010-12-08 21:16:57 -0800992 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
David S. Miller0dbaee32010-12-13 12:52:14 -0800996static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
David S. Miller0dbaee32010-12-13 12:52:14 -0800998 struct net_device *dev = dst->dev;
999 unsigned int mtu = dst_mtu(dst);
1000 struct net *net = dev_net(dev);
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1003
Daniel Lezcano55786892008-03-04 13:47:47 -08001004 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1005 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001008 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1009 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1010 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * rely only on pmtu discovery"
1012 */
1013 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1014 mtu = IPV6_MAXPLEN;
1015 return mtu;
1016}
1017
David S. Millerd33e4552010-12-14 13:01:14 -08001018static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1019{
1020 unsigned int mtu = IPV6_MIN_MTU;
1021 struct inet6_dev *idev;
1022
1023 rcu_read_lock();
1024 idev = __in6_dev_get(dst->dev);
1025 if (idev)
1026 mtu = idev->cnf.mtu6;
1027 rcu_read_unlock();
1028
1029 return mtu;
1030}
1031
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001032static struct dst_entry *icmp6_dst_gc_list;
1033static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001034
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001035struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001037 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct rt6_info *rt;
1040 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001041 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (unlikely(idev == NULL))
1044 return NULL;
1045
David S. Miller957c6652011-06-24 15:25:00 -07001046 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (unlikely(rt == NULL)) {
1048 in6_dev_put(idev);
1049 goto out;
1050 }
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (neigh)
1053 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001054 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001056 if (IS_ERR(neigh))
1057 neigh = NULL;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 rt->rt6i_idev = idev;
David S. Miller69cce1d2011-07-17 23:09:49 -07001061 dst_set_neighbour(&rt->dst, neigh);
Changli Gaod8d1f302010-06-10 23:31:35 -07001062 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001063 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001064 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001066 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001067 rt->dst.next = icmp6_dst_gc_list;
1068 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001069 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Daniel Lezcano55786892008-03-04 13:47:47 -08001071 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001074 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001077int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Hagen Paul Pfeifere9476e952011-02-25 05:45:19 +00001079 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001080 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001082 spin_lock_bh(&icmp6_dst_lock);
1083 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 while ((dst = *pprev) != NULL) {
1086 if (!atomic_read(&dst->__refcnt)) {
1087 *pprev = dst->next;
1088 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 } else {
1090 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001091 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 }
1094
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001095 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001096
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001097 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
David S. Miller1e493d12008-09-10 17:27:15 -07001100static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1101 void *arg)
1102{
1103 struct dst_entry *dst, **pprev;
1104
1105 spin_lock_bh(&icmp6_dst_lock);
1106 pprev = &icmp6_dst_gc_list;
1107 while ((dst = *pprev) != NULL) {
1108 struct rt6_info *rt = (struct rt6_info *) dst;
1109 if (func(rt, arg)) {
1110 *pprev = dst->next;
1111 dst_free(dst);
1112 } else {
1113 pprev = &dst->next;
1114 }
1115 }
1116 spin_unlock_bh(&icmp6_dst_lock);
1117}
1118
Daniel Lezcano569d3642008-01-18 03:56:57 -08001119static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001122 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001123 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1124 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1125 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1126 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1127 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001128 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Eric Dumazetfc66f952010-10-08 06:37:34 +00001130 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001131 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001132 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto out;
1134
Benjamin Thery6891a342008-03-04 13:49:47 -08001135 net->ipv6.ip6_rt_gc_expire++;
1136 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1137 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001138 entries = dst_entries_get_slow(ops);
1139 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001140 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001142 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001143 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146/* Clean host part of a prefix. Not necessary in radix tree,
1147 but results in cleaner routing tables.
1148
1149 Remove it only when all the things will work!
1150 */
1151
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001152int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
David S. Miller5170ae82010-12-12 21:35:57 -08001154 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001155 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001156 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001157 struct inet6_dev *idev;
1158
1159 rcu_read_lock();
1160 idev = __in6_dev_get(dev);
1161 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001162 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001163 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001164 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001165 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 return hoplimit;
1168}
David S. Millerabbf46a2010-12-12 21:14:46 -08001169EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171/*
1172 *
1173 */
1174
Thomas Graf86872cb2006-08-22 00:01:08 -07001175int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001178 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct rt6_info *rt = NULL;
1180 struct net_device *dev = NULL;
1181 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001182 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int addr_type;
1184
Thomas Graf86872cb2006-08-22 00:01:08 -07001185 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EINVAL;
1187#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001188 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return -EINVAL;
1190#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001191 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001193 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!dev)
1195 goto out;
1196 idev = in6_dev_get(dev);
1197 if (!idev)
1198 goto out;
1199 }
1200
Thomas Graf86872cb2006-08-22 00:01:08 -07001201 if (cfg->fc_metric == 0)
1202 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Daniel Lezcano55786892008-03-04 13:47:47 -08001204 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001205 if (table == NULL) {
1206 err = -ENOBUFS;
1207 goto out;
1208 }
1209
David S. Miller957c6652011-06-24 15:25:00 -07001210 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (rt == NULL) {
1213 err = -ENOMEM;
1214 goto out;
1215 }
1216
Changli Gaod8d1f302010-06-10 23:31:35 -07001217 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001218 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1219 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1220 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Thomas Graf86872cb2006-08-22 00:01:08 -07001222 if (cfg->fc_protocol == RTPROT_UNSPEC)
1223 cfg->fc_protocol = RTPROT_BOOT;
1224 rt->rt6i_protocol = cfg->fc_protocol;
1225
1226 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001229 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001230 else if (cfg->fc_flags & RTF_LOCAL)
1231 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001233 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Changli Gaod8d1f302010-06-10 23:31:35 -07001235 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Thomas Graf86872cb2006-08-22 00:01:08 -07001237 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1238 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001240 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001243 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1244 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#endif
1246
Thomas Graf86872cb2006-08-22 00:01:08 -07001247 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* We cannot add true routes via loopback here,
1250 they would result in kernel looping; promote them to reject routes
1251 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001252 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001253 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1254 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001256 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (dev) {
1258 dev_put(dev);
1259 in6_dev_put(idev);
1260 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001261 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dev_hold(dev);
1263 idev = in6_dev_get(dev);
1264 if (!idev) {
1265 err = -ENODEV;
1266 goto out;
1267 }
1268 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001269 rt->dst.output = ip6_pkt_discard_out;
1270 rt->dst.input = ip6_pkt_discard;
1271 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1273 goto install_route;
1274 }
1275
Thomas Graf86872cb2006-08-22 00:01:08 -07001276 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001277 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int gwa_type;
1279
Thomas Graf86872cb2006-08-22 00:01:08 -07001280 gw_addr = &cfg->fc_gateway;
1281 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 gwa_type = ipv6_addr_type(gw_addr);
1283
1284 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1285 struct rt6_info *grt;
1286
1287 /* IPv6 strictly inhibits using not link-local
1288 addresses as nexthop address.
1289 Otherwise, router will not able to send redirects.
1290 It is very good, but in some (rare!) circumstances
1291 (SIT, PtP, NBMA NOARP links) it is handy to allow
1292 some exceptions. --ANK
1293 */
1294 err = -EINVAL;
1295 if (!(gwa_type&IPV6_ADDR_UNICAST))
1296 goto out;
1297
Daniel Lezcano55786892008-03-04 13:47:47 -08001298 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 err = -EHOSTUNREACH;
1301 if (grt == NULL)
1302 goto out;
1303 if (dev) {
1304 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001305 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto out;
1307 }
1308 } else {
1309 dev = grt->rt6i_dev;
1310 idev = grt->rt6i_idev;
1311 dev_hold(dev);
1312 in6_dev_hold(grt->rt6i_idev);
1313 }
1314 if (!(grt->rt6i_flags&RTF_GATEWAY))
1315 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001316 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (err)
1319 goto out;
1320 }
1321 err = -EINVAL;
1322 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1323 goto out;
1324 }
1325
1326 err = -ENODEV;
1327 if (dev == NULL)
1328 goto out;
1329
Daniel Walterc3968a82011-04-13 21:10:57 +00001330 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1331 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1332 err = -EINVAL;
1333 goto out;
1334 }
1335 ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1336 rt->rt6i_prefsrc.plen = 128;
1337 } else
1338 rt->rt6i_prefsrc.plen = 0;
1339
Thomas Graf86872cb2006-08-22 00:01:08 -07001340 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
David S. Miller69cce1d2011-07-17 23:09:49 -07001341 struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1342 if (IS_ERR(n)) {
1343 err = PTR_ERR(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto out;
1345 }
David S. Miller69cce1d2011-07-17 23:09:49 -07001346 dst_set_neighbour(&rt->dst, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Thomas Graf86872cb2006-08-22 00:01:08 -07001349 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001352 if (cfg->fc_mx) {
1353 struct nlattr *nla;
1354 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Thomas Graf86872cb2006-08-22 00:01:08 -07001356 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001357 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001358
1359 if (type) {
1360 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 err = -EINVAL;
1362 goto out;
1363 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001364
David S. Millerdefb3512010-12-08 21:16:57 -08001365 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
1369
Changli Gaod8d1f302010-06-10 23:31:35 -07001370 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001372 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001373
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001374 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001375
Thomas Graf86872cb2006-08-22 00:01:08 -07001376 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378out:
1379 if (dev)
1380 dev_put(dev);
1381 if (idev)
1382 in6_dev_put(idev);
1383 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001384 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return err;
1386}
1387
Thomas Graf86872cb2006-08-22 00:01:08 -07001388static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001391 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001392 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001394 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001395 return -ENOENT;
1396
Thomas Grafc71099a2006-08-04 23:20:06 -07001397 table = rt->rt6i_table;
1398 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Thomas Graf86872cb2006-08-22 00:01:08 -07001400 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001401 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Thomas Grafc71099a2006-08-04 23:20:06 -07001403 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return err;
1406}
1407
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001408int ip6_del_rt(struct rt6_info *rt)
1409{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001410 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001411 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001412 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001413 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001414}
1415
Thomas Graf86872cb2006-08-22 00:01:08 -07001416static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Thomas Grafc71099a2006-08-04 23:20:06 -07001418 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct fib6_node *fn;
1420 struct rt6_info *rt;
1421 int err = -ESRCH;
1422
Daniel Lezcano55786892008-03-04 13:47:47 -08001423 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001424 if (table == NULL)
1425 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Thomas Grafc71099a2006-08-04 23:20:06 -07001427 read_lock_bh(&table->tb6_lock);
1428
1429 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001430 &cfg->fc_dst, cfg->fc_dst_len,
1431 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001434 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001435 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001437 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001439 if (cfg->fc_flags & RTF_GATEWAY &&
1440 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001442 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001444 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001445 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Thomas Graf86872cb2006-08-22 00:01:08 -07001447 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001450 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 return err;
1453}
1454
1455/*
1456 * Handle redirects
1457 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001458struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001459 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001460 struct in6_addr gateway;
1461};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001463static struct rt6_info *__ip6_route_redirect(struct net *net,
1464 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001465 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001466 int flags)
1467{
David S. Miller4c9483b2011-03-12 16:22:43 -05001468 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001469 struct rt6_info *rt;
1470 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001473 * Get the "current" route for this destination and
1474 * check if the redirect has come from approriate router.
1475 *
1476 * RFC 2461 specifies that redirects should only be
1477 * accepted if they come from the nexthop to the target.
1478 * Due to the way the routes are chosen, this notion
1479 * is a bit fuzzy and one might need to check all possible
1480 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Thomas Grafc71099a2006-08-04 23:20:06 -07001483 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001484 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001485restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001486 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001487 /*
1488 * Current route is on-link; redirect is always invalid.
1489 *
1490 * Seems, previous statement is not true. It could
1491 * be node, which looks for us as on-link (f.e. proxy ndisc)
1492 * But then router serving it might decide, that we should
1493 * know truth 8)8) --ANK (980726).
1494 */
1495 if (rt6_check_expired(rt))
1496 continue;
1497 if (!(rt->rt6i_flags & RTF_GATEWAY))
1498 continue;
David S. Miller4c9483b2011-03-12 16:22:43 -05001499 if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001500 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001501 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001502 continue;
1503 break;
1504 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001505
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001506 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001507 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001508 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001509out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001510 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001511
1512 read_unlock_bh(&table->tb6_lock);
1513
1514 return rt;
1515};
1516
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001517static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1518 const struct in6_addr *src,
1519 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001520 struct net_device *dev)
1521{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001522 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001523 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001524 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001525 .fl6 = {
1526 .flowi6_oif = dev->ifindex,
1527 .daddr = *dest,
1528 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001529 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001530 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001531
Brian Haley86c36ce2009-10-07 13:58:01 -07001532 ipv6_addr_copy(&rdfl.gateway, gateway);
1533
Thomas Grafadaa70b2006-10-13 15:01:03 -07001534 if (rt6_need_strict(dest))
1535 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001536
David S. Miller4c9483b2011-03-12 16:22:43 -05001537 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001538 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001539}
1540
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001541void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1542 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001543 struct neighbour *neigh, u8 *lladdr, int on_link)
1544{
1545 struct rt6_info *rt, *nrt = NULL;
1546 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001547 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001548
1549 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1550
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001551 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (net_ratelimit())
1553 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1554 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /*
1559 * We have finally decided to accept it.
1560 */
1561
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001562 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1564 NEIGH_UPDATE_F_OVERRIDE|
1565 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1566 NEIGH_UPDATE_F_ISROUTER))
1567 );
1568
1569 /*
1570 * Redirect received -> path was valid.
1571 * Look, redirects are sent only in response to data packets,
1572 * so that this nexthop apparently is reachable. --ANK
1573 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001574 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* Duplicate redirect: silently ignore. */
David S. Miller69cce1d2011-07-17 23:09:49 -07001577 if (neigh == dst_get_neighbour(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto out;
1579
1580 nrt = ip6_rt_copy(rt);
1581 if (nrt == NULL)
1582 goto out;
1583
1584 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1585 if (on_link)
1586 nrt->rt6i_flags &= ~RTF_GATEWAY;
1587
1588 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1589 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001590 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
David S. Miller69cce1d2011-07-17 23:09:49 -07001593 dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Thomas Graf40e22e82006-08-22 00:00:45 -07001595 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto out;
1597
Changli Gaod8d1f302010-06-10 23:31:35 -07001598 netevent.old = &rt->dst;
1599 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001600 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001603 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return;
1605 }
1606
1607out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001608 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/*
1612 * Handle ICMP "packet too big" messages
1613 * i.e. Path MTU discovery
1614 */
1615
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001616static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001617 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct rt6_info *rt, *nrt;
1620 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001621again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001622 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (rt == NULL)
1624 return;
1625
Andrey Vagind3052b52010-12-11 15:20:11 +00001626 if (rt6_check_expired(rt)) {
1627 ip6_del_rt(rt);
1628 goto again;
1629 }
1630
Changli Gaod8d1f302010-06-10 23:31:35 -07001631 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 goto out;
1633
1634 if (pmtu < IPV6_MIN_MTU) {
1635 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001636 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * MTU (1280) and a fragment header should always be included
1638 * after a node receiving Too Big message reporting PMTU is
1639 * less than the IPv6 Minimum Link MTU.
1640 */
1641 pmtu = IPV6_MIN_MTU;
1642 allfrag = 1;
1643 }
1644
1645 /* New mtu received -> path was valid.
1646 They are sent only in response to data packets,
1647 so that this nexthop apparently is reachable. --ANK
1648 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001649 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /* Host route. If it is static, it would be better
1652 not to override it, but add new one, so that
1653 when cache entry will expire old pmtu
1654 would return automatically.
1655 */
1656 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001657 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1658 if (allfrag) {
1659 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1660 features |= RTAX_FEATURE_ALLFRAG;
1661 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1662 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001663 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1665 goto out;
1666 }
1667
1668 /* Network route.
1669 Two cases are possible:
1670 1. It is connected route. Action: COW
1671 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1672 */
David S. Miller69cce1d2011-07-17 23:09:49 -07001673 if (!dst_get_neighbour(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001674 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001675 else
1676 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001677
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001678 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001679 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1680 if (allfrag) {
1681 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1682 features |= RTAX_FEATURE_ALLFRAG;
1683 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1684 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001685
1686 /* According to RFC 1981, detecting PMTU increase shouldn't be
1687 * happened within 5 mins, the recommended timer is 10 mins.
1688 * Here this route expiration time is set to ip6_rt_mtu_expires
1689 * which is 10 mins. After 10 mins the decreased pmtu is expired
1690 * and detecting PMTU increase will be automatically happened.
1691 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001692 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001693 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1694
Thomas Graf40e22e82006-08-22 00:00:45 -07001695 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001698 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001701void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001702 struct net_device *dev, u32 pmtu)
1703{
1704 struct net *net = dev_net(dev);
1705
1706 /*
1707 * RFC 1981 states that a node "MUST reduce the size of the packets it
1708 * is sending along the path" that caused the Packet Too Big message.
1709 * Since it's not possible in the general case to determine which
1710 * interface was used to send the original packet, we update the MTU
1711 * on the interface that will be used to send future packets. We also
1712 * update the MTU on the interface that received the Packet Too Big in
1713 * case the original packet was forced out that interface with
1714 * SO_BINDTODEVICE or similar. This is the next best thing to the
1715 * correct behaviour, which would be to update the MTU on all
1716 * interfaces.
1717 */
1718 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1719 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/*
1723 * Misc support functions
1724 */
1725
1726static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1727{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001728 struct net *net = dev_net(ort->rt6i_dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001729 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
David S. Miller957c6652011-06-24 15:25:00 -07001730 ort->dst.dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001733 rt->dst.input = ort->dst.input;
1734 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
David S. Millerdefb3512010-12-08 21:16:57 -08001736 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001737 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rt->rt6i_idev = ort->rt6i_idev;
1739 if (rt->rt6i_idev)
1740 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001741 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 rt->rt6i_expires = 0;
1743
1744 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1745 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1746 rt->rt6i_metric = 0;
1747
1748 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1749#ifdef CONFIG_IPV6_SUBTREES
1750 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1751#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001752 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
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,
David S. Miller957c6652011-06-24 15:25:00 -07002009 net->loopback_dev, 0);
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
David S. Miller11d53b42011-06-24 15:23:34 -07002021 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002022 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. Miller69cce1d2011-07-17 23:09:49 -07002038 dst_set_neighbour(&rt->dst, 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
David S. Miller69cce1d2011-07-17 23:09:49 -07002403 if (dst_get_neighbour(&rt->dst))
2404 NLA_PUT(skb, RTA_GATEWAY, 16, &dst_get_neighbour(&rt->dst)->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;
David S. Miller69cce1d2011-07-17 23:09:49 -07002588 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002590 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002593 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002595 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
David S. Miller69cce1d2011-07-17 23:09:49 -07002597 n = dst_get_neighbour(&rt->dst);
2598 if (n) {
2599 seq_printf(m, "%pi6", n->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002601 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002603 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002604 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2605 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002606 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608}
2609
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002610static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002612 struct net *net = (struct net *)m->private;
2613 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002617static int ipv6_route_open(struct inode *inode, struct file *file)
2618{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002619 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002620}
2621
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002622static const struct file_operations ipv6_route_proc_fops = {
2623 .owner = THIS_MODULE,
2624 .open = ipv6_route_open,
2625 .read = seq_read,
2626 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002627 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002628};
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2631{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002632 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002634 net->ipv6.rt6_stats->fib_nodes,
2635 net->ipv6.rt6_stats->fib_route_nodes,
2636 net->ipv6.rt6_stats->fib_rt_alloc,
2637 net->ipv6.rt6_stats->fib_rt_entries,
2638 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002639 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002640 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 return 0;
2643}
2644
2645static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2646{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002647 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002648}
2649
Arjan van de Ven9a321442007-02-12 00:55:35 -08002650static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 .owner = THIS_MODULE,
2652 .open = rt6_stats_seq_open,
2653 .read = seq_read,
2654 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002655 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656};
2657#endif /* CONFIG_PROC_FS */
2658
2659#ifdef CONFIG_SYSCTL
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002662int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 void __user *buffer, size_t *lenp, loff_t *ppos)
2664{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002665 struct net *net;
2666 int delay;
2667 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002669
2670 net = (struct net *)ctl->extra1;
2671 delay = net->ipv6.sysctl.flush_delay;
2672 proc_dointvec(ctl, write, buffer, lenp, ppos);
2673 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002677ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002678 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002680 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002682 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002683 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 },
2685 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002687 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 .maxlen = sizeof(int),
2689 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002690 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 },
2692 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002694 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .maxlen = sizeof(int),
2696 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002697 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 },
2699 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002701 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 .maxlen = sizeof(int),
2703 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002704 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 },
2706 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002708 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 .maxlen = sizeof(int),
2710 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002711 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 },
2713 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002715 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .maxlen = sizeof(int),
2717 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002718 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 },
2720 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002722 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 .maxlen = sizeof(int),
2724 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002725 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 },
2727 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002729 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 .maxlen = sizeof(int),
2731 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002732 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 },
2734 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002736 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .maxlen = sizeof(int),
2738 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002739 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 },
2741 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002743 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 .maxlen = sizeof(int),
2745 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002746 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002748 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749};
2750
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002751struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002752{
2753 struct ctl_table *table;
2754
2755 table = kmemdup(ipv6_route_table_template,
2756 sizeof(ipv6_route_table_template),
2757 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002758
2759 if (table) {
2760 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002761 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002762 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002763 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2764 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2765 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2766 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2767 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2768 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2769 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002770 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002771 }
2772
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002773 return table;
2774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775#endif
2776
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002777static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002778{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002779 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002780
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002781 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2782 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002783
Eric Dumazetfc66f952010-10-08 06:37:34 +00002784 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2785 goto out_ip6_dst_ops;
2786
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002787 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2788 sizeof(*net->ipv6.ip6_null_entry),
2789 GFP_KERNEL);
2790 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002791 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002792 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002793 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002794 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002795 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2796 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002797
2798#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2799 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2800 sizeof(*net->ipv6.ip6_prohibit_entry),
2801 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002802 if (!net->ipv6.ip6_prohibit_entry)
2803 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002804 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002805 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002806 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002807 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2808 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002809
2810 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2811 sizeof(*net->ipv6.ip6_blk_hole_entry),
2812 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002813 if (!net->ipv6.ip6_blk_hole_entry)
2814 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002815 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002816 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002817 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002818 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2819 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002820#endif
2821
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002822 net->ipv6.sysctl.flush_delay = 0;
2823 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2824 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2825 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2826 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2827 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2828 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2829 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2830
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002831#ifdef CONFIG_PROC_FS
2832 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2833 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2834#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002835 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2836
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002837 ret = 0;
2838out:
2839 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002840
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002841#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2842out_ip6_prohibit_entry:
2843 kfree(net->ipv6.ip6_prohibit_entry);
2844out_ip6_null_entry:
2845 kfree(net->ipv6.ip6_null_entry);
2846#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002847out_ip6_dst_entries:
2848 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002849out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002850 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002851}
2852
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002853static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002854{
2855#ifdef CONFIG_PROC_FS
2856 proc_net_remove(net, "ipv6_route");
2857 proc_net_remove(net, "rt6_stats");
2858#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002859 kfree(net->ipv6.ip6_null_entry);
2860#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2861 kfree(net->ipv6.ip6_prohibit_entry);
2862 kfree(net->ipv6.ip6_blk_hole_entry);
2863#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002864 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002865}
2866
2867static struct pernet_operations ip6_route_net_ops = {
2868 .init = ip6_route_net_init,
2869 .exit = ip6_route_net_exit,
2870};
2871
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002872static struct notifier_block ip6_route_dev_notifier = {
2873 .notifier_call = ip6_route_dev_notify,
2874 .priority = 0,
2875};
2876
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002877int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002879 int ret;
2880
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002881 ret = -ENOMEM;
2882 ip6_dst_ops_template.kmem_cachep =
2883 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2884 SLAB_HWCACHE_ALIGN, NULL);
2885 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002886 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002887
Eric Dumazetfc66f952010-10-08 06:37:34 +00002888 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002889 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002890 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002891
Eric Dumazetfc66f952010-10-08 06:37:34 +00002892 ret = register_pernet_subsys(&ip6_route_net_ops);
2893 if (ret)
2894 goto out_dst_entries;
2895
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002896 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2897
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002898 /* Registering of the loopback is done before this portion of code,
2899 * the loopback reference in rt6_info will not be taken, do it
2900 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002901 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002902 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2903 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002904 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002905 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002906 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002907 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2908 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002909 ret = fib6_init();
2910 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002911 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002912
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002913 ret = xfrm6_init();
2914 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002915 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002916
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002917 ret = fib6_rules_init();
2918 if (ret)
2919 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002920
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002921 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002922 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2923 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2924 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002925 goto fib6_rules_init;
2926
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002927 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002928 if (ret)
2929 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002930
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002931out:
2932 return ret;
2933
2934fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002935 fib6_rules_cleanup();
2936xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002937 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002938out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002939 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002940out_register_subsys:
2941 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002942out_dst_entries:
2943 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002944out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002945 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
2949void ip6_route_cleanup(void)
2950{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002951 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002952 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002955 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002956 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002957 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}