blob: d1f3e19b06c79ade99a6b34eb385ee15753c6c77 [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 *
8 * $Id: route.c,v 1.56 2001/10/31 21:55:55 davem Exp $
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/* Changes:
17 *
18 * YOSHIFUJI Hideaki @USAGI
19 * reworked default router selection.
20 * - respect outgoing interface
21 * - select from (probably) reachable routers (i.e.
22 * routers in REACHABLE, STALE, DELAY or PROBE states).
23 * - always select the same router if it is (probably)
24 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070025 * Ville Nuorvala
26 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/times.h>
33#include <linux/socket.h>
34#include <linux/sockios.h>
35#include <linux/net.h>
36#include <linux/route.h>
37#include <linux/netdevice.h>
38#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090039#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/proc_fs.h>
43#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080044#include <linux/nsproxy.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/snmp.h>
47#include <net/ipv6.h>
48#include <net/ip6_fib.h>
49#include <net/ip6_route.h>
50#include <net/ndisc.h>
51#include <net/addrconf.h>
52#include <net/tcp.h>
53#include <linux/rtnetlink.h>
54#include <net/dst.h>
55#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <asm/uaccess.h>
60
61#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
64
65/* Set to 3 to get tracing. */
66#define RT6_DEBUG 2
67
68#if RT6_DEBUG >= 3
69#define RDBG(x) printk x
70#define RT6_TRACE(x...) printk(KERN_DEBUG x)
71#else
72#define RDBG(x)
73#define RT6_TRACE(x...) do { ; } while (0)
74#endif
75
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -080076#define CLONE_OFFLINK_ROUTE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
79static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
80static struct dst_entry *ip6_negative_advice(struct dst_entry *);
81static void ip6_dst_destroy(struct dst_entry *);
82static void ip6_dst_ifdown(struct dst_entry *,
83 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080084static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static int ip6_pkt_discard(struct sk_buff *skb);
87static int ip6_pkt_discard_out(struct sk_buff *skb);
88static void ip6_link_failure(struct sk_buff *skb);
89static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
90
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080091#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080092static struct rt6_info *rt6_add_route_info(struct net *net,
93 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080094 struct in6_addr *gwaddr, int ifindex,
95 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080096static struct rt6_info *rt6_get_route_info(struct net *net,
97 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080098 struct in6_addr *gwaddr, int ifindex);
99#endif
100
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800101static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .family = AF_INET6,
103 .protocol = __constant_htons(ETH_P_IPV6),
104 .gc = ip6_dst_gc,
105 .gc_thresh = 1024,
106 .check = ip6_dst_check,
107 .destroy = ip6_dst_destroy,
108 .ifdown = ip6_dst_ifdown,
109 .negative_advice = ip6_negative_advice,
110 .link_failure = ip6_link_failure,
111 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700112 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .entry_size = sizeof(struct rt6_info),
Eric Dumazete2422972008-01-30 20:07:45 -0800114 .entries = ATOMIC_INIT(0),
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,
123 .protocol = __constant_htons(ETH_P_IPV6),
124 .destroy = ip6_dst_destroy,
125 .check = ip6_dst_check,
126 .update_pmtu = ip6_rt_blackhole_update_pmtu,
127 .entry_size = sizeof(struct rt6_info),
Eric Dumazete2422972008-01-30 20:07:45 -0800128 .entries = ATOMIC_INIT(0),
David S. Miller14e50e52007-05-24 18:17:54 -0700129};
130
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800131static struct rt6_info ip6_null_entry_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .u = {
133 .dst = {
134 .__refcnt = ATOMIC_INIT(1),
135 .__use = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .obsolete = -1,
137 .error = -ENETUNREACH,
138 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
139 .input = ip6_pkt_discard,
140 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 },
143 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
144 .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 = {
Thomas Graf101367c2006-08-04 03:39:02 -0700154 .u = {
155 .dst = {
156 .__refcnt = ATOMIC_INIT(1),
157 .__use = 1,
Thomas Graf101367c2006-08-04 03:39:02 -0700158 .obsolete = -1,
159 .error = -EACCES,
160 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
Thomas Graf9ce8ade2006-10-18 20:46:54 -0700161 .input = ip6_pkt_prohibit,
162 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700163 }
164 },
165 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
166 .rt6i_metric = ~(u32) 0,
167 .rt6i_ref = ATOMIC_INIT(1),
168};
169
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800170static struct rt6_info ip6_blk_hole_entry_template = {
Thomas Graf101367c2006-08-04 03:39:02 -0700171 .u = {
172 .dst = {
173 .__refcnt = ATOMIC_INIT(1),
174 .__use = 1,
Thomas Graf101367c2006-08-04 03:39:02 -0700175 .obsolete = -1,
176 .error = -EINVAL,
177 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
Herbert Xu352e5122007-11-13 21:34:06 -0800178 .input = dst_discard,
179 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700180 }
181 },
182 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
183 .rt6i_metric = ~(u32) 0,
184 .rt6i_ref = ATOMIC_INIT(1),
185};
186
187#endif
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* allocate dst with ip6_dst_ops */
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800190static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800192 return (struct rt6_info *)dst_alloc(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void ip6_dst_destroy(struct dst_entry *dst)
196{
197 struct rt6_info *rt = (struct rt6_info *)dst;
198 struct inet6_dev *idev = rt->rt6i_idev;
199
200 if (idev != NULL) {
201 rt->rt6i_idev = NULL;
202 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
207 int how)
208{
209 struct rt6_info *rt = (struct rt6_info *)dst;
210 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800211 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900212 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800214 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
215 struct inet6_dev *loopback_idev =
216 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (loopback_idev != NULL) {
218 rt->rt6i_idev = loopback_idev;
219 in6_dev_put(idev);
220 }
221 }
222}
223
224static __inline__ int rt6_check_expired(const struct rt6_info *rt)
225{
226 return (rt->rt6i_flags & RTF_EXPIRES &&
227 time_after(jiffies, rt->rt6i_expires));
228}
229
Thomas Grafc71099a2006-08-04 23:20:06 -0700230static inline int rt6_need_strict(struct in6_addr *daddr)
231{
232 return (ipv6_addr_type(daddr) &
233 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700237 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800240static inline struct rt6_info *rt6_device_match(struct net *net,
241 struct rt6_info *rt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int oif,
243 int strict)
244{
245 struct rt6_info *local = NULL;
246 struct rt6_info *sprt;
247
248 if (oif) {
Eric Dumazet7cc48262007-02-09 16:22:57 -0800249 for (sprt = rt; sprt; sprt = sprt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct net_device *dev = sprt->rt6i_dev;
251 if (dev->ifindex == oif)
252 return sprt;
253 if (dev->flags & IFF_LOOPBACK) {
254 if (sprt->rt6i_idev == NULL ||
255 sprt->rt6i_idev->dev->ifindex != oif) {
256 if (strict && oif)
257 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900258 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 local->rt6i_idev->dev->ifindex == oif))
260 continue;
261 }
262 local = sprt;
263 }
264 }
265
266 if (local)
267 return local;
268
269 if (strict)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800270 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 return rt;
273}
274
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800275#ifdef CONFIG_IPV6_ROUTER_PREF
276static void rt6_probe(struct rt6_info *rt)
277{
278 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
279 /*
280 * Okay, this does not seem to be appropriate
281 * for now, however, we need to check if it
282 * is really so; aka Router Reachability Probing.
283 *
284 * Router Reachability Probe MUST be rate-limited
285 * to no more than one per minute.
286 */
287 if (!neigh || (neigh->nud_state & NUD_VALID))
288 return;
289 read_lock_bh(&neigh->lock);
290 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800291 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800292 struct in6_addr mcaddr;
293 struct in6_addr *target;
294
295 neigh->updated = jiffies;
296 read_unlock_bh(&neigh->lock);
297
298 target = (struct in6_addr *)&neigh->primary_key;
299 addrconf_addr_solict_mult(target, &mcaddr);
300 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
301 } else
302 read_unlock_bh(&neigh->lock);
303}
304#else
305static inline void rt6_probe(struct rt6_info *rt)
306{
307 return;
308}
309#endif
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800312 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700314static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800316 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700317 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800318 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700319 if ((dev->flags & IFF_LOOPBACK) &&
320 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
321 return 1;
322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Dave Jonesb6f99a22007-03-22 12:27:49 -0700325static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800327 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800328 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700329 if (rt->rt6i_flags & RTF_NONEXTHOP ||
330 !(rt->rt6i_flags & RTF_GATEWAY))
331 m = 1;
332 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800333 read_lock_bh(&neigh->lock);
334 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700335 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800336#ifdef CONFIG_IPV6_ROUTER_PREF
337 else if (neigh->nud_state & NUD_FAILED)
338 m = 0;
339#endif
340 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800341 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800342 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800343 } else
344 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800345 return m;
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800348static int rt6_score_route(struct rt6_info *rt, int oif,
349 int strict)
350{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700351 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900352
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700353 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700354 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800355 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800356#ifdef CONFIG_IPV6_ROUTER_PREF
357 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
358#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700359 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800360 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800361 return -1;
362 return m;
363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David S. Millerf11e6652007-03-24 20:36:25 -0700365static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
366 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800367{
David S. Millerf11e6652007-03-24 20:36:25 -0700368 int m;
369
370 if (rt6_check_expired(rt))
371 goto out;
372
373 m = rt6_score_route(rt, oif, strict);
374 if (m < 0)
375 goto out;
376
377 if (m > *mpri) {
378 if (strict & RT6_LOOKUP_F_REACHABLE)
379 rt6_probe(match);
380 *mpri = m;
381 match = rt;
382 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
383 rt6_probe(rt);
384 }
385
386out:
387 return match;
388}
389
390static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
391 struct rt6_info *rr_head,
392 u32 metric, int oif, int strict)
393{
394 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800395 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David S. Millerf11e6652007-03-24 20:36:25 -0700397 match = NULL;
398 for (rt = rr_head; rt && rt->rt6i_metric == metric;
399 rt = rt->u.dst.rt6_next)
400 match = find_match(rt, oif, strict, &mpri, match);
401 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
402 rt = rt->u.dst.rt6_next)
403 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800404
David S. Millerf11e6652007-03-24 20:36:25 -0700405 return match;
406}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800407
David S. Millerf11e6652007-03-24 20:36:25 -0700408static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
409{
410 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800411 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David S. Millerf11e6652007-03-24 20:36:25 -0700413 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800414 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
David S. Millerf11e6652007-03-24 20:36:25 -0700416 rt0 = fn->rr_ptr;
417 if (!rt0)
418 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David S. Millerf11e6652007-03-24 20:36:25 -0700420 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800422 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700423 (strict & RT6_LOOKUP_F_REACHABLE)) {
424 struct rt6_info *next = rt0->u.dst.rt6_next;
425
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800426 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700427 if (!next || next->rt6i_metric != rt0->rt6i_metric)
428 next = fn->leaf;
429
430 if (next != rt0)
431 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
David S. Millerf11e6652007-03-24 20:36:25 -0700434 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800435 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900437 net = dev_net(rt0->rt6i_dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800438 return (match ? match : net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800441#ifdef CONFIG_IPV6_ROUTE_INFO
442int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
443 struct in6_addr *gwaddr)
444{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900445 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800446 struct route_info *rinfo = (struct route_info *) opt;
447 struct in6_addr prefix_buf, *prefix;
448 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900449 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800450 struct rt6_info *rt;
451
452 if (len < sizeof(struct route_info)) {
453 return -EINVAL;
454 }
455
456 /* Sanity check for prefix_len and length */
457 if (rinfo->length > 3) {
458 return -EINVAL;
459 } else if (rinfo->prefix_len > 128) {
460 return -EINVAL;
461 } else if (rinfo->prefix_len > 64) {
462 if (rinfo->length < 2) {
463 return -EINVAL;
464 }
465 } else if (rinfo->prefix_len > 0) {
466 if (rinfo->length < 1) {
467 return -EINVAL;
468 }
469 }
470
471 pref = rinfo->route_pref;
472 if (pref == ICMPV6_ROUTER_PREF_INVALID)
473 pref = ICMPV6_ROUTER_PREF_MEDIUM;
474
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900475 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800476
477 if (rinfo->length == 3)
478 prefix = (struct in6_addr *)rinfo->prefix;
479 else {
480 /* this function is safe */
481 ipv6_addr_prefix(&prefix_buf,
482 (struct in6_addr *)rinfo->prefix,
483 rinfo->prefix_len);
484 prefix = &prefix_buf;
485 }
486
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800487 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
488 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800489
490 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700491 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800492 rt = NULL;
493 }
494
495 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800496 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800497 pref);
498 else if (rt)
499 rt->rt6i_flags = RTF_ROUTEINFO |
500 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
501
502 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900503 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800504 rt->rt6i_flags &= ~RTF_EXPIRES;
505 } else {
506 rt->rt6i_expires = jiffies + HZ * lifetime;
507 rt->rt6i_flags |= RTF_EXPIRES;
508 }
509 dst_release(&rt->u.dst);
510 }
511 return 0;
512}
513#endif
514
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800515#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700516do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800517 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700518 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700519 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700520 if (fn->fn_flags & RTN_TL_ROOT) \
521 goto out; \
522 pn = fn->parent; \
523 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800524 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700525 else \
526 fn = pn; \
527 if (fn->fn_flags & RTN_RTINFO) \
528 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700529 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700530 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700531} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700532
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800533static struct rt6_info *ip6_pol_route_lookup(struct net *net,
534 struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700535 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct fib6_node *fn;
538 struct rt6_info *rt;
539
Thomas Grafc71099a2006-08-04 23:20:06 -0700540 read_lock_bh(&table->tb6_lock);
541 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
542restart:
543 rt = fn->leaf;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800544 rt = rt6_device_match(net, rt, fl->oif, flags);
545 BACKTRACK(net, &fl->fl6_src);
Thomas Grafc71099a2006-08-04 23:20:06 -0700546out:
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800547 dst_use(&rt->u.dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700548 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700549 return rt;
550
551}
552
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900553struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
554 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700555{
556 struct flowi fl = {
557 .oif = oif,
558 .nl_u = {
559 .ip6_u = {
560 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700561 },
562 },
563 };
564 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700565 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700566
Thomas Grafadaa70b2006-10-13 15:01:03 -0700567 if (saddr) {
568 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
569 flags |= RT6_LOOKUP_F_HAS_SADDR;
570 }
571
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800572 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700573 if (dst->error == 0)
574 return (struct rt6_info *) dst;
575
576 dst_release(dst);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579}
580
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900581EXPORT_SYMBOL(rt6_lookup);
582
Thomas Grafc71099a2006-08-04 23:20:06 -0700583/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 It takes new route entry, the addition fails by any reason the
585 route is freed. In any case, if caller does not hold it, it may
586 be destroyed.
587 */
588
Thomas Graf86872cb2006-08-22 00:01:08 -0700589static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700592 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Thomas Grafc71099a2006-08-04 23:20:06 -0700594 table = rt->rt6i_table;
595 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700596 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700597 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return err;
600}
601
Thomas Graf40e22e82006-08-22 00:00:45 -0700602int ip6_ins_rt(struct rt6_info *rt)
603{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800604 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900605 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800606 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800607 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700608}
609
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800610static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
611 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct rt6_info *rt;
614
615 /*
616 * Clone the route.
617 */
618
619 rt = ip6_rt_copy(ort);
620
621 if (rt) {
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900622 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
623 if (rt->rt6i_dst.plen != 128 &&
624 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
625 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900629 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 rt->rt6i_dst.plen = 128;
631 rt->rt6i_flags |= RTF_CACHE;
632 rt->u.dst.flags |= DST_HOST;
633
634#ifdef CONFIG_IPV6_SUBTREES
635 if (rt->rt6i_src.plen && saddr) {
636 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
637 rt->rt6i_src.plen = 128;
638 }
639#endif
640
641 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
642
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800645 return rt;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800648static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
649{
650 struct rt6_info *rt = ip6_rt_copy(ort);
651 if (rt) {
652 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
653 rt->rt6i_dst.plen = 128;
654 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800655 rt->u.dst.flags |= DST_HOST;
656 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
657 }
658 return rt;
659}
660
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800661static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
662 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800665 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700666 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800668 int err;
YOSHIFUJI Hideakiea659e02006-11-06 09:45:45 -0800669 int reachable = ipv6_devconf.forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700671 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700674 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800676restart_2:
Thomas Grafc71099a2006-08-04 23:20:06 -0700677 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700680 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800681
682 BACKTRACK(net, &fl->fl6_src);
683 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800684 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800685 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800687 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700688 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800689
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800690 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800691 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800692 else {
693#if CLONE_OFFLINK_ROUTE
694 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
695#else
696 goto out2;
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800699
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800700 dst_release(&rt->u.dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800701 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800702
703 dst_hold(&rt->u.dst);
704 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700705 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800706 if (!err)
707 goto out2;
708 }
709
710 if (--attempts <= 0)
711 goto out2;
712
713 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700714 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800715 * released someone could insert this route. Relookup.
716 */
717 dst_release(&rt->u.dst);
718 goto relookup;
719
720out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800721 if (reachable) {
722 reachable = 0;
723 goto restart_2;
724 }
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800725 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700726 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727out2:
728 rt->u.dst.lastuse = jiffies;
729 rt->u.dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700730
731 return rt;
732}
733
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800734static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700735 struct flowi *fl, int flags)
736{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800737 return ip6_pol_route(net, table, fl->iif, fl, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700738}
739
Thomas Grafc71099a2006-08-04 23:20:06 -0700740void ip6_route_input(struct sk_buff *skb)
741{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700742 struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900743 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700744 int flags = RT6_LOOKUP_F_HAS_SADDR;
Thomas Grafc71099a2006-08-04 23:20:06 -0700745 struct flowi fl = {
746 .iif = skb->dev->ifindex,
747 .nl_u = {
748 .ip6_u = {
749 .daddr = iph->daddr,
750 .saddr = iph->saddr,
Al Viro90bcaf72006-11-08 00:25:17 -0800751 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
Thomas Grafc71099a2006-08-04 23:20:06 -0700752 },
753 },
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900754 .mark = skb->mark,
Thomas Grafc71099a2006-08-04 23:20:06 -0700755 .proto = iph->nexthdr,
756 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700757
758 if (rt6_need_strict(&iph->daddr))
759 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700760
Daniel Lezcano55786892008-03-04 13:47:47 -0800761 skb->dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input);
Thomas Grafc71099a2006-08-04 23:20:06 -0700762}
763
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800764static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700765 struct flowi *fl, int flags)
766{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800767 return ip6_pol_route(net, table, fl->oif, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700768}
769
Daniel Lezcano4591db42008-03-05 10:48:10 -0800770struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
771 struct flowi *fl)
Thomas Grafc71099a2006-08-04 23:20:06 -0700772{
773 int flags = 0;
774
775 if (rt6_need_strict(&fl->fl6_dst))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700776 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700777
Thomas Grafadaa70b2006-10-13 15:01:03 -0700778 if (!ipv6_addr_any(&fl->fl6_src))
779 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900780 else if (sk) {
781 unsigned int prefs = inet6_sk(sk)->srcprefs;
782 if (prefs & IPV6_PREFER_SRC_TMP)
783 flags |= RT6_LOOKUP_F_SRCPREF_TMP;
784 if (prefs & IPV6_PREFER_SRC_PUBLIC)
785 flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
786 if (prefs & IPV6_PREFER_SRC_COA)
787 flags |= RT6_LOOKUP_F_SRCPREF_COA;
788 }
Thomas Grafadaa70b2006-10-13 15:01:03 -0700789
Daniel Lezcano4591db42008-03-05 10:48:10 -0800790 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900793EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David S. Miller14e50e52007-05-24 18:17:54 -0700795int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
796{
797 struct rt6_info *ort = (struct rt6_info *) *dstp;
798 struct rt6_info *rt = (struct rt6_info *)
799 dst_alloc(&ip6_dst_blackhole_ops);
800 struct dst_entry *new = NULL;
801
802 if (rt) {
803 new = &rt->u.dst;
804
805 atomic_set(&new->__refcnt, 1);
806 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800807 new->input = dst_discard;
808 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700809
810 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
811 new->dev = ort->u.dst.dev;
812 if (new->dev)
813 dev_hold(new->dev);
814 rt->rt6i_idev = ort->rt6i_idev;
815 if (rt->rt6i_idev)
816 in6_dev_hold(rt->rt6i_idev);
817 rt->rt6i_expires = 0;
818
819 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
820 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
821 rt->rt6i_metric = 0;
822
823 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
824#ifdef CONFIG_IPV6_SUBTREES
825 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
826#endif
827
828 dst_free(new);
829 }
830
831 dst_release(*dstp);
832 *dstp = new;
833 return (new ? 0 : -ENOMEM);
834}
835EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * Destination cache support functions
839 */
840
841static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
842{
843 struct rt6_info *rt;
844
845 rt = (struct rt6_info *) dst;
846
847 if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
848 return dst;
849
850 return NULL;
851}
852
853static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
854{
855 struct rt6_info *rt = (struct rt6_info *) dst;
856
857 if (rt) {
858 if (rt->rt6i_flags & RTF_CACHE)
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700859 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
861 dst_release(dst);
862 }
863 return NULL;
864}
865
866static void ip6_link_failure(struct sk_buff *skb)
867{
868 struct rt6_info *rt;
869
870 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
871
872 rt = (struct rt6_info *) skb->dst;
873 if (rt) {
874 if (rt->rt6i_flags&RTF_CACHE) {
875 dst_set_expires(&rt->u.dst, 0);
876 rt->rt6i_flags |= RTF_EXPIRES;
877 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
878 rt->rt6i_node->fn_sernum = -1;
879 }
880}
881
882static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
883{
884 struct rt6_info *rt6 = (struct rt6_info*)dst;
885
886 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
887 rt6->rt6i_flags |= RTF_MODIFIED;
888 if (mtu < IPV6_MIN_MTU) {
889 mtu = IPV6_MIN_MTU;
890 dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
891 }
892 dst->metrics[RTAX_MTU-1] = mtu;
Tom Tucker8d717402006-07-30 20:43:36 -0700893 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static int ipv6_get_mtu(struct net_device *dev);
898
Daniel Lezcano55786892008-03-04 13:47:47 -0800899static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
902
Daniel Lezcano55786892008-03-04 13:47:47 -0800903 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
904 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900907 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
908 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
909 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * rely only on pmtu discovery"
911 */
912 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
913 mtu = IPV6_MAXPLEN;
914 return mtu;
915}
916
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800917static struct dst_entry *icmp6_dst_gc_list;
918static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700919
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800920struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900922 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct rt6_info *rt;
925 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900926 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (unlikely(idev == NULL))
929 return NULL;
930
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800931 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (unlikely(rt == NULL)) {
933 in6_dev_put(idev);
934 goto out;
935 }
936
937 dev_hold(dev);
938 if (neigh)
939 neigh_hold(neigh);
940 else
941 neigh = ndisc_get_neigh(dev, addr);
942
943 rt->rt6i_dev = dev;
944 rt->rt6i_idev = idev;
945 rt->rt6i_nexthop = neigh;
946 atomic_set(&rt->u.dst.__refcnt, 1);
947 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
948 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -0800949 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800950 rt->u.dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952#if 0 /* there's no chance to use these for ndisc */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900953 rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
954 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 : 0;
956 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
957 rt->rt6i_dst.plen = 128;
958#endif
959
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800960 spin_lock_bh(&icmp6_dst_lock);
961 rt->u.dst.next = icmp6_dst_gc_list;
962 icmp6_dst_gc_list = &rt->u.dst;
963 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Daniel Lezcano55786892008-03-04 13:47:47 -0800965 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967out:
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +0900968 return &rt->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800971int icmp6_dst_gc(int *more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct dst_entry *dst, *next, **pprev;
974 int freed;
975
976 next = NULL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900977 freed = 0;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700978
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800979 spin_lock_bh(&icmp6_dst_lock);
980 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 while ((dst = *pprev) != NULL) {
983 if (!atomic_read(&dst->__refcnt)) {
984 *pprev = dst->next;
985 dst_free(dst);
986 freed++;
987 } else {
988 pprev = &dst->next;
989 (*more)++;
990 }
991 }
992
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800993 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return freed;
996}
997
Daniel Lezcano569d3642008-01-18 03:56:57 -0800998static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long now = jiffies;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001001 struct net *net = ops->dst_net;
1002 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1003 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1004 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1005 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1006 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Daniel Lezcano7019b782008-03-04 13:50:14 -08001008 if (time_after(rt_last_gc + rt_min_interval, now) &&
1009 atomic_read(&ops->entries) <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto out;
1011
Benjamin Thery6891a342008-03-04 13:49:47 -08001012 net->ipv6.ip6_rt_gc_expire++;
1013 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1014 net->ipv6.ip6_rt_last_gc = now;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001015 if (atomic_read(&ops->entries) < ops->gc_thresh)
1016 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001018 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1019 return (atomic_read(&ops->entries) > rt_max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/* Clean host part of a prefix. Not necessary in radix tree,
1023 but results in cleaner routing tables.
1024
1025 Remove it only when all the things will work!
1026 */
1027
1028static int ipv6_get_mtu(struct net_device *dev)
1029{
1030 int mtu = IPV6_MIN_MTU;
1031 struct inet6_dev *idev;
1032
1033 idev = in6_dev_get(dev);
1034 if (idev) {
1035 mtu = idev->cnf.mtu6;
1036 in6_dev_put(idev);
1037 }
1038 return mtu;
1039}
1040
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001041int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001043 int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
1044 if (hoplimit < 0) {
1045 struct net_device *dev = dst->dev;
1046 struct inet6_dev *idev = in6_dev_get(dev);
1047 if (idev) {
1048 hoplimit = idev->cnf.hop_limit;
1049 in6_dev_put(idev);
1050 } else
1051 hoplimit = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 return hoplimit;
1054}
1055
1056/*
1057 *
1058 */
1059
Thomas Graf86872cb2006-08-22 00:01:08 -07001060int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001063 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct rt6_info *rt = NULL;
1065 struct net_device *dev = NULL;
1066 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001067 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int addr_type;
1069
Thomas Graf86872cb2006-08-22 00:01:08 -07001070 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EINVAL;
1072#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001073 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EINVAL;
1075#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001076 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001078 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!dev)
1080 goto out;
1081 idev = in6_dev_get(dev);
1082 if (!idev)
1083 goto out;
1084 }
1085
Thomas Graf86872cb2006-08-22 00:01:08 -07001086 if (cfg->fc_metric == 0)
1087 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Daniel Lezcano55786892008-03-04 13:47:47 -08001089 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001090 if (table == NULL) {
1091 err = -ENOBUFS;
1092 goto out;
1093 }
1094
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001095 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (rt == NULL) {
1098 err = -ENOMEM;
1099 goto out;
1100 }
1101
1102 rt->u.dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001103 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1104 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1105 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Thomas Graf86872cb2006-08-22 00:01:08 -07001107 if (cfg->fc_protocol == RTPROT_UNSPEC)
1108 cfg->fc_protocol = RTPROT_BOOT;
1109 rt->rt6i_protocol = cfg->fc_protocol;
1110
1111 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (addr_type & IPV6_ADDR_MULTICAST)
1114 rt->u.dst.input = ip6_mc_input;
1115 else
1116 rt->u.dst.input = ip6_forward;
1117
1118 rt->u.dst.output = ip6_output;
1119
Thomas Graf86872cb2006-08-22 00:01:08 -07001120 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1121 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (rt->rt6i_dst.plen == 128)
1123 rt->u.dst.flags = DST_HOST;
1124
1125#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001126 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1127 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129
Thomas Graf86872cb2006-08-22 00:01:08 -07001130 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* We cannot add true routes via loopback here,
1133 they would result in kernel looping; promote them to reject routes
1134 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001135 if ((cfg->fc_flags & RTF_REJECT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
1137 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001138 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (dev) {
1140 dev_put(dev);
1141 in6_dev_put(idev);
1142 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001143 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dev_hold(dev);
1145 idev = in6_dev_get(dev);
1146 if (!idev) {
1147 err = -ENODEV;
1148 goto out;
1149 }
1150 }
1151 rt->u.dst.output = ip6_pkt_discard_out;
1152 rt->u.dst.input = ip6_pkt_discard;
1153 rt->u.dst.error = -ENETUNREACH;
1154 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1155 goto install_route;
1156 }
1157
Thomas Graf86872cb2006-08-22 00:01:08 -07001158 if (cfg->fc_flags & RTF_GATEWAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct in6_addr *gw_addr;
1160 int gwa_type;
1161
Thomas Graf86872cb2006-08-22 00:01:08 -07001162 gw_addr = &cfg->fc_gateway;
1163 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 gwa_type = ipv6_addr_type(gw_addr);
1165
1166 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1167 struct rt6_info *grt;
1168
1169 /* IPv6 strictly inhibits using not link-local
1170 addresses as nexthop address.
1171 Otherwise, router will not able to send redirects.
1172 It is very good, but in some (rare!) circumstances
1173 (SIT, PtP, NBMA NOARP links) it is handy to allow
1174 some exceptions. --ANK
1175 */
1176 err = -EINVAL;
1177 if (!(gwa_type&IPV6_ADDR_UNICAST))
1178 goto out;
1179
Daniel Lezcano55786892008-03-04 13:47:47 -08001180 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 err = -EHOSTUNREACH;
1183 if (grt == NULL)
1184 goto out;
1185 if (dev) {
1186 if (dev != grt->rt6i_dev) {
1187 dst_release(&grt->u.dst);
1188 goto out;
1189 }
1190 } else {
1191 dev = grt->rt6i_dev;
1192 idev = grt->rt6i_idev;
1193 dev_hold(dev);
1194 in6_dev_hold(grt->rt6i_idev);
1195 }
1196 if (!(grt->rt6i_flags&RTF_GATEWAY))
1197 err = 0;
1198 dst_release(&grt->u.dst);
1199
1200 if (err)
1201 goto out;
1202 }
1203 err = -EINVAL;
1204 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1205 goto out;
1206 }
1207
1208 err = -ENODEV;
1209 if (dev == NULL)
1210 goto out;
1211
Thomas Graf86872cb2006-08-22 00:01:08 -07001212 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1214 if (IS_ERR(rt->rt6i_nexthop)) {
1215 err = PTR_ERR(rt->rt6i_nexthop);
1216 rt->rt6i_nexthop = NULL;
1217 goto out;
1218 }
1219 }
1220
Thomas Graf86872cb2006-08-22 00:01:08 -07001221 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001224 if (cfg->fc_mx) {
1225 struct nlattr *nla;
1226 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Thomas Graf86872cb2006-08-22 00:01:08 -07001228 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001229 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001230
1231 if (type) {
1232 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 err = -EINVAL;
1234 goto out;
1235 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001236
1237 rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 }
1241
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001242 if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001244 if (!dst_metric(&rt->u.dst, RTAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001246 if (!dst_metric(&rt->u.dst, RTAX_ADVMSS))
Daniel Lezcano55786892008-03-04 13:47:47 -08001247 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rt->u.dst.dev = dev;
1249 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001250 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001251
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001252 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001253
Thomas Graf86872cb2006-08-22 00:01:08 -07001254 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256out:
1257 if (dev)
1258 dev_put(dev);
1259 if (idev)
1260 in6_dev_put(idev);
1261 if (rt)
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001262 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return err;
1264}
1265
Thomas Graf86872cb2006-08-22 00:01:08 -07001266static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001269 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001270 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001272 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001273 return -ENOENT;
1274
Thomas Grafc71099a2006-08-04 23:20:06 -07001275 table = rt->rt6i_table;
1276 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Thomas Graf86872cb2006-08-22 00:01:08 -07001278 err = fib6_del(rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 dst_release(&rt->u.dst);
1280
Thomas Grafc71099a2006-08-04 23:20:06 -07001281 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 return err;
1284}
1285
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001286int ip6_del_rt(struct rt6_info *rt)
1287{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001288 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001289 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001290 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001291 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001292}
1293
Thomas Graf86872cb2006-08-22 00:01:08 -07001294static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Thomas Grafc71099a2006-08-04 23:20:06 -07001296 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct fib6_node *fn;
1298 struct rt6_info *rt;
1299 int err = -ESRCH;
1300
Daniel Lezcano55786892008-03-04 13:47:47 -08001301 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001302 if (table == NULL)
1303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Grafc71099a2006-08-04 23:20:06 -07001305 read_lock_bh(&table->tb6_lock);
1306
1307 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001308 &cfg->fc_dst, cfg->fc_dst_len,
1309 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (fn) {
Eric Dumazet7cc48262007-02-09 16:22:57 -08001312 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001313 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001315 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001317 if (cfg->fc_flags & RTF_GATEWAY &&
1318 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001320 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 continue;
1322 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001323 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Thomas Graf86872cb2006-08-22 00:01:08 -07001325 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001328 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 return err;
1331}
1332
1333/*
1334 * Handle redirects
1335 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001336struct ip6rd_flowi {
1337 struct flowi fl;
1338 struct in6_addr gateway;
1339};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001341static struct rt6_info *__ip6_route_redirect(struct net *net,
1342 struct fib6_table *table,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001343 struct flowi *fl,
1344 int flags)
1345{
1346 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1347 struct rt6_info *rt;
1348 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001351 * Get the "current" route for this destination and
1352 * check if the redirect has come from approriate router.
1353 *
1354 * RFC 2461 specifies that redirects should only be
1355 * accepted if they come from the nexthop to the target.
1356 * Due to the way the routes are chosen, this notion
1357 * is a bit fuzzy and one might need to check all possible
1358 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Thomas Grafc71099a2006-08-04 23:20:06 -07001361 read_lock_bh(&table->tb6_lock);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001362 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001363restart:
Eric Dumazet7cc48262007-02-09 16:22:57 -08001364 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001365 /*
1366 * Current route is on-link; redirect is always invalid.
1367 *
1368 * Seems, previous statement is not true. It could
1369 * be node, which looks for us as on-link (f.e. proxy ndisc)
1370 * But then router serving it might decide, that we should
1371 * know truth 8)8) --ANK (980726).
1372 */
1373 if (rt6_check_expired(rt))
1374 continue;
1375 if (!(rt->rt6i_flags & RTF_GATEWAY))
1376 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001377 if (fl->oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001378 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001379 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001380 continue;
1381 break;
1382 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001383
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001384 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001385 rt = net->ipv6.ip6_null_entry;
1386 BACKTRACK(net, &fl->fl6_src);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001387out:
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001388 dst_hold(&rt->u.dst);
1389
1390 read_unlock_bh(&table->tb6_lock);
1391
1392 return rt;
1393};
1394
1395static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1396 struct in6_addr *src,
1397 struct in6_addr *gateway,
1398 struct net_device *dev)
1399{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001400 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001401 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001402 struct ip6rd_flowi rdfl = {
1403 .fl = {
1404 .oif = dev->ifindex,
1405 .nl_u = {
1406 .ip6_u = {
1407 .daddr = *dest,
1408 .saddr = *src,
1409 },
1410 },
1411 },
1412 .gateway = *gateway,
1413 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001414
1415 if (rt6_need_strict(dest))
1416 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001417
Daniel Lezcano55786892008-03-04 13:47:47 -08001418 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001419 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001420}
1421
1422void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1423 struct in6_addr *saddr,
1424 struct neighbour *neigh, u8 *lladdr, int on_link)
1425{
1426 struct rt6_info *rt, *nrt = NULL;
1427 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001428 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001429
1430 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1431
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001432 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (net_ratelimit())
1434 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1435 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
1440 * We have finally decided to accept it.
1441 */
1442
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001443 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1445 NEIGH_UPDATE_F_OVERRIDE|
1446 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1447 NEIGH_UPDATE_F_ISROUTER))
1448 );
1449
1450 /*
1451 * Redirect received -> path was valid.
1452 * Look, redirects are sent only in response to data packets,
1453 * so that this nexthop apparently is reachable. --ANK
1454 */
1455 dst_confirm(&rt->u.dst);
1456
1457 /* Duplicate redirect: silently ignore. */
1458 if (neigh == rt->u.dst.neighbour)
1459 goto out;
1460
1461 nrt = ip6_rt_copy(rt);
1462 if (nrt == NULL)
1463 goto out;
1464
1465 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1466 if (on_link)
1467 nrt->rt6i_flags &= ~RTF_GATEWAY;
1468
1469 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1470 nrt->rt6i_dst.plen = 128;
1471 nrt->u.dst.flags |= DST_HOST;
1472
1473 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1474 nrt->rt6i_nexthop = neigh_clone(neigh);
1475 /* Reset pmtu, it may be better */
1476 nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001477 nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
Daniel Lezcano55786892008-03-04 13:47:47 -08001478 dst_mtu(&nrt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Thomas Graf40e22e82006-08-22 00:00:45 -07001480 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto out;
1482
Tom Tucker8d717402006-07-30 20:43:36 -07001483 netevent.old = &rt->u.dst;
1484 netevent.new = &nrt->u.dst;
1485 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001488 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490 }
1491
1492out:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001493 dst_release(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return;
1495}
1496
1497/*
1498 * Handle ICMP "packet too big" messages
1499 * i.e. Path MTU discovery
1500 */
1501
1502void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1503 struct net_device *dev, u32 pmtu)
1504{
1505 struct rt6_info *rt, *nrt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001506 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int allfrag = 0;
1508
Daniel Lezcano55786892008-03-04 13:47:47 -08001509 rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (rt == NULL)
1511 return;
1512
1513 if (pmtu >= dst_mtu(&rt->u.dst))
1514 goto out;
1515
1516 if (pmtu < IPV6_MIN_MTU) {
1517 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001518 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * MTU (1280) and a fragment header should always be included
1520 * after a node receiving Too Big message reporting PMTU is
1521 * less than the IPv6 Minimum Link MTU.
1522 */
1523 pmtu = IPV6_MIN_MTU;
1524 allfrag = 1;
1525 }
1526
1527 /* New mtu received -> path was valid.
1528 They are sent only in response to data packets,
1529 so that this nexthop apparently is reachable. --ANK
1530 */
1531 dst_confirm(&rt->u.dst);
1532
1533 /* Host route. If it is static, it would be better
1534 not to override it, but add new one, so that
1535 when cache entry will expire old pmtu
1536 would return automatically.
1537 */
1538 if (rt->rt6i_flags & RTF_CACHE) {
1539 rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1540 if (allfrag)
1541 rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
Daniel Lezcano55786892008-03-04 13:47:47 -08001542 dst_set_expires(&rt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1544 goto out;
1545 }
1546
1547 /* Network route.
1548 Two cases are possible:
1549 1. It is connected route. Action: COW
1550 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1551 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001552 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001553 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001554 else
1555 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001556
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001557 if (nrt) {
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001558 nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1559 if (allfrag)
1560 nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1561
1562 /* According to RFC 1981, detecting PMTU increase shouldn't be
1563 * happened within 5 mins, the recommended timer is 10 mins.
1564 * Here this route expiration time is set to ip6_rt_mtu_expires
1565 * which is 10 mins. After 10 mins the decreased pmtu is expired
1566 * and detecting PMTU increase will be automatically happened.
1567 */
Daniel Lezcano55786892008-03-04 13:47:47 -08001568 dst_set_expires(&nrt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001569 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1570
Thomas Graf40e22e82006-08-22 00:00:45 -07001571 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573out:
1574 dst_release(&rt->u.dst);
1575}
1576
1577/*
1578 * Misc support functions
1579 */
1580
1581static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1582{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001583 struct net *net = dev_net(ort->rt6i_dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001584 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (rt) {
1587 rt->u.dst.input = ort->u.dst.input;
1588 rt->u.dst.output = ort->u.dst.output;
1589
1590 memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
Ville Nuorvala22e1e4d2006-10-16 22:14:26 -07001591 rt->u.dst.error = ort->u.dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 rt->u.dst.dev = ort->u.dst.dev;
1593 if (rt->u.dst.dev)
1594 dev_hold(rt->u.dst.dev);
1595 rt->rt6i_idev = ort->rt6i_idev;
1596 if (rt->rt6i_idev)
1597 in6_dev_hold(rt->rt6i_idev);
1598 rt->u.dst.lastuse = jiffies;
1599 rt->rt6i_expires = 0;
1600
1601 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1602 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1603 rt->rt6i_metric = 0;
1604
1605 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1606#ifdef CONFIG_IPV6_SUBTREES
1607 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1608#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001609 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611 return rt;
1612}
1613
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001614#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001615static struct rt6_info *rt6_get_route_info(struct net *net,
1616 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001617 struct in6_addr *gwaddr, int ifindex)
1618{
1619 struct fib6_node *fn;
1620 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001621 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001622
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001623 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001624 if (table == NULL)
1625 return NULL;
1626
1627 write_lock_bh(&table->tb6_lock);
1628 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001629 if (!fn)
1630 goto out;
1631
Eric Dumazet7cc48262007-02-09 16:22:57 -08001632 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001633 if (rt->rt6i_dev->ifindex != ifindex)
1634 continue;
1635 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1636 continue;
1637 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1638 continue;
1639 dst_hold(&rt->u.dst);
1640 break;
1641 }
1642out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001643 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001644 return rt;
1645}
1646
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001647static struct rt6_info *rt6_add_route_info(struct net *net,
1648 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001649 struct in6_addr *gwaddr, int ifindex,
1650 unsigned pref)
1651{
Thomas Graf86872cb2006-08-22 00:01:08 -07001652 struct fib6_config cfg = {
1653 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001654 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001655 .fc_ifindex = ifindex,
1656 .fc_dst_len = prefixlen,
1657 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1658 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001659 .fc_nlinfo.pid = 0,
1660 .fc_nlinfo.nlh = NULL,
1661 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001662 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001663
Thomas Graf86872cb2006-08-22 00:01:08 -07001664 ipv6_addr_copy(&cfg.fc_dst, prefix);
1665 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1666
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001667 /* We should treat it as a default route if prefix length is 0. */
1668 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001669 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001670
Thomas Graf86872cb2006-08-22 00:01:08 -07001671 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001672
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001673 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001674}
1675#endif
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001680 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001682 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001683 if (table == NULL)
1684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Thomas Grafc71099a2006-08-04 23:20:06 -07001686 write_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001687 for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001689 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1691 break;
1692 }
1693 if (rt)
1694 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001695 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return rt;
1697}
1698
1699struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001700 struct net_device *dev,
1701 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Thomas Graf86872cb2006-08-22 00:01:08 -07001703 struct fib6_config cfg = {
1704 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001705 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001706 .fc_ifindex = dev->ifindex,
1707 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1708 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001709 .fc_nlinfo.pid = 0,
1710 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001711 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001712 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Thomas Graf86872cb2006-08-22 00:01:08 -07001714 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Thomas Graf86872cb2006-08-22 00:01:08 -07001716 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return rt6_get_dflt_router(gwaddr, dev);
1719}
1720
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001721void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001724 struct fib6_table *table;
1725
1726 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001727 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001728 if (table == NULL)
1729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001732 read_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001733 for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1735 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001736 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001737 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto restart;
1739 }
1740 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001741 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Daniel Lezcano55786892008-03-04 13:47:47 -08001744static void rtmsg_to_fib6_config(struct net *net,
1745 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001746 struct fib6_config *cfg)
1747{
1748 memset(cfg, 0, sizeof(*cfg));
1749
1750 cfg->fc_table = RT6_TABLE_MAIN;
1751 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1752 cfg->fc_metric = rtmsg->rtmsg_metric;
1753 cfg->fc_expires = rtmsg->rtmsg_info;
1754 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1755 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1756 cfg->fc_flags = rtmsg->rtmsg_flags;
1757
Daniel Lezcano55786892008-03-04 13:47:47 -08001758 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001759
Thomas Graf86872cb2006-08-22 00:01:08 -07001760 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1761 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1762 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1763}
1764
Daniel Lezcano55786892008-03-04 13:47:47 -08001765int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Thomas Graf86872cb2006-08-22 00:01:08 -07001767 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct in6_rtmsg rtmsg;
1769 int err;
1770
1771 switch(cmd) {
1772 case SIOCADDRT: /* Add a route */
1773 case SIOCDELRT: /* Delete a route */
1774 if (!capable(CAP_NET_ADMIN))
1775 return -EPERM;
1776 err = copy_from_user(&rtmsg, arg,
1777 sizeof(struct in6_rtmsg));
1778 if (err)
1779 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001780
Daniel Lezcano55786892008-03-04 13:47:47 -08001781 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 rtnl_lock();
1784 switch (cmd) {
1785 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001786 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001789 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 break;
1791 default:
1792 err = -EINVAL;
1793 }
1794 rtnl_unlock();
1795
1796 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 return -EINVAL;
1800}
1801
1802/*
1803 * Drop the packet on the floor
1804 */
1805
Ilpo Järvinen50eb4312008-01-12 03:21:00 -08001806static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001808 int type;
1809 switch (ipstats_mib_noroutes) {
1810 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001811 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001812 if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
1813 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INADDRERRORS);
1814 break;
1815 }
1816 /* FALLTHROUGH */
1817 case IPSTATS_MIB_OUTNOROUTES:
1818 IP6_INC_STATS(ip6_dst_idev(skb->dst), ipstats_mib_noroutes);
1819 break;
1820 }
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001821 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 kfree_skb(skb);
1823 return 0;
1824}
1825
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001826static int ip6_pkt_discard(struct sk_buff *skb)
1827{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001828 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001829}
1830
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001831static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001834 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
David S. Miller6723ab52006-10-18 21:20:57 -07001837#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1838
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001839static int ip6_pkt_prohibit(struct sk_buff *skb)
1840{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001841 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001842}
1843
1844static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1845{
1846 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001847 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001848}
1849
David S. Miller6723ab52006-10-18 21:20:57 -07001850#endif
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852/*
1853 * Allocate a dst for local (unicast / anycast) address.
1854 */
1855
1856struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1857 const struct in6_addr *addr,
1858 int anycast)
1859{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001860 struct net *net = dev_net(idev->dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001861 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (rt == NULL)
1864 return ERR_PTR(-ENOMEM);
1865
Daniel Lezcano55786892008-03-04 13:47:47 -08001866 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 in6_dev_hold(idev);
1868
1869 rt->u.dst.flags = DST_HOST;
1870 rt->u.dst.input = ip6_input;
1871 rt->u.dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08001872 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rt->rt6i_idev = idev;
1874 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -08001875 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
1877 rt->u.dst.obsolete = -1;
1878
1879 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09001880 if (anycast)
1881 rt->rt6i_flags |= RTF_ANYCAST;
1882 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rt->rt6i_flags |= RTF_LOCAL;
1884 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1885 if (rt->rt6i_nexthop == NULL) {
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001886 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return ERR_PTR(-ENOMEM);
1888 }
1889
1890 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1891 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08001892 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 atomic_set(&rt->u.dst.__refcnt, 1);
1895
1896 return rt;
1897}
1898
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001899struct arg_dev_net {
1900 struct net_device *dev;
1901 struct net *net;
1902};
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904static int fib6_ifdown(struct rt6_info *rt, void *arg)
1905{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001906 struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
1907 struct net *net = ((struct arg_dev_net *)arg)->net;
1908
1909 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
1910 rt != net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 RT6_TRACE("deleted by ifdown %p\n", rt);
1912 return -1;
1913 }
1914 return 0;
1915}
1916
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001917void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001919 struct arg_dev_net adn = {
1920 .dev = dev,
1921 .net = net,
1922 };
1923
1924 fib6_clean_all(net, fib6_ifdown, 0, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927struct rt6_mtu_change_arg
1928{
1929 struct net_device *dev;
1930 unsigned mtu;
1931};
1932
1933static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
1934{
1935 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
1936 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001937 struct net *net = dev_net(arg->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* In IPv6 pmtu discovery is not optional,
1940 so that RTAX_MTU lock cannot disable it.
1941 We still use this lock to block changes
1942 caused by addrconf/ndisc.
1943 */
1944
1945 idev = __in6_dev_get(arg->dev);
1946 if (idev == NULL)
1947 return 0;
1948
1949 /* For administrative MTU increase, there is no way to discover
1950 IPv6 PMTU increase, so PMTU increase should be updated here.
1951 Since RFC 1981 doesn't include administrative MTU increase
1952 update PMTU increase is a MUST. (i.e. jumbo frame)
1953 */
1954 /*
1955 If new MTU is less than route PMTU, this new MTU will be the
1956 lowest MTU in the path, update the route PMTU to reflect PMTU
1957 decreases; if new MTU is greater than route PMTU, and the
1958 old MTU is the lowest MTU in the path, update the route PMTU
1959 to reflect the increase. In this case if the other nodes' MTU
1960 also have the lowest MTU, TOO BIG MESSAGE will be lead to
1961 PMTU discouvery.
1962 */
1963 if (rt->rt6i_dev == arg->dev &&
1964 !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
Jim Paris23717792008-01-31 16:36:25 -08001965 (dst_mtu(&rt->u.dst) >= arg->mtu ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001966 (dst_mtu(&rt->u.dst) < arg->mtu &&
Simon Arlott566cfd82007-07-26 00:09:55 -07001967 dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
Daniel Lezcano55786892008-03-04 13:47:47 -08001969 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return 0;
1972}
1973
1974void rt6_mtu_change(struct net_device *dev, unsigned mtu)
1975{
Thomas Grafc71099a2006-08-04 23:20:06 -07001976 struct rt6_mtu_change_arg arg = {
1977 .dev = dev,
1978 .mtu = mtu,
1979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001981 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Patrick McHardyef7c79e2007-06-05 12:38:30 -07001984static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07001985 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07001986 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07001987 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07001988 [RTA_PRIORITY] = { .type = NLA_U32 },
1989 [RTA_METRICS] = { .type = NLA_NESTED },
1990};
1991
1992static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1993 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Thomas Graf86872cb2006-08-22 00:01:08 -07001995 struct rtmsg *rtm;
1996 struct nlattr *tb[RTA_MAX+1];
1997 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Thomas Graf86872cb2006-08-22 00:01:08 -07001999 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2000 if (err < 0)
2001 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Thomas Graf86872cb2006-08-22 00:01:08 -07002003 err = -EINVAL;
2004 rtm = nlmsg_data(nlh);
2005 memset(cfg, 0, sizeof(*cfg));
2006
2007 cfg->fc_table = rtm->rtm_table;
2008 cfg->fc_dst_len = rtm->rtm_dst_len;
2009 cfg->fc_src_len = rtm->rtm_src_len;
2010 cfg->fc_flags = RTF_UP;
2011 cfg->fc_protocol = rtm->rtm_protocol;
2012
2013 if (rtm->rtm_type == RTN_UNREACHABLE)
2014 cfg->fc_flags |= RTF_REJECT;
2015
2016 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2017 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002018 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002019
2020 if (tb[RTA_GATEWAY]) {
2021 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2022 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002024
2025 if (tb[RTA_DST]) {
2026 int plen = (rtm->rtm_dst_len + 7) >> 3;
2027
2028 if (nla_len(tb[RTA_DST]) < plen)
2029 goto errout;
2030
2031 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002033
2034 if (tb[RTA_SRC]) {
2035 int plen = (rtm->rtm_src_len + 7) >> 3;
2036
2037 if (nla_len(tb[RTA_SRC]) < plen)
2038 goto errout;
2039
2040 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002042
2043 if (tb[RTA_OIF])
2044 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2045
2046 if (tb[RTA_PRIORITY])
2047 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2048
2049 if (tb[RTA_METRICS]) {
2050 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2051 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002053
2054 if (tb[RTA_TABLE])
2055 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2056
2057 err = 0;
2058errout:
2059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Thomas Grafc127ea22007-03-22 11:58:32 -07002062static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Thomas Graf86872cb2006-08-22 00:01:08 -07002064 struct fib6_config cfg;
2065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Thomas Graf86872cb2006-08-22 00:01:08 -07002067 err = rtm_to_fib6_config(skb, nlh, &cfg);
2068 if (err < 0)
2069 return err;
2070
2071 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Thomas Grafc127ea22007-03-22 11:58:32 -07002074static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Thomas Graf86872cb2006-08-22 00:01:08 -07002076 struct fib6_config cfg;
2077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Thomas Graf86872cb2006-08-22 00:01:08 -07002079 err = rtm_to_fib6_config(skb, nlh, &cfg);
2080 if (err < 0)
2081 return err;
2082
2083 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Thomas Graf339bf982006-11-10 14:10:15 -08002086static inline size_t rt6_nlmsg_size(void)
2087{
2088 return NLMSG_ALIGN(sizeof(struct rtmsg))
2089 + nla_total_size(16) /* RTA_SRC */
2090 + nla_total_size(16) /* RTA_DST */
2091 + nla_total_size(16) /* RTA_GATEWAY */
2092 + nla_total_size(16) /* RTA_PREFSRC */
2093 + nla_total_size(4) /* RTA_TABLE */
2094 + nla_total_size(4) /* RTA_IIF */
2095 + nla_total_size(4) /* RTA_OIF */
2096 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002097 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002098 + nla_total_size(sizeof(struct rta_cacheinfo));
2099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002102 struct in6_addr *dst, struct in6_addr *src,
2103 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002104 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002107 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002108 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002109 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 if (prefix) { /* user wants prefix routes only */
2112 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2113 /* success since this is not a prefix route */
2114 return 1;
2115 }
2116 }
2117
Thomas Graf2d7202b2006-08-22 00:01:27 -07002118 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2119 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002120 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002121
2122 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 rtm->rtm_family = AF_INET6;
2124 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2125 rtm->rtm_src_len = rt->rt6i_src.plen;
2126 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002127 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002128 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002129 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002130 table = RT6_TABLE_UNSPEC;
2131 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002132 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (rt->rt6i_flags&RTF_REJECT)
2134 rtm->rtm_type = RTN_UNREACHABLE;
2135 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2136 rtm->rtm_type = RTN_LOCAL;
2137 else
2138 rtm->rtm_type = RTN_UNICAST;
2139 rtm->rtm_flags = 0;
2140 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2141 rtm->rtm_protocol = rt->rt6i_protocol;
2142 if (rt->rt6i_flags&RTF_DYNAMIC)
2143 rtm->rtm_protocol = RTPROT_REDIRECT;
2144 else if (rt->rt6i_flags & RTF_ADDRCONF)
2145 rtm->rtm_protocol = RTPROT_KERNEL;
2146 else if (rt->rt6i_flags&RTF_DEFAULT)
2147 rtm->rtm_protocol = RTPROT_RA;
2148
2149 if (rt->rt6i_flags&RTF_CACHE)
2150 rtm->rtm_flags |= RTM_F_CLONED;
2151
2152 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002153 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002154 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002156 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157#ifdef CONFIG_IPV6_SUBTREES
2158 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002159 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002160 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002162 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002164 if (iif) {
2165#ifdef CONFIG_IPV6_MROUTE
2166 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2167 int err = ip6mr_get_route(skb, rtm, nowait);
2168 if (err <= 0) {
2169 if (!nowait) {
2170 if (err == 0)
2171 return 0;
2172 goto nla_put_failure;
2173 } else {
2174 if (err == -EMSGSIZE)
2175 goto nla_put_failure;
2176 }
2177 }
2178 } else
2179#endif
2180 NLA_PUT_U32(skb, RTA_IIF, iif);
2181 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct in6_addr saddr_buf;
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09002183 if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09002184 dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002185 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002189 goto nla_put_failure;
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (rt->u.dst.neighbour)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002192 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (rt->u.dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002195 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2196
2197 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002198
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002199 if (!(rt->rt6i_flags & RTF_EXPIRES))
2200 expires = 0;
2201 else if (rt->rt6i_expires - jiffies < INT_MAX)
2202 expires = rt->rt6i_expires - jiffies;
2203 else
2204 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002205
Thomas Grafe3703b32006-11-27 09:27:07 -08002206 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2207 expires, rt->u.dst.error) < 0)
2208 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Thomas Graf2d7202b2006-08-22 00:01:27 -07002210 return nlmsg_end(skb, nlh);
2211
2212nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002213 nlmsg_cancel(skb, nlh);
2214 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Patrick McHardy1b43af52006-08-10 23:11:17 -07002217int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2220 int prefix;
2221
Thomas Graf2d7202b2006-08-22 00:01:27 -07002222 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2223 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2225 } else
2226 prefix = 0;
2227
2228 return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2229 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002230 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Thomas Grafc127ea22007-03-22 11:58:32 -07002233static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002235 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002236 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002238 struct sk_buff *skb;
2239 struct rtmsg *rtm;
2240 struct flowi fl;
2241 int err, iif = 0;
2242
2243 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2244 if (err < 0)
2245 goto errout;
2246
2247 err = -EINVAL;
2248 memset(&fl, 0, sizeof(fl));
2249
2250 if (tb[RTA_SRC]) {
2251 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2252 goto errout;
2253
2254 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2255 }
2256
2257 if (tb[RTA_DST]) {
2258 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2259 goto errout;
2260
2261 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2262 }
2263
2264 if (tb[RTA_IIF])
2265 iif = nla_get_u32(tb[RTA_IIF]);
2266
2267 if (tb[RTA_OIF])
2268 fl.oif = nla_get_u32(tb[RTA_OIF]);
2269
2270 if (iif) {
2271 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002272 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002273 if (!dev) {
2274 err = -ENODEV;
2275 goto errout;
2276 }
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002280 if (skb == NULL) {
2281 err = -ENOBUFS;
2282 goto errout;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 /* Reserve room for dummy headers, this skb can pass
2286 through good chunk of routing engine.
2287 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002288 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2290
Daniel Lezcano8a3edd82008-03-07 11:14:16 -08002291 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 skb->dst = &rt->u.dst;
2293
Thomas Grafab364a62006-08-22 00:01:47 -07002294 err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002296 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002298 kfree_skb(skb);
2299 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
Daniel Lezcano55786892008-03-04 13:47:47 -08002302 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002303errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Thomas Graf86872cb2006-08-22 00:01:08 -07002307void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002310 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002311 u32 seq;
2312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002314 err = -ENOBUFS;
2315 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002316
Thomas Graf339bf982006-11-10 14:10:15 -08002317 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002318 if (skb == NULL)
2319 goto errout;
2320
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002321 err = rt6_fill_node(skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002322 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002323 if (err < 0) {
2324 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2325 WARN_ON(err == -EMSGSIZE);
2326 kfree_skb(skb);
2327 goto errout;
2328 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002329 err = rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2330 info->nlh, gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002331errout:
2332 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002333 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002336static int ip6_route_dev_notify(struct notifier_block *this,
2337 unsigned long event, void *data)
2338{
2339 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002340 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002341
2342 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2343 net->ipv6.ip6_null_entry->u.dst.dev = dev;
2344 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2345#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2346 net->ipv6.ip6_prohibit_entry->u.dst.dev = dev;
2347 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2348 net->ipv6.ip6_blk_hole_entry->u.dst.dev = dev;
2349 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2350#endif
2351 }
2352
2353 return NOTIFY_OK;
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356/*
2357 * /proc
2358 */
2359
2360#ifdef CONFIG_PROC_FS
2361
2362#define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
2363
2364struct rt6_proc_arg
2365{
2366 char *buffer;
2367 int offset;
2368 int length;
2369 int skip;
2370 int len;
2371};
2372
2373static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2374{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002375 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002377 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
2378 rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380#ifdef CONFIG_IPV6_SUBTREES
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002381 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
2382 rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002384 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385#endif
2386
2387 if (rt->rt6i_nexthop) {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002388 seq_printf(m, NIP6_SEQFMT,
2389 NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002391 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002393 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2394 rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
2395 rt->u.dst.__use, rt->rt6i_flags,
2396 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return 0;
2398}
2399
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002400static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002402 struct net *net = (struct net *)m->private;
2403 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002407static int ipv6_route_open(struct inode *inode, struct file *file)
2408{
Pavel Emelyanova2333522008-03-26 16:49:40 -07002409 int err;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002410 struct net *net = get_proc_net(inode);
2411 if (!net)
2412 return -ENXIO;
Pavel Emelyanova2333522008-03-26 16:49:40 -07002413
2414 err = single_open(file, ipv6_route_show, net);
2415 if (err < 0) {
2416 put_net(net);
2417 return err;
2418 }
2419
2420 return 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002421}
2422
2423static int ipv6_route_release(struct inode *inode, struct file *file)
2424{
2425 struct seq_file *seq = file->private_data;
2426 struct net *net = seq->private;
2427 put_net(net);
2428 return single_release(inode, file);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002429}
2430
2431static const struct file_operations ipv6_route_proc_fops = {
2432 .owner = THIS_MODULE,
2433 .open = ipv6_route_open,
2434 .read = seq_read,
2435 .llseek = seq_lseek,
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002436 .release = ipv6_route_release,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002437};
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2440{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002441 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002443 net->ipv6.rt6_stats->fib_nodes,
2444 net->ipv6.rt6_stats->fib_route_nodes,
2445 net->ipv6.rt6_stats->fib_rt_alloc,
2446 net->ipv6.rt6_stats->fib_rt_entries,
2447 net->ipv6.rt6_stats->fib_rt_cache,
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002448 atomic_read(&net->ipv6.ip6_dst_ops->entries),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002449 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 return 0;
2452}
2453
2454static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2455{
Pavel Emelyanova2333522008-03-26 16:49:40 -07002456 int err;
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002457 struct net *net = get_proc_net(inode);
Pavel Emelyanova2333522008-03-26 16:49:40 -07002458 if (!net)
2459 return -ENXIO;
2460
2461 err = single_open(file, rt6_stats_seq_show, net);
2462 if (err < 0) {
2463 put_net(net);
2464 return err;
2465 }
2466
2467 return 0;
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002468}
2469
2470static int rt6_stats_seq_release(struct inode *inode, struct file *file)
2471{
2472 struct seq_file *seq = file->private_data;
2473 struct net *net = (struct net *)seq->private;
2474 put_net(net);
2475 return single_release(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Arjan van de Ven9a321442007-02-12 00:55:35 -08002478static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .owner = THIS_MODULE,
2480 .open = rt6_stats_seq_open,
2481 .read = seq_read,
2482 .llseek = seq_lseek,
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002483 .release = rt6_stats_seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484};
2485#endif /* CONFIG_PROC_FS */
2486
2487#ifdef CONFIG_SYSCTL
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static
2490int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
2491 void __user *buffer, size_t *lenp, loff_t *ppos)
2492{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002493 struct net *net = current->nsproxy->net_ns;
2494 int delay = net->ipv6.sysctl.flush_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (write) {
2496 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002497 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return 0;
2499 } else
2500 return -EINVAL;
2501}
2502
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002503ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002504 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002506 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002508 .mode = 0200,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002509 .proc_handler = &ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 },
2511 {
2512 .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
2513 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002514 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 .maxlen = sizeof(int),
2516 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002517 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 },
2519 {
2520 .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
2521 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002522 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .maxlen = sizeof(int),
2524 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002525 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
2527 {
2528 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
2529 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002530 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 .maxlen = sizeof(int),
2532 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002533 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 .strategy = &sysctl_jiffies,
2535 },
2536 {
2537 .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
2538 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002539 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .maxlen = sizeof(int),
2541 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002542 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 .strategy = &sysctl_jiffies,
2544 },
2545 {
2546 .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
2547 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002548 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .maxlen = sizeof(int),
2550 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002551 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 .strategy = &sysctl_jiffies,
2553 },
2554 {
2555 .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
2556 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002557 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 .maxlen = sizeof(int),
2559 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002560 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 .strategy = &sysctl_jiffies,
2562 },
2563 {
2564 .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
2565 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002566 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 .maxlen = sizeof(int),
2568 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002569 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 .strategy = &sysctl_jiffies,
2571 },
2572 {
2573 .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
2574 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002575 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 .maxlen = sizeof(int),
2577 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002578 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .strategy = &sysctl_jiffies,
2580 },
2581 {
2582 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
2583 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002584 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 .maxlen = sizeof(int),
2586 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002587 .proc_handler = &proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 .strategy = &sysctl_ms_jiffies,
2589 },
2590 { .ctl_name = 0 }
2591};
2592
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002593struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2594{
2595 struct ctl_table *table;
2596
2597 table = kmemdup(ipv6_route_table_template,
2598 sizeof(ipv6_route_table_template),
2599 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002600
2601 if (table) {
2602 table[0].data = &net->ipv6.sysctl.flush_delay;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002603 table[1].data = &net->ipv6.ip6_dst_ops->gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002604 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2605 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2606 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2607 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2608 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2609 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2610 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2611 }
2612
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002613 return table;
2614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615#endif
2616
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002617static int ip6_route_net_init(struct net *net)
2618{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002619 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002620
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002621 net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
2622 sizeof(*net->ipv6.ip6_dst_ops),
2623 GFP_KERNEL);
2624 if (!net->ipv6.ip6_dst_ops)
2625 goto out;
Denis V. Lunev48115be2008-04-16 02:01:34 -07002626 net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002627
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002628 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2629 sizeof(*net->ipv6.ip6_null_entry),
2630 GFP_KERNEL);
2631 if (!net->ipv6.ip6_null_entry)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002632 goto out_ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002633 net->ipv6.ip6_null_entry->u.dst.path =
2634 (struct dst_entry *)net->ipv6.ip6_null_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002635 net->ipv6.ip6_null_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002636
2637#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2638 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2639 sizeof(*net->ipv6.ip6_prohibit_entry),
2640 GFP_KERNEL);
2641 if (!net->ipv6.ip6_prohibit_entry) {
2642 kfree(net->ipv6.ip6_null_entry);
2643 goto out;
2644 }
2645 net->ipv6.ip6_prohibit_entry->u.dst.path =
2646 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002647 net->ipv6.ip6_prohibit_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002648
2649 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2650 sizeof(*net->ipv6.ip6_blk_hole_entry),
2651 GFP_KERNEL);
2652 if (!net->ipv6.ip6_blk_hole_entry) {
2653 kfree(net->ipv6.ip6_null_entry);
2654 kfree(net->ipv6.ip6_prohibit_entry);
2655 goto out;
2656 }
2657 net->ipv6.ip6_blk_hole_entry->u.dst.path =
2658 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002659 net->ipv6.ip6_blk_hole_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002660#endif
2661
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002662#ifdef CONFIG_PROC_FS
2663 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2664 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2665#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002666 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2667
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002668 ret = 0;
2669out:
2670 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002671
2672out_ip6_dst_ops:
Denis V. Lunev48115be2008-04-16 02:01:34 -07002673 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002674 kfree(net->ipv6.ip6_dst_ops);
2675 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002676}
2677
2678static void ip6_route_net_exit(struct net *net)
2679{
2680#ifdef CONFIG_PROC_FS
2681 proc_net_remove(net, "ipv6_route");
2682 proc_net_remove(net, "rt6_stats");
2683#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002684 kfree(net->ipv6.ip6_null_entry);
2685#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2686 kfree(net->ipv6.ip6_prohibit_entry);
2687 kfree(net->ipv6.ip6_blk_hole_entry);
2688#endif
Denis V. Lunev48115be2008-04-16 02:01:34 -07002689 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002690 kfree(net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002691}
2692
2693static struct pernet_operations ip6_route_net_ops = {
2694 .init = ip6_route_net_init,
2695 .exit = ip6_route_net_exit,
2696};
2697
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002698static struct notifier_block ip6_route_dev_notifier = {
2699 .notifier_call = ip6_route_dev_notify,
2700 .priority = 0,
2701};
2702
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002703int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002705 int ret;
2706
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002707 ret = -ENOMEM;
2708 ip6_dst_ops_template.kmem_cachep =
2709 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2710 SLAB_HWCACHE_ALIGN, NULL);
2711 if (!ip6_dst_ops_template.kmem_cachep)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002712 goto out;;
David S. Miller14e50e52007-05-24 18:17:54 -07002713
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002714 ret = register_pernet_subsys(&ip6_route_net_ops);
2715 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002716 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002717
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002718 /* Registering of the loopback is done before this portion of code,
2719 * the loopback reference in rt6_info will not be taken, do it
2720 * manually for init_net */
2721 init_net.ipv6.ip6_null_entry->u.dst.dev = init_net.loopback_dev;
2722 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2723 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2724 init_net.ipv6.ip6_prohibit_entry->u.dst.dev = init_net.loopback_dev;
2725 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2726 init_net.ipv6.ip6_blk_hole_entry->u.dst.dev = init_net.loopback_dev;
2727 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2728 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002729 ret = fib6_init();
2730 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002731 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002732
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002733 ret = xfrm6_init();
2734 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002735 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002736
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002737 ret = fib6_rules_init();
2738 if (ret)
2739 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002740
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002741 ret = -ENOBUFS;
2742 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2743 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2744 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2745 goto fib6_rules_init;
2746
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002747 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002748 if (ret)
2749 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002750
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002751out:
2752 return ret;
2753
2754fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002755 fib6_rules_cleanup();
2756xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002757 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002758out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002759 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002760out_register_subsys:
2761 unregister_pernet_subsys(&ip6_route_net_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002762out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002763 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
2767void ip6_route_cleanup(void)
2768{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002769 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002770 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002773 unregister_pernet_subsys(&ip6_route_net_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002774 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}