blob: 18c486cf498756b5a4536d17798854ba137ba37f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
29#include <linux/types.h>
30#include <linux/times.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
33#include <linux/net.h>
34#include <linux/route.h>
35#include <linux/netdevice.h>
36#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090037#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080042#include <linux/nsproxy.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020043#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/snmp.h>
45#include <net/ipv6.h>
46#include <net/ip6_fib.h>
47#include <net/ip6_route.h>
48#include <net/ndisc.h>
49#include <net/addrconf.h>
50#include <net/tcp.h>
51#include <linux/rtnetlink.h>
52#include <net/dst.h>
53#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070054#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070055#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/uaccess.h>
58
59#ifdef CONFIG_SYSCTL
60#include <linux/sysctl.h>
61#endif
62
63/* Set to 3 to get tracing. */
64#define RT6_DEBUG 2
65
66#if RT6_DEBUG >= 3
67#define RDBG(x) printk x
68#define RT6_TRACE(x...) printk(KERN_DEBUG x)
69#else
70#define RDBG(x)
71#define RT6_TRACE(x...) do { ; } while (0)
72#endif
73
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -080074#define CLONE_OFFLINK_ROUTE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
77static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
78static struct dst_entry *ip6_negative_advice(struct dst_entry *);
79static void ip6_dst_destroy(struct dst_entry *);
80static void ip6_dst_ifdown(struct dst_entry *,
81 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080082static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static int ip6_pkt_discard(struct sk_buff *skb);
85static int ip6_pkt_discard_out(struct sk_buff *skb);
86static void ip6_link_failure(struct sk_buff *skb);
87static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
88
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080089#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080090static struct rt6_info *rt6_add_route_info(struct net *net,
91 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080092 struct in6_addr *gwaddr, int ifindex,
93 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080094static struct rt6_info *rt6_get_route_info(struct net *net,
95 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080096 struct in6_addr *gwaddr, int ifindex);
97#endif
98
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -080099static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .family = AF_INET6,
101 .protocol = __constant_htons(ETH_P_IPV6),
102 .gc = ip6_dst_gc,
103 .gc_thresh = 1024,
104 .check = ip6_dst_check,
105 .destroy = ip6_dst_destroy,
106 .ifdown = ip6_dst_ifdown,
107 .negative_advice = ip6_negative_advice,
108 .link_failure = ip6_link_failure,
109 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700110 .local_out = __ip6_local_out,
Eric Dumazete2422972008-01-30 20:07:45 -0800111 .entries = ATOMIC_INIT(0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
David S. Miller14e50e52007-05-24 18:17:54 -0700114static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
115{
116}
117
118static struct dst_ops ip6_dst_blackhole_ops = {
119 .family = AF_INET6,
120 .protocol = __constant_htons(ETH_P_IPV6),
121 .destroy = ip6_dst_destroy,
122 .check = ip6_dst_check,
123 .update_pmtu = ip6_rt_blackhole_update_pmtu,
Eric Dumazete2422972008-01-30 20:07:45 -0800124 .entries = ATOMIC_INIT(0),
David S. Miller14e50e52007-05-24 18:17:54 -0700125};
126
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800127static struct rt6_info ip6_null_entry_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .u = {
129 .dst = {
130 .__refcnt = ATOMIC_INIT(1),
131 .__use = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .obsolete = -1,
133 .error = -ENETUNREACH,
134 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
135 .input = ip6_pkt_discard,
136 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 },
139 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
140 .rt6i_metric = ~(u32) 0,
141 .rt6i_ref = ATOMIC_INIT(1),
142};
143
Thomas Graf101367c2006-08-04 03:39:02 -0700144#ifdef CONFIG_IPV6_MULTIPLE_TABLES
145
David S. Miller6723ab52006-10-18 21:20:57 -0700146static int ip6_pkt_prohibit(struct sk_buff *skb);
147static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700148
Adrian Bunk280a34c2008-04-21 02:29:32 -0700149static struct rt6_info ip6_prohibit_entry_template = {
Thomas Graf101367c2006-08-04 03:39:02 -0700150 .u = {
151 .dst = {
152 .__refcnt = ATOMIC_INIT(1),
153 .__use = 1,
Thomas Graf101367c2006-08-04 03:39:02 -0700154 .obsolete = -1,
155 .error = -EACCES,
156 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
Thomas Graf9ce8ade2006-10-18 20:46:54 -0700157 .input = ip6_pkt_prohibit,
158 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700159 }
160 },
161 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
162 .rt6i_metric = ~(u32) 0,
163 .rt6i_ref = ATOMIC_INIT(1),
164};
165
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800166static struct rt6_info ip6_blk_hole_entry_template = {
Thomas Graf101367c2006-08-04 03:39:02 -0700167 .u = {
168 .dst = {
169 .__refcnt = ATOMIC_INIT(1),
170 .__use = 1,
Thomas Graf101367c2006-08-04 03:39:02 -0700171 .obsolete = -1,
172 .error = -EINVAL,
173 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
Herbert Xu352e5122007-11-13 21:34:06 -0800174 .input = dst_discard,
175 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700176 }
177 },
178 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
179 .rt6i_metric = ~(u32) 0,
180 .rt6i_ref = ATOMIC_INIT(1),
181};
182
183#endif
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* allocate dst with ip6_dst_ops */
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800186static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800188 return (struct rt6_info *)dst_alloc(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void ip6_dst_destroy(struct dst_entry *dst)
192{
193 struct rt6_info *rt = (struct rt6_info *)dst;
194 struct inet6_dev *idev = rt->rt6i_idev;
195
196 if (idev != NULL) {
197 rt->rt6i_idev = NULL;
198 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
203 int how)
204{
205 struct rt6_info *rt = (struct rt6_info *)dst;
206 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800207 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900208 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800210 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
211 struct inet6_dev *loopback_idev =
212 in6_dev_get(loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (loopback_idev != NULL) {
214 rt->rt6i_idev = loopback_idev;
215 in6_dev_put(idev);
216 }
217 }
218}
219
220static __inline__ int rt6_check_expired(const struct rt6_info *rt)
221{
222 return (rt->rt6i_flags & RTF_EXPIRES &&
223 time_after(jiffies, rt->rt6i_expires));
224}
225
Thomas Grafc71099a2006-08-04 23:20:06 -0700226static inline int rt6_need_strict(struct in6_addr *daddr)
227{
228 return (ipv6_addr_type(daddr) &
YOSHIFUJI Hideaki5ce83af2008-06-25 16:58:17 +0900229 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK));
Thomas Grafc71099a2006-08-04 23:20:06 -0700230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700233 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800236static inline struct rt6_info *rt6_device_match(struct net *net,
237 struct rt6_info *rt,
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900238 struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700240 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct rt6_info *local = NULL;
243 struct rt6_info *sprt;
244
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900245 if (!oif && ipv6_addr_any(saddr))
246 goto out;
247
248 for (sprt = rt; sprt; sprt = sprt->u.dst.rt6_next) {
249 struct net_device *dev = sprt->rt6i_dev;
250
251 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (dev->ifindex == oif)
253 return sprt;
254 if (dev->flags & IFF_LOOPBACK) {
255 if (sprt->rt6i_idev == NULL ||
256 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700257 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900259 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 local->rt6i_idev->dev->ifindex == oif))
261 continue;
262 }
263 local = sprt;
264 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900265 } else {
266 if (ipv6_chk_addr(net, saddr, dev,
267 flags & RT6_LOOKUP_F_IFACE))
268 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900272 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (local)
274 return local;
275
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700276 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800277 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900279out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return rt;
281}
282
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800283#ifdef CONFIG_IPV6_ROUTER_PREF
284static void rt6_probe(struct rt6_info *rt)
285{
286 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
287 /*
288 * Okay, this does not seem to be appropriate
289 * for now, however, we need to check if it
290 * is really so; aka Router Reachability Probing.
291 *
292 * Router Reachability Probe MUST be rate-limited
293 * to no more than one per minute.
294 */
295 if (!neigh || (neigh->nud_state & NUD_VALID))
296 return;
297 read_lock_bh(&neigh->lock);
298 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800299 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800300 struct in6_addr mcaddr;
301 struct in6_addr *target;
302
303 neigh->updated = jiffies;
304 read_unlock_bh(&neigh->lock);
305
306 target = (struct in6_addr *)&neigh->primary_key;
307 addrconf_addr_solict_mult(target, &mcaddr);
308 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
309 } else
310 read_unlock_bh(&neigh->lock);
311}
312#else
313static inline void rt6_probe(struct rt6_info *rt)
314{
315 return;
316}
317#endif
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800320 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700322static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800324 struct net_device *dev = rt->rt6i_dev;
David S. Miller161980f2007-04-06 11:42:27 -0700325 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800326 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700327 if ((dev->flags & IFF_LOOPBACK) &&
328 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
329 return 1;
330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Dave Jonesb6f99a22007-03-22 12:27:49 -0700333static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800335 struct neighbour *neigh = rt->rt6i_nexthop;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800336 int m;
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700337 if (rt->rt6i_flags & RTF_NONEXTHOP ||
338 !(rt->rt6i_flags & RTF_GATEWAY))
339 m = 1;
340 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800341 read_lock_bh(&neigh->lock);
342 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700343 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800344#ifdef CONFIG_IPV6_ROUTER_PREF
345 else if (neigh->nud_state & NUD_FAILED)
346 m = 0;
347#endif
348 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800349 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800350 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800351 } else
352 m = 0;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800353 return m;
354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800356static int rt6_score_route(struct rt6_info *rt, int oif,
357 int strict)
358{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700359 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900360
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700361 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700362 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800363 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800364#ifdef CONFIG_IPV6_ROUTER_PREF
365 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
366#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700367 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800368 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800369 return -1;
370 return m;
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David S. Millerf11e6652007-03-24 20:36:25 -0700373static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
374 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800375{
David S. Millerf11e6652007-03-24 20:36:25 -0700376 int m;
377
378 if (rt6_check_expired(rt))
379 goto out;
380
381 m = rt6_score_route(rt, oif, strict);
382 if (m < 0)
383 goto out;
384
385 if (m > *mpri) {
386 if (strict & RT6_LOOKUP_F_REACHABLE)
387 rt6_probe(match);
388 *mpri = m;
389 match = rt;
390 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
391 rt6_probe(rt);
392 }
393
394out:
395 return match;
396}
397
398static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
399 struct rt6_info *rr_head,
400 u32 metric, int oif, int strict)
401{
402 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800403 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David S. Millerf11e6652007-03-24 20:36:25 -0700405 match = NULL;
406 for (rt = rr_head; rt && rt->rt6i_metric == metric;
407 rt = rt->u.dst.rt6_next)
408 match = find_match(rt, oif, strict, &mpri, match);
409 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
410 rt = rt->u.dst.rt6_next)
411 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800412
David S. Millerf11e6652007-03-24 20:36:25 -0700413 return match;
414}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800415
David S. Millerf11e6652007-03-24 20:36:25 -0700416static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
417{
418 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800419 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David S. Millerf11e6652007-03-24 20:36:25 -0700421 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800422 __func__, fn->leaf, oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David S. Millerf11e6652007-03-24 20:36:25 -0700424 rt0 = fn->rr_ptr;
425 if (!rt0)
426 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David S. Millerf11e6652007-03-24 20:36:25 -0700428 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800430 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700431 (strict & RT6_LOOKUP_F_REACHABLE)) {
432 struct rt6_info *next = rt0->u.dst.rt6_next;
433
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800434 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700435 if (!next || next->rt6i_metric != rt0->rt6i_metric)
436 next = fn->leaf;
437
438 if (next != rt0)
439 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
David S. Millerf11e6652007-03-24 20:36:25 -0700442 RT6_TRACE("%s() => %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800443 __func__, match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900445 net = dev_net(rt0->rt6i_dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800446 return (match ? match : net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800449#ifdef CONFIG_IPV6_ROUTE_INFO
450int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
451 struct in6_addr *gwaddr)
452{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900453 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800454 struct route_info *rinfo = (struct route_info *) opt;
455 struct in6_addr prefix_buf, *prefix;
456 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900457 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800458 struct rt6_info *rt;
459
460 if (len < sizeof(struct route_info)) {
461 return -EINVAL;
462 }
463
464 /* Sanity check for prefix_len and length */
465 if (rinfo->length > 3) {
466 return -EINVAL;
467 } else if (rinfo->prefix_len > 128) {
468 return -EINVAL;
469 } else if (rinfo->prefix_len > 64) {
470 if (rinfo->length < 2) {
471 return -EINVAL;
472 }
473 } else if (rinfo->prefix_len > 0) {
474 if (rinfo->length < 1) {
475 return -EINVAL;
476 }
477 }
478
479 pref = rinfo->route_pref;
480 if (pref == ICMPV6_ROUTER_PREF_INVALID)
481 pref = ICMPV6_ROUTER_PREF_MEDIUM;
482
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900483 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800484
485 if (rinfo->length == 3)
486 prefix = (struct in6_addr *)rinfo->prefix;
487 else {
488 /* this function is safe */
489 ipv6_addr_prefix(&prefix_buf,
490 (struct in6_addr *)rinfo->prefix,
491 rinfo->prefix_len);
492 prefix = &prefix_buf;
493 }
494
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800495 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
496 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800497
498 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700499 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800500 rt = NULL;
501 }
502
503 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800504 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800505 pref);
506 else if (rt)
507 rt->rt6i_flags = RTF_ROUTEINFO |
508 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
509
510 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900511 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800512 rt->rt6i_flags &= ~RTF_EXPIRES;
513 } else {
514 rt->rt6i_expires = jiffies + HZ * lifetime;
515 rt->rt6i_flags |= RTF_EXPIRES;
516 }
517 dst_release(&rt->u.dst);
518 }
519 return 0;
520}
521#endif
522
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800523#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700524do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800525 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700526 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700527 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700528 if (fn->fn_flags & RTN_TL_ROOT) \
529 goto out; \
530 pn = fn->parent; \
531 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800532 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700533 else \
534 fn = pn; \
535 if (fn->fn_flags & RTN_RTINFO) \
536 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700537 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700538 } \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700539} while(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700540
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800541static struct rt6_info *ip6_pol_route_lookup(struct net *net,
542 struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700543 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct fib6_node *fn;
546 struct rt6_info *rt;
547
Thomas Grafc71099a2006-08-04 23:20:06 -0700548 read_lock_bh(&table->tb6_lock);
549 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
550restart:
551 rt = fn->leaf;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900552 rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800553 BACKTRACK(net, &fl->fl6_src);
Thomas Grafc71099a2006-08-04 23:20:06 -0700554out:
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800555 dst_use(&rt->u.dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700556 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700557 return rt;
558
559}
560
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900561struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
562 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700563{
564 struct flowi fl = {
565 .oif = oif,
566 .nl_u = {
567 .ip6_u = {
568 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700569 },
570 },
571 };
572 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700573 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700574
Thomas Grafadaa70b2006-10-13 15:01:03 -0700575 if (saddr) {
576 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
577 flags |= RT6_LOOKUP_F_HAS_SADDR;
578 }
579
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800580 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700581 if (dst->error == 0)
582 return (struct rt6_info *) dst;
583
584 dst_release(dst);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return NULL;
587}
588
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900589EXPORT_SYMBOL(rt6_lookup);
590
Thomas Grafc71099a2006-08-04 23:20:06 -0700591/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 It takes new route entry, the addition fails by any reason the
593 route is freed. In any case, if caller does not hold it, it may
594 be destroyed.
595 */
596
Thomas Graf86872cb2006-08-22 00:01:08 -0700597static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700600 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Thomas Grafc71099a2006-08-04 23:20:06 -0700602 table = rt->rt6i_table;
603 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700604 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700605 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 return err;
608}
609
Thomas Graf40e22e82006-08-22 00:00:45 -0700610int ip6_ins_rt(struct rt6_info *rt)
611{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800612 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900613 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800614 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800615 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700616}
617
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800618static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
619 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct rt6_info *rt;
622
623 /*
624 * Clone the route.
625 */
626
627 rt = ip6_rt_copy(ort);
628
629 if (rt) {
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900630 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
631 if (rt->rt6i_dst.plen != 128 &&
632 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
633 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900637 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rt->rt6i_dst.plen = 128;
639 rt->rt6i_flags |= RTF_CACHE;
640 rt->u.dst.flags |= DST_HOST;
641
642#ifdef CONFIG_IPV6_SUBTREES
643 if (rt->rt6i_src.plen && saddr) {
644 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
645 rt->rt6i_src.plen = 128;
646 }
647#endif
648
649 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
650
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800653 return rt;
654}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800656static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
657{
658 struct rt6_info *rt = ip6_rt_copy(ort);
659 if (rt) {
660 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
661 rt->rt6i_dst.plen = 128;
662 rt->rt6i_flags |= RTF_CACHE;
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800663 rt->u.dst.flags |= DST_HOST;
664 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
665 }
666 return rt;
667}
668
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800669static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
670 struct flowi *fl, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800673 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700674 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800676 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700677 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700679 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700682 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800684restart_2:
Thomas Grafc71099a2006-08-04 23:20:06 -0700685 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700688 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800689
690 BACKTRACK(net, &fl->fl6_src);
691 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800692 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800695 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700696 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800697
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800698 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800699 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800700 else {
701#if CLONE_OFFLINK_ROUTE
702 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
703#else
704 goto out2;
705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800707
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800708 dst_release(&rt->u.dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800709 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800710
711 dst_hold(&rt->u.dst);
712 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700713 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800714 if (!err)
715 goto out2;
716 }
717
718 if (--attempts <= 0)
719 goto out2;
720
721 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700722 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800723 * released someone could insert this route. Relookup.
724 */
725 dst_release(&rt->u.dst);
726 goto relookup;
727
728out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800729 if (reachable) {
730 reachable = 0;
731 goto restart_2;
732 }
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800733 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700734 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735out2:
736 rt->u.dst.lastuse = jiffies;
737 rt->u.dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700738
739 return rt;
740}
741
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800742static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700743 struct flowi *fl, int flags)
744{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800745 return ip6_pol_route(net, table, fl->iif, fl, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700746}
747
Thomas Grafc71099a2006-08-04 23:20:06 -0700748void ip6_route_input(struct sk_buff *skb)
749{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700750 struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900751 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700752 int flags = RT6_LOOKUP_F_HAS_SADDR;
Thomas Grafc71099a2006-08-04 23:20:06 -0700753 struct flowi fl = {
754 .iif = skb->dev->ifindex,
755 .nl_u = {
756 .ip6_u = {
757 .daddr = iph->daddr,
758 .saddr = iph->saddr,
Al Viro90bcaf72006-11-08 00:25:17 -0800759 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
Thomas Grafc71099a2006-08-04 23:20:06 -0700760 },
761 },
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900762 .mark = skb->mark,
Thomas Grafc71099a2006-08-04 23:20:06 -0700763 .proto = iph->nexthdr,
764 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700765
766 if (rt6_need_strict(&iph->daddr))
767 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700768
Daniel Lezcano55786892008-03-04 13:47:47 -0800769 skb->dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input);
Thomas Grafc71099a2006-08-04 23:20:06 -0700770}
771
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800772static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
Thomas Grafc71099a2006-08-04 23:20:06 -0700773 struct flowi *fl, int flags)
774{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800775 return ip6_pol_route(net, table, fl->oif, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700776}
777
Daniel Lezcano4591db42008-03-05 10:48:10 -0800778struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
779 struct flowi *fl)
Thomas Grafc71099a2006-08-04 23:20:06 -0700780{
781 int flags = 0;
782
783 if (rt6_need_strict(&fl->fl6_dst))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700784 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700785
Thomas Grafadaa70b2006-10-13 15:01:03 -0700786 if (!ipv6_addr_any(&fl->fl6_src))
787 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900788 else if (sk) {
789 unsigned int prefs = inet6_sk(sk)->srcprefs;
790 if (prefs & IPV6_PREFER_SRC_TMP)
791 flags |= RT6_LOOKUP_F_SRCPREF_TMP;
792 if (prefs & IPV6_PREFER_SRC_PUBLIC)
793 flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
794 if (prefs & IPV6_PREFER_SRC_COA)
795 flags |= RT6_LOOKUP_F_SRCPREF_COA;
796 }
Thomas Grafadaa70b2006-10-13 15:01:03 -0700797
Daniel Lezcano4591db42008-03-05 10:48:10 -0800798 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900801EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
David S. Miller14e50e52007-05-24 18:17:54 -0700803int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
804{
805 struct rt6_info *ort = (struct rt6_info *) *dstp;
806 struct rt6_info *rt = (struct rt6_info *)
807 dst_alloc(&ip6_dst_blackhole_ops);
808 struct dst_entry *new = NULL;
809
810 if (rt) {
811 new = &rt->u.dst;
812
813 atomic_set(&new->__refcnt, 1);
814 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800815 new->input = dst_discard;
816 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700817
818 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
819 new->dev = ort->u.dst.dev;
820 if (new->dev)
821 dev_hold(new->dev);
822 rt->rt6i_idev = ort->rt6i_idev;
823 if (rt->rt6i_idev)
824 in6_dev_hold(rt->rt6i_idev);
825 rt->rt6i_expires = 0;
826
827 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
828 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
829 rt->rt6i_metric = 0;
830
831 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
832#ifdef CONFIG_IPV6_SUBTREES
833 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
834#endif
835
836 dst_free(new);
837 }
838
839 dst_release(*dstp);
840 *dstp = new;
841 return (new ? 0 : -ENOMEM);
842}
843EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/*
846 * Destination cache support functions
847 */
848
849static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
850{
851 struct rt6_info *rt;
852
853 rt = (struct rt6_info *) dst;
854
855 if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
856 return dst;
857
858 return NULL;
859}
860
861static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
862{
863 struct rt6_info *rt = (struct rt6_info *) dst;
864
865 if (rt) {
866 if (rt->rt6i_flags & RTF_CACHE)
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700867 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 else
869 dst_release(dst);
870 }
871 return NULL;
872}
873
874static void ip6_link_failure(struct sk_buff *skb)
875{
876 struct rt6_info *rt;
877
878 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
879
880 rt = (struct rt6_info *) skb->dst;
881 if (rt) {
882 if (rt->rt6i_flags&RTF_CACHE) {
883 dst_set_expires(&rt->u.dst, 0);
884 rt->rt6i_flags |= RTF_EXPIRES;
885 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
886 rt->rt6i_node->fn_sernum = -1;
887 }
888}
889
890static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
891{
892 struct rt6_info *rt6 = (struct rt6_info*)dst;
893
894 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
895 rt6->rt6i_flags |= RTF_MODIFIED;
896 if (mtu < IPV6_MIN_MTU) {
897 mtu = IPV6_MIN_MTU;
898 dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
899 }
900 dst->metrics[RTAX_MTU-1] = mtu;
Tom Tucker8d717402006-07-30 20:43:36 -0700901 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static int ipv6_get_mtu(struct net_device *dev);
906
Daniel Lezcano55786892008-03-04 13:47:47 -0800907static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
910
Daniel Lezcano55786892008-03-04 13:47:47 -0800911 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
912 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900915 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
916 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
917 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * rely only on pmtu discovery"
919 */
920 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
921 mtu = IPV6_MAXPLEN;
922 return mtu;
923}
924
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800925static struct dst_entry *icmp6_dst_gc_list;
926static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700927
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800928struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900930 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct rt6_info *rt;
933 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900934 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (unlikely(idev == NULL))
937 return NULL;
938
Benjamin Theryf2fc6a52008-03-04 13:49:23 -0800939 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (unlikely(rt == NULL)) {
941 in6_dev_put(idev);
942 goto out;
943 }
944
945 dev_hold(dev);
946 if (neigh)
947 neigh_hold(neigh);
948 else
949 neigh = ndisc_get_neigh(dev, addr);
950
951 rt->rt6i_dev = dev;
952 rt->rt6i_idev = idev;
953 rt->rt6i_nexthop = neigh;
954 atomic_set(&rt->u.dst.__refcnt, 1);
955 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
956 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -0800957 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800958 rt->u.dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960#if 0 /* there's no chance to use these for ndisc */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900961 rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
962 ? DST_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 : 0;
964 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
965 rt->rt6i_dst.plen = 128;
966#endif
967
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800968 spin_lock_bh(&icmp6_dst_lock);
969 rt->u.dst.next = icmp6_dst_gc_list;
970 icmp6_dst_gc_list = &rt->u.dst;
971 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Daniel Lezcano55786892008-03-04 13:47:47 -0800973 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975out:
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +0900976 return &rt->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -0700979int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct dst_entry *dst, *next, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -0700982 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 next = NULL;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700985
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800986 spin_lock_bh(&icmp6_dst_lock);
987 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 while ((dst = *pprev) != NULL) {
990 if (!atomic_read(&dst->__refcnt)) {
991 *pprev = dst->next;
992 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 } else {
994 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -0700995 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800999 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001000
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001001 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
David S. Miller1e493d12008-09-10 17:27:15 -07001004static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1005 void *arg)
1006{
1007 struct dst_entry *dst, **pprev;
1008
1009 spin_lock_bh(&icmp6_dst_lock);
1010 pprev = &icmp6_dst_gc_list;
1011 while ((dst = *pprev) != NULL) {
1012 struct rt6_info *rt = (struct rt6_info *) dst;
1013 if (func(rt, arg)) {
1014 *pprev = dst->next;
1015 dst_free(dst);
1016 } else {
1017 pprev = &dst->next;
1018 }
1019 }
1020 spin_unlock_bh(&icmp6_dst_lock);
1021}
1022
Daniel Lezcano569d3642008-01-18 03:56:57 -08001023static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned long now = jiffies;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001026 struct net *net = ops->dst_net;
1027 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1028 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1029 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1030 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1031 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Daniel Lezcano7019b782008-03-04 13:50:14 -08001033 if (time_after(rt_last_gc + rt_min_interval, now) &&
1034 atomic_read(&ops->entries) <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto out;
1036
Benjamin Thery6891a342008-03-04 13:49:47 -08001037 net->ipv6.ip6_rt_gc_expire++;
1038 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1039 net->ipv6.ip6_rt_last_gc = now;
Daniel Lezcano7019b782008-03-04 13:50:14 -08001040 if (atomic_read(&ops->entries) < ops->gc_thresh)
1041 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001043 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1044 return (atomic_read(&ops->entries) > rt_max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/* Clean host part of a prefix. Not necessary in radix tree,
1048 but results in cleaner routing tables.
1049
1050 Remove it only when all the things will work!
1051 */
1052
1053static int ipv6_get_mtu(struct net_device *dev)
1054{
1055 int mtu = IPV6_MIN_MTU;
1056 struct inet6_dev *idev;
1057
1058 idev = in6_dev_get(dev);
1059 if (idev) {
1060 mtu = idev->cnf.mtu6;
1061 in6_dev_put(idev);
1062 }
1063 return mtu;
1064}
1065
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001066int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001068 int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
1069 if (hoplimit < 0) {
1070 struct net_device *dev = dst->dev;
1071 struct inet6_dev *idev = in6_dev_get(dev);
1072 if (idev) {
1073 hoplimit = idev->cnf.hop_limit;
1074 in6_dev_put(idev);
1075 } else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001076 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 return hoplimit;
1079}
1080
1081/*
1082 *
1083 */
1084
Thomas Graf86872cb2006-08-22 00:01:08 -07001085int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001088 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct rt6_info *rt = NULL;
1090 struct net_device *dev = NULL;
1091 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001092 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int addr_type;
1094
Thomas Graf86872cb2006-08-22 00:01:08 -07001095 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return -EINVAL;
1097#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001098 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -EINVAL;
1100#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001101 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001103 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!dev)
1105 goto out;
1106 idev = in6_dev_get(dev);
1107 if (!idev)
1108 goto out;
1109 }
1110
Thomas Graf86872cb2006-08-22 00:01:08 -07001111 if (cfg->fc_metric == 0)
1112 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Daniel Lezcano55786892008-03-04 13:47:47 -08001114 table = fib6_new_table(net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001115 if (table == NULL) {
1116 err = -ENOBUFS;
1117 goto out;
1118 }
1119
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001120 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (rt == NULL) {
1123 err = -ENOMEM;
1124 goto out;
1125 }
1126
1127 rt->u.dst.obsolete = -1;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001128 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1129 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1130 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Thomas Graf86872cb2006-08-22 00:01:08 -07001132 if (cfg->fc_protocol == RTPROT_UNSPEC)
1133 cfg->fc_protocol = RTPROT_BOOT;
1134 rt->rt6i_protocol = cfg->fc_protocol;
1135
1136 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (addr_type & IPV6_ADDR_MULTICAST)
1139 rt->u.dst.input = ip6_mc_input;
1140 else
1141 rt->u.dst.input = ip6_forward;
1142
1143 rt->u.dst.output = ip6_output;
1144
Thomas Graf86872cb2006-08-22 00:01:08 -07001145 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1146 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (rt->rt6i_dst.plen == 128)
1148 rt->u.dst.flags = DST_HOST;
1149
1150#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001151 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1152 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#endif
1154
Thomas Graf86872cb2006-08-22 00:01:08 -07001155 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* We cannot add true routes via loopback here,
1158 they would result in kernel looping; promote them to reject routes
1159 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001160 if ((cfg->fc_flags & RTF_REJECT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
1162 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001163 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (dev) {
1165 dev_put(dev);
1166 in6_dev_put(idev);
1167 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001168 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 dev_hold(dev);
1170 idev = in6_dev_get(dev);
1171 if (!idev) {
1172 err = -ENODEV;
1173 goto out;
1174 }
1175 }
1176 rt->u.dst.output = ip6_pkt_discard_out;
1177 rt->u.dst.input = ip6_pkt_discard;
1178 rt->u.dst.error = -ENETUNREACH;
1179 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1180 goto install_route;
1181 }
1182
Thomas Graf86872cb2006-08-22 00:01:08 -07001183 if (cfg->fc_flags & RTF_GATEWAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct in6_addr *gw_addr;
1185 int gwa_type;
1186
Thomas Graf86872cb2006-08-22 00:01:08 -07001187 gw_addr = &cfg->fc_gateway;
1188 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 gwa_type = ipv6_addr_type(gw_addr);
1190
1191 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1192 struct rt6_info *grt;
1193
1194 /* IPv6 strictly inhibits using not link-local
1195 addresses as nexthop address.
1196 Otherwise, router will not able to send redirects.
1197 It is very good, but in some (rare!) circumstances
1198 (SIT, PtP, NBMA NOARP links) it is handy to allow
1199 some exceptions. --ANK
1200 */
1201 err = -EINVAL;
1202 if (!(gwa_type&IPV6_ADDR_UNICAST))
1203 goto out;
1204
Daniel Lezcano55786892008-03-04 13:47:47 -08001205 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 err = -EHOSTUNREACH;
1208 if (grt == NULL)
1209 goto out;
1210 if (dev) {
1211 if (dev != grt->rt6i_dev) {
1212 dst_release(&grt->u.dst);
1213 goto out;
1214 }
1215 } else {
1216 dev = grt->rt6i_dev;
1217 idev = grt->rt6i_idev;
1218 dev_hold(dev);
1219 in6_dev_hold(grt->rt6i_idev);
1220 }
1221 if (!(grt->rt6i_flags&RTF_GATEWAY))
1222 err = 0;
1223 dst_release(&grt->u.dst);
1224
1225 if (err)
1226 goto out;
1227 }
1228 err = -EINVAL;
1229 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1230 goto out;
1231 }
1232
1233 err = -ENODEV;
1234 if (dev == NULL)
1235 goto out;
1236
Thomas Graf86872cb2006-08-22 00:01:08 -07001237 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1239 if (IS_ERR(rt->rt6i_nexthop)) {
1240 err = PTR_ERR(rt->rt6i_nexthop);
1241 rt->rt6i_nexthop = NULL;
1242 goto out;
1243 }
1244 }
1245
Thomas Graf86872cb2006-08-22 00:01:08 -07001246 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001249 if (cfg->fc_mx) {
1250 struct nlattr *nla;
1251 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Thomas Graf86872cb2006-08-22 00:01:08 -07001253 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001254 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001255
1256 if (type) {
1257 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 err = -EINVAL;
1259 goto out;
1260 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001261
1262 rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 }
1266
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001267 if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
Rami Rosen1ca615f2008-08-06 02:34:21 -07001269 if (!dst_mtu(&rt->u.dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
Satoru SATOH5ffc02a2008-05-04 22:14:42 -07001271 if (!dst_metric(&rt->u.dst, RTAX_ADVMSS))
Daniel Lezcano55786892008-03-04 13:47:47 -08001272 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rt->u.dst.dev = dev;
1274 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001275 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001276
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001277 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001278
Thomas Graf86872cb2006-08-22 00:01:08 -07001279 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281out:
1282 if (dev)
1283 dev_put(dev);
1284 if (idev)
1285 in6_dev_put(idev);
1286 if (rt)
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001287 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return err;
1289}
1290
Thomas Graf86872cb2006-08-22 00:01:08 -07001291static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001294 struct fib6_table *table;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001295 struct net *net = dev_net(rt->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001297 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001298 return -ENOENT;
1299
Thomas Grafc71099a2006-08-04 23:20:06 -07001300 table = rt->rt6i_table;
1301 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Thomas Graf86872cb2006-08-22 00:01:08 -07001303 err = fib6_del(rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 dst_release(&rt->u.dst);
1305
Thomas Grafc71099a2006-08-04 23:20:06 -07001306 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 return err;
1309}
1310
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001311int ip6_del_rt(struct rt6_info *rt)
1312{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001313 struct nl_info info = {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001314 .nl_net = dev_net(rt->rt6i_dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001315 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001316 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001317}
1318
Thomas Graf86872cb2006-08-22 00:01:08 -07001319static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Thomas Grafc71099a2006-08-04 23:20:06 -07001321 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct fib6_node *fn;
1323 struct rt6_info *rt;
1324 int err = -ESRCH;
1325
Daniel Lezcano55786892008-03-04 13:47:47 -08001326 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
Thomas Grafc71099a2006-08-04 23:20:06 -07001327 if (table == NULL)
1328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Thomas Grafc71099a2006-08-04 23:20:06 -07001330 read_lock_bh(&table->tb6_lock);
1331
1332 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001333 &cfg->fc_dst, cfg->fc_dst_len,
1334 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (fn) {
Eric Dumazet7cc48262007-02-09 16:22:57 -08001337 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001338 if (cfg->fc_ifindex &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 (rt->rt6i_dev == NULL ||
Thomas Graf86872cb2006-08-22 00:01:08 -07001340 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001342 if (cfg->fc_flags & RTF_GATEWAY &&
1343 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001345 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 continue;
1347 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001348 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Thomas Graf86872cb2006-08-22 00:01:08 -07001350 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001353 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 return err;
1356}
1357
1358/*
1359 * Handle redirects
1360 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001361struct ip6rd_flowi {
1362 struct flowi fl;
1363 struct in6_addr gateway;
1364};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001366static struct rt6_info *__ip6_route_redirect(struct net *net,
1367 struct fib6_table *table,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001368 struct flowi *fl,
1369 int flags)
1370{
1371 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1372 struct rt6_info *rt;
1373 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001376 * Get the "current" route for this destination and
1377 * check if the redirect has come from approriate router.
1378 *
1379 * RFC 2461 specifies that redirects should only be
1380 * accepted if they come from the nexthop to the target.
1381 * Due to the way the routes are chosen, this notion
1382 * is a bit fuzzy and one might need to check all possible
1383 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Thomas Grafc71099a2006-08-04 23:20:06 -07001386 read_lock_bh(&table->tb6_lock);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001387 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001388restart:
Eric Dumazet7cc48262007-02-09 16:22:57 -08001389 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001390 /*
1391 * Current route is on-link; redirect is always invalid.
1392 *
1393 * Seems, previous statement is not true. It could
1394 * be node, which looks for us as on-link (f.e. proxy ndisc)
1395 * But then router serving it might decide, that we should
1396 * know truth 8)8) --ANK (980726).
1397 */
1398 if (rt6_check_expired(rt))
1399 continue;
1400 if (!(rt->rt6i_flags & RTF_GATEWAY))
1401 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001402 if (fl->oif != rt->rt6i_dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001403 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001404 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001405 continue;
1406 break;
1407 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001408
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001409 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001410 rt = net->ipv6.ip6_null_entry;
1411 BACKTRACK(net, &fl->fl6_src);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001412out:
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001413 dst_hold(&rt->u.dst);
1414
1415 read_unlock_bh(&table->tb6_lock);
1416
1417 return rt;
1418};
1419
1420static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1421 struct in6_addr *src,
1422 struct in6_addr *gateway,
1423 struct net_device *dev)
1424{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001425 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001426 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001427 struct ip6rd_flowi rdfl = {
1428 .fl = {
1429 .oif = dev->ifindex,
1430 .nl_u = {
1431 .ip6_u = {
1432 .daddr = *dest,
1433 .saddr = *src,
1434 },
1435 },
1436 },
1437 .gateway = *gateway,
1438 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001439
1440 if (rt6_need_strict(dest))
1441 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001442
Daniel Lezcano55786892008-03-04 13:47:47 -08001443 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001444 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001445}
1446
1447void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1448 struct in6_addr *saddr,
1449 struct neighbour *neigh, u8 *lladdr, int on_link)
1450{
1451 struct rt6_info *rt, *nrt = NULL;
1452 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001453 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001454
1455 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1456
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001457 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (net_ratelimit())
1459 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1460 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * We have finally decided to accept it.
1466 */
1467
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001468 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1470 NEIGH_UPDATE_F_OVERRIDE|
1471 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1472 NEIGH_UPDATE_F_ISROUTER))
1473 );
1474
1475 /*
1476 * Redirect received -> path was valid.
1477 * Look, redirects are sent only in response to data packets,
1478 * so that this nexthop apparently is reachable. --ANK
1479 */
1480 dst_confirm(&rt->u.dst);
1481
1482 /* Duplicate redirect: silently ignore. */
1483 if (neigh == rt->u.dst.neighbour)
1484 goto out;
1485
1486 nrt = ip6_rt_copy(rt);
1487 if (nrt == NULL)
1488 goto out;
1489
1490 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1491 if (on_link)
1492 nrt->rt6i_flags &= ~RTF_GATEWAY;
1493
1494 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1495 nrt->rt6i_dst.plen = 128;
1496 nrt->u.dst.flags |= DST_HOST;
1497
1498 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1499 nrt->rt6i_nexthop = neigh_clone(neigh);
1500 /* Reset pmtu, it may be better */
1501 nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001502 nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
Daniel Lezcano55786892008-03-04 13:47:47 -08001503 dst_mtu(&nrt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Thomas Graf40e22e82006-08-22 00:00:45 -07001505 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto out;
1507
Tom Tucker8d717402006-07-30 20:43:36 -07001508 netevent.old = &rt->u.dst;
1509 netevent.new = &nrt->u.dst;
1510 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (rt->rt6i_flags&RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001513 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return;
1515 }
1516
1517out:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001518 dst_release(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return;
1520}
1521
1522/*
1523 * Handle ICMP "packet too big" messages
1524 * i.e. Path MTU discovery
1525 */
1526
1527void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1528 struct net_device *dev, u32 pmtu)
1529{
1530 struct rt6_info *rt, *nrt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001531 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int allfrag = 0;
1533
Daniel Lezcano55786892008-03-04 13:47:47 -08001534 rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (rt == NULL)
1536 return;
1537
1538 if (pmtu >= dst_mtu(&rt->u.dst))
1539 goto out;
1540
1541 if (pmtu < IPV6_MIN_MTU) {
1542 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001543 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * MTU (1280) and a fragment header should always be included
1545 * after a node receiving Too Big message reporting PMTU is
1546 * less than the IPv6 Minimum Link MTU.
1547 */
1548 pmtu = IPV6_MIN_MTU;
1549 allfrag = 1;
1550 }
1551
1552 /* New mtu received -> path was valid.
1553 They are sent only in response to data packets,
1554 so that this nexthop apparently is reachable. --ANK
1555 */
1556 dst_confirm(&rt->u.dst);
1557
1558 /* Host route. If it is static, it would be better
1559 not to override it, but add new one, so that
1560 when cache entry will expire old pmtu
1561 would return automatically.
1562 */
1563 if (rt->rt6i_flags & RTF_CACHE) {
1564 rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1565 if (allfrag)
1566 rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
Daniel Lezcano55786892008-03-04 13:47:47 -08001567 dst_set_expires(&rt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1569 goto out;
1570 }
1571
1572 /* Network route.
1573 Two cases are possible:
1574 1. It is connected route. Action: COW
1575 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1576 */
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001577 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001578 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001579 else
1580 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001581
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001582 if (nrt) {
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001583 nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1584 if (allfrag)
1585 nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1586
1587 /* According to RFC 1981, detecting PMTU increase shouldn't be
1588 * happened within 5 mins, the recommended timer is 10 mins.
1589 * Here this route expiration time is set to ip6_rt_mtu_expires
1590 * which is 10 mins. After 10 mins the decreased pmtu is expired
1591 * and detecting PMTU increase will be automatically happened.
1592 */
Daniel Lezcano55786892008-03-04 13:47:47 -08001593 dst_set_expires(&nrt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001594 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1595
Thomas Graf40e22e82006-08-22 00:00:45 -07001596 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598out:
1599 dst_release(&rt->u.dst);
1600}
1601
1602/*
1603 * Misc support functions
1604 */
1605
1606static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1607{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001608 struct net *net = dev_net(ort->rt6i_dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001609 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 if (rt) {
1612 rt->u.dst.input = ort->u.dst.input;
1613 rt->u.dst.output = ort->u.dst.output;
1614
1615 memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
Ville Nuorvala22e1e4d2006-10-16 22:14:26 -07001616 rt->u.dst.error = ort->u.dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 rt->u.dst.dev = ort->u.dst.dev;
1618 if (rt->u.dst.dev)
1619 dev_hold(rt->u.dst.dev);
1620 rt->rt6i_idev = ort->rt6i_idev;
1621 if (rt->rt6i_idev)
1622 in6_dev_hold(rt->rt6i_idev);
1623 rt->u.dst.lastuse = jiffies;
1624 rt->rt6i_expires = 0;
1625
1626 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1627 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1628 rt->rt6i_metric = 0;
1629
1630 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1631#ifdef CONFIG_IPV6_SUBTREES
1632 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1633#endif
Thomas Grafc71099a2006-08-04 23:20:06 -07001634 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 return rt;
1637}
1638
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001639#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001640static struct rt6_info *rt6_get_route_info(struct net *net,
1641 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001642 struct in6_addr *gwaddr, int ifindex)
1643{
1644 struct fib6_node *fn;
1645 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001646 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001647
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001648 table = fib6_get_table(net, RT6_TABLE_INFO);
Thomas Grafc71099a2006-08-04 23:20:06 -07001649 if (table == NULL)
1650 return NULL;
1651
1652 write_lock_bh(&table->tb6_lock);
1653 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001654 if (!fn)
1655 goto out;
1656
Eric Dumazet7cc48262007-02-09 16:22:57 -08001657 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001658 if (rt->rt6i_dev->ifindex != ifindex)
1659 continue;
1660 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1661 continue;
1662 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1663 continue;
1664 dst_hold(&rt->u.dst);
1665 break;
1666 }
1667out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001668 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001669 return rt;
1670}
1671
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001672static struct rt6_info *rt6_add_route_info(struct net *net,
1673 struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001674 struct in6_addr *gwaddr, int ifindex,
1675 unsigned pref)
1676{
Thomas Graf86872cb2006-08-22 00:01:08 -07001677 struct fib6_config cfg = {
1678 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001679 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001680 .fc_ifindex = ifindex,
1681 .fc_dst_len = prefixlen,
1682 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1683 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001684 .fc_nlinfo.pid = 0,
1685 .fc_nlinfo.nlh = NULL,
1686 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001687 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001688
Thomas Graf86872cb2006-08-22 00:01:08 -07001689 ipv6_addr_copy(&cfg.fc_dst, prefix);
1690 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1691
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001692 /* We should treat it as a default route if prefix length is 0. */
1693 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001694 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001695
Thomas Graf86872cb2006-08-22 00:01:08 -07001696 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001697
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001698 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001699}
1700#endif
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001705 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001707 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001708 if (table == NULL)
1709 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Thomas Grafc71099a2006-08-04 23:20:06 -07001711 write_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001712 for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (dev == rt->rt6i_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001714 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1716 break;
1717 }
1718 if (rt)
1719 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001720 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return rt;
1722}
1723
1724struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001725 struct net_device *dev,
1726 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Thomas Graf86872cb2006-08-22 00:01:08 -07001728 struct fib6_config cfg = {
1729 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001730 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001731 .fc_ifindex = dev->ifindex,
1732 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1733 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001734 .fc_nlinfo.pid = 0,
1735 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001736 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Thomas Graf86872cb2006-08-22 00:01:08 -07001739 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Thomas Graf86872cb2006-08-22 00:01:08 -07001741 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return rt6_get_dflt_router(gwaddr, dev);
1744}
1745
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001746void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001749 struct fib6_table *table;
1750
1751 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001752 table = fib6_get_table(net, RT6_TABLE_DFLT);
Thomas Grafc71099a2006-08-04 23:20:06 -07001753 if (table == NULL)
1754 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001757 read_lock_bh(&table->tb6_lock);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001758 for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1760 dst_hold(&rt->u.dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001761 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001762 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto restart;
1764 }
1765 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001766 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Daniel Lezcano55786892008-03-04 13:47:47 -08001769static void rtmsg_to_fib6_config(struct net *net,
1770 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001771 struct fib6_config *cfg)
1772{
1773 memset(cfg, 0, sizeof(*cfg));
1774
1775 cfg->fc_table = RT6_TABLE_MAIN;
1776 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1777 cfg->fc_metric = rtmsg->rtmsg_metric;
1778 cfg->fc_expires = rtmsg->rtmsg_info;
1779 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1780 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1781 cfg->fc_flags = rtmsg->rtmsg_flags;
1782
Daniel Lezcano55786892008-03-04 13:47:47 -08001783 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001784
Thomas Graf86872cb2006-08-22 00:01:08 -07001785 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1786 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1787 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1788}
1789
Daniel Lezcano55786892008-03-04 13:47:47 -08001790int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Thomas Graf86872cb2006-08-22 00:01:08 -07001792 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct in6_rtmsg rtmsg;
1794 int err;
1795
1796 switch(cmd) {
1797 case SIOCADDRT: /* Add a route */
1798 case SIOCDELRT: /* Delete a route */
1799 if (!capable(CAP_NET_ADMIN))
1800 return -EPERM;
1801 err = copy_from_user(&rtmsg, arg,
1802 sizeof(struct in6_rtmsg));
1803 if (err)
1804 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001805
Daniel Lezcano55786892008-03-04 13:47:47 -08001806 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rtnl_lock();
1809 switch (cmd) {
1810 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001811 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 break;
1813 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
1816 default:
1817 err = -EINVAL;
1818 }
1819 rtnl_unlock();
1820
1821 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 return -EINVAL;
1825}
1826
1827/*
1828 * Drop the packet on the floor
1829 */
1830
Ilpo Järvinen50eb4312008-01-12 03:21:00 -08001831static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001833 int type;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001834 struct dst_entry *dst = skb->dst;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001835 switch (ipstats_mib_noroutes) {
1836 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001837 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001838 if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001839 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1840 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001841 break;
1842 }
1843 /* FALLTHROUGH */
1844 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001845 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1846 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001847 break;
1848 }
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001849 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 kfree_skb(skb);
1851 return 0;
1852}
1853
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001854static int ip6_pkt_discard(struct sk_buff *skb)
1855{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001856 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001857}
1858
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001859static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001862 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
David S. Miller6723ab52006-10-18 21:20:57 -07001865#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1866
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001867static int ip6_pkt_prohibit(struct sk_buff *skb)
1868{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001869 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001870}
1871
1872static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1873{
1874 skb->dev = skb->dst->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07001875 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07001876}
1877
David S. Miller6723ab52006-10-18 21:20:57 -07001878#endif
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * Allocate a dst for local (unicast / anycast) address.
1882 */
1883
1884struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1885 const struct in6_addr *addr,
1886 int anycast)
1887{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001888 struct net *net = dev_net(idev->dev);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08001889 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (rt == NULL)
1892 return ERR_PTR(-ENOMEM);
1893
Daniel Lezcano55786892008-03-04 13:47:47 -08001894 dev_hold(net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 in6_dev_hold(idev);
1896
1897 rt->u.dst.flags = DST_HOST;
1898 rt->u.dst.input = ip6_input;
1899 rt->u.dst.output = ip6_output;
Daniel Lezcano55786892008-03-04 13:47:47 -08001900 rt->rt6i_dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rt->rt6i_idev = idev;
1902 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
Daniel Lezcano55786892008-03-04 13:47:47 -08001903 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
1905 rt->u.dst.obsolete = -1;
1906
1907 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09001908 if (anycast)
1909 rt->rt6i_flags |= RTF_ANYCAST;
1910 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 rt->rt6i_flags |= RTF_LOCAL;
1912 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1913 if (rt->rt6i_nexthop == NULL) {
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +09001914 dst_free(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return ERR_PTR(-ENOMEM);
1916 }
1917
1918 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1919 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08001920 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 atomic_set(&rt->u.dst.__refcnt, 1);
1923
1924 return rt;
1925}
1926
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001927struct arg_dev_net {
1928 struct net_device *dev;
1929 struct net *net;
1930};
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932static int fib6_ifdown(struct rt6_info *rt, void *arg)
1933{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001934 struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
1935 struct net *net = ((struct arg_dev_net *)arg)->net;
1936
1937 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
1938 rt != net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 RT6_TRACE("deleted by ifdown %p\n", rt);
1940 return -1;
1941 }
1942 return 0;
1943}
1944
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001945void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001947 struct arg_dev_net adn = {
1948 .dev = dev,
1949 .net = net,
1950 };
1951
1952 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07001953 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956struct rt6_mtu_change_arg
1957{
1958 struct net_device *dev;
1959 unsigned mtu;
1960};
1961
1962static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
1963{
1964 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
1965 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001966 struct net *net = dev_net(arg->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* In IPv6 pmtu discovery is not optional,
1969 so that RTAX_MTU lock cannot disable it.
1970 We still use this lock to block changes
1971 caused by addrconf/ndisc.
1972 */
1973
1974 idev = __in6_dev_get(arg->dev);
1975 if (idev == NULL)
1976 return 0;
1977
1978 /* For administrative MTU increase, there is no way to discover
1979 IPv6 PMTU increase, so PMTU increase should be updated here.
1980 Since RFC 1981 doesn't include administrative MTU increase
1981 update PMTU increase is a MUST. (i.e. jumbo frame)
1982 */
1983 /*
1984 If new MTU is less than route PMTU, this new MTU will be the
1985 lowest MTU in the path, update the route PMTU to reflect PMTU
1986 decreases; if new MTU is greater than route PMTU, and the
1987 old MTU is the lowest MTU in the path, update the route PMTU
1988 to reflect the increase. In this case if the other nodes' MTU
1989 also have the lowest MTU, TOO BIG MESSAGE will be lead to
1990 PMTU discouvery.
1991 */
1992 if (rt->rt6i_dev == arg->dev &&
1993 !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
Jim Paris23717792008-01-31 16:36:25 -08001994 (dst_mtu(&rt->u.dst) >= arg->mtu ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001995 (dst_mtu(&rt->u.dst) < arg->mtu &&
Simon Arlott566cfd82007-07-26 00:09:55 -07001996 dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
Daniel Lezcano55786892008-03-04 13:47:47 -08001998 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07001999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return 0;
2001}
2002
2003void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2004{
Thomas Grafc71099a2006-08-04 23:20:06 -07002005 struct rt6_mtu_change_arg arg = {
2006 .dev = dev,
2007 .mtu = mtu,
2008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002010 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002013static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002014 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002015 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002016 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002017 [RTA_PRIORITY] = { .type = NLA_U32 },
2018 [RTA_METRICS] = { .type = NLA_NESTED },
2019};
2020
2021static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2022 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Thomas Graf86872cb2006-08-22 00:01:08 -07002024 struct rtmsg *rtm;
2025 struct nlattr *tb[RTA_MAX+1];
2026 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Thomas Graf86872cb2006-08-22 00:01:08 -07002028 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2029 if (err < 0)
2030 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Thomas Graf86872cb2006-08-22 00:01:08 -07002032 err = -EINVAL;
2033 rtm = nlmsg_data(nlh);
2034 memset(cfg, 0, sizeof(*cfg));
2035
2036 cfg->fc_table = rtm->rtm_table;
2037 cfg->fc_dst_len = rtm->rtm_dst_len;
2038 cfg->fc_src_len = rtm->rtm_src_len;
2039 cfg->fc_flags = RTF_UP;
2040 cfg->fc_protocol = rtm->rtm_protocol;
2041
2042 if (rtm->rtm_type == RTN_UNREACHABLE)
2043 cfg->fc_flags |= RTF_REJECT;
2044
2045 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2046 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002047 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002048
2049 if (tb[RTA_GATEWAY]) {
2050 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2051 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002053
2054 if (tb[RTA_DST]) {
2055 int plen = (rtm->rtm_dst_len + 7) >> 3;
2056
2057 if (nla_len(tb[RTA_DST]) < plen)
2058 goto errout;
2059
2060 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002062
2063 if (tb[RTA_SRC]) {
2064 int plen = (rtm->rtm_src_len + 7) >> 3;
2065
2066 if (nla_len(tb[RTA_SRC]) < plen)
2067 goto errout;
2068
2069 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002071
2072 if (tb[RTA_OIF])
2073 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2074
2075 if (tb[RTA_PRIORITY])
2076 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2077
2078 if (tb[RTA_METRICS]) {
2079 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2080 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002082
2083 if (tb[RTA_TABLE])
2084 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2085
2086 err = 0;
2087errout:
2088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Thomas Grafc127ea22007-03-22 11:58:32 -07002091static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Thomas Graf86872cb2006-08-22 00:01:08 -07002093 struct fib6_config cfg;
2094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Thomas Graf86872cb2006-08-22 00:01:08 -07002096 err = rtm_to_fib6_config(skb, nlh, &cfg);
2097 if (err < 0)
2098 return err;
2099
2100 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Thomas Grafc127ea22007-03-22 11:58:32 -07002103static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Thomas Graf86872cb2006-08-22 00:01:08 -07002105 struct fib6_config cfg;
2106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Thomas Graf86872cb2006-08-22 00:01:08 -07002108 err = rtm_to_fib6_config(skb, nlh, &cfg);
2109 if (err < 0)
2110 return err;
2111
2112 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Thomas Graf339bf982006-11-10 14:10:15 -08002115static inline size_t rt6_nlmsg_size(void)
2116{
2117 return NLMSG_ALIGN(sizeof(struct rtmsg))
2118 + nla_total_size(16) /* RTA_SRC */
2119 + nla_total_size(16) /* RTA_DST */
2120 + nla_total_size(16) /* RTA_GATEWAY */
2121 + nla_total_size(16) /* RTA_PREFSRC */
2122 + nla_total_size(4) /* RTA_TABLE */
2123 + nla_total_size(4) /* RTA_IIF */
2124 + nla_total_size(4) /* RTA_OIF */
2125 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002126 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002127 + nla_total_size(sizeof(struct rta_cacheinfo));
2128}
2129
Brian Haley191cd582008-08-14 15:33:21 -07002130static int rt6_fill_node(struct net *net,
2131 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002132 struct in6_addr *dst, struct in6_addr *src,
2133 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002134 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002137 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002138 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002139 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (prefix) { /* user wants prefix routes only */
2142 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2143 /* success since this is not a prefix route */
2144 return 1;
2145 }
2146 }
2147
Thomas Graf2d7202b2006-08-22 00:01:27 -07002148 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2149 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002150 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002151
2152 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 rtm->rtm_family = AF_INET6;
2154 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2155 rtm->rtm_src_len = rt->rt6i_src.plen;
2156 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002157 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002158 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002159 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002160 table = RT6_TABLE_UNSPEC;
2161 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002162 NLA_PUT_U32(skb, RTA_TABLE, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (rt->rt6i_flags&RTF_REJECT)
2164 rtm->rtm_type = RTN_UNREACHABLE;
2165 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2166 rtm->rtm_type = RTN_LOCAL;
2167 else
2168 rtm->rtm_type = RTN_UNICAST;
2169 rtm->rtm_flags = 0;
2170 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2171 rtm->rtm_protocol = rt->rt6i_protocol;
2172 if (rt->rt6i_flags&RTF_DYNAMIC)
2173 rtm->rtm_protocol = RTPROT_REDIRECT;
2174 else if (rt->rt6i_flags & RTF_ADDRCONF)
2175 rtm->rtm_protocol = RTPROT_KERNEL;
2176 else if (rt->rt6i_flags&RTF_DEFAULT)
2177 rtm->rtm_protocol = RTPROT_RA;
2178
2179 if (rt->rt6i_flags&RTF_CACHE)
2180 rtm->rtm_flags |= RTM_F_CLONED;
2181
2182 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002183 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002184 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002186 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifdef CONFIG_IPV6_SUBTREES
2188 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002189 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002190 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002192 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002194 if (iif) {
2195#ifdef CONFIG_IPV6_MROUTE
2196 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002197 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002198 if (err <= 0) {
2199 if (!nowait) {
2200 if (err == 0)
2201 return 0;
2202 goto nla_put_failure;
2203 } else {
2204 if (err == -EMSGSIZE)
2205 goto nla_put_failure;
2206 }
2207 }
2208 } else
2209#endif
2210 NLA_PUT_U32(skb, RTA_IIF, iif);
2211 } else if (dst) {
Brian Haley5e0115e2008-08-13 01:58:57 -07002212 struct inet6_dev *idev = ip6_dst_idev(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct in6_addr saddr_buf;
Brian Haley191cd582008-08-14 15:33:21 -07002214 if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09002215 dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002216 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002220 goto nla_put_failure;
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (rt->u.dst.neighbour)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002223 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (rt->u.dst.dev)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002226 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2227
2228 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002229
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002230 if (!(rt->rt6i_flags & RTF_EXPIRES))
2231 expires = 0;
2232 else if (rt->rt6i_expires - jiffies < INT_MAX)
2233 expires = rt->rt6i_expires - jiffies;
2234 else
2235 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002236
Thomas Grafe3703b32006-11-27 09:27:07 -08002237 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2238 expires, rt->u.dst.error) < 0)
2239 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Thomas Graf2d7202b2006-08-22 00:01:27 -07002241 return nlmsg_end(skb, nlh);
2242
2243nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002244 nlmsg_cancel(skb, nlh);
2245 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Patrick McHardy1b43af52006-08-10 23:11:17 -07002248int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2251 int prefix;
2252
Thomas Graf2d7202b2006-08-22 00:01:27 -07002253 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2254 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2256 } else
2257 prefix = 0;
2258
Brian Haley191cd582008-08-14 15:33:21 -07002259 return rt6_fill_node(arg->net,
2260 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002262 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Thomas Grafc127ea22007-03-22 11:58:32 -07002265static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002267 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002268 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002270 struct sk_buff *skb;
2271 struct rtmsg *rtm;
2272 struct flowi fl;
2273 int err, iif = 0;
2274
2275 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2276 if (err < 0)
2277 goto errout;
2278
2279 err = -EINVAL;
2280 memset(&fl, 0, sizeof(fl));
2281
2282 if (tb[RTA_SRC]) {
2283 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2284 goto errout;
2285
2286 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2287 }
2288
2289 if (tb[RTA_DST]) {
2290 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2291 goto errout;
2292
2293 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2294 }
2295
2296 if (tb[RTA_IIF])
2297 iif = nla_get_u32(tb[RTA_IIF]);
2298
2299 if (tb[RTA_OIF])
2300 fl.oif = nla_get_u32(tb[RTA_OIF]);
2301
2302 if (iif) {
2303 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002304 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002305 if (!dev) {
2306 err = -ENODEV;
2307 goto errout;
2308 }
2309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafab364a62006-08-22 00:01:47 -07002312 if (skb == NULL) {
2313 err = -ENOBUFS;
2314 goto errout;
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Reserve room for dummy headers, this skb can pass
2318 through good chunk of routing engine.
2319 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002320 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2322
Daniel Lezcano8a3edd82008-03-07 11:14:16 -08002323 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 skb->dst = &rt->u.dst;
2325
Brian Haley191cd582008-08-14 15:33:21 -07002326 err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002328 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002330 kfree_skb(skb);
2331 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333
Daniel Lezcano55786892008-03-04 13:47:47 -08002334 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002335errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Thomas Graf86872cb2006-08-22 00:01:08 -07002339void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002342 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002343 u32 seq;
2344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002346 err = -ENOBUFS;
2347 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002348
Thomas Graf339bf982006-11-10 14:10:15 -08002349 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002350 if (skb == NULL)
2351 goto errout;
2352
Brian Haley191cd582008-08-14 15:33:21 -07002353 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002354 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002355 if (err < 0) {
2356 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2357 WARN_ON(err == -EMSGSIZE);
2358 kfree_skb(skb);
2359 goto errout;
2360 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002361 err = rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2362 info->nlh, gfp_any());
Thomas Graf21713eb2006-08-15 00:35:24 -07002363errout:
2364 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002365 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002368static int ip6_route_dev_notify(struct notifier_block *this,
2369 unsigned long event, void *data)
2370{
2371 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002372 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002373
2374 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2375 net->ipv6.ip6_null_entry->u.dst.dev = dev;
2376 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2377#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2378 net->ipv6.ip6_prohibit_entry->u.dst.dev = dev;
2379 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2380 net->ipv6.ip6_blk_hole_entry->u.dst.dev = dev;
2381 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2382#endif
2383 }
2384
2385 return NOTIFY_OK;
2386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/*
2389 * /proc
2390 */
2391
2392#ifdef CONFIG_PROC_FS
2393
2394#define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
2395
2396struct rt6_proc_arg
2397{
2398 char *buffer;
2399 int offset;
2400 int length;
2401 int skip;
2402 int len;
2403};
2404
2405static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2406{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002407 struct seq_file *m = p_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002409 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002412 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002414 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415#endif
2416
2417 if (rt->rt6i_nexthop) {
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002418 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002420 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002422 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2423 rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
2424 rt->u.dst.__use, rt->rt6i_flags,
2425 rt->rt6i_dev ? rt->rt6i_dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427}
2428
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002429static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002431 struct net *net = (struct net *)m->private;
2432 fib6_clean_all(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002436static int ipv6_route_open(struct inode *inode, struct file *file)
2437{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002438 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002439}
2440
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002441static const struct file_operations ipv6_route_proc_fops = {
2442 .owner = THIS_MODULE,
2443 .open = ipv6_route_open,
2444 .read = seq_read,
2445 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002446 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002447};
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2450{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002451 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002453 net->ipv6.rt6_stats->fib_nodes,
2454 net->ipv6.rt6_stats->fib_route_nodes,
2455 net->ipv6.rt6_stats->fib_rt_alloc,
2456 net->ipv6.rt6_stats->fib_rt_entries,
2457 net->ipv6.rt6_stats->fib_rt_cache,
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002458 atomic_read(&net->ipv6.ip6_dst_ops->entries),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002459 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 return 0;
2462}
2463
2464static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2465{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002466 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002467}
2468
Arjan van de Ven9a321442007-02-12 00:55:35 -08002469static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .owner = THIS_MODULE,
2471 .open = rt6_stats_seq_open,
2472 .read = seq_read,
2473 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002474 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475};
2476#endif /* CONFIG_PROC_FS */
2477
2478#ifdef CONFIG_SYSCTL
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480static
2481int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
2482 void __user *buffer, size_t *lenp, loff_t *ppos)
2483{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002484 struct net *net = current->nsproxy->net_ns;
2485 int delay = net->ipv6.sysctl.flush_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (write) {
2487 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002488 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return 0;
2490 } else
2491 return -EINVAL;
2492}
2493
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002494ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002495 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002497 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002499 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002500 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 },
2502 {
2503 .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
2504 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002505 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .maxlen = sizeof(int),
2507 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002508 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 },
2510 {
2511 .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
2512 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002513 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .maxlen = sizeof(int),
2515 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002516 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 },
2518 {
2519 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
2520 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002521 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 .maxlen = sizeof(int),
2523 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002524 .proc_handler = proc_dointvec_jiffies,
2525 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
2527 {
2528 .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
2529 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002530 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 .maxlen = sizeof(int),
2532 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002533 .proc_handler = proc_dointvec_jiffies,
2534 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 },
2536 {
2537 .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
2538 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002539 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .maxlen = sizeof(int),
2541 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002542 .proc_handler = proc_dointvec_jiffies,
2543 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 },
2545 {
2546 .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
2547 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002548 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .maxlen = sizeof(int),
2550 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002551 .proc_handler = proc_dointvec_jiffies,
2552 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 },
2554 {
2555 .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
2556 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002557 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 .maxlen = sizeof(int),
2559 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002560 .proc_handler = proc_dointvec_jiffies,
2561 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 },
2563 {
2564 .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
2565 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002566 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 .maxlen = sizeof(int),
2568 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002569 .proc_handler = proc_dointvec_jiffies,
2570 .strategy = sysctl_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 },
2572 {
2573 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
2574 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002575 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 .maxlen = sizeof(int),
2577 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002578 .proc_handler = proc_dointvec_ms_jiffies,
2579 .strategy = sysctl_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 },
2581 { .ctl_name = 0 }
2582};
2583
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002584struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2585{
2586 struct ctl_table *table;
2587
2588 table = kmemdup(ipv6_route_table_template,
2589 sizeof(ipv6_route_table_template),
2590 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002591
2592 if (table) {
2593 table[0].data = &net->ipv6.sysctl.flush_delay;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002594 table[1].data = &net->ipv6.ip6_dst_ops->gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002595 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2596 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2597 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2598 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2599 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2600 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2601 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2602 }
2603
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002604 return table;
2605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606#endif
2607
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002608static int ip6_route_net_init(struct net *net)
2609{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002610 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002611
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002612 net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
2613 sizeof(*net->ipv6.ip6_dst_ops),
2614 GFP_KERNEL);
2615 if (!net->ipv6.ip6_dst_ops)
2616 goto out;
Denis V. Lunev48115be2008-04-16 02:01:34 -07002617 net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002618
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002619 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2620 sizeof(*net->ipv6.ip6_null_entry),
2621 GFP_KERNEL);
2622 if (!net->ipv6.ip6_null_entry)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002623 goto out_ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002624 net->ipv6.ip6_null_entry->u.dst.path =
2625 (struct dst_entry *)net->ipv6.ip6_null_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002626 net->ipv6.ip6_null_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002627
2628#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2629 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2630 sizeof(*net->ipv6.ip6_prohibit_entry),
2631 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002632 if (!net->ipv6.ip6_prohibit_entry)
2633 goto out_ip6_null_entry;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002634 net->ipv6.ip6_prohibit_entry->u.dst.path =
2635 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002636 net->ipv6.ip6_prohibit_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002637
2638 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2639 sizeof(*net->ipv6.ip6_blk_hole_entry),
2640 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002641 if (!net->ipv6.ip6_blk_hole_entry)
2642 goto out_ip6_prohibit_entry;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002643 net->ipv6.ip6_blk_hole_entry->u.dst.path =
2644 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002645 net->ipv6.ip6_blk_hole_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002646#endif
2647
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002648 net->ipv6.sysctl.flush_delay = 0;
2649 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2650 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2651 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2652 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2653 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2654 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2655 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2656
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002657#ifdef CONFIG_PROC_FS
2658 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2659 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2660#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002661 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2662
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002663 ret = 0;
2664out:
2665 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002666
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002667#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2668out_ip6_prohibit_entry:
2669 kfree(net->ipv6.ip6_prohibit_entry);
2670out_ip6_null_entry:
2671 kfree(net->ipv6.ip6_null_entry);
2672#endif
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002673out_ip6_dst_ops:
Denis V. Lunev48115be2008-04-16 02:01:34 -07002674 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002675 kfree(net->ipv6.ip6_dst_ops);
2676 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002677}
2678
2679static void ip6_route_net_exit(struct net *net)
2680{
2681#ifdef CONFIG_PROC_FS
2682 proc_net_remove(net, "ipv6_route");
2683 proc_net_remove(net, "rt6_stats");
2684#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002685 kfree(net->ipv6.ip6_null_entry);
2686#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2687 kfree(net->ipv6.ip6_prohibit_entry);
2688 kfree(net->ipv6.ip6_blk_hole_entry);
2689#endif
Denis V. Lunev48115be2008-04-16 02:01:34 -07002690 release_net(net->ipv6.ip6_dst_ops->dst_net);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002691 kfree(net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002692}
2693
2694static struct pernet_operations ip6_route_net_ops = {
2695 .init = ip6_route_net_init,
2696 .exit = ip6_route_net_exit,
2697};
2698
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002699static struct notifier_block ip6_route_dev_notifier = {
2700 .notifier_call = ip6_route_dev_notify,
2701 .priority = 0,
2702};
2703
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002704int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002706 int ret;
2707
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002708 ret = -ENOMEM;
2709 ip6_dst_ops_template.kmem_cachep =
2710 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2711 SLAB_HWCACHE_ALIGN, NULL);
2712 if (!ip6_dst_ops_template.kmem_cachep)
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002713 goto out;;
David S. Miller14e50e52007-05-24 18:17:54 -07002714
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002715 ret = register_pernet_subsys(&ip6_route_net_ops);
2716 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002717 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002718
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002719 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2720
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002721 /* Registering of the loopback is done before this portion of code,
2722 * the loopback reference in rt6_info will not be taken, do it
2723 * manually for init_net */
2724 init_net.ipv6.ip6_null_entry->u.dst.dev = init_net.loopback_dev;
2725 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2726 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2727 init_net.ipv6.ip6_prohibit_entry->u.dst.dev = init_net.loopback_dev;
2728 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2729 init_net.ipv6.ip6_blk_hole_entry->u.dst.dev = init_net.loopback_dev;
2730 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2731 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002732 ret = fib6_init();
2733 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002734 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002735
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002736 ret = xfrm6_init();
2737 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002738 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08002739
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002740 ret = fib6_rules_init();
2741 if (ret)
2742 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08002743
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002744 ret = -ENOBUFS;
2745 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2746 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2747 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2748 goto fib6_rules_init;
2749
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002750 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002751 if (ret)
2752 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002753
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002754out:
2755 return ret;
2756
2757fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002758 fib6_rules_cleanup();
2759xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002760 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002761out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002762 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002763out_register_subsys:
2764 unregister_pernet_subsys(&ip6_route_net_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002765out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002766 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770void ip6_route_cleanup(void)
2771{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002772 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07002773 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002776 unregister_pernet_subsys(&ip6_route_net_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002777 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}