blob: b7a4a875a26a366707cdb733d00f1d0a1905894a [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 Xu862b82c2007-11-13 21:43:11 -0800112 .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;
449 u32 lifetime;
450 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
Al Viroe69a4adc2006-11-14 20:56:00 -0800475 lifetime = ntohl(rinfo->lifetime);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800476 if (lifetime == 0xffffffff) {
477 /* infinity */
YOSHIFUJI Hideakia3264432008-05-19 16:54:29 -0700478 } else if (lifetime > 0x7fffffff/HZ - 1) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800479 /* Avoid arithmetic overflow */
480 lifetime = 0x7fffffff/HZ - 1;
481 }
482
483 if (rinfo->length == 3)
484 prefix = (struct in6_addr *)rinfo->prefix;
485 else {
486 /* this function is safe */
487 ipv6_addr_prefix(&prefix_buf,
488 (struct in6_addr *)rinfo->prefix,
489 rinfo->prefix_len);
490 prefix = &prefix_buf;
491 }
492
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800493 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
494 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800495
496 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700497 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800498 rt = NULL;
499 }
500
501 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800502 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800503 pref);
504 else if (rt)
505 rt->rt6i_flags = RTF_ROUTEINFO |
506 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
507
508 if (rt) {
509 if (lifetime == 0xffffffff) {
510 rt->rt6i_flags &= ~RTF_EXPIRES;
511 } else {
512 rt->rt6i_expires = jiffies + HZ * lifetime;
513 rt->rt6i_flags |= RTF_EXPIRES;
514 }
515 dst_release(&rt->u.dst);
516 }
517 return 0;
518}
519#endif
520
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800521#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700522do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800523 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700524 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700525 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700526 if (fn->fn_flags & RTN_TL_ROOT) \
527 goto out; \
528 pn = fn->parent; \
529 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800530 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700531 else \
532 fn = pn; \
533 if (fn->fn_flags & RTN_RTINFO) \
534 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700535 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700536 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700537} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700538
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800539static struct rt6_info *ip6_pol_route_lookup(struct net *net,
540 struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700541 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct fib6_node *fn;
544 struct rt6_info *rt;
545
Thomas Grafc71099a2006-08-04 23:20:06 -0700546 read_lock_bh(&table->tb6_lock);
547 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
548restart:
549 rt = fn->leaf;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800550 rt = rt6_device_match(net, rt, fl->oif, flags);
551 BACKTRACK(net, &fl->fl6_src);
Thomas Grafc71099a2006-08-04 23:20:06 -0700552out:
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800553 dst_use(&rt->u.dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700554 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700555 return rt;
556
557}
558
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900559struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
560 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700561{
562 struct flowi fl = {
563 .oif = oif,
564 .nl_u = {
565 .ip6_u = {
566 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700567 },
568 },
569 };
570 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700571 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700572
Thomas Grafadaa70b2006-10-13 15:01:03 -0700573 if (saddr) {
574 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
575 flags |= RT6_LOOKUP_F_HAS_SADDR;
576 }
577
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800578 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700579 if (dst->error == 0)
580 return (struct rt6_info *) dst;
581
582 dst_release(dst);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return NULL;
585}
586
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900587EXPORT_SYMBOL(rt6_lookup);
588
Thomas Grafc71099a2006-08-04 23:20:06 -0700589/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 It takes new route entry, the addition fails by any reason the
591 route is freed. In any case, if caller does not hold it, it may
592 be destroyed.
593 */
594
Thomas Graf86872cb2006-08-22 00:01:08 -0700595static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700598 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Thomas Grafc71099a2006-08-04 23:20:06 -0700600 table = rt->rt6i_table;
601 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700602 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700603 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 return err;
606}
607
Thomas Graf40e22e82006-08-22 00:00:45 -0700608int ip6_ins_rt(struct rt6_info *rt)
609{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800610 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900611 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800612 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800613 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700614}
615
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800616static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
617 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct rt6_info *rt;
620
621 /*
622 * Clone the route.
623 */
624
625 rt = ip6_rt_copy(ort);
626
627 if (rt) {
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900628 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
629 if (rt->rt6i_dst.plen != 128 &&
630 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
631 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900635 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 rt->rt6i_dst.plen = 128;
637 rt->rt6i_flags |= RTF_CACHE;
638 rt->u.dst.flags |= DST_HOST;
639
640#ifdef CONFIG_IPV6_SUBTREES
641 if (rt->rt6i_src.plen && saddr) {
642 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
643 rt->rt6i_src.plen = 128;
644 }
645#endif
646
647 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
648
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800651 return rt;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800654static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
655{
656 struct rt6_info *rt = ip6_rt_copy(ort);
657 if (rt) {
658 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
659 rt->rt6i_dst.plen = 128;
660 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800661 rt->u.dst.flags |= DST_HOST;
662 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
663 }
664 return rt;
665}
666
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800667static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
668 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800671 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700672 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800674 int err;
YOSHIFUJI Hideakiea659e02006-11-06 09:45:45 -0800675 int reachable = ipv6_devconf.forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700677 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700680 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800682restart_2:
Thomas Grafc71099a2006-08-04 23:20:06 -0700683 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700686 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800687
688 BACKTRACK(net, &fl->fl6_src);
689 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800690 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800693 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700694 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800695
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800696 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800697 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800698 else {
699#if CLONE_OFFLINK_ROUTE
700 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
701#else
702 goto out2;
703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800705
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800706 dst_release(&rt->u.dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800707 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800708
709 dst_hold(&rt->u.dst);
710 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700711 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800712 if (!err)
713 goto out2;
714 }
715
716 if (--attempts <= 0)
717 goto out2;
718
719 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700720 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800721 * released someone could insert this route. Relookup.
722 */
723 dst_release(&rt->u.dst);
724 goto relookup;
725
726out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800727 if (reachable) {
728 reachable = 0;
729 goto restart_2;
730 }
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800731 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700732 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733out2:
734 rt->u.dst.lastuse = jiffies;
735 rt->u.dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700736
737 return rt;
738}
739
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800740static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700741 struct flowi *fl, int flags)
742{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800743 return ip6_pol_route(net, table, fl->iif, fl, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700744}
745
Thomas Grafc71099a2006-08-04 23:20:06 -0700746void ip6_route_input(struct sk_buff *skb)
747{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700748 struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900749 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700750 int flags = RT6_LOOKUP_F_HAS_SADDR;
Thomas Grafc71099a2006-08-04 23:20:06 -0700751 struct flowi fl = {
752 .iif = skb->dev->ifindex,
753 .nl_u = {
754 .ip6_u = {
755 .daddr = iph->daddr,
756 .saddr = iph->saddr,
Al Viro90bcaf72006-11-08 00:25:17 -0800757 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
Thomas Grafc71099a2006-08-04 23:20:06 -0700758 },
759 },
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900760 .mark = skb->mark,
Thomas Grafc71099a2006-08-04 23:20:06 -0700761 .proto = iph->nexthdr,
762 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700763
764 if (rt6_need_strict(&iph->daddr))
765 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700766
Daniel Lezcano55786892008-03-04 13:47:47 -0800767 skb->dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input);
Thomas Grafc71099a2006-08-04 23:20:06 -0700768}
769
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800770static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700771 struct flowi *fl, int flags)
772{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800773 return ip6_pol_route(net, table, fl->oif, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700774}
775
Daniel Lezcano4591db42008-03-05 10:48:10 -0800776struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
777 struct flowi *fl)
Thomas Grafc71099a2006-08-04 23:20:06 -0700778{
779 int flags = 0;
780
781 if (rt6_need_strict(&fl->fl6_dst))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700782 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700783
Thomas Grafadaa70b2006-10-13 15:01:03 -0700784 if (!ipv6_addr_any(&fl->fl6_src))
785 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900786 else if (sk) {
787 unsigned int prefs = inet6_sk(sk)->srcprefs;
788 if (prefs & IPV6_PREFER_SRC_TMP)
789 flags |= RT6_LOOKUP_F_SRCPREF_TMP;
790 if (prefs & IPV6_PREFER_SRC_PUBLIC)
791 flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
792 if (prefs & IPV6_PREFER_SRC_COA)
793 flags |= RT6_LOOKUP_F_SRCPREF_COA;
794 }
Thomas Grafadaa70b2006-10-13 15:01:03 -0700795
Daniel Lezcano4591db42008-03-05 10:48:10 -0800796 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900799EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David S. Miller14e50e52007-05-24 18:17:54 -0700801int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
802{
803 struct rt6_info *ort = (struct rt6_info *) *dstp;
804 struct rt6_info *rt = (struct rt6_info *)
805 dst_alloc(&ip6_dst_blackhole_ops);
806 struct dst_entry *new = NULL;
807
808 if (rt) {
809 new = &rt->u.dst;
810
811 atomic_set(&new->__refcnt, 1);
812 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800813 new->input = dst_discard;
814 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700815
816 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
817 new->dev = ort->u.dst.dev;
818 if (new->dev)
819 dev_hold(new->dev);
820 rt->rt6i_idev = ort->rt6i_idev;
821 if (rt->rt6i_idev)
822 in6_dev_hold(rt->rt6i_idev);
823 rt->rt6i_expires = 0;
824
825 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
826 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
827 rt->rt6i_metric = 0;
828
829 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
830#ifdef CONFIG_IPV6_SUBTREES
831 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
832#endif
833
834 dst_free(new);
835 }
836
837 dst_release(*dstp);
838 *dstp = new;
839 return (new ? 0 : -ENOMEM);
840}
841EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*
844 * Destination cache support functions
845 */
846
847static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
848{
849 struct rt6_info *rt;
850
851 rt = (struct rt6_info *) dst;
852
853 if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
854 return dst;
855
856 return NULL;
857}
858
859static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
860{
861 struct rt6_info *rt = (struct rt6_info *) dst;
862
863 if (rt) {
864 if (rt->rt6i_flags & RTF_CACHE)
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700865 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 else
867 dst_release(dst);
868 }
869 return NULL;
870}
871
872static void ip6_link_failure(struct sk_buff *skb)
873{
874 struct rt6_info *rt;
875
876 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
877
878 rt = (struct rt6_info *) skb->dst;
879 if (rt) {
880 if (rt->rt6i_flags&RTF_CACHE) {
881 dst_set_expires(&rt->u.dst, 0);
882 rt->rt6i_flags |= RTF_EXPIRES;
883 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
884 rt->rt6i_node->fn_sernum = -1;
885 }
886}
887
888static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
889{
890 struct rt6_info *rt6 = (struct rt6_info*)dst;
891
892 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
893 rt6->rt6i_flags |= RTF_MODIFIED;
894 if (mtu < IPV6_MIN_MTU) {
895 mtu = IPV6_MIN_MTU;
896 dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
897 }
898 dst->metrics[RTAX_MTU-1] = mtu;
Tom Tucker8d717402006-07-30 20:43:36 -0700899 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903static int ipv6_get_mtu(struct net_device *dev);
904
Daniel Lezcano55786892008-03-04 13:47:47 -0800905static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
908
Daniel Lezcano55786892008-03-04 13:47:47 -0800909 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
910 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900913 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
914 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
915 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * rely only on pmtu discovery"
917 */
918 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
919 mtu = IPV6_MAXPLEN;
920 return mtu;
921}
922
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800923static struct dst_entry *icmp6_dst_gc_list;
924static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700925
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800926struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900928 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct rt6_info *rt;
931 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900932 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (unlikely(idev == NULL))
935 return NULL;
936
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800937 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (unlikely(rt == NULL)) {
939 in6_dev_put(idev);
940 goto out;
941 }
942
943 dev_hold(dev);
944 if (neigh)
945 neigh_hold(neigh);
946 else
947 neigh = ndisc_get_neigh(dev, addr);
948
949 rt->rt6i_dev = dev;
950 rt->rt6i_idev = idev;
951 rt->rt6i_nexthop = neigh;
952 atomic_set(&rt->u.dst.__refcnt, 1);
953 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
954 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -0800955 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800956 rt->u.dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958#if 0 /* there's no chance to use these for ndisc */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900959 rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
960 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 : 0;
962 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
963 rt->rt6i_dst.plen = 128;
964#endif
965
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800966 spin_lock_bh(&icmp6_dst_lock);
967 rt->u.dst.next = icmp6_dst_gc_list;
968 icmp6_dst_gc_list = &rt->u.dst;
969 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Daniel Lezcano55786892008-03-04 13:47:47 -0800971 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973out:
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +0900974 return &rt->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800977int icmp6_dst_gc(int *more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct dst_entry *dst, *next, **pprev;
980 int freed;
981
982 next = NULL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900983 freed = 0;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700984
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800985 spin_lock_bh(&icmp6_dst_lock);
986 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 while ((dst = *pprev) != NULL) {
989 if (!atomic_read(&dst->__refcnt)) {
990 *pprev = dst->next;
991 dst_free(dst);
992 freed++;
993 } else {
994 pprev = &dst->next;
995 (*more)++;
996 }
997 }
998
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800999 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return freed;
1002}
1003
Daniel Lezcano569d3642008-01-18 03:56:57 -08001004static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long now = jiffies;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001007 struct net *net = ops->dst_net;
1008 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1009 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1010 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1011 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1012 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Daniel Lezcano7019b782008-03-04 13:50:14 -08001014 if (time_after(rt_last_gc + rt_min_interval, now) &&
1015 atomic_read(&ops->entries) <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017
Benjamin Thery6891a342008-03-04 13:49:47 -08001018 net->ipv6.ip6_rt_gc_expire++;
1019 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1020 net->ipv6.ip6_rt_last_gc = now;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001021 if (atomic_read(&ops->entries) < ops->gc_thresh)
1022 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001024 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1025 return (atomic_read(&ops->entries) > rt_max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028/* Clean host part of a prefix. Not necessary in radix tree,
1029 but results in cleaner routing tables.
1030
1031 Remove it only when all the things will work!
1032 */
1033
1034static int ipv6_get_mtu(struct net_device *dev)
1035{
1036 int mtu = IPV6_MIN_MTU;
1037 struct inet6_dev *idev;
1038
1039 idev = in6_dev_get(dev);
1040 if (idev) {
1041 mtu = idev->cnf.mtu6;
1042 in6_dev_put(idev);
1043 }
1044 return mtu;
1045}
1046
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001047int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001049 int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
1050 if (hoplimit < 0) {
1051 struct net_device *dev = dst->dev;
1052 struct inet6_dev *idev = in6_dev_get(dev);
1053 if (idev) {
1054 hoplimit = idev->cnf.hop_limit;
1055 in6_dev_put(idev);
1056 } else
1057 hoplimit = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return hoplimit;
1060}
1061
1062/*
1063 *
1064 */
1065
Thomas Graf86872cb2006-08-22 00:01:08 -07001066int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001069 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct rt6_info *rt = NULL;
1071 struct net_device *dev = NULL;
1072 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001073 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 int addr_type;
1075
Thomas Graf86872cb2006-08-22 00:01:08 -07001076 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
1078#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001079 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -EINVAL;
1081#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001082 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001084 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (!dev)
1086 goto out;
1087 idev = in6_dev_get(dev);
1088 if (!idev)
1089 goto out;
1090 }
1091
Thomas Graf86872cb2006-08-22 00:01:08 -07001092 if (cfg->fc_metric == 0)
1093 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Daniel Lezcano55786892008-03-04 13:47:47 -08001095 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001096 if (table == NULL) {
1097 err = -ENOBUFS;
1098 goto out;
1099 }
1100
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001101 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (rt == NULL) {
1104 err = -ENOMEM;
1105 goto out;
1106 }
1107
1108 rt->u.dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001109 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1110 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1111 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Thomas Graf86872cb2006-08-22 00:01:08 -07001113 if (cfg->fc_protocol == RTPROT_UNSPEC)
1114 cfg->fc_protocol = RTPROT_BOOT;
1115 rt->rt6i_protocol = cfg->fc_protocol;
1116
1117 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (addr_type & IPV6_ADDR_MULTICAST)
1120 rt->u.dst.input = ip6_mc_input;
1121 else
1122 rt->u.dst.input = ip6_forward;
1123
1124 rt->u.dst.output = ip6_output;
1125
Thomas Graf86872cb2006-08-22 00:01:08 -07001126 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1127 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (rt->rt6i_dst.plen == 128)
1129 rt->u.dst.flags = DST_HOST;
1130
1131#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001132 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1133 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#endif
1135
Thomas Graf86872cb2006-08-22 00:01:08 -07001136 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* We cannot add true routes via loopback here,
1139 they would result in kernel looping; promote them to reject routes
1140 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001141 if ((cfg->fc_flags & RTF_REJECT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
1143 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001144 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (dev) {
1146 dev_put(dev);
1147 in6_dev_put(idev);
1148 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001149 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 dev_hold(dev);
1151 idev = in6_dev_get(dev);
1152 if (!idev) {
1153 err = -ENODEV;
1154 goto out;
1155 }
1156 }
1157 rt->u.dst.output = ip6_pkt_discard_out;
1158 rt->u.dst.input = ip6_pkt_discard;
1159 rt->u.dst.error = -ENETUNREACH;
1160 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1161 goto install_route;
1162 }
1163
Thomas Graf86872cb2006-08-22 00:01:08 -07001164 if (cfg->fc_flags & RTF_GATEWAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct in6_addr *gw_addr;
1166 int gwa_type;
1167
Thomas Graf86872cb2006-08-22 00:01:08 -07001168 gw_addr = &cfg->fc_gateway;
1169 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 gwa_type = ipv6_addr_type(gw_addr);
1171
1172 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1173 struct rt6_info *grt;
1174
1175 /* IPv6 strictly inhibits using not link-local
1176 addresses as nexthop address.
1177 Otherwise, router will not able to send redirects.
1178 It is very good, but in some (rare!) circumstances
1179 (SIT, PtP, NBMA NOARP links) it is handy to allow
1180 some exceptions. --ANK
1181 */
1182 err = -EINVAL;
1183 if (!(gwa_type&IPV6_ADDR_UNICAST))
1184 goto out;
1185
Daniel Lezcano55786892008-03-04 13:47:47 -08001186 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 err = -EHOSTUNREACH;
1189 if (grt == NULL)
1190 goto out;
1191 if (dev) {
1192 if (dev != grt->rt6i_dev) {
1193 dst_release(&grt->u.dst);
1194 goto out;
1195 }
1196 } else {
1197 dev = grt->rt6i_dev;
1198 idev = grt->rt6i_idev;
1199 dev_hold(dev);
1200 in6_dev_hold(grt->rt6i_idev);
1201 }
1202 if (!(grt->rt6i_flags&RTF_GATEWAY))
1203 err = 0;
1204 dst_release(&grt->u.dst);
1205
1206 if (err)
1207 goto out;
1208 }
1209 err = -EINVAL;
1210 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1211 goto out;
1212 }
1213
1214 err = -ENODEV;
1215 if (dev == NULL)
1216 goto out;
1217
Thomas Graf86872cb2006-08-22 00:01:08 -07001218 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1220 if (IS_ERR(rt->rt6i_nexthop)) {
1221 err = PTR_ERR(rt->rt6i_nexthop);
1222 rt->rt6i_nexthop = NULL;
1223 goto out;
1224 }
1225 }
1226
Thomas Graf86872cb2006-08-22 00:01:08 -07001227 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001230 if (cfg->fc_mx) {
1231 struct nlattr *nla;
1232 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Thomas Graf86872cb2006-08-22 00:01:08 -07001234 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001235 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001236
1237 if (type) {
1238 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 err = -EINVAL;
1240 goto out;
1241 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001242
1243 rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 }
1247
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001248 if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001250 if (!dst_metric(&rt->u.dst, RTAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001252 if (!dst_metric(&rt->u.dst, RTAX_ADVMSS))
Daniel Lezcano55786892008-03-04 13:47:47 -08001253 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 rt->u.dst.dev = dev;
1255 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001256 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001257
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001258 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001259
Thomas Graf86872cb2006-08-22 00:01:08 -07001260 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262out:
1263 if (dev)
1264 dev_put(dev);
1265 if (idev)
1266 in6_dev_put(idev);
1267 if (rt)
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001268 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return err;
1270}
1271
Thomas Graf86872cb2006-08-22 00:01:08 -07001272static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001275 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001276 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001278 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001279 return -ENOENT;
1280
Thomas Grafc71099a2006-08-04 23:20:06 -07001281 table = rt->rt6i_table;
1282 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Thomas Graf86872cb2006-08-22 00:01:08 -07001284 err = fib6_del(rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 dst_release(&rt->u.dst);
1286
Thomas Grafc71099a2006-08-04 23:20:06 -07001287 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 return err;
1290}
1291
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001292int ip6_del_rt(struct rt6_info *rt)
1293{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001294 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001295 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001296 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001297 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001298}
1299
Thomas Graf86872cb2006-08-22 00:01:08 -07001300static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Thomas Grafc71099a2006-08-04 23:20:06 -07001302 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct fib6_node *fn;
1304 struct rt6_info *rt;
1305 int err = -ESRCH;
1306
Daniel Lezcano55786892008-03-04 13:47:47 -08001307 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001308 if (table == NULL)
1309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Thomas Grafc71099a2006-08-04 23:20:06 -07001311 read_lock_bh(&table->tb6_lock);
1312
1313 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001314 &cfg->fc_dst, cfg->fc_dst_len,
1315 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (fn) {
Eric Dumazet7cc48262007-02-09 16:22:57 -08001318 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001319 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001321 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001323 if (cfg->fc_flags & RTF_GATEWAY &&
1324 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001326 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 continue;
1328 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001329 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Thomas Graf86872cb2006-08-22 00:01:08 -07001331 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001334 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 return err;
1337}
1338
1339/*
1340 * Handle redirects
1341 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001342struct ip6rd_flowi {
1343 struct flowi fl;
1344 struct in6_addr gateway;
1345};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001347static struct rt6_info *__ip6_route_redirect(struct net *net,
1348 struct fib6_table *table,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001349 struct flowi *fl,
1350 int flags)
1351{
1352 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1353 struct rt6_info *rt;
1354 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001357 * Get the "current" route for this destination and
1358 * check if the redirect has come from approriate router.
1359 *
1360 * RFC 2461 specifies that redirects should only be
1361 * accepted if they come from the nexthop to the target.
1362 * Due to the way the routes are chosen, this notion
1363 * is a bit fuzzy and one might need to check all possible
1364 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Thomas Grafc71099a2006-08-04 23:20:06 -07001367 read_lock_bh(&table->tb6_lock);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001368 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001369restart:
Eric Dumazet7cc48262007-02-09 16:22:57 -08001370 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001371 /*
1372 * Current route is on-link; redirect is always invalid.
1373 *
1374 * Seems, previous statement is not true. It could
1375 * be node, which looks for us as on-link (f.e. proxy ndisc)
1376 * But then router serving it might decide, that we should
1377 * know truth 8)8) --ANK (980726).
1378 */
1379 if (rt6_check_expired(rt))
1380 continue;
1381 if (!(rt->rt6i_flags & RTF_GATEWAY))
1382 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001383 if (fl->oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001384 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001385 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001386 continue;
1387 break;
1388 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001389
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001390 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001391 rt = net->ipv6.ip6_null_entry;
1392 BACKTRACK(net, &fl->fl6_src);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001393out:
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001394 dst_hold(&rt->u.dst);
1395
1396 read_unlock_bh(&table->tb6_lock);
1397
1398 return rt;
1399};
1400
1401static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1402 struct in6_addr *src,
1403 struct in6_addr *gateway,
1404 struct net_device *dev)
1405{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001406 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001407 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001408 struct ip6rd_flowi rdfl = {
1409 .fl = {
1410 .oif = dev->ifindex,
1411 .nl_u = {
1412 .ip6_u = {
1413 .daddr = *dest,
1414 .saddr = *src,
1415 },
1416 },
1417 },
1418 .gateway = *gateway,
1419 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001420
1421 if (rt6_need_strict(dest))
1422 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001423
Daniel Lezcano55786892008-03-04 13:47:47 -08001424 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001425 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001426}
1427
1428void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1429 struct in6_addr *saddr,
1430 struct neighbour *neigh, u8 *lladdr, int on_link)
1431{
1432 struct rt6_info *rt, *nrt = NULL;
1433 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001434 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001435
1436 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1437
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001438 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (net_ratelimit())
1440 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1441 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
1446 * We have finally decided to accept it.
1447 */
1448
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001449 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1451 NEIGH_UPDATE_F_OVERRIDE|
1452 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1453 NEIGH_UPDATE_F_ISROUTER))
1454 );
1455
1456 /*
1457 * Redirect received -> path was valid.
1458 * Look, redirects are sent only in response to data packets,
1459 * so that this nexthop apparently is reachable. --ANK
1460 */
1461 dst_confirm(&rt->u.dst);
1462
1463 /* Duplicate redirect: silently ignore. */
1464 if (neigh == rt->u.dst.neighbour)
1465 goto out;
1466
1467 nrt = ip6_rt_copy(rt);
1468 if (nrt == NULL)
1469 goto out;
1470
1471 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1472 if (on_link)
1473 nrt->rt6i_flags &= ~RTF_GATEWAY;
1474
1475 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1476 nrt->rt6i_dst.plen = 128;
1477 nrt->u.dst.flags |= DST_HOST;
1478
1479 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1480 nrt->rt6i_nexthop = neigh_clone(neigh);
1481 /* Reset pmtu, it may be better */
1482 nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001483 nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
Daniel Lezcano55786892008-03-04 13:47:47 -08001484 dst_mtu(&nrt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Thomas Graf40e22e82006-08-22 00:00:45 -07001486 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 goto out;
1488
Tom Tucker8d717402006-07-30 20:43:36 -07001489 netevent.old = &rt->u.dst;
1490 netevent.new = &nrt->u.dst;
1491 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001494 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return;
1496 }
1497
1498out:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001499 dst_release(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return;
1501}
1502
1503/*
1504 * Handle ICMP "packet too big" messages
1505 * i.e. Path MTU discovery
1506 */
1507
1508void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1509 struct net_device *dev, u32 pmtu)
1510{
1511 struct rt6_info *rt, *nrt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001512 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int allfrag = 0;
1514
Daniel Lezcano55786892008-03-04 13:47:47 -08001515 rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (rt == NULL)
1517 return;
1518
1519 if (pmtu >= dst_mtu(&rt->u.dst))
1520 goto out;
1521
1522 if (pmtu < IPV6_MIN_MTU) {
1523 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001524 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * MTU (1280) and a fragment header should always be included
1526 * after a node receiving Too Big message reporting PMTU is
1527 * less than the IPv6 Minimum Link MTU.
1528 */
1529 pmtu = IPV6_MIN_MTU;
1530 allfrag = 1;
1531 }
1532
1533 /* New mtu received -> path was valid.
1534 They are sent only in response to data packets,
1535 so that this nexthop apparently is reachable. --ANK
1536 */
1537 dst_confirm(&rt->u.dst);
1538
1539 /* Host route. If it is static, it would be better
1540 not to override it, but add new one, so that
1541 when cache entry will expire old pmtu
1542 would return automatically.
1543 */
1544 if (rt->rt6i_flags & RTF_CACHE) {
1545 rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1546 if (allfrag)
1547 rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
Daniel Lezcano55786892008-03-04 13:47:47 -08001548 dst_set_expires(&rt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1550 goto out;
1551 }
1552
1553 /* Network route.
1554 Two cases are possible:
1555 1. It is connected route. Action: COW
1556 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1557 */
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001558 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001559 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001560 else
1561 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001562
YOSHIFUJI Hideakid5315b52006-03-20 16:58:48 -08001563 if (nrt) {
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001564 nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1565 if (allfrag)
1566 nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1567
1568 /* According to RFC 1981, detecting PMTU increase shouldn't be
1569 * happened within 5 mins, the recommended timer is 10 mins.
1570 * Here this route expiration time is set to ip6_rt_mtu_expires
1571 * which is 10 mins. After 10 mins the decreased pmtu is expired
1572 * and detecting PMTU increase will be automatically happened.
1573 */
Daniel Lezcano55786892008-03-04 13:47:47 -08001574 dst_set_expires(&nrt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001575 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1576
Thomas Graf40e22e82006-08-22 00:00:45 -07001577 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out:
1580 dst_release(&rt->u.dst);
1581}
1582
1583/*
1584 * Misc support functions
1585 */
1586
1587static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1588{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001589 struct net *net = dev_net(ort->rt6i_dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001590 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (rt) {
1593 rt->u.dst.input = ort->u.dst.input;
1594 rt->u.dst.output = ort->u.dst.output;
1595
1596 memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
Ville Nuorvala22e1e4d2006-10-16 22:14:26 -07001597 rt->u.dst.error = ort->u.dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 rt->u.dst.dev = ort->u.dst.dev;
1599 if (rt->u.dst.dev)
1600 dev_hold(rt->u.dst.dev);
1601 rt->rt6i_idev = ort->rt6i_idev;
1602 if (rt->rt6i_idev)
1603 in6_dev_hold(rt->rt6i_idev);
1604 rt->u.dst.lastuse = jiffies;
1605 rt->rt6i_expires = 0;
1606
1607 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1608 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1609 rt->rt6i_metric = 0;
1610
1611 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1612#ifdef CONFIG_IPV6_SUBTREES
1613 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1614#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001615 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 return rt;
1618}
1619
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001620#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001621static struct rt6_info *rt6_get_route_info(struct net *net,
1622 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001623 struct in6_addr *gwaddr, int ifindex)
1624{
1625 struct fib6_node *fn;
1626 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001627 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001628
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001629 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001630 if (table == NULL)
1631 return NULL;
1632
1633 write_lock_bh(&table->tb6_lock);
1634 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001635 if (!fn)
1636 goto out;
1637
Eric Dumazet7cc48262007-02-09 16:22:57 -08001638 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001639 if (rt->rt6i_dev->ifindex != ifindex)
1640 continue;
1641 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1642 continue;
1643 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1644 continue;
1645 dst_hold(&rt->u.dst);
1646 break;
1647 }
1648out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001649 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001650 return rt;
1651}
1652
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001653static struct rt6_info *rt6_add_route_info(struct net *net,
1654 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001655 struct in6_addr *gwaddr, int ifindex,
1656 unsigned pref)
1657{
Thomas Graf86872cb2006-08-22 00:01:08 -07001658 struct fib6_config cfg = {
1659 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001660 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001661 .fc_ifindex = ifindex,
1662 .fc_dst_len = prefixlen,
1663 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1664 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001665 .fc_nlinfo.pid = 0,
1666 .fc_nlinfo.nlh = NULL,
1667 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001668 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001669
Thomas Graf86872cb2006-08-22 00:01:08 -07001670 ipv6_addr_copy(&cfg.fc_dst, prefix);
1671 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1672
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001673 /* We should treat it as a default route if prefix length is 0. */
1674 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001675 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001676
Thomas Graf86872cb2006-08-22 00:01:08 -07001677 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001678
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001679 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001680}
1681#endif
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001686 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001688 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001689 if (table == NULL)
1690 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Thomas Grafc71099a2006-08-04 23:20:06 -07001692 write_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001693 for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001695 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1697 break;
1698 }
1699 if (rt)
1700 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001701 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return rt;
1703}
1704
1705struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001706 struct net_device *dev,
1707 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Thomas Graf86872cb2006-08-22 00:01:08 -07001709 struct fib6_config cfg = {
1710 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001711 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001712 .fc_ifindex = dev->ifindex,
1713 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1714 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001715 .fc_nlinfo.pid = 0,
1716 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001717 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Thomas Graf86872cb2006-08-22 00:01:08 -07001720 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Thomas Graf86872cb2006-08-22 00:01:08 -07001722 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return rt6_get_dflt_router(gwaddr, dev);
1725}
1726
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001727void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001730 struct fib6_table *table;
1731
1732 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001733 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001734 if (table == NULL)
1735 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001738 read_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001739 for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1741 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001742 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001743 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto restart;
1745 }
1746 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001747 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Daniel Lezcano55786892008-03-04 13:47:47 -08001750static void rtmsg_to_fib6_config(struct net *net,
1751 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001752 struct fib6_config *cfg)
1753{
1754 memset(cfg, 0, sizeof(*cfg));
1755
1756 cfg->fc_table = RT6_TABLE_MAIN;
1757 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1758 cfg->fc_metric = rtmsg->rtmsg_metric;
1759 cfg->fc_expires = rtmsg->rtmsg_info;
1760 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1761 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1762 cfg->fc_flags = rtmsg->rtmsg_flags;
1763
Daniel Lezcano55786892008-03-04 13:47:47 -08001764 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001765
Thomas Graf86872cb2006-08-22 00:01:08 -07001766 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1767 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1768 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1769}
1770
Daniel Lezcano55786892008-03-04 13:47:47 -08001771int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Thomas Graf86872cb2006-08-22 00:01:08 -07001773 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct in6_rtmsg rtmsg;
1775 int err;
1776
1777 switch(cmd) {
1778 case SIOCADDRT: /* Add a route */
1779 case SIOCDELRT: /* Delete a route */
1780 if (!capable(CAP_NET_ADMIN))
1781 return -EPERM;
1782 err = copy_from_user(&rtmsg, arg,
1783 sizeof(struct in6_rtmsg));
1784 if (err)
1785 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001786
Daniel Lezcano55786892008-03-04 13:47:47 -08001787 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 rtnl_lock();
1790 switch (cmd) {
1791 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001792 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 break;
1794 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001795 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 break;
1797 default:
1798 err = -EINVAL;
1799 }
1800 rtnl_unlock();
1801
1802 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 return -EINVAL;
1806}
1807
1808/*
1809 * Drop the packet on the floor
1810 */
1811
Ilpo Järvinen50eb4312008-01-12 03:21:00 -08001812static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001814 int type;
1815 switch (ipstats_mib_noroutes) {
1816 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001817 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001818 if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
1819 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INADDRERRORS);
1820 break;
1821 }
1822 /* FALLTHROUGH */
1823 case IPSTATS_MIB_OUTNOROUTES:
1824 IP6_INC_STATS(ip6_dst_idev(skb->dst), ipstats_mib_noroutes);
1825 break;
1826 }
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001827 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 kfree_skb(skb);
1829 return 0;
1830}
1831
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001832static int ip6_pkt_discard(struct sk_buff *skb)
1833{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001834 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001835}
1836
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001837static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001840 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
David S. Miller6723ab52006-10-18 21:20:57 -07001843#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1844
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001845static int ip6_pkt_prohibit(struct sk_buff *skb)
1846{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001847 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001848}
1849
1850static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1851{
1852 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001853 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001854}
1855
David S. Miller6723ab52006-10-18 21:20:57 -07001856#endif
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * Allocate a dst for local (unicast / anycast) address.
1860 */
1861
1862struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1863 const struct in6_addr *addr,
1864 int anycast)
1865{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001866 struct net *net = dev_net(idev->dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001867 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 if (rt == NULL)
1870 return ERR_PTR(-ENOMEM);
1871
Daniel Lezcano55786892008-03-04 13:47:47 -08001872 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 in6_dev_hold(idev);
1874
1875 rt->u.dst.flags = DST_HOST;
1876 rt->u.dst.input = ip6_input;
1877 rt->u.dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08001878 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rt->rt6i_idev = idev;
1880 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -08001881 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
1883 rt->u.dst.obsolete = -1;
1884
1885 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09001886 if (anycast)
1887 rt->rt6i_flags |= RTF_ANYCAST;
1888 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rt->rt6i_flags |= RTF_LOCAL;
1890 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1891 if (rt->rt6i_nexthop == NULL) {
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001892 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return ERR_PTR(-ENOMEM);
1894 }
1895
1896 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1897 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08001898 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 atomic_set(&rt->u.dst.__refcnt, 1);
1901
1902 return rt;
1903}
1904
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001905struct arg_dev_net {
1906 struct net_device *dev;
1907 struct net *net;
1908};
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910static int fib6_ifdown(struct rt6_info *rt, void *arg)
1911{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001912 struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
1913 struct net *net = ((struct arg_dev_net *)arg)->net;
1914
1915 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
1916 rt != net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 RT6_TRACE("deleted by ifdown %p\n", rt);
1918 return -1;
1919 }
1920 return 0;
1921}
1922
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001923void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001925 struct arg_dev_net adn = {
1926 .dev = dev,
1927 .net = net,
1928 };
1929
1930 fib6_clean_all(net, fib6_ifdown, 0, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933struct rt6_mtu_change_arg
1934{
1935 struct net_device *dev;
1936 unsigned mtu;
1937};
1938
1939static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
1940{
1941 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
1942 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001943 struct net *net = dev_net(arg->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 /* In IPv6 pmtu discovery is not optional,
1946 so that RTAX_MTU lock cannot disable it.
1947 We still use this lock to block changes
1948 caused by addrconf/ndisc.
1949 */
1950
1951 idev = __in6_dev_get(arg->dev);
1952 if (idev == NULL)
1953 return 0;
1954
1955 /* For administrative MTU increase, there is no way to discover
1956 IPv6 PMTU increase, so PMTU increase should be updated here.
1957 Since RFC 1981 doesn't include administrative MTU increase
1958 update PMTU increase is a MUST. (i.e. jumbo frame)
1959 */
1960 /*
1961 If new MTU is less than route PMTU, this new MTU will be the
1962 lowest MTU in the path, update the route PMTU to reflect PMTU
1963 decreases; if new MTU is greater than route PMTU, and the
1964 old MTU is the lowest MTU in the path, update the route PMTU
1965 to reflect the increase. In this case if the other nodes' MTU
1966 also have the lowest MTU, TOO BIG MESSAGE will be lead to
1967 PMTU discouvery.
1968 */
1969 if (rt->rt6i_dev == arg->dev &&
1970 !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
Jim Paris23717792008-01-31 16:36:25 -08001971 (dst_mtu(&rt->u.dst) >= arg->mtu ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001972 (dst_mtu(&rt->u.dst) < arg->mtu &&
Simon Arlott566cfd82007-07-26 00:09:55 -07001973 dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
Daniel Lezcano55786892008-03-04 13:47:47 -08001975 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978}
1979
1980void rt6_mtu_change(struct net_device *dev, unsigned mtu)
1981{
Thomas Grafc71099a2006-08-04 23:20:06 -07001982 struct rt6_mtu_change_arg arg = {
1983 .dev = dev,
1984 .mtu = mtu,
1985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001987 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
Patrick McHardyef7c79e2007-06-05 12:38:30 -07001990static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07001991 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07001992 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07001993 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07001994 [RTA_PRIORITY] = { .type = NLA_U32 },
1995 [RTA_METRICS] = { .type = NLA_NESTED },
1996};
1997
1998static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1999 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Thomas Graf86872cb2006-08-22 00:01:08 -07002001 struct rtmsg *rtm;
2002 struct nlattr *tb[RTA_MAX+1];
2003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Thomas Graf86872cb2006-08-22 00:01:08 -07002005 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2006 if (err < 0)
2007 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Thomas Graf86872cb2006-08-22 00:01:08 -07002009 err = -EINVAL;
2010 rtm = nlmsg_data(nlh);
2011 memset(cfg, 0, sizeof(*cfg));
2012
2013 cfg->fc_table = rtm->rtm_table;
2014 cfg->fc_dst_len = rtm->rtm_dst_len;
2015 cfg->fc_src_len = rtm->rtm_src_len;
2016 cfg->fc_flags = RTF_UP;
2017 cfg->fc_protocol = rtm->rtm_protocol;
2018
2019 if (rtm->rtm_type == RTN_UNREACHABLE)
2020 cfg->fc_flags |= RTF_REJECT;
2021
2022 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2023 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002024 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002025
2026 if (tb[RTA_GATEWAY]) {
2027 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2028 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002030
2031 if (tb[RTA_DST]) {
2032 int plen = (rtm->rtm_dst_len + 7) >> 3;
2033
2034 if (nla_len(tb[RTA_DST]) < plen)
2035 goto errout;
2036
2037 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002039
2040 if (tb[RTA_SRC]) {
2041 int plen = (rtm->rtm_src_len + 7) >> 3;
2042
2043 if (nla_len(tb[RTA_SRC]) < plen)
2044 goto errout;
2045
2046 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002048
2049 if (tb[RTA_OIF])
2050 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2051
2052 if (tb[RTA_PRIORITY])
2053 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2054
2055 if (tb[RTA_METRICS]) {
2056 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2057 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002059
2060 if (tb[RTA_TABLE])
2061 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2062
2063 err = 0;
2064errout:
2065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Thomas Grafc127ea22007-03-22 11:58:32 -07002068static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Thomas Graf86872cb2006-08-22 00:01:08 -07002070 struct fib6_config cfg;
2071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Thomas Graf86872cb2006-08-22 00:01:08 -07002073 err = rtm_to_fib6_config(skb, nlh, &cfg);
2074 if (err < 0)
2075 return err;
2076
2077 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Thomas Grafc127ea22007-03-22 11:58:32 -07002080static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Thomas Graf86872cb2006-08-22 00:01:08 -07002082 struct fib6_config cfg;
2083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Thomas Graf86872cb2006-08-22 00:01:08 -07002085 err = rtm_to_fib6_config(skb, nlh, &cfg);
2086 if (err < 0)
2087 return err;
2088
2089 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Thomas Graf339bf982006-11-10 14:10:15 -08002092static inline size_t rt6_nlmsg_size(void)
2093{
2094 return NLMSG_ALIGN(sizeof(struct rtmsg))
2095 + nla_total_size(16) /* RTA_SRC */
2096 + nla_total_size(16) /* RTA_DST */
2097 + nla_total_size(16) /* RTA_GATEWAY */
2098 + nla_total_size(16) /* RTA_PREFSRC */
2099 + nla_total_size(4) /* RTA_TABLE */
2100 + nla_total_size(4) /* RTA_IIF */
2101 + nla_total_size(4) /* RTA_OIF */
2102 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002103 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002104 + nla_total_size(sizeof(struct rta_cacheinfo));
2105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002108 struct in6_addr *dst, struct in6_addr *src,
2109 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002110 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002113 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002114 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002115 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (prefix) { /* user wants prefix routes only */
2118 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2119 /* success since this is not a prefix route */
2120 return 1;
2121 }
2122 }
2123
Thomas Graf2d7202b2006-08-22 00:01:27 -07002124 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2125 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002126 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002127
2128 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 rtm->rtm_family = AF_INET6;
2130 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2131 rtm->rtm_src_len = rt->rt6i_src.plen;
2132 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002133 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002134 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002135 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002136 table = RT6_TABLE_UNSPEC;
2137 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002138 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (rt->rt6i_flags&RTF_REJECT)
2140 rtm->rtm_type = RTN_UNREACHABLE;
2141 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2142 rtm->rtm_type = RTN_LOCAL;
2143 else
2144 rtm->rtm_type = RTN_UNICAST;
2145 rtm->rtm_flags = 0;
2146 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2147 rtm->rtm_protocol = rt->rt6i_protocol;
2148 if (rt->rt6i_flags&RTF_DYNAMIC)
2149 rtm->rtm_protocol = RTPROT_REDIRECT;
2150 else if (rt->rt6i_flags & RTF_ADDRCONF)
2151 rtm->rtm_protocol = RTPROT_KERNEL;
2152 else if (rt->rt6i_flags&RTF_DEFAULT)
2153 rtm->rtm_protocol = RTPROT_RA;
2154
2155 if (rt->rt6i_flags&RTF_CACHE)
2156 rtm->rtm_flags |= RTM_F_CLONED;
2157
2158 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002159 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002160 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002162 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163#ifdef CONFIG_IPV6_SUBTREES
2164 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002165 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002166 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002168 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002170 if (iif) {
2171#ifdef CONFIG_IPV6_MROUTE
2172 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2173 int err = ip6mr_get_route(skb, rtm, nowait);
2174 if (err <= 0) {
2175 if (!nowait) {
2176 if (err == 0)
2177 return 0;
2178 goto nla_put_failure;
2179 } else {
2180 if (err == -EMSGSIZE)
2181 goto nla_put_failure;
2182 }
2183 }
2184 } else
2185#endif
2186 NLA_PUT_U32(skb, RTA_IIF, iif);
2187 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct in6_addr saddr_buf;
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09002189 if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09002190 dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002191 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002195 goto nla_put_failure;
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (rt->u.dst.neighbour)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002198 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (rt->u.dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002201 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2202
2203 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002204
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002205 expires = (rt->rt6i_flags & RTF_EXPIRES) ?
2206 rt->rt6i_expires - jiffies : 0;
2207
Thomas Grafe3703b32006-11-27 09:27:07 -08002208 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2209 expires, rt->u.dst.error) < 0)
2210 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Thomas Graf2d7202b2006-08-22 00:01:27 -07002212 return nlmsg_end(skb, nlh);
2213
2214nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002215 nlmsg_cancel(skb, nlh);
2216 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Patrick McHardy1b43af52006-08-10 23:11:17 -07002219int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2222 int prefix;
2223
Thomas Graf2d7202b2006-08-22 00:01:27 -07002224 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2225 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2227 } else
2228 prefix = 0;
2229
2230 return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2231 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002232 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Thomas Grafc127ea22007-03-22 11:58:32 -07002235static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002237 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002238 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002240 struct sk_buff *skb;
2241 struct rtmsg *rtm;
2242 struct flowi fl;
2243 int err, iif = 0;
2244
2245 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2246 if (err < 0)
2247 goto errout;
2248
2249 err = -EINVAL;
2250 memset(&fl, 0, sizeof(fl));
2251
2252 if (tb[RTA_SRC]) {
2253 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2254 goto errout;
2255
2256 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2257 }
2258
2259 if (tb[RTA_DST]) {
2260 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2261 goto errout;
2262
2263 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2264 }
2265
2266 if (tb[RTA_IIF])
2267 iif = nla_get_u32(tb[RTA_IIF]);
2268
2269 if (tb[RTA_OIF])
2270 fl.oif = nla_get_u32(tb[RTA_OIF]);
2271
2272 if (iif) {
2273 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002274 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002275 if (!dev) {
2276 err = -ENODEV;
2277 goto errout;
2278 }
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002282 if (skb == NULL) {
2283 err = -ENOBUFS;
2284 goto errout;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Reserve room for dummy headers, this skb can pass
2288 through good chunk of routing engine.
2289 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002290 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2292
Daniel Lezcano8a3edd82008-03-07 11:14:16 -08002293 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 skb->dst = &rt->u.dst;
2295
Thomas Grafab364a62006-08-22 00:01:47 -07002296 err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002298 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002300 kfree_skb(skb);
2301 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
Daniel Lezcano55786892008-03-04 13:47:47 -08002304 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002305errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Thomas Graf86872cb2006-08-22 00:01:08 -07002309void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002312 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002313 u32 seq;
2314 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002316 err = -ENOBUFS;
2317 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002318
Thomas Graf339bf982006-11-10 14:10:15 -08002319 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002320 if (skb == NULL)
2321 goto errout;
2322
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002323 err = rt6_fill_node(skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002324 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002325 if (err < 0) {
2326 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2327 WARN_ON(err == -EMSGSIZE);
2328 kfree_skb(skb);
2329 goto errout;
2330 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002331 err = rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2332 info->nlh, gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002333errout:
2334 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002335 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002338static int ip6_route_dev_notify(struct notifier_block *this,
2339 unsigned long event, void *data)
2340{
2341 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002342 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002343
2344 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2345 net->ipv6.ip6_null_entry->u.dst.dev = dev;
2346 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2347#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2348 net->ipv6.ip6_prohibit_entry->u.dst.dev = dev;
2349 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2350 net->ipv6.ip6_blk_hole_entry->u.dst.dev = dev;
2351 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2352#endif
2353 }
2354
2355 return NOTIFY_OK;
2356}
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/*
2359 * /proc
2360 */
2361
2362#ifdef CONFIG_PROC_FS
2363
2364#define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
2365
2366struct rt6_proc_arg
2367{
2368 char *buffer;
2369 int offset;
2370 int length;
2371 int skip;
2372 int len;
2373};
2374
2375static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2376{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002377 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002379 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
2380 rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382#ifdef CONFIG_IPV6_SUBTREES
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002383 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
2384 rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002386 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387#endif
2388
2389 if (rt->rt6i_nexthop) {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002390 seq_printf(m, NIP6_SEQFMT,
2391 NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002393 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002395 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2396 rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
2397 rt->u.dst.__use, rt->rt6i_flags,
2398 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return 0;
2400}
2401
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002402static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002404 struct net *net = (struct net *)m->private;
2405 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002409static int ipv6_route_open(struct inode *inode, struct file *file)
2410{
Pavel Emelyanova2333522008-03-26 16:49:40 -07002411 int err;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002412 struct net *net = get_proc_net(inode);
2413 if (!net)
2414 return -ENXIO;
Pavel Emelyanova2333522008-03-26 16:49:40 -07002415
2416 err = single_open(file, ipv6_route_show, net);
2417 if (err < 0) {
2418 put_net(net);
2419 return err;
2420 }
2421
2422 return 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002423}
2424
2425static int ipv6_route_release(struct inode *inode, struct file *file)
2426{
2427 struct seq_file *seq = file->private_data;
2428 struct net *net = seq->private;
2429 put_net(net);
2430 return single_release(inode, file);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002431}
2432
2433static const struct file_operations ipv6_route_proc_fops = {
2434 .owner = THIS_MODULE,
2435 .open = ipv6_route_open,
2436 .read = seq_read,
2437 .llseek = seq_lseek,
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002438 .release = ipv6_route_release,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002439};
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2442{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002443 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002445 net->ipv6.rt6_stats->fib_nodes,
2446 net->ipv6.rt6_stats->fib_route_nodes,
2447 net->ipv6.rt6_stats->fib_rt_alloc,
2448 net->ipv6.rt6_stats->fib_rt_entries,
2449 net->ipv6.rt6_stats->fib_rt_cache,
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002450 atomic_read(&net->ipv6.ip6_dst_ops->entries),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002451 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 return 0;
2454}
2455
2456static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2457{
Pavel Emelyanova2333522008-03-26 16:49:40 -07002458 int err;
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002459 struct net *net = get_proc_net(inode);
Pavel Emelyanova2333522008-03-26 16:49:40 -07002460 if (!net)
2461 return -ENXIO;
2462
2463 err = single_open(file, rt6_stats_seq_show, net);
2464 if (err < 0) {
2465 put_net(net);
2466 return err;
2467 }
2468
2469 return 0;
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002470}
2471
2472static int rt6_stats_seq_release(struct inode *inode, struct file *file)
2473{
2474 struct seq_file *seq = file->private_data;
2475 struct net *net = (struct net *)seq->private;
2476 put_net(net);
2477 return single_release(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Arjan van de Ven9a321442007-02-12 00:55:35 -08002480static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 .owner = THIS_MODULE,
2482 .open = rt6_stats_seq_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002485 .release = rt6_stats_seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486};
2487#endif /* CONFIG_PROC_FS */
2488
2489#ifdef CONFIG_SYSCTL
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static
2492int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
2493 void __user *buffer, size_t *lenp, loff_t *ppos)
2494{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002495 struct net *net = current->nsproxy->net_ns;
2496 int delay = net->ipv6.sysctl.flush_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (write) {
2498 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002499 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501 } else
2502 return -EINVAL;
2503}
2504
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002505ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002506 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002508 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002510 .mode = 0200,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002511 .proc_handler = &ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 },
2513 {
2514 .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
2515 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002516 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 .maxlen = sizeof(int),
2518 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002519 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 },
2521 {
2522 .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
2523 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002524 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 .maxlen = sizeof(int),
2526 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002527 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 },
2529 {
2530 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
2531 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002532 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .maxlen = sizeof(int),
2534 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002535 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 .strategy = &sysctl_jiffies,
2537 },
2538 {
2539 .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
2540 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002541 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .maxlen = sizeof(int),
2543 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002544 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 .strategy = &sysctl_jiffies,
2546 },
2547 {
2548 .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
2549 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002550 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .maxlen = sizeof(int),
2552 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002553 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 .strategy = &sysctl_jiffies,
2555 },
2556 {
2557 .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
2558 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002559 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 .maxlen = sizeof(int),
2561 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002562 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 .strategy = &sysctl_jiffies,
2564 },
2565 {
2566 .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
2567 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002568 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 .maxlen = sizeof(int),
2570 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002571 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 .strategy = &sysctl_jiffies,
2573 },
2574 {
2575 .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
2576 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002577 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .maxlen = sizeof(int),
2579 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002580 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 .strategy = &sysctl_jiffies,
2582 },
2583 {
2584 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
2585 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002586 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 .maxlen = sizeof(int),
2588 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002589 .proc_handler = &proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 .strategy = &sysctl_ms_jiffies,
2591 },
2592 { .ctl_name = 0 }
2593};
2594
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002595struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2596{
2597 struct ctl_table *table;
2598
2599 table = kmemdup(ipv6_route_table_template,
2600 sizeof(ipv6_route_table_template),
2601 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002602
2603 if (table) {
2604 table[0].data = &net->ipv6.sysctl.flush_delay;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002605 table[1].data = &net->ipv6.ip6_dst_ops->gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002606 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2607 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2608 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2609 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2610 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2611 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2612 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2613 }
2614
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002615 return table;
2616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617#endif
2618
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002619static int ip6_route_net_init(struct net *net)
2620{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002621 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002622
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002623 net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
2624 sizeof(*net->ipv6.ip6_dst_ops),
2625 GFP_KERNEL);
2626 if (!net->ipv6.ip6_dst_ops)
2627 goto out;
Denis V. Lunev48115be2008-04-16 02:01:34 -07002628 net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002629
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002630 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2631 sizeof(*net->ipv6.ip6_null_entry),
2632 GFP_KERNEL);
2633 if (!net->ipv6.ip6_null_entry)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002634 goto out_ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002635 net->ipv6.ip6_null_entry->u.dst.path =
2636 (struct dst_entry *)net->ipv6.ip6_null_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002637 net->ipv6.ip6_null_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002638
2639#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2640 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2641 sizeof(*net->ipv6.ip6_prohibit_entry),
2642 GFP_KERNEL);
2643 if (!net->ipv6.ip6_prohibit_entry) {
2644 kfree(net->ipv6.ip6_null_entry);
2645 goto out;
2646 }
2647 net->ipv6.ip6_prohibit_entry->u.dst.path =
2648 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002649 net->ipv6.ip6_prohibit_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002650
2651 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2652 sizeof(*net->ipv6.ip6_blk_hole_entry),
2653 GFP_KERNEL);
2654 if (!net->ipv6.ip6_blk_hole_entry) {
2655 kfree(net->ipv6.ip6_null_entry);
2656 kfree(net->ipv6.ip6_prohibit_entry);
2657 goto out;
2658 }
2659 net->ipv6.ip6_blk_hole_entry->u.dst.path =
2660 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002661 net->ipv6.ip6_blk_hole_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002662#endif
2663
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002664#ifdef CONFIG_PROC_FS
2665 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2666 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2667#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002668 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2669
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002670 ret = 0;
2671out:
2672 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002673
2674out_ip6_dst_ops:
Denis V. Lunev48115be2008-04-16 02:01:34 -07002675 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002676 kfree(net->ipv6.ip6_dst_ops);
2677 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002678}
2679
2680static void ip6_route_net_exit(struct net *net)
2681{
2682#ifdef CONFIG_PROC_FS
2683 proc_net_remove(net, "ipv6_route");
2684 proc_net_remove(net, "rt6_stats");
2685#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002686 kfree(net->ipv6.ip6_null_entry);
2687#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2688 kfree(net->ipv6.ip6_prohibit_entry);
2689 kfree(net->ipv6.ip6_blk_hole_entry);
2690#endif
Denis V. Lunev48115be2008-04-16 02:01:34 -07002691 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002692 kfree(net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002693}
2694
2695static struct pernet_operations ip6_route_net_ops = {
2696 .init = ip6_route_net_init,
2697 .exit = ip6_route_net_exit,
2698};
2699
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002700static struct notifier_block ip6_route_dev_notifier = {
2701 .notifier_call = ip6_route_dev_notify,
2702 .priority = 0,
2703};
2704
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002705int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002707 int ret;
2708
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002709 ret = -ENOMEM;
2710 ip6_dst_ops_template.kmem_cachep =
2711 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2712 SLAB_HWCACHE_ALIGN, NULL);
2713 if (!ip6_dst_ops_template.kmem_cachep)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002714 goto out;;
David S. Miller14e50e52007-05-24 18:17:54 -07002715
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002716 ret = register_pernet_subsys(&ip6_route_net_ops);
2717 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002718 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002719
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002720 /* Registering of the loopback is done before this portion of code,
2721 * the loopback reference in rt6_info will not be taken, do it
2722 * manually for init_net */
2723 init_net.ipv6.ip6_null_entry->u.dst.dev = init_net.loopback_dev;
2724 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2725 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2726 init_net.ipv6.ip6_prohibit_entry->u.dst.dev = init_net.loopback_dev;
2727 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2728 init_net.ipv6.ip6_blk_hole_entry->u.dst.dev = init_net.loopback_dev;
2729 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2730 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002731 ret = fib6_init();
2732 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002733 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002734
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002735 ret = xfrm6_init();
2736 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002737 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002738
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002739 ret = fib6_rules_init();
2740 if (ret)
2741 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002742
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002743 ret = -ENOBUFS;
2744 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2745 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2746 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2747 goto fib6_rules_init;
2748
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002749 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002750 if (ret)
2751 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002752
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002753out:
2754 return ret;
2755
2756fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002757 fib6_rules_cleanup();
2758xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002759 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002760out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002761 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002762out_register_subsys:
2763 unregister_pernet_subsys(&ip6_route_net_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002764out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002765 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
2769void ip6_route_cleanup(void)
2770{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002771 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002772 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002775 unregister_pernet_subsys(&ip6_route_net_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002776 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}