blob: 45fafa018f12f410850a5a5335f27e590c8fa65d [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,
92 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080093 struct in6_addr *gwaddr, int ifindex,
94 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080095static struct rt6_info *rt6_get_route_info(struct net *net,
96 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080097 struct in6_addr *gwaddr, int ifindex);
98#endif
99
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800100static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800102 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .gc = ip6_dst_gc,
104 .gc_thresh = 1024,
105 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800106 .default_advmss = ip6_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800107 .default_mtu = ip6_default_mtu,
David S. Miller62fa8a82011-01-26 20:51:05 -0800108 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .destroy = ip6_dst_destroy,
110 .ifdown = ip6_dst_ifdown,
111 .negative_advice = ip6_negative_advice,
112 .link_failure = ip6_link_failure,
113 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700114 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
David S. Miller14e50e52007-05-24 18:17:54 -0700117static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
118{
119}
120
121static struct dst_ops ip6_dst_blackhole_ops = {
122 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800123 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700124 .destroy = ip6_dst_destroy,
125 .check = ip6_dst_check,
126 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Miller14e50e52007-05-24 18:17:54 -0700127};
128
David S. Miller62fa8a82011-01-26 20:51:05 -0800129static const u32 ip6_template_metrics[RTAX_MAX] = {
130 [RTAX_HOPLIMIT - 1] = 255,
131};
132
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800133static struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700134 .dst = {
135 .__refcnt = ATOMIC_INIT(1),
136 .__use = 1,
137 .obsolete = -1,
138 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700139 .input = ip6_pkt_discard,
140 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 },
142 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700143 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .rt6i_metric = ~(u32) 0,
145 .rt6i_ref = ATOMIC_INIT(1),
146};
147
Thomas Graf101367c2006-08-04 03:39:02 -0700148#ifdef CONFIG_IPV6_MULTIPLE_TABLES
149
David S. Miller6723ab52006-10-18 21:20:57 -0700150static int ip6_pkt_prohibit(struct sk_buff *skb);
151static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700152
Adrian Bunk280a34c2008-04-21 02:29:32 -0700153static struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700154 .dst = {
155 .__refcnt = ATOMIC_INIT(1),
156 .__use = 1,
157 .obsolete = -1,
158 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700159 .input = ip6_pkt_prohibit,
160 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700161 },
162 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700163 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700164 .rt6i_metric = ~(u32) 0,
165 .rt6i_ref = ATOMIC_INIT(1),
166};
167
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800168static struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700169 .dst = {
170 .__refcnt = ATOMIC_INIT(1),
171 .__use = 1,
172 .obsolete = -1,
173 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700174 .input = dst_discard,
175 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700176 },
177 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700178 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700179 .rt6i_metric = ~(u32) 0,
180 .rt6i_ref = ATOMIC_INIT(1),
181};
182
183#endif
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* allocate dst with ip6_dst_ops */
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800186static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800188 return (struct rt6_info *)dst_alloc(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void ip6_dst_destroy(struct dst_entry *dst)
192{
193 struct rt6_info *rt = (struct rt6_info *)dst;
194 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800195 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (idev != NULL) {
198 rt->rt6i_idev = NULL;
199 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900200 }
David S. Miller62fa8a82011-01-26 20:51:05 -0800201 dst_destroy_metrics_generic(dst);
David S. Millerb3419362010-11-30 12:27:11 -0800202 if (peer) {
203 BUG_ON(!(rt->rt6i_flags & RTF_CACHE));
204 rt->rt6i_peer = NULL;
205 inet_putpeer(peer);
206 }
207}
208
209void rt6_bind_peer(struct rt6_info *rt, int create)
210{
211 struct inet_peer *peer;
212
213 if (WARN_ON(!(rt->rt6i_flags & RTF_CACHE)))
214 return;
215
216 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
217 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
218 inet_putpeer(peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
222 int how)
223{
224 struct rt6_info *rt = (struct rt6_info *)dst;
225 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800226 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900227 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800229 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
230 struct inet6_dev *loopback_idev =
231 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (loopback_idev != NULL) {
233 rt->rt6i_idev = loopback_idev;
234 in6_dev_put(idev);
235 }
236 }
237}
238
239static __inline__ int rt6_check_expired(const struct rt6_info *rt)
240{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000241 return (rt->rt6i_flags & RTF_EXPIRES) &&
242 time_after(jiffies, rt->rt6i_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Thomas Grafc71099a2006-08-04 23:20:06 -0700245static inline int rt6_need_strict(struct in6_addr *daddr)
246{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000247 return ipv6_addr_type(daddr) &
248 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700252 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800255static inline struct rt6_info *rt6_device_match(struct net *net,
256 struct rt6_info *rt,
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900257 struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700259 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct rt6_info *local = NULL;
262 struct rt6_info *sprt;
263
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900264 if (!oif && ipv6_addr_any(saddr))
265 goto out;
266
Changli Gaod8d1f302010-06-10 23:31:35 -0700267 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900268 struct net_device *dev = sprt->rt6i_dev;
269
270 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (dev->ifindex == oif)
272 return sprt;
273 if (dev->flags & IFF_LOOPBACK) {
274 if (sprt->rt6i_idev == NULL ||
275 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700276 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900278 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 local->rt6i_idev->dev->ifindex == oif))
280 continue;
281 }
282 local = sprt;
283 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900284 } else {
285 if (ipv6_chk_addr(net, saddr, dev,
286 flags & RT6_LOOKUP_F_IFACE))
287 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900291 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (local)
293 return local;
294
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700295 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800296 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900298out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return rt;
300}
301
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800302#ifdef CONFIG_IPV6_ROUTER_PREF
303static void rt6_probe(struct rt6_info *rt)
304{
305 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
306 /*
307 * Okay, this does not seem to be appropriate
308 * for now, however, we need to check if it
309 * is really so; aka Router Reachability Probing.
310 *
311 * Router Reachability Probe MUST be rate-limited
312 * to no more than one per minute.
313 */
314 if (!neigh || (neigh->nud_state & NUD_VALID))
315 return;
316 read_lock_bh(&neigh->lock);
317 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800318 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800319 struct in6_addr mcaddr;
320 struct in6_addr *target;
321
322 neigh->updated = jiffies;
323 read_unlock_bh(&neigh->lock);
324
325 target = (struct in6_addr *)&neigh->primary_key;
326 addrconf_addr_solict_mult(target, &mcaddr);
327 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
328 } else
329 read_unlock_bh(&neigh->lock);
330}
331#else
332static inline void rt6_probe(struct rt6_info *rt)
333{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800334}
335#endif
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800338 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700340static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800342 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700343 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800344 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700345 if ((dev->flags & IFF_LOOPBACK) &&
346 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
347 return 1;
348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Dave Jonesb6f99a22007-03-22 12:27:49 -0700351static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800353 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800354 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700355 if (rt->rt6i_flags & RTF_NONEXTHOP ||
356 !(rt->rt6i_flags & RTF_GATEWAY))
357 m = 1;
358 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800359 read_lock_bh(&neigh->lock);
360 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700361 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800362#ifdef CONFIG_IPV6_ROUTER_PREF
363 else if (neigh->nud_state & NUD_FAILED)
364 m = 0;
365#endif
366 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800367 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800368 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800369 } else
370 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800371 return m;
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800374static int rt6_score_route(struct rt6_info *rt, int oif,
375 int strict)
376{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700377 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900378
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700379 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700380 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800381 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800382#ifdef CONFIG_IPV6_ROUTER_PREF
383 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
384#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700385 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800386 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800387 return -1;
388 return m;
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David S. Millerf11e6652007-03-24 20:36:25 -0700391static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
392 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800393{
David S. Millerf11e6652007-03-24 20:36:25 -0700394 int m;
395
396 if (rt6_check_expired(rt))
397 goto out;
398
399 m = rt6_score_route(rt, oif, strict);
400 if (m < 0)
401 goto out;
402
403 if (m > *mpri) {
404 if (strict & RT6_LOOKUP_F_REACHABLE)
405 rt6_probe(match);
406 *mpri = m;
407 match = rt;
408 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
409 rt6_probe(rt);
410 }
411
412out:
413 return match;
414}
415
416static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
417 struct rt6_info *rr_head,
418 u32 metric, int oif, int strict)
419{
420 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800421 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David S. Millerf11e6652007-03-24 20:36:25 -0700423 match = NULL;
424 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700425 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700426 match = find_match(rt, oif, strict, &mpri, match);
427 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700428 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700429 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800430
David S. Millerf11e6652007-03-24 20:36:25 -0700431 return match;
432}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800433
David S. Millerf11e6652007-03-24 20:36:25 -0700434static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
435{
436 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800437 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David S. Millerf11e6652007-03-24 20:36:25 -0700439 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800440 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
David S. Millerf11e6652007-03-24 20:36:25 -0700442 rt0 = fn->rr_ptr;
443 if (!rt0)
444 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
David S. Millerf11e6652007-03-24 20:36:25 -0700446 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800448 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700449 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700450 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700451
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800452 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700453 if (!next || next->rt6i_metric != rt0->rt6i_metric)
454 next = fn->leaf;
455
456 if (next != rt0)
457 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
David S. Millerf11e6652007-03-24 20:36:25 -0700460 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800461 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900463 net = dev_net(rt0->rt6i_dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000464 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800467#ifdef CONFIG_IPV6_ROUTE_INFO
468int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
469 struct in6_addr *gwaddr)
470{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900471 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800472 struct route_info *rinfo = (struct route_info *) opt;
473 struct in6_addr prefix_buf, *prefix;
474 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900475 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800476 struct rt6_info *rt;
477
478 if (len < sizeof(struct route_info)) {
479 return -EINVAL;
480 }
481
482 /* Sanity check for prefix_len and length */
483 if (rinfo->length > 3) {
484 return -EINVAL;
485 } else if (rinfo->prefix_len > 128) {
486 return -EINVAL;
487 } else if (rinfo->prefix_len > 64) {
488 if (rinfo->length < 2) {
489 return -EINVAL;
490 }
491 } else if (rinfo->prefix_len > 0) {
492 if (rinfo->length < 1) {
493 return -EINVAL;
494 }
495 }
496
497 pref = rinfo->route_pref;
498 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000499 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800500
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900501 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800502
503 if (rinfo->length == 3)
504 prefix = (struct in6_addr *)rinfo->prefix;
505 else {
506 /* this function is safe */
507 ipv6_addr_prefix(&prefix_buf,
508 (struct in6_addr *)rinfo->prefix,
509 rinfo->prefix_len);
510 prefix = &prefix_buf;
511 }
512
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800513 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
514 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800515
516 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700517 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800518 rt = NULL;
519 }
520
521 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800522 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800523 pref);
524 else if (rt)
525 rt->rt6i_flags = RTF_ROUTEINFO |
526 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
527
528 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900529 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800530 rt->rt6i_flags &= ~RTF_EXPIRES;
531 } else {
532 rt->rt6i_expires = jiffies + HZ * lifetime;
533 rt->rt6i_flags |= RTF_EXPIRES;
534 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700535 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800536 }
537 return 0;
538}
539#endif
540
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800541#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700542do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800543 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700544 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700545 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700546 if (fn->fn_flags & RTN_TL_ROOT) \
547 goto out; \
548 pn = fn->parent; \
549 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800550 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700551 else \
552 fn = pn; \
553 if (fn->fn_flags & RTN_RTINFO) \
554 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700555 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700556 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700557} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700558
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800559static struct rt6_info *ip6_pol_route_lookup(struct net *net,
560 struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700561 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct fib6_node *fn;
564 struct rt6_info *rt;
565
Thomas Grafc71099a2006-08-04 23:20:06 -0700566 read_lock_bh(&table->tb6_lock);
567 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
568restart:
569 rt = fn->leaf;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900570 rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800571 BACKTRACK(net, &fl->fl6_src);
Thomas Grafc71099a2006-08-04 23:20:06 -0700572out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700573 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700574 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700575 return rt;
576
577}
578
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900579struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
580 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700581{
582 struct flowi fl = {
583 .oif = oif,
Changli Gao58116622010-11-12 18:43:55 +0000584 .fl6_dst = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700585 };
586 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700587 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700588
Thomas Grafadaa70b2006-10-13 15:01:03 -0700589 if (saddr) {
590 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
591 flags |= RT6_LOOKUP_F_HAS_SADDR;
592 }
593
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800594 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700595 if (dst->error == 0)
596 return (struct rt6_info *) dst;
597
598 dst_release(dst);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return NULL;
601}
602
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900603EXPORT_SYMBOL(rt6_lookup);
604
Thomas Grafc71099a2006-08-04 23:20:06 -0700605/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 It takes new route entry, the addition fails by any reason the
607 route is freed. In any case, if caller does not hold it, it may
608 be destroyed.
609 */
610
Thomas Graf86872cb2006-08-22 00:01:08 -0700611static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700614 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Thomas Grafc71099a2006-08-04 23:20:06 -0700616 table = rt->rt6i_table;
617 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700618 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700619 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return err;
622}
623
Thomas Graf40e22e82006-08-22 00:00:45 -0700624int ip6_ins_rt(struct rt6_info *rt)
625{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800626 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900627 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800628 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800629 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700630}
631
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800632static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
633 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct rt6_info *rt;
636
637 /*
638 * Clone the route.
639 */
640
641 rt = ip6_rt_copy(ort);
642
643 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800644 struct neighbour *neigh;
645 int attempts = !in_softirq();
646
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900647 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
648 if (rt->rt6i_dst.plen != 128 &&
649 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
650 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900654 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rt->rt6i_dst.plen = 128;
656 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700657 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659#ifdef CONFIG_IPV6_SUBTREES
660 if (rt->rt6i_src.plen && saddr) {
661 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
662 rt->rt6i_src.plen = 128;
663 }
664#endif
665
David S. Miller14deae42009-01-04 16:04:39 -0800666 retry:
667 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
668 if (IS_ERR(neigh)) {
669 struct net *net = dev_net(rt->rt6i_dev);
670 int saved_rt_min_interval =
671 net->ipv6.sysctl.ip6_rt_gc_min_interval;
672 int saved_rt_elasticity =
673 net->ipv6.sysctl.ip6_rt_gc_elasticity;
674
675 if (attempts-- > 0) {
676 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
677 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
678
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000679 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800680
681 net->ipv6.sysctl.ip6_rt_gc_elasticity =
682 saved_rt_elasticity;
683 net->ipv6.sysctl.ip6_rt_gc_min_interval =
684 saved_rt_min_interval;
685 goto retry;
686 }
687
688 if (net_ratelimit())
689 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700690 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700691 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800692 return NULL;
693 }
694 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800698 return rt;
699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800701static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
702{
703 struct rt6_info *rt = ip6_rt_copy(ort);
704 if (rt) {
705 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
706 rt->rt6i_dst.plen = 128;
707 rt->rt6i_flags |= RTF_CACHE;
Changli Gaod8d1f302010-06-10 23:31:35 -0700708 rt->dst.flags |= DST_HOST;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800709 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
710 }
711 return rt;
712}
713
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800714static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
715 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800718 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700719 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800721 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700722 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700724 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700727 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800729restart_2:
Thomas Grafc71099a2006-08-04 23:20:06 -0700730 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700733 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800734
735 BACKTRACK(net, &fl->fl6_src);
736 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800737 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Changli Gaod8d1f302010-06-10 23:31:35 -0700740 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700741 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800742
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800743 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800744 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
David S. Millerd80bc0f2011-01-24 16:01:58 -0800745 else
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800746 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800747
Changli Gaod8d1f302010-06-10 23:31:35 -0700748 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800749 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800750
Changli Gaod8d1f302010-06-10 23:31:35 -0700751 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800752 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700753 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800754 if (!err)
755 goto out2;
756 }
757
758 if (--attempts <= 0)
759 goto out2;
760
761 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700762 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800763 * released someone could insert this route. Relookup.
764 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700765 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800766 goto relookup;
767
768out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800769 if (reachable) {
770 reachable = 0;
771 goto restart_2;
772 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700773 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700774 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700776 rt->dst.lastuse = jiffies;
777 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700778
779 return rt;
780}
781
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800782static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700783 struct flowi *fl, int flags)
784{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800785 return ip6_pol_route(net, table, fl->iif, fl, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700786}
787
Thomas Grafc71099a2006-08-04 23:20:06 -0700788void ip6_route_input(struct sk_buff *skb)
789{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700790 struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900791 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700792 int flags = RT6_LOOKUP_F_HAS_SADDR;
Thomas Grafc71099a2006-08-04 23:20:06 -0700793 struct flowi fl = {
794 .iif = skb->dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000795 .fl6_dst = iph->daddr,
796 .fl6_src = iph->saddr,
797 .fl6_flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900798 .mark = skb->mark,
Thomas Grafc71099a2006-08-04 23:20:06 -0700799 .proto = iph->nexthdr,
800 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700801
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800802 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700803 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700804
Eric Dumazetadf30902009-06-02 05:19:30 +0000805 skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700806}
807
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800808static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700809 struct flowi *fl, int flags)
810{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800811 return ip6_pol_route(net, table, fl->oif, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700812}
813
Daniel Lezcano4591db42008-03-05 10:48:10 -0800814struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
815 struct flowi *fl)
Thomas Grafc71099a2006-08-04 23:20:06 -0700816{
817 int flags = 0;
818
Brian Haley6057fd72010-05-28 23:02:35 -0700819 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700820 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700821
Thomas Grafadaa70b2006-10-13 15:01:03 -0700822 if (!ipv6_addr_any(&fl->fl6_src))
823 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000824 else if (sk)
825 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700826
Daniel Lezcano4591db42008-03-05 10:48:10 -0800827 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900830EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
David S. Miller14e50e52007-05-24 18:17:54 -0700832int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
833{
834 struct rt6_info *ort = (struct rt6_info *) *dstp;
835 struct rt6_info *rt = (struct rt6_info *)
836 dst_alloc(&ip6_dst_blackhole_ops);
837 struct dst_entry *new = NULL;
838
839 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700840 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700841
842 atomic_set(&new->__refcnt, 1);
843 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800844 new->input = dst_discard;
845 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700846
David S. Millerdefb3512010-12-08 21:16:57 -0800847 dst_copy_metrics(new, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -0700848 new->dev = ort->dst.dev;
David S. Miller14e50e52007-05-24 18:17:54 -0700849 if (new->dev)
850 dev_hold(new->dev);
851 rt->rt6i_idev = ort->rt6i_idev;
852 if (rt->rt6i_idev)
853 in6_dev_hold(rt->rt6i_idev);
854 rt->rt6i_expires = 0;
855
856 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
857 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
858 rt->rt6i_metric = 0;
859
860 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
861#ifdef CONFIG_IPV6_SUBTREES
862 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
863#endif
864
865 dst_free(new);
866 }
867
868 dst_release(*dstp);
869 *dstp = new;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000870 return new ? 0 : -ENOMEM;
David S. Miller14e50e52007-05-24 18:17:54 -0700871}
872EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Destination cache support functions
876 */
877
878static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
879{
880 struct rt6_info *rt;
881
882 rt = (struct rt6_info *) dst;
883
Herbert Xu10414442010-03-18 23:00:22 +0000884 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return dst;
886
887 return NULL;
888}
889
890static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
891{
892 struct rt6_info *rt = (struct rt6_info *) dst;
893
894 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000895 if (rt->rt6i_flags & RTF_CACHE) {
896 if (rt6_check_expired(rt)) {
897 ip6_del_rt(rt);
898 dst = NULL;
899 }
900 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000902 dst = NULL;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000905 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static void ip6_link_failure(struct sk_buff *skb)
909{
910 struct rt6_info *rt;
911
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000912 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Dumazetadf30902009-06-02 05:19:30 +0000914 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (rt) {
916 if (rt->rt6i_flags&RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700917 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 rt->rt6i_flags |= RTF_EXPIRES;
919 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
920 rt->rt6i_node->fn_sernum = -1;
921 }
922}
923
924static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
925{
926 struct rt6_info *rt6 = (struct rt6_info*)dst;
927
928 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
929 rt6->rt6i_flags |= RTF_MODIFIED;
930 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -0800931 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -0800933 features |= RTAX_FEATURE_ALLFRAG;
934 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
David S. Millerdefb3512010-12-08 21:16:57 -0800936 dst_metric_set(dst, RTAX_MTU, mtu);
Tom Tucker8d717402006-07-30 20:43:36 -0700937 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939}
940
David S. Miller0dbaee32010-12-13 12:52:14 -0800941static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
David S. Miller0dbaee32010-12-13 12:52:14 -0800943 struct net_device *dev = dst->dev;
944 unsigned int mtu = dst_mtu(dst);
945 struct net *net = dev_net(dev);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
948
Daniel Lezcano55786892008-03-04 13:47:47 -0800949 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
950 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900953 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
954 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
955 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * rely only on pmtu discovery"
957 */
958 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
959 mtu = IPV6_MAXPLEN;
960 return mtu;
961}
962
David S. Millerd33e4552010-12-14 13:01:14 -0800963static unsigned int ip6_default_mtu(const struct dst_entry *dst)
964{
965 unsigned int mtu = IPV6_MIN_MTU;
966 struct inet6_dev *idev;
967
968 rcu_read_lock();
969 idev = __in6_dev_get(dst->dev);
970 if (idev)
971 mtu = idev->cnf.mtu6;
972 rcu_read_unlock();
973
974 return mtu;
975}
976
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800977static struct dst_entry *icmp6_dst_gc_list;
978static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700979
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800980struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900982 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct rt6_info *rt;
985 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900986 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if (unlikely(idev == NULL))
989 return NULL;
990
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000991 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (unlikely(rt == NULL)) {
993 in6_dev_put(idev);
994 goto out;
995 }
996
997 dev_hold(dev);
998 if (neigh)
999 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001000 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 neigh = ndisc_get_neigh(dev, addr);
David S. Miller14deae42009-01-04 16:04:39 -08001002 if (IS_ERR(neigh))
1003 neigh = NULL;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 rt->rt6i_dev = dev;
1007 rt->rt6i_idev = idev;
1008 rt->rt6i_nexthop = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001009 atomic_set(&rt->dst.__refcnt, 1);
David S. Millerdefb3512010-12-08 21:16:57 -08001010 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Changli Gaod8d1f302010-06-10 23:31:35 -07001011 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013#if 0 /* there's no chance to use these for ndisc */
Changli Gaod8d1f302010-06-10 23:31:35 -07001014 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001015 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 : 0;
1017 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1018 rt->rt6i_dst.plen = 128;
1019#endif
1020
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001021 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001022 rt->dst.next = icmp6_dst_gc_list;
1023 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001024 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Daniel Lezcano55786892008-03-04 13:47:47 -08001026 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001029 return &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001032int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct dst_entry *dst, *next, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001035 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 next = NULL;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001038
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001039 spin_lock_bh(&icmp6_dst_lock);
1040 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 while ((dst = *pprev) != NULL) {
1043 if (!atomic_read(&dst->__refcnt)) {
1044 *pprev = dst->next;
1045 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 } else {
1047 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001048 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 }
1051
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001052 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001053
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001054 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
David S. Miller1e493d12008-09-10 17:27:15 -07001057static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1058 void *arg)
1059{
1060 struct dst_entry *dst, **pprev;
1061
1062 spin_lock_bh(&icmp6_dst_lock);
1063 pprev = &icmp6_dst_gc_list;
1064 while ((dst = *pprev) != NULL) {
1065 struct rt6_info *rt = (struct rt6_info *) dst;
1066 if (func(rt, arg)) {
1067 *pprev = dst->next;
1068 dst_free(dst);
1069 } else {
1070 pprev = &dst->next;
1071 }
1072 }
1073 spin_unlock_bh(&icmp6_dst_lock);
1074}
1075
Daniel Lezcano569d3642008-01-18 03:56:57 -08001076static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001079 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001080 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1081 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1082 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1083 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1084 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001085 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Eric Dumazetfc66f952010-10-08 06:37:34 +00001087 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001088 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001089 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto out;
1091
Benjamin Thery6891a342008-03-04 13:49:47 -08001092 net->ipv6.ip6_rt_gc_expire++;
1093 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1094 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001095 entries = dst_entries_get_slow(ops);
1096 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001097 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001099 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001100 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103/* Clean host part of a prefix. Not necessary in radix tree,
1104 but results in cleaner routing tables.
1105
1106 Remove it only when all the things will work!
1107 */
1108
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001109int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
David S. Miller5170ae82010-12-12 21:35:57 -08001111 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001112 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001113 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001114 struct inet6_dev *idev;
1115
1116 rcu_read_lock();
1117 idev = __in6_dev_get(dev);
1118 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001119 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001120 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001121 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 return hoplimit;
1125}
David S. Millerabbf46a2010-12-12 21:14:46 -08001126EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/*
1129 *
1130 */
1131
Thomas Graf86872cb2006-08-22 00:01:08 -07001132int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001135 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct rt6_info *rt = NULL;
1137 struct net_device *dev = NULL;
1138 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001139 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int addr_type;
1141
Thomas Graf86872cb2006-08-22 00:01:08 -07001142 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return -EINVAL;
1144#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001145 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return -EINVAL;
1147#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001148 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001150 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!dev)
1152 goto out;
1153 idev = in6_dev_get(dev);
1154 if (!idev)
1155 goto out;
1156 }
1157
Thomas Graf86872cb2006-08-22 00:01:08 -07001158 if (cfg->fc_metric == 0)
1159 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Daniel Lezcano55786892008-03-04 13:47:47 -08001161 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001162 if (table == NULL) {
1163 err = -ENOBUFS;
1164 goto out;
1165 }
1166
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001167 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (rt == NULL) {
1170 err = -ENOMEM;
1171 goto out;
1172 }
1173
Changli Gaod8d1f302010-06-10 23:31:35 -07001174 rt->dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001175 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1176 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1177 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Graf86872cb2006-08-22 00:01:08 -07001179 if (cfg->fc_protocol == RTPROT_UNSPEC)
1180 cfg->fc_protocol = RTPROT_BOOT;
1181 rt->rt6i_protocol = cfg->fc_protocol;
1182
1183 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001186 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001187 else if (cfg->fc_flags & RTF_LOCAL)
1188 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001190 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Changli Gaod8d1f302010-06-10 23:31:35 -07001192 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Thomas Graf86872cb2006-08-22 00:01:08 -07001194 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1195 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (rt->rt6i_dst.plen == 128)
Changli Gaod8d1f302010-06-10 23:31:35 -07001197 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001200 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1201 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#endif
1203
Thomas Graf86872cb2006-08-22 00:01:08 -07001204 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* We cannot add true routes via loopback here,
1207 they would result in kernel looping; promote them to reject routes
1208 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001209 if ((cfg->fc_flags & RTF_REJECT) ||
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001210 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1211 && !(cfg->fc_flags&RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001213 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (dev) {
1215 dev_put(dev);
1216 in6_dev_put(idev);
1217 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001218 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dev_hold(dev);
1220 idev = in6_dev_get(dev);
1221 if (!idev) {
1222 err = -ENODEV;
1223 goto out;
1224 }
1225 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001226 rt->dst.output = ip6_pkt_discard_out;
1227 rt->dst.input = ip6_pkt_discard;
1228 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1230 goto install_route;
1231 }
1232
Thomas Graf86872cb2006-08-22 00:01:08 -07001233 if (cfg->fc_flags & RTF_GATEWAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct in6_addr *gw_addr;
1235 int gwa_type;
1236
Thomas Graf86872cb2006-08-22 00:01:08 -07001237 gw_addr = &cfg->fc_gateway;
1238 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 gwa_type = ipv6_addr_type(gw_addr);
1240
1241 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1242 struct rt6_info *grt;
1243
1244 /* IPv6 strictly inhibits using not link-local
1245 addresses as nexthop address.
1246 Otherwise, router will not able to send redirects.
1247 It is very good, but in some (rare!) circumstances
1248 (SIT, PtP, NBMA NOARP links) it is handy to allow
1249 some exceptions. --ANK
1250 */
1251 err = -EINVAL;
1252 if (!(gwa_type&IPV6_ADDR_UNICAST))
1253 goto out;
1254
Daniel Lezcano55786892008-03-04 13:47:47 -08001255 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 err = -EHOSTUNREACH;
1258 if (grt == NULL)
1259 goto out;
1260 if (dev) {
1261 if (dev != grt->rt6i_dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001262 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto out;
1264 }
1265 } else {
1266 dev = grt->rt6i_dev;
1267 idev = grt->rt6i_idev;
1268 dev_hold(dev);
1269 in6_dev_hold(grt->rt6i_idev);
1270 }
1271 if (!(grt->rt6i_flags&RTF_GATEWAY))
1272 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001273 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (err)
1276 goto out;
1277 }
1278 err = -EINVAL;
1279 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1280 goto out;
1281 }
1282
1283 err = -ENODEV;
1284 if (dev == NULL)
1285 goto out;
1286
Thomas Graf86872cb2006-08-22 00:01:08 -07001287 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1289 if (IS_ERR(rt->rt6i_nexthop)) {
1290 err = PTR_ERR(rt->rt6i_nexthop);
1291 rt->rt6i_nexthop = NULL;
1292 goto out;
1293 }
1294 }
1295
Thomas Graf86872cb2006-08-22 00:01:08 -07001296 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001299 if (cfg->fc_mx) {
1300 struct nlattr *nla;
1301 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Thomas Graf86872cb2006-08-22 00:01:08 -07001303 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001304 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001305
1306 if (type) {
1307 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 err = -EINVAL;
1309 goto out;
1310 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001311
David S. Millerdefb3512010-12-08 21:16:57 -08001312 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 }
1316
Changli Gaod8d1f302010-06-10 23:31:35 -07001317 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001319 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001320
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001321 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001322
Thomas Graf86872cb2006-08-22 00:01:08 -07001323 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325out:
1326 if (dev)
1327 dev_put(dev);
1328 if (idev)
1329 in6_dev_put(idev);
1330 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001331 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return err;
1333}
1334
Thomas Graf86872cb2006-08-22 00:01:08 -07001335static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001338 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001339 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001341 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001342 return -ENOENT;
1343
Thomas Grafc71099a2006-08-04 23:20:06 -07001344 table = rt->rt6i_table;
1345 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Thomas Graf86872cb2006-08-22 00:01:08 -07001347 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001348 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Thomas Grafc71099a2006-08-04 23:20:06 -07001350 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 return err;
1353}
1354
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001355int ip6_del_rt(struct rt6_info *rt)
1356{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001357 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001358 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001359 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001360 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001361}
1362
Thomas Graf86872cb2006-08-22 00:01:08 -07001363static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Thomas Grafc71099a2006-08-04 23:20:06 -07001365 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct fib6_node *fn;
1367 struct rt6_info *rt;
1368 int err = -ESRCH;
1369
Daniel Lezcano55786892008-03-04 13:47:47 -08001370 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001371 if (table == NULL)
1372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Thomas Grafc71099a2006-08-04 23:20:06 -07001374 read_lock_bh(&table->tb6_lock);
1375
1376 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001377 &cfg->fc_dst, cfg->fc_dst_len,
1378 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001381 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001382 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001384 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001386 if (cfg->fc_flags & RTF_GATEWAY &&
1387 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001389 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001391 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001392 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Thomas Graf86872cb2006-08-22 00:01:08 -07001394 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001397 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return err;
1400}
1401
1402/*
1403 * Handle redirects
1404 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001405struct ip6rd_flowi {
1406 struct flowi fl;
1407 struct in6_addr gateway;
1408};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001410static struct rt6_info *__ip6_route_redirect(struct net *net,
1411 struct fib6_table *table,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001412 struct flowi *fl,
1413 int flags)
1414{
1415 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1416 struct rt6_info *rt;
1417 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001420 * Get the "current" route for this destination and
1421 * check if the redirect has come from approriate router.
1422 *
1423 * RFC 2461 specifies that redirects should only be
1424 * accepted if they come from the nexthop to the target.
1425 * Due to the way the routes are chosen, this notion
1426 * is a bit fuzzy and one might need to check all possible
1427 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Thomas Grafc71099a2006-08-04 23:20:06 -07001430 read_lock_bh(&table->tb6_lock);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001431 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001432restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001433 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001434 /*
1435 * Current route is on-link; redirect is always invalid.
1436 *
1437 * Seems, previous statement is not true. It could
1438 * be node, which looks for us as on-link (f.e. proxy ndisc)
1439 * But then router serving it might decide, that we should
1440 * know truth 8)8) --ANK (980726).
1441 */
1442 if (rt6_check_expired(rt))
1443 continue;
1444 if (!(rt->rt6i_flags & RTF_GATEWAY))
1445 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001446 if (fl->oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001447 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001448 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001449 continue;
1450 break;
1451 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001452
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001453 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001454 rt = net->ipv6.ip6_null_entry;
1455 BACKTRACK(net, &fl->fl6_src);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001456out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001457 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001458
1459 read_unlock_bh(&table->tb6_lock);
1460
1461 return rt;
1462};
1463
1464static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1465 struct in6_addr *src,
1466 struct in6_addr *gateway,
1467 struct net_device *dev)
1468{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001469 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001470 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001471 struct ip6rd_flowi rdfl = {
1472 .fl = {
1473 .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +00001474 .fl6_dst = *dest,
1475 .fl6_src = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001476 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001477 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001478
Brian Haley86c36ce2009-10-07 13:58:01 -07001479 ipv6_addr_copy(&rdfl.gateway, gateway);
1480
Thomas Grafadaa70b2006-10-13 15:01:03 -07001481 if (rt6_need_strict(dest))
1482 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001483
Daniel Lezcano55786892008-03-04 13:47:47 -08001484 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001485 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001486}
1487
1488void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1489 struct in6_addr *saddr,
1490 struct neighbour *neigh, u8 *lladdr, int on_link)
1491{
1492 struct rt6_info *rt, *nrt = NULL;
1493 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001494 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001495
1496 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1497
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001498 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (net_ratelimit())
1500 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1501 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /*
1506 * We have finally decided to accept it.
1507 */
1508
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001509 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1511 NEIGH_UPDATE_F_OVERRIDE|
1512 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1513 NEIGH_UPDATE_F_ISROUTER))
1514 );
1515
1516 /*
1517 * Redirect received -> path was valid.
1518 * Look, redirects are sent only in response to data packets,
1519 * so that this nexthop apparently is reachable. --ANK
1520 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001521 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 /* Duplicate redirect: silently ignore. */
Changli Gaod8d1f302010-06-10 23:31:35 -07001524 if (neigh == rt->dst.neighbour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 goto out;
1526
1527 nrt = ip6_rt_copy(rt);
1528 if (nrt == NULL)
1529 goto out;
1530
1531 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1532 if (on_link)
1533 nrt->rt6i_flags &= ~RTF_GATEWAY;
1534
1535 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1536 nrt->rt6i_dst.plen = 128;
Changli Gaod8d1f302010-06-10 23:31:35 -07001537 nrt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1540 nrt->rt6i_nexthop = neigh_clone(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Thomas Graf40e22e82006-08-22 00:00:45 -07001542 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto out;
1544
Changli Gaod8d1f302010-06-10 23:31:35 -07001545 netevent.old = &rt->dst;
1546 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001547 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001550 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return;
1552 }
1553
1554out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001555 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
1558/*
1559 * Handle ICMP "packet too big" messages
1560 * i.e. Path MTU discovery
1561 */
1562
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001563static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
1564 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct rt6_info *rt, *nrt;
1567 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001568again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001569 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (rt == NULL)
1571 return;
1572
Andrey Vagind3052b52010-12-11 15:20:11 +00001573 if (rt6_check_expired(rt)) {
1574 ip6_del_rt(rt);
1575 goto again;
1576 }
1577
Changli Gaod8d1f302010-06-10 23:31:35 -07001578 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 goto out;
1580
1581 if (pmtu < IPV6_MIN_MTU) {
1582 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001583 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * MTU (1280) and a fragment header should always be included
1585 * after a node receiving Too Big message reporting PMTU is
1586 * less than the IPv6 Minimum Link MTU.
1587 */
1588 pmtu = IPV6_MIN_MTU;
1589 allfrag = 1;
1590 }
1591
1592 /* New mtu received -> path was valid.
1593 They are sent only in response to data packets,
1594 so that this nexthop apparently is reachable. --ANK
1595 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001596 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /* Host route. If it is static, it would be better
1599 not to override it, but add new one, so that
1600 when cache entry will expire old pmtu
1601 would return automatically.
1602 */
1603 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001604 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1605 if (allfrag) {
1606 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1607 features |= RTAX_FEATURE_ALLFRAG;
1608 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1609 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001610 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1612 goto out;
1613 }
1614
1615 /* Network route.
1616 Two cases are possible:
1617 1. It is connected route. Action: COW
1618 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1619 */
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001620 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001621 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001622 else
1623 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001624
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001625 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001626 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1627 if (allfrag) {
1628 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1629 features |= RTAX_FEATURE_ALLFRAG;
1630 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1631 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001632
1633 /* According to RFC 1981, detecting PMTU increase shouldn't be
1634 * happened within 5 mins, the recommended timer is 10 mins.
1635 * Here this route expiration time is set to ip6_rt_mtu_expires
1636 * which is 10 mins. After 10 mins the decreased pmtu is expired
1637 * and detecting PMTU increase will be automatically happened.
1638 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001639 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001640 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1641
Thomas Graf40e22e82006-08-22 00:00:45 -07001642 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001645 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001648void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1649 struct net_device *dev, u32 pmtu)
1650{
1651 struct net *net = dev_net(dev);
1652
1653 /*
1654 * RFC 1981 states that a node "MUST reduce the size of the packets it
1655 * is sending along the path" that caused the Packet Too Big message.
1656 * Since it's not possible in the general case to determine which
1657 * interface was used to send the original packet, we update the MTU
1658 * on the interface that will be used to send future packets. We also
1659 * update the MTU on the interface that received the Packet Too Big in
1660 * case the original packet was forced out that interface with
1661 * SO_BINDTODEVICE or similar. This is the next best thing to the
1662 * correct behaviour, which would be to update the MTU on all
1663 * interfaces.
1664 */
1665 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1666 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/*
1670 * Misc support functions
1671 */
1672
1673static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1674{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001675 struct net *net = dev_net(ort->rt6i_dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001676 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001679 rt->dst.input = ort->dst.input;
1680 rt->dst.output = ort->dst.output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David S. Millerdefb3512010-12-08 21:16:57 -08001682 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001683 rt->dst.error = ort->dst.error;
1684 rt->dst.dev = ort->dst.dev;
1685 if (rt->dst.dev)
1686 dev_hold(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 rt->rt6i_idev = ort->rt6i_idev;
1688 if (rt->rt6i_idev)
1689 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001690 rt->dst.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 rt->rt6i_expires = 0;
1692
1693 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1694 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1695 rt->rt6i_metric = 0;
1696
1697 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1698#ifdef CONFIG_IPV6_SUBTREES
1699 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1700#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001701 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703 return rt;
1704}
1705
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001706#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001707static struct rt6_info *rt6_get_route_info(struct net *net,
1708 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001709 struct in6_addr *gwaddr, int ifindex)
1710{
1711 struct fib6_node *fn;
1712 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001713 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001714
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001715 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001716 if (table == NULL)
1717 return NULL;
1718
1719 write_lock_bh(&table->tb6_lock);
1720 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001721 if (!fn)
1722 goto out;
1723
Changli Gaod8d1f302010-06-10 23:31:35 -07001724 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001725 if (rt->rt6i_dev->ifindex != ifindex)
1726 continue;
1727 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1728 continue;
1729 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1730 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001732 break;
1733 }
1734out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001735 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001736 return rt;
1737}
1738
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001739static struct rt6_info *rt6_add_route_info(struct net *net,
1740 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001741 struct in6_addr *gwaddr, int ifindex,
1742 unsigned pref)
1743{
Thomas Graf86872cb2006-08-22 00:01:08 -07001744 struct fib6_config cfg = {
1745 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001746 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001747 .fc_ifindex = ifindex,
1748 .fc_dst_len = prefixlen,
1749 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1750 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001751 .fc_nlinfo.pid = 0,
1752 .fc_nlinfo.nlh = NULL,
1753 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001754 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001755
Thomas Graf86872cb2006-08-22 00:01:08 -07001756 ipv6_addr_copy(&cfg.fc_dst, prefix);
1757 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1758
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001759 /* We should treat it as a default route if prefix length is 0. */
1760 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001761 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001762
Thomas Graf86872cb2006-08-22 00:01:08 -07001763 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001764
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001765 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001766}
1767#endif
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001772 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001774 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001775 if (table == NULL)
1776 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Thomas Grafc71099a2006-08-04 23:20:06 -07001778 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001779 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001781 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1783 break;
1784 }
1785 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001786 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001787 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return rt;
1789}
1790
1791struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001792 struct net_device *dev,
1793 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Thomas Graf86872cb2006-08-22 00:01:08 -07001795 struct fib6_config cfg = {
1796 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001797 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 .fc_ifindex = dev->ifindex,
1799 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1800 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001801 .fc_nlinfo.pid = 0,
1802 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001803 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001804 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Thomas Graf86872cb2006-08-22 00:01:08 -07001806 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Thomas Graf86872cb2006-08-22 00:01:08 -07001808 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return rt6_get_dflt_router(gwaddr, dev);
1811}
1812
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001813void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001816 struct fib6_table *table;
1817
1818 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001819 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001820 if (table == NULL)
1821 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001824 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001825 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001827 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001828 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001829 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto restart;
1831 }
1832 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001833 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Daniel Lezcano55786892008-03-04 13:47:47 -08001836static void rtmsg_to_fib6_config(struct net *net,
1837 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001838 struct fib6_config *cfg)
1839{
1840 memset(cfg, 0, sizeof(*cfg));
1841
1842 cfg->fc_table = RT6_TABLE_MAIN;
1843 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1844 cfg->fc_metric = rtmsg->rtmsg_metric;
1845 cfg->fc_expires = rtmsg->rtmsg_info;
1846 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1847 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1848 cfg->fc_flags = rtmsg->rtmsg_flags;
1849
Daniel Lezcano55786892008-03-04 13:47:47 -08001850 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001851
Thomas Graf86872cb2006-08-22 00:01:08 -07001852 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1853 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1854 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1855}
1856
Daniel Lezcano55786892008-03-04 13:47:47 -08001857int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Thomas Graf86872cb2006-08-22 00:01:08 -07001859 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct in6_rtmsg rtmsg;
1861 int err;
1862
1863 switch(cmd) {
1864 case SIOCADDRT: /* Add a route */
1865 case SIOCDELRT: /* Delete a route */
1866 if (!capable(CAP_NET_ADMIN))
1867 return -EPERM;
1868 err = copy_from_user(&rtmsg, arg,
1869 sizeof(struct in6_rtmsg));
1870 if (err)
1871 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001872
Daniel Lezcano55786892008-03-04 13:47:47 -08001873 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 rtnl_lock();
1876 switch (cmd) {
1877 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001878 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001881 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
1883 default:
1884 err = -EINVAL;
1885 }
1886 rtnl_unlock();
1887
1888 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 return -EINVAL;
1892}
1893
1894/*
1895 * Drop the packet on the floor
1896 */
1897
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07001898static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001900 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00001901 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001902 switch (ipstats_mib_noroutes) {
1903 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001904 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00001905 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001906 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1907 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001908 break;
1909 }
1910 /* FALLTHROUGH */
1911 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001912 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1913 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001914 break;
1915 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001916 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 kfree_skb(skb);
1918 return 0;
1919}
1920
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001921static int ip6_pkt_discard(struct sk_buff *skb)
1922{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001923 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001924}
1925
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001926static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Eric Dumazetadf30902009-06-02 05:19:30 +00001928 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001929 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
David S. Miller6723ab52006-10-18 21:20:57 -07001932#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1933
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001934static int ip6_pkt_prohibit(struct sk_buff *skb)
1935{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001936 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001937}
1938
1939static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1940{
Eric Dumazetadf30902009-06-02 05:19:30 +00001941 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001942 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001943}
1944
David S. Miller6723ab52006-10-18 21:20:57 -07001945#endif
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/*
1948 * Allocate a dst for local (unicast / anycast) address.
1949 */
1950
1951struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1952 const struct in6_addr *addr,
1953 int anycast)
1954{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001955 struct net *net = dev_net(idev->dev);
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001956 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -08001957 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Ben Greear40385652010-11-08 12:33:48 +00001959 if (rt == NULL) {
1960 if (net_ratelimit())
1961 pr_warning("IPv6: Maximum number of routes reached,"
1962 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00001964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Daniel Lezcano55786892008-03-04 13:47:47 -08001966 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 in6_dev_hold(idev);
1968
Changli Gaod8d1f302010-06-10 23:31:35 -07001969 rt->dst.flags = DST_HOST;
1970 rt->dst.input = ip6_input;
1971 rt->dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08001972 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 rt->rt6i_idev = idev;
David S. Millerdefb3512010-12-08 21:16:57 -08001974 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
Changli Gaod8d1f302010-06-10 23:31:35 -07001975 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09001978 if (anycast)
1979 rt->rt6i_flags |= RTF_ANYCAST;
1980 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller14deae42009-01-04 16:04:39 -08001982 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1983 if (IS_ERR(neigh)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001984 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -08001985
1986 /* We are casting this because that is the return
1987 * value type. But an errno encoded pointer is the
1988 * same regardless of the underlying pointer type,
1989 * and that's what we are returning. So this is OK.
1990 */
1991 return (struct rt6_info *) neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
David S. Miller14deae42009-01-04 16:04:39 -08001993 rt->rt6i_nexthop = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1996 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08001997 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Changli Gaod8d1f302010-06-10 23:31:35 -07001999 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 return rt;
2002}
2003
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002004struct arg_dev_net {
2005 struct net_device *dev;
2006 struct net *net;
2007};
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static int fib6_ifdown(struct rt6_info *rt, void *arg)
2010{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002011 const struct arg_dev_net *adn = arg;
2012 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002013
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002014 if ((rt->rt6i_dev == dev || dev == NULL) &&
2015 rt != adn->net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 RT6_TRACE("deleted by ifdown %p\n", rt);
2017 return -1;
2018 }
2019 return 0;
2020}
2021
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002022void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002024 struct arg_dev_net adn = {
2025 .dev = dev,
2026 .net = net,
2027 };
2028
2029 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002030 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033struct rt6_mtu_change_arg
2034{
2035 struct net_device *dev;
2036 unsigned mtu;
2037};
2038
2039static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2040{
2041 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2042 struct inet6_dev *idev;
2043
2044 /* In IPv6 pmtu discovery is not optional,
2045 so that RTAX_MTU lock cannot disable it.
2046 We still use this lock to block changes
2047 caused by addrconf/ndisc.
2048 */
2049
2050 idev = __in6_dev_get(arg->dev);
2051 if (idev == NULL)
2052 return 0;
2053
2054 /* For administrative MTU increase, there is no way to discover
2055 IPv6 PMTU increase, so PMTU increase should be updated here.
2056 Since RFC 1981 doesn't include administrative MTU increase
2057 update PMTU increase is a MUST. (i.e. jumbo frame)
2058 */
2059 /*
2060 If new MTU is less than route PMTU, this new MTU will be the
2061 lowest MTU in the path, update the route PMTU to reflect PMTU
2062 decreases; if new MTU is greater than route PMTU, and the
2063 old MTU is the lowest MTU in the path, update the route PMTU
2064 to reflect the increase. In this case if the other nodes' MTU
2065 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2066 PMTU discouvery.
2067 */
2068 if (rt->rt6i_dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002069 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2070 (dst_mtu(&rt->dst) >= arg->mtu ||
2071 (dst_mtu(&rt->dst) < arg->mtu &&
2072 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002073 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076}
2077
2078void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2079{
Thomas Grafc71099a2006-08-04 23:20:06 -07002080 struct rt6_mtu_change_arg arg = {
2081 .dev = dev,
2082 .mtu = mtu,
2083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002085 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002088static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002089 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002090 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002091 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002092 [RTA_PRIORITY] = { .type = NLA_U32 },
2093 [RTA_METRICS] = { .type = NLA_NESTED },
2094};
2095
2096static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2097 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Thomas Graf86872cb2006-08-22 00:01:08 -07002099 struct rtmsg *rtm;
2100 struct nlattr *tb[RTA_MAX+1];
2101 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Thomas Graf86872cb2006-08-22 00:01:08 -07002103 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2104 if (err < 0)
2105 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Thomas Graf86872cb2006-08-22 00:01:08 -07002107 err = -EINVAL;
2108 rtm = nlmsg_data(nlh);
2109 memset(cfg, 0, sizeof(*cfg));
2110
2111 cfg->fc_table = rtm->rtm_table;
2112 cfg->fc_dst_len = rtm->rtm_dst_len;
2113 cfg->fc_src_len = rtm->rtm_src_len;
2114 cfg->fc_flags = RTF_UP;
2115 cfg->fc_protocol = rtm->rtm_protocol;
2116
2117 if (rtm->rtm_type == RTN_UNREACHABLE)
2118 cfg->fc_flags |= RTF_REJECT;
2119
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002120 if (rtm->rtm_type == RTN_LOCAL)
2121 cfg->fc_flags |= RTF_LOCAL;
2122
Thomas Graf86872cb2006-08-22 00:01:08 -07002123 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2124 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002125 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002126
2127 if (tb[RTA_GATEWAY]) {
2128 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2129 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002131
2132 if (tb[RTA_DST]) {
2133 int plen = (rtm->rtm_dst_len + 7) >> 3;
2134
2135 if (nla_len(tb[RTA_DST]) < plen)
2136 goto errout;
2137
2138 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002140
2141 if (tb[RTA_SRC]) {
2142 int plen = (rtm->rtm_src_len + 7) >> 3;
2143
2144 if (nla_len(tb[RTA_SRC]) < plen)
2145 goto errout;
2146
2147 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002149
2150 if (tb[RTA_OIF])
2151 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2152
2153 if (tb[RTA_PRIORITY])
2154 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2155
2156 if (tb[RTA_METRICS]) {
2157 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2158 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002160
2161 if (tb[RTA_TABLE])
2162 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2163
2164 err = 0;
2165errout:
2166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Thomas Grafc127ea22007-03-22 11:58:32 -07002169static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Thomas Graf86872cb2006-08-22 00:01:08 -07002171 struct fib6_config cfg;
2172 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Thomas Graf86872cb2006-08-22 00:01:08 -07002174 err = rtm_to_fib6_config(skb, nlh, &cfg);
2175 if (err < 0)
2176 return err;
2177
2178 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Thomas Grafc127ea22007-03-22 11:58:32 -07002181static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Thomas Graf86872cb2006-08-22 00:01:08 -07002183 struct fib6_config cfg;
2184 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Thomas Graf86872cb2006-08-22 00:01:08 -07002186 err = rtm_to_fib6_config(skb, nlh, &cfg);
2187 if (err < 0)
2188 return err;
2189
2190 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Thomas Graf339bf982006-11-10 14:10:15 -08002193static inline size_t rt6_nlmsg_size(void)
2194{
2195 return NLMSG_ALIGN(sizeof(struct rtmsg))
2196 + nla_total_size(16) /* RTA_SRC */
2197 + nla_total_size(16) /* RTA_DST */
2198 + nla_total_size(16) /* RTA_GATEWAY */
2199 + nla_total_size(16) /* RTA_PREFSRC */
2200 + nla_total_size(4) /* RTA_TABLE */
2201 + nla_total_size(4) /* RTA_IIF */
2202 + nla_total_size(4) /* RTA_OIF */
2203 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002204 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002205 + nla_total_size(sizeof(struct rta_cacheinfo));
2206}
2207
Brian Haley191cd582008-08-14 15:33:21 -07002208static int rt6_fill_node(struct net *net,
2209 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002210 struct in6_addr *dst, struct in6_addr *src,
2211 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002212 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002215 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002216 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002217 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 if (prefix) { /* user wants prefix routes only */
2220 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2221 /* success since this is not a prefix route */
2222 return 1;
2223 }
2224 }
2225
Thomas Graf2d7202b2006-08-22 00:01:27 -07002226 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2227 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002228 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002229
2230 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 rtm->rtm_family = AF_INET6;
2232 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2233 rtm->rtm_src_len = rt->rt6i_src.plen;
2234 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002235 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002236 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002237 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002238 table = RT6_TABLE_UNSPEC;
2239 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002240 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (rt->rt6i_flags&RTF_REJECT)
2242 rtm->rtm_type = RTN_UNREACHABLE;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002243 else if (rt->rt6i_flags&RTF_LOCAL)
2244 rtm->rtm_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2246 rtm->rtm_type = RTN_LOCAL;
2247 else
2248 rtm->rtm_type = RTN_UNICAST;
2249 rtm->rtm_flags = 0;
2250 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2251 rtm->rtm_protocol = rt->rt6i_protocol;
2252 if (rt->rt6i_flags&RTF_DYNAMIC)
2253 rtm->rtm_protocol = RTPROT_REDIRECT;
2254 else if (rt->rt6i_flags & RTF_ADDRCONF)
2255 rtm->rtm_protocol = RTPROT_KERNEL;
2256 else if (rt->rt6i_flags&RTF_DEFAULT)
2257 rtm->rtm_protocol = RTPROT_RA;
2258
2259 if (rt->rt6i_flags&RTF_CACHE)
2260 rtm->rtm_flags |= RTM_F_CLONED;
2261
2262 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002263 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002264 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002266 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267#ifdef CONFIG_IPV6_SUBTREES
2268 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002269 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002270 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002272 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002274 if (iif) {
2275#ifdef CONFIG_IPV6_MROUTE
2276 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002277 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002278 if (err <= 0) {
2279 if (!nowait) {
2280 if (err == 0)
2281 return 0;
2282 goto nla_put_failure;
2283 } else {
2284 if (err == -EMSGSIZE)
2285 goto nla_put_failure;
2286 }
2287 }
2288 } else
2289#endif
2290 NLA_PUT_U32(skb, RTA_IIF, iif);
2291 } else if (dst) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002292 struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct in6_addr saddr_buf;
Brian Haley191cd582008-08-14 15:33:21 -07002294 if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09002295 dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002296 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002298
David S. Millerdefb3512010-12-08 21:16:57 -08002299 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002300 goto nla_put_failure;
2301
Changli Gaod8d1f302010-06-10 23:31:35 -07002302 if (rt->dst.neighbour)
2303 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002304
Changli Gaod8d1f302010-06-10 23:31:35 -07002305 if (rt->dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002306 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2307
2308 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002309
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002310 if (!(rt->rt6i_flags & RTF_EXPIRES))
2311 expires = 0;
2312 else if (rt->rt6i_expires - jiffies < INT_MAX)
2313 expires = rt->rt6i_expires - jiffies;
2314 else
2315 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002316
Changli Gaod8d1f302010-06-10 23:31:35 -07002317 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2318 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002319 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Thomas Graf2d7202b2006-08-22 00:01:27 -07002321 return nlmsg_end(skb, nlh);
2322
2323nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002324 nlmsg_cancel(skb, nlh);
2325 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Patrick McHardy1b43af52006-08-10 23:11:17 -07002328int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2331 int prefix;
2332
Thomas Graf2d7202b2006-08-22 00:01:27 -07002333 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2334 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2336 } else
2337 prefix = 0;
2338
Brian Haley191cd582008-08-14 15:33:21 -07002339 return rt6_fill_node(arg->net,
2340 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002342 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Thomas Grafc127ea22007-03-22 11:58:32 -07002345static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002347 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002348 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002350 struct sk_buff *skb;
2351 struct rtmsg *rtm;
2352 struct flowi fl;
2353 int err, iif = 0;
2354
2355 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2356 if (err < 0)
2357 goto errout;
2358
2359 err = -EINVAL;
2360 memset(&fl, 0, sizeof(fl));
2361
2362 if (tb[RTA_SRC]) {
2363 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2364 goto errout;
2365
2366 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2367 }
2368
2369 if (tb[RTA_DST]) {
2370 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2371 goto errout;
2372
2373 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2374 }
2375
2376 if (tb[RTA_IIF])
2377 iif = nla_get_u32(tb[RTA_IIF]);
2378
2379 if (tb[RTA_OIF])
2380 fl.oif = nla_get_u32(tb[RTA_OIF]);
2381
2382 if (iif) {
2383 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002384 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002385 if (!dev) {
2386 err = -ENODEV;
2387 goto errout;
2388 }
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002392 if (skb == NULL) {
2393 err = -ENOBUFS;
2394 goto errout;
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 /* Reserve room for dummy headers, this skb can pass
2398 through good chunk of routing engine.
2399 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002400 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2402
Daniel Lezcano8a3edd82008-03-07 11:14:16 -08002403 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
Changli Gaod8d1f302010-06-10 23:31:35 -07002404 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Brian Haley191cd582008-08-14 15:33:21 -07002406 err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002408 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002410 kfree_skb(skb);
2411 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
Daniel Lezcano55786892008-03-04 13:47:47 -08002414 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002415errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
Thomas Graf86872cb2006-08-22 00:01:08 -07002419void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002422 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002423 u32 seq;
2424 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002426 err = -ENOBUFS;
2427 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002428
Thomas Graf339bf982006-11-10 14:10:15 -08002429 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002430 if (skb == NULL)
2431 goto errout;
2432
Brian Haley191cd582008-08-14 15:33:21 -07002433 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002434 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002435 if (err < 0) {
2436 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2437 WARN_ON(err == -EMSGSIZE);
2438 kfree_skb(skb);
2439 goto errout;
2440 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002441 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2442 info->nlh, gfp_any());
2443 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002444errout:
2445 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002446 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002449static int ip6_route_dev_notify(struct notifier_block *this,
2450 unsigned long event, void *data)
2451{
2452 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002453 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002454
2455 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002456 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002457 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2458#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002459 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002460 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002461 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002462 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2463#endif
2464 }
2465
2466 return NOTIFY_OK;
2467}
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469/*
2470 * /proc
2471 */
2472
2473#ifdef CONFIG_PROC_FS
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475struct rt6_proc_arg
2476{
2477 char *buffer;
2478 int offset;
2479 int length;
2480 int skip;
2481 int len;
2482};
2483
2484static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2485{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002486 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002488 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002491 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002493 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494#endif
2495
2496 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002497 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002499 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002501 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002502 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2503 rt->dst.__use, rt->rt6i_flags,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002504 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return 0;
2506}
2507
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002508static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002510 struct net *net = (struct net *)m->private;
2511 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002515static int ipv6_route_open(struct inode *inode, struct file *file)
2516{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002517 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002518}
2519
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002520static const struct file_operations ipv6_route_proc_fops = {
2521 .owner = THIS_MODULE,
2522 .open = ipv6_route_open,
2523 .read = seq_read,
2524 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002525 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002526};
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2529{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002530 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002532 net->ipv6.rt6_stats->fib_nodes,
2533 net->ipv6.rt6_stats->fib_route_nodes,
2534 net->ipv6.rt6_stats->fib_rt_alloc,
2535 net->ipv6.rt6_stats->fib_rt_entries,
2536 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002537 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002538 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 return 0;
2541}
2542
2543static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2544{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002545 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002546}
2547
Arjan van de Ven9a321442007-02-12 00:55:35 -08002548static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .owner = THIS_MODULE,
2550 .open = rt6_stats_seq_open,
2551 .read = seq_read,
2552 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002553 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554};
2555#endif /* CONFIG_PROC_FS */
2556
2557#ifdef CONFIG_SYSCTL
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002560int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 void __user *buffer, size_t *lenp, loff_t *ppos)
2562{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002563 struct net *net = current->nsproxy->net_ns;
2564 int delay = net->ipv6.sysctl.flush_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (write) {
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002566 proc_dointvec(ctl, write, buffer, lenp, ppos);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002567 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return 0;
2569 } else
2570 return -EINVAL;
2571}
2572
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002573ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002574 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002576 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002578 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002579 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 },
2581 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002583 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 .maxlen = sizeof(int),
2585 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002586 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 },
2588 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002590 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 .maxlen = sizeof(int),
2592 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002593 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 },
2595 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002597 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 .maxlen = sizeof(int),
2599 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002600 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 },
2602 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002604 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 .maxlen = sizeof(int),
2606 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002607 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 },
2609 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002611 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 .maxlen = sizeof(int),
2613 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002614 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 },
2616 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002618 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 .maxlen = sizeof(int),
2620 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002621 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 },
2623 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002625 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 .maxlen = sizeof(int),
2627 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002628 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 },
2630 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002632 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 .maxlen = sizeof(int),
2634 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002635 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 },
2637 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002639 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .maxlen = sizeof(int),
2641 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002642 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002644 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645};
2646
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002647struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002648{
2649 struct ctl_table *table;
2650
2651 table = kmemdup(ipv6_route_table_template,
2652 sizeof(ipv6_route_table_template),
2653 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002654
2655 if (table) {
2656 table[0].data = &net->ipv6.sysctl.flush_delay;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002657 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002658 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2659 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2660 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2661 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2662 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2663 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2664 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002665 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002666 }
2667
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002668 return table;
2669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670#endif
2671
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002672static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002673{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002674 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002675
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002676 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2677 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002678
Eric Dumazetfc66f952010-10-08 06:37:34 +00002679 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2680 goto out_ip6_dst_ops;
2681
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002682 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2683 sizeof(*net->ipv6.ip6_null_entry),
2684 GFP_KERNEL);
2685 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002686 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002687 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002688 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002689 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002690 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2691 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002692
2693#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2694 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2695 sizeof(*net->ipv6.ip6_prohibit_entry),
2696 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002697 if (!net->ipv6.ip6_prohibit_entry)
2698 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002699 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002700 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002701 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002702 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2703 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002704
2705 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2706 sizeof(*net->ipv6.ip6_blk_hole_entry),
2707 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002708 if (!net->ipv6.ip6_blk_hole_entry)
2709 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002710 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002711 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002712 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002713 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2714 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002715#endif
2716
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002717 net->ipv6.sysctl.flush_delay = 0;
2718 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2719 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2720 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2721 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2722 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2723 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2724 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2725
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002726#ifdef CONFIG_PROC_FS
2727 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2728 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2729#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002730 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2731
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002732 ret = 0;
2733out:
2734 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002735
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002736#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2737out_ip6_prohibit_entry:
2738 kfree(net->ipv6.ip6_prohibit_entry);
2739out_ip6_null_entry:
2740 kfree(net->ipv6.ip6_null_entry);
2741#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002742out_ip6_dst_entries:
2743 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002744out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002745 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002746}
2747
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002748static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002749{
2750#ifdef CONFIG_PROC_FS
2751 proc_net_remove(net, "ipv6_route");
2752 proc_net_remove(net, "rt6_stats");
2753#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002754 kfree(net->ipv6.ip6_null_entry);
2755#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2756 kfree(net->ipv6.ip6_prohibit_entry);
2757 kfree(net->ipv6.ip6_blk_hole_entry);
2758#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002759 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002760}
2761
2762static struct pernet_operations ip6_route_net_ops = {
2763 .init = ip6_route_net_init,
2764 .exit = ip6_route_net_exit,
2765};
2766
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002767static struct notifier_block ip6_route_dev_notifier = {
2768 .notifier_call = ip6_route_dev_notify,
2769 .priority = 0,
2770};
2771
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002772int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002774 int ret;
2775
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002776 ret = -ENOMEM;
2777 ip6_dst_ops_template.kmem_cachep =
2778 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2779 SLAB_HWCACHE_ALIGN, NULL);
2780 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002781 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002782
Eric Dumazetfc66f952010-10-08 06:37:34 +00002783 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002784 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002785 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002786
Eric Dumazetfc66f952010-10-08 06:37:34 +00002787 ret = register_pernet_subsys(&ip6_route_net_ops);
2788 if (ret)
2789 goto out_dst_entries;
2790
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002791 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2792
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002793 /* Registering of the loopback is done before this portion of code,
2794 * the loopback reference in rt6_info will not be taken, do it
2795 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002796 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002797 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2798 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002799 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002800 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002801 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002802 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2803 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002804 ret = fib6_init();
2805 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002806 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002807
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002808 ret = xfrm6_init();
2809 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002810 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002811
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002812 ret = fib6_rules_init();
2813 if (ret)
2814 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002815
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002816 ret = -ENOBUFS;
2817 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2818 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2819 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2820 goto fib6_rules_init;
2821
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002822 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002823 if (ret)
2824 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002825
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002826out:
2827 return ret;
2828
2829fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002830 fib6_rules_cleanup();
2831xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002832 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002833out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002834 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002835out_register_subsys:
2836 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002837out_dst_entries:
2838 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002839out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002840 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
2844void ip6_route_cleanup(void)
2845{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002846 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002847 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002850 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002851 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002852 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}