blob: 7d7f30697eadee6773272c5e68ae006b21e38511 [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/times.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
34#include <linux/net.h>
35#include <linux/route.h>
36#include <linux/netdevice.h>
37#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090038#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080043#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/snmp.h>
47#include <net/ipv6.h>
48#include <net/ip6_fib.h>
49#include <net/ip6_route.h>
50#include <net/ndisc.h>
51#include <net/addrconf.h>
52#include <net/tcp.h>
53#include <linux/rtnetlink.h>
54#include <net/dst.h>
55#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <asm/uaccess.h>
60
61#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
64
Eric Dumazet21efcfa2011-07-19 20:18:36 +000065static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
66 const struct in6_addr *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080068static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000069static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static struct dst_entry *ip6_negative_advice(struct dst_entry *);
71static void ip6_dst_destroy(struct dst_entry *);
72static void ip6_dst_ifdown(struct dst_entry *,
73 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080074static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static int ip6_pkt_discard(struct sk_buff *skb);
77static int ip6_pkt_discard_out(struct sk_buff *skb);
78static void ip6_link_failure(struct sk_buff *skb);
79static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
80
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080081#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080082static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000083 const struct in6_addr *prefix, int prefixlen,
84 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080085 unsigned pref);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -080086static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000087 const struct in6_addr *prefix, int prefixlen,
88 const struct in6_addr *gwaddr, int ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080089#endif
90
David S. Miller06582542011-01-27 14:58:42 -080091static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
92{
93 struct rt6_info *rt = (struct rt6_info *) dst;
94 struct inet_peer *peer;
95 u32 *p = NULL;
96
Yan, Zheng8e2ec632011-09-05 21:34:30 +000097 if (!(rt->dst.flags & DST_HOST))
98 return NULL;
99
David S. Miller06582542011-01-27 14:58:42 -0800100 if (!rt->rt6i_peer)
101 rt6_bind_peer(rt, 1);
102
103 peer = rt->rt6i_peer;
104 if (peer) {
105 u32 *old_p = __DST_METRICS_PTR(old);
106 unsigned long prev, new;
107
108 p = peer->metrics;
109 if (inet_metrics_new(peer))
110 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
111
112 new = (unsigned long) p;
113 prev = cmpxchg(&dst->_metrics, old, new);
114
115 if (prev != old) {
116 p = __DST_METRICS_PTR(prev);
117 if (prev & DST_METRICS_READ_ONLY)
118 p = NULL;
119 }
120 }
121 return p;
122}
123
David S. Miller39232972012-01-26 15:22:32 -0500124static inline const void *choose_neigh_daddr(struct rt6_info *rt, const void *daddr)
125{
126 struct in6_addr *p = &rt->rt6i_gateway;
127
128 if (p->s6_addr32[0] | p->s6_addr32[1] |
129 p->s6_addr32[2] | p->s6_addr32[3])
130 return (const void *) p;
131 return daddr;
132}
133
David S. Millerd3aaeb32011-07-18 00:40:17 -0700134static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
135{
David S. Miller39232972012-01-26 15:22:32 -0500136 struct rt6_info *rt = (struct rt6_info *) dst;
137 struct neighbour *n;
138
139 daddr = choose_neigh_daddr(rt, daddr);
140 n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500141 if (n)
142 return n;
143 return neigh_create(&nd_tbl, daddr, dst->dev);
144}
145
David S. Miller8ade06c2011-12-29 18:51:57 -0500146static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
David S. Millerf83c7792011-12-28 15:41:23 -0500147{
David S. Miller8ade06c2011-12-29 18:51:57 -0500148 struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
149 if (!n) {
150 n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
151 if (IS_ERR(n))
152 return PTR_ERR(n);
153 }
David S. Millerf83c7792011-12-28 15:41:23 -0500154 dst_set_neighbour(&rt->dst, n);
155
156 return 0;
David S. Millerd3aaeb32011-07-18 00:40:17 -0700157}
158
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800159static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800161 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .gc = ip6_dst_gc,
163 .gc_thresh = 1024,
164 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800165 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000166 .mtu = ip6_mtu,
David S. Miller06582542011-01-27 14:58:42 -0800167 .cow_metrics = ipv6_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .destroy = ip6_dst_destroy,
169 .ifdown = ip6_dst_ifdown,
170 .negative_advice = ip6_negative_advice,
171 .link_failure = ip6_link_failure,
172 .update_pmtu = ip6_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700173 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700174 .neigh_lookup = ip6_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Steffen Klassertebb762f2011-11-23 02:12:51 +0000177static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800178{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000179 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
180
181 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800182}
183
David S. Miller14e50e52007-05-24 18:17:54 -0700184static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
185{
186}
187
Held Bernhard0972ddb2011-04-24 22:07:32 +0000188static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
189 unsigned long old)
190{
191 return NULL;
192}
193
David S. Miller14e50e52007-05-24 18:17:54 -0700194static struct dst_ops ip6_dst_blackhole_ops = {
195 .family = AF_INET6,
Harvey Harrison09640e632009-02-01 00:45:17 -0800196 .protocol = cpu_to_be16(ETH_P_IPV6),
David S. Miller14e50e52007-05-24 18:17:54 -0700197 .destroy = ip6_dst_destroy,
198 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000199 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800200 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700201 .update_pmtu = ip6_rt_blackhole_update_pmtu,
Held Bernhard0972ddb2011-04-24 22:07:32 +0000202 .cow_metrics = ip6_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700203 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700204};
205
David S. Miller62fa8a82011-01-26 20:51:05 -0800206static const u32 ip6_template_metrics[RTAX_MAX] = {
207 [RTAX_HOPLIMIT - 1] = 255,
208};
209
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800210static struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700211 .dst = {
212 .__refcnt = ATOMIC_INIT(1),
213 .__use = 1,
214 .obsolete = -1,
215 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700216 .input = ip6_pkt_discard,
217 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 },
219 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700220 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .rt6i_metric = ~(u32) 0,
222 .rt6i_ref = ATOMIC_INIT(1),
223};
224
Thomas Graf101367c2006-08-04 03:39:02 -0700225#ifdef CONFIG_IPV6_MULTIPLE_TABLES
226
David S. Miller6723ab52006-10-18 21:20:57 -0700227static int ip6_pkt_prohibit(struct sk_buff *skb);
228static int ip6_pkt_prohibit_out(struct sk_buff *skb);
David S. Miller6723ab52006-10-18 21:20:57 -0700229
Adrian Bunk280a34c2008-04-21 02:29:32 -0700230static struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700231 .dst = {
232 .__refcnt = ATOMIC_INIT(1),
233 .__use = 1,
234 .obsolete = -1,
235 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700236 .input = ip6_pkt_prohibit,
237 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700238 },
239 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700240 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700241 .rt6i_metric = ~(u32) 0,
242 .rt6i_ref = ATOMIC_INIT(1),
243};
244
Daniel Lezcanobdb32892008-03-04 13:48:10 -0800245static struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700246 .dst = {
247 .__refcnt = ATOMIC_INIT(1),
248 .__use = 1,
249 .obsolete = -1,
250 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700251 .input = dst_discard,
252 .output = dst_discard,
Thomas Graf101367c2006-08-04 03:39:02 -0700253 },
254 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700255 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700256 .rt6i_metric = ~(u32) 0,
257 .rt6i_ref = ATOMIC_INIT(1),
258};
259
260#endif
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* allocate dst with ip6_dst_ops */
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700263static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
David S. Miller957c6652011-06-24 15:25:00 -0700264 struct net_device *dev,
265 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David S. Miller957c6652011-06-24 15:25:00 -0700267 struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
David S. Millercf911662011-04-28 14:31:47 -0700268
David S. Miller38308472011-12-03 18:02:47 -0500269 if (rt)
Madalin Bucurfbe58182011-09-26 07:04:56 +0000270 memset(&rt->rt6i_table, 0,
David S. Miller38308472011-12-03 18:02:47 -0500271 sizeof(*rt) - sizeof(struct dst_entry));
David S. Millercf911662011-04-28 14:31:47 -0700272
273 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276static void ip6_dst_destroy(struct dst_entry *dst)
277{
278 struct rt6_info *rt = (struct rt6_info *)dst;
279 struct inet6_dev *idev = rt->rt6i_idev;
David S. Millerb3419362010-11-30 12:27:11 -0800280 struct inet_peer *peer = rt->rt6i_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Yan, Zheng8e2ec632011-09-05 21:34:30 +0000282 if (!(rt->dst.flags & DST_HOST))
283 dst_destroy_metrics_generic(dst);
284
David S. Miller38308472011-12-03 18:02:47 -0500285 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 rt->rt6i_idev = NULL;
287 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900288 }
David S. Millerb3419362010-11-30 12:27:11 -0800289 if (peer) {
David S. Millerb3419362010-11-30 12:27:11 -0800290 rt->rt6i_peer = NULL;
291 inet_putpeer(peer);
292 }
293}
294
David S. Miller6431cbc2011-02-07 20:38:06 -0800295static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
296
297static u32 rt6_peer_genid(void)
298{
299 return atomic_read(&__rt6_peer_genid);
300}
301
David S. Millerb3419362010-11-30 12:27:11 -0800302void rt6_bind_peer(struct rt6_info *rt, int create)
303{
304 struct inet_peer *peer;
305
David S. Millerb3419362010-11-30 12:27:11 -0800306 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
307 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
308 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -0800309 else
310 rt->rt6i_peer_genid = rt6_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
314 int how)
315{
316 struct rt6_info *rt = (struct rt6_info *)dst;
317 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800318 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900319 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David S. Miller38308472011-12-03 18:02:47 -0500321 if (dev != loopback_dev && idev && idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800322 struct inet6_dev *loopback_idev =
323 in6_dev_get(loopback_dev);
David S. Miller38308472011-12-03 18:02:47 -0500324 if (loopback_idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 rt->rt6i_idev = loopback_idev;
326 in6_dev_put(idev);
327 }
328 }
329}
330
331static __inline__ int rt6_check_expired(const struct rt6_info *rt)
332{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000333 return (rt->rt6i_flags & RTF_EXPIRES) &&
David S. Millerd1918542011-12-28 20:19:20 -0500334 time_after(jiffies, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000337static inline int rt6_need_strict(const struct in6_addr *daddr)
Thomas Grafc71099a2006-08-04 23:20:06 -0700338{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000339 return ipv6_addr_type(daddr) &
340 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
Thomas Grafc71099a2006-08-04 23:20:06 -0700341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/*
Thomas Grafc71099a2006-08-04 23:20:06 -0700344 * Route lookup. Any table->tb6_lock is implied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
346
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800347static inline struct rt6_info *rt6_device_match(struct net *net,
348 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000349 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700351 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct rt6_info *local = NULL;
354 struct rt6_info *sprt;
355
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900356 if (!oif && ipv6_addr_any(saddr))
357 goto out;
358
Changli Gaod8d1f302010-06-10 23:31:35 -0700359 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -0500360 struct net_device *dev = sprt->dst.dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900361
362 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (dev->ifindex == oif)
364 return sprt;
365 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500366 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 sprt->rt6i_idev->dev->ifindex != oif) {
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700368 if (flags & RT6_LOOKUP_F_IFACE && oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900370 if (local && (!oif ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 local->rt6i_idev->dev->ifindex == oif))
372 continue;
373 }
374 local = sprt;
375 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900376 } else {
377 if (ipv6_chk_addr(net, saddr, dev,
378 flags & RT6_LOOKUP_F_IFACE))
379 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900383 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (local)
385 return local;
386
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700387 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800388 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return rt;
392}
393
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800394#ifdef CONFIG_IPV6_ROUTER_PREF
395static void rt6_probe(struct rt6_info *rt)
396{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000397 struct neighbour *neigh;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800398 /*
399 * Okay, this does not seem to be appropriate
400 * for now, however, we need to check if it
401 * is really so; aka Router Reachability Probing.
402 *
403 * Router Reachability Probe MUST be rate-limited
404 * to no more than one per minute.
405 */
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000406 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000407 neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800408 if (!neigh || (neigh->nud_state & NUD_VALID))
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000409 goto out;
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800410 read_lock_bh(&neigh->lock);
411 if (!(neigh->nud_state & NUD_VALID) &&
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800412 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800413 struct in6_addr mcaddr;
414 struct in6_addr *target;
415
416 neigh->updated = jiffies;
417 read_unlock_bh(&neigh->lock);
418
419 target = (struct in6_addr *)&neigh->primary_key;
420 addrconf_addr_solict_mult(target, &mcaddr);
David S. Millerd1918542011-12-28 20:19:20 -0500421 ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000422 } else {
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800423 read_unlock_bh(&neigh->lock);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000424 }
425out:
426 rcu_read_unlock();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800427}
428#else
429static inline void rt6_probe(struct rt6_info *rt)
430{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800431}
432#endif
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800435 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700437static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
David S. Millerd1918542011-12-28 20:19:20 -0500439 struct net_device *dev = rt->dst.dev;
David S. Miller161980f2007-04-06 11:42:27 -0700440 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800441 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700442 if ((dev->flags & IFF_LOOPBACK) &&
443 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
444 return 1;
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Dave Jonesb6f99a22007-03-22 12:27:49 -0700448static inline int rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000450 struct neighbour *neigh;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800451 int m;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000452
453 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000454 neigh = dst_get_neighbour_noref(&rt->dst);
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700455 if (rt->rt6i_flags & RTF_NONEXTHOP ||
456 !(rt->rt6i_flags & RTF_GATEWAY))
457 m = 1;
458 else if (neigh) {
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800459 read_lock_bh(&neigh->lock);
460 if (neigh->nud_state & NUD_VALID)
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700461 m = 2;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800462#ifdef CONFIG_IPV6_ROUTER_PREF
463 else if (neigh->nud_state & NUD_FAILED)
464 m = 0;
465#endif
466 else
YOSHIFUJI Hideakiea73ee22006-11-06 09:45:44 -0800467 m = 1;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800468 read_unlock_bh(&neigh->lock);
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800469 } else
470 m = 0;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000471 rcu_read_unlock();
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800472 return m;
473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800475static int rt6_score_route(struct rt6_info *rt, int oif,
476 int strict)
477{
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700478 int m, n;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900479
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700480 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700481 if (!m && (strict & RT6_LOOKUP_F_IFACE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800482 return -1;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800483#ifdef CONFIG_IPV6_ROUTER_PREF
484 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
485#endif
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700486 n = rt6_check_neigh(rt);
YOSHIFUJI Hideaki557e92e2006-11-06 09:45:45 -0800487 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800488 return -1;
489 return m;
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David S. Millerf11e6652007-03-24 20:36:25 -0700492static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
493 int *mpri, struct rt6_info *match)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800494{
David S. Millerf11e6652007-03-24 20:36:25 -0700495 int m;
496
497 if (rt6_check_expired(rt))
498 goto out;
499
500 m = rt6_score_route(rt, oif, strict);
501 if (m < 0)
502 goto out;
503
504 if (m > *mpri) {
505 if (strict & RT6_LOOKUP_F_REACHABLE)
506 rt6_probe(match);
507 *mpri = m;
508 match = rt;
509 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
510 rt6_probe(rt);
511 }
512
513out:
514 return match;
515}
516
517static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
518 struct rt6_info *rr_head,
519 u32 metric, int oif, int strict)
520{
521 struct rt6_info *rt, *match;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800522 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Millerf11e6652007-03-24 20:36:25 -0700524 match = NULL;
525 for (rt = rr_head; rt && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700526 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700527 match = find_match(rt, oif, strict, &mpri, match);
528 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
Changli Gaod8d1f302010-06-10 23:31:35 -0700529 rt = rt->dst.rt6_next)
David S. Millerf11e6652007-03-24 20:36:25 -0700530 match = find_match(rt, oif, strict, &mpri, match);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800531
David S. Millerf11e6652007-03-24 20:36:25 -0700532 return match;
533}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800534
David S. Millerf11e6652007-03-24 20:36:25 -0700535static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
536{
537 struct rt6_info *match, *rt0;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800538 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David S. Millerf11e6652007-03-24 20:36:25 -0700540 rt0 = fn->rr_ptr;
541 if (!rt0)
542 fn->rr_ptr = rt0 = fn->leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David S. Millerf11e6652007-03-24 20:36:25 -0700544 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800546 if (!match &&
David S. Millerf11e6652007-03-24 20:36:25 -0700547 (strict & RT6_LOOKUP_F_REACHABLE)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700548 struct rt6_info *next = rt0->dst.rt6_next;
David S. Millerf11e6652007-03-24 20:36:25 -0700549
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800550 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700551 if (!next || next->rt6i_metric != rt0->rt6i_metric)
552 next = fn->leaf;
553
554 if (next != rt0)
555 fn->rr_ptr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
David S. Millerd1918542011-12-28 20:19:20 -0500558 net = dev_net(rt0->dst.dev);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000559 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800562#ifdef CONFIG_IPV6_ROUTE_INFO
563int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000564 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800565{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900566 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800567 struct route_info *rinfo = (struct route_info *) opt;
568 struct in6_addr prefix_buf, *prefix;
569 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900570 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800571 struct rt6_info *rt;
572
573 if (len < sizeof(struct route_info)) {
574 return -EINVAL;
575 }
576
577 /* Sanity check for prefix_len and length */
578 if (rinfo->length > 3) {
579 return -EINVAL;
580 } else if (rinfo->prefix_len > 128) {
581 return -EINVAL;
582 } else if (rinfo->prefix_len > 64) {
583 if (rinfo->length < 2) {
584 return -EINVAL;
585 }
586 } else if (rinfo->prefix_len > 0) {
587 if (rinfo->length < 1) {
588 return -EINVAL;
589 }
590 }
591
592 pref = rinfo->route_pref;
593 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000594 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800595
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900596 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800597
598 if (rinfo->length == 3)
599 prefix = (struct in6_addr *)rinfo->prefix;
600 else {
601 /* this function is safe */
602 ipv6_addr_prefix(&prefix_buf,
603 (struct in6_addr *)rinfo->prefix,
604 rinfo->prefix_len);
605 prefix = &prefix_buf;
606 }
607
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800608 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
609 dev->ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800610
611 if (rt && !lifetime) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700612 ip6_del_rt(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800613 rt = NULL;
614 }
615
616 if (!rt && lifetime)
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800617 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800618 pref);
619 else if (rt)
620 rt->rt6i_flags = RTF_ROUTEINFO |
621 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
622
623 if (rt) {
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900624 if (!addrconf_finite_timeout(lifetime)) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800625 rt->rt6i_flags &= ~RTF_EXPIRES;
626 } else {
David S. Millerd1918542011-12-28 20:19:20 -0500627 rt->dst.expires = jiffies + HZ * lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800628 rt->rt6i_flags |= RTF_EXPIRES;
629 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700630 dst_release(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800631 }
632 return 0;
633}
634#endif
635
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800636#define BACKTRACK(__net, saddr) \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700637do { \
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800638 if (rt == __net->ipv6.ip6_null_entry) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700639 struct fib6_node *pn; \
Ville Nuorvalae0eda7b2006-10-16 22:11:11 -0700640 while (1) { \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700641 if (fn->fn_flags & RTN_TL_ROOT) \
642 goto out; \
643 pn = fn->parent; \
644 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
Kim Nordlund8bce65b2006-12-13 16:38:29 -0800645 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
YOSHIFUJI Hideaki982f56f2006-08-23 17:22:39 -0700646 else \
647 fn = pn; \
648 if (fn->fn_flags & RTN_RTINFO) \
649 goto restart; \
Thomas Grafc71099a2006-08-04 23:20:06 -0700650 } \
Thomas Grafc71099a2006-08-04 23:20:06 -0700651 } \
David S. Miller38308472011-12-03 18:02:47 -0500652} while (0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700653
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800654static struct rt6_info *ip6_pol_route_lookup(struct net *net,
655 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500656 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct fib6_node *fn;
659 struct rt6_info *rt;
660
Thomas Grafc71099a2006-08-04 23:20:06 -0700661 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -0500662 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700663restart:
664 rt = fn->leaf;
David S. Miller4c9483b2011-03-12 16:22:43 -0500665 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
666 BACKTRACK(net, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -0700667out:
Changli Gaod8d1f302010-06-10 23:31:35 -0700668 dst_use(&rt->dst, jiffies);
Thomas Grafc71099a2006-08-04 23:20:06 -0700669 read_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -0700670 return rt;
671
672}
673
Florian Westphalea6e5742011-09-05 16:05:44 +0200674struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
675 int flags)
676{
677 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
678}
679EXPORT_SYMBOL_GPL(ip6_route_lookup);
680
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900681struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
682 const struct in6_addr *saddr, int oif, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -0700683{
David S. Miller4c9483b2011-03-12 16:22:43 -0500684 struct flowi6 fl6 = {
685 .flowi6_oif = oif,
686 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700687 };
688 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700689 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -0700690
Thomas Grafadaa70b2006-10-13 15:01:03 -0700691 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500692 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -0700693 flags |= RT6_LOOKUP_F_HAS_SADDR;
694 }
695
David S. Miller4c9483b2011-03-12 16:22:43 -0500696 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700697 if (dst->error == 0)
698 return (struct rt6_info *) dst;
699
700 dst_release(dst);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return NULL;
703}
704
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900705EXPORT_SYMBOL(rt6_lookup);
706
Thomas Grafc71099a2006-08-04 23:20:06 -0700707/* ip6_ins_rt is called with FREE table->tb6_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 It takes new route entry, the addition fails by any reason the
709 route is freed. In any case, if caller does not hold it, it may
710 be destroyed.
711 */
712
Thomas Graf86872cb2006-08-22 00:01:08 -0700713static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -0700716 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Thomas Grafc71099a2006-08-04 23:20:06 -0700718 table = rt->rt6i_table;
719 write_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -0700720 err = fib6_add(&table->tb6_root, rt, info);
Thomas Grafc71099a2006-08-04 23:20:06 -0700721 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 return err;
724}
725
Thomas Graf40e22e82006-08-22 00:00:45 -0700726int ip6_ins_rt(struct rt6_info *rt)
727{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800728 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -0500729 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800730 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -0800731 return __ip6_ins_rt(rt, &info);
Thomas Graf40e22e82006-08-22 00:00:45 -0700732}
733
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000734static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort,
735 const struct in6_addr *daddr,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000736 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct rt6_info *rt;
739
740 /*
741 * Clone the route.
742 */
743
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000744 rt = ip6_rt_copy(ort, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (rt) {
David S. Miller14deae42009-01-04 16:04:39 -0800747 int attempts = !in_softirq();
748
David S. Miller38308472011-12-03 18:02:47 -0500749 if (!(rt->rt6i_flags & RTF_GATEWAY)) {
David S. Millerbb3c3682011-12-13 17:35:06 -0500750 if (ort->rt6i_dst.plen != 128 &&
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000751 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900752 rt->rt6i_flags |= RTF_ANYCAST;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000753 rt->rt6i_gateway = *daddr;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +0900754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 rt->rt6i_flags |= RTF_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758#ifdef CONFIG_IPV6_SUBTREES
759 if (rt->rt6i_src.plen && saddr) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000760 rt->rt6i_src.addr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 rt->rt6i_src.plen = 128;
762 }
763#endif
764
David S. Miller14deae42009-01-04 16:04:39 -0800765 retry:
David S. Miller8ade06c2011-12-29 18:51:57 -0500766 if (rt6_bind_neighbour(rt, rt->dst.dev)) {
David S. Millerd1918542011-12-28 20:19:20 -0500767 struct net *net = dev_net(rt->dst.dev);
David S. Miller14deae42009-01-04 16:04:39 -0800768 int saved_rt_min_interval =
769 net->ipv6.sysctl.ip6_rt_gc_min_interval;
770 int saved_rt_elasticity =
771 net->ipv6.sysctl.ip6_rt_gc_elasticity;
772
773 if (attempts-- > 0) {
774 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
775 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
776
Alexey Dobriyan86393e52009-08-29 01:34:49 +0000777 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
David S. Miller14deae42009-01-04 16:04:39 -0800778
779 net->ipv6.sysctl.ip6_rt_gc_elasticity =
780 saved_rt_elasticity;
781 net->ipv6.sysctl.ip6_rt_gc_min_interval =
782 saved_rt_min_interval;
783 goto retry;
784 }
785
786 if (net_ratelimit())
787 printk(KERN_WARNING
Ulrich Weber7e1b33e2010-09-27 15:02:18 -0700788 "ipv6: Neighbour table overflow.\n");
Changli Gaod8d1f302010-06-10 23:31:35 -0700789 dst_free(&rt->dst);
David S. Miller14deae42009-01-04 16:04:39 -0800790 return NULL;
791 }
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -0800794 return rt;
795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000797static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
798 const struct in6_addr *daddr)
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800799{
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000800 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
801
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800802 if (rt) {
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800803 rt->rt6i_flags |= RTF_CACHE;
David Miller27217452011-12-02 16:52:08 +0000804 dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst)));
YOSHIFUJI Hideaki299d9932006-03-20 16:58:32 -0800805 }
806 return rt;
807}
808
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800809static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
David S. Miller4c9483b2011-03-12 16:22:43 -0500810 struct flowi6 *fl6, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct fib6_node *fn;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800813 struct rt6_info *rt, *nrt;
Thomas Grafc71099a2006-08-04 23:20:06 -0700814 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int attempts = 3;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800816 int err;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700817 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700819 strict |= flags & RT6_LOOKUP_F_IFACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821relookup:
Thomas Grafc71099a2006-08-04 23:20:06 -0700822 read_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800824restart_2:
David S. Miller4c9483b2011-03-12 16:22:43 -0500825 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827restart:
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700828 rt = rt6_select(fn, oif, strict | reachable);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800829
David S. Miller4c9483b2011-03-12 16:22:43 -0500830 BACKTRACK(net, &fl6->saddr);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800831 if (rt == net->ipv6.ip6_null_entry ||
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800832 rt->rt6i_flags & RTF_CACHE)
YOSHIFUJI Hideaki1ddef0442006-03-20 17:01:24 -0800833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Changli Gaod8d1f302010-06-10 23:31:35 -0700835 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700836 read_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -0800837
David Miller27217452011-12-02 16:52:08 +0000838 if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
David S. Miller4c9483b2011-03-12 16:22:43 -0500839 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800840 else if (!(rt->dst.flags & DST_HOST))
David S. Miller4c9483b2011-03-12 16:22:43 -0500841 nrt = rt6_alloc_clone(rt, &fl6->daddr);
David S. Miller7343ff32011-03-09 19:55:25 -0800842 else
843 goto out2;
YOSHIFUJI Hideakie40cf352006-03-20 16:59:27 -0800844
Changli Gaod8d1f302010-06-10 23:31:35 -0700845 dst_release(&rt->dst);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800846 rt = nrt ? : net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800847
Changli Gaod8d1f302010-06-10 23:31:35 -0700848 dst_hold(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800849 if (nrt) {
Thomas Graf40e22e82006-08-22 00:00:45 -0700850 err = ip6_ins_rt(nrt);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800851 if (!err)
852 goto out2;
853 }
854
855 if (--attempts <= 0)
856 goto out2;
857
858 /*
Thomas Grafc71099a2006-08-04 23:20:06 -0700859 * Race condition! In the gap, when table->tb6_lock was
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800860 * released someone could insert this route. Relookup.
861 */
Changli Gaod8d1f302010-06-10 23:31:35 -0700862 dst_release(&rt->dst);
YOSHIFUJI Hideaki519fbd82006-03-20 17:00:05 -0800863 goto relookup;
864
865out:
YOSHIFUJI Hideaki8238dd02006-03-20 17:04:35 -0800866 if (reachable) {
867 reachable = 0;
868 goto restart_2;
869 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700870 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -0700871 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872out2:
Changli Gaod8d1f302010-06-10 23:31:35 -0700873 rt->dst.lastuse = jiffies;
874 rt->dst.__use++;
Thomas Grafc71099a2006-08-04 23:20:06 -0700875
876 return rt;
877}
878
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800879static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500880 struct flowi6 *fl6, int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700881{
David S. Miller4c9483b2011-03-12 16:22:43 -0500882 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -0700883}
884
Thomas Grafc71099a2006-08-04 23:20:06 -0700885void ip6_route_input(struct sk_buff *skb)
886{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000887 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900888 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700889 int flags = RT6_LOOKUP_F_HAS_SADDR;
David S. Miller4c9483b2011-03-12 16:22:43 -0500890 struct flowi6 fl6 = {
891 .flowi6_iif = skb->dev->ifindex,
892 .daddr = iph->daddr,
893 .saddr = iph->saddr,
David S. Miller38308472011-12-03 18:02:47 -0500894 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
David S. Miller4c9483b2011-03-12 16:22:43 -0500895 .flowi6_mark = skb->mark,
896 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -0700897 };
Thomas Grafadaa70b2006-10-13 15:01:03 -0700898
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800899 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
Thomas Grafadaa70b2006-10-13 15:01:03 -0700900 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700901
David S. Miller4c9483b2011-03-12 16:22:43 -0500902 skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
Thomas Grafc71099a2006-08-04 23:20:06 -0700903}
904
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800905static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -0500906 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -0700907{
David S. Miller4c9483b2011-03-12 16:22:43 -0500908 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700909}
910
Florian Westphal9c7a4f9c2011-03-22 19:17:36 -0700911struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500912 struct flowi6 *fl6)
Thomas Grafc71099a2006-08-04 23:20:06 -0700913{
914 int flags = 0;
915
David S. Miller4c9483b2011-03-12 16:22:43 -0500916 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700917 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -0700918
David S. Miller4c9483b2011-03-12 16:22:43 -0500919 if (!ipv6_addr_any(&fl6->saddr))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700920 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +0000921 else if (sk)
922 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -0700923
David S. Miller4c9483b2011-03-12 16:22:43 -0500924 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900927EXPORT_SYMBOL(ip6_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
David S. Miller2774c132011-03-01 14:59:04 -0800929struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -0700930{
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700931 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -0700932 struct dst_entry *new = NULL;
933
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700934 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
David S. Miller14e50e52007-05-24 18:17:54 -0700935 if (rt) {
David S. Millercf911662011-04-28 14:31:47 -0700936 memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
937
Changli Gaod8d1f302010-06-10 23:31:35 -0700938 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -0700939
David S. Miller14e50e52007-05-24 18:17:54 -0700940 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -0800941 new->input = dst_discard;
942 new->output = dst_discard;
David S. Miller14e50e52007-05-24 18:17:54 -0700943
Eric Dumazet21efcfa2011-07-19 20:18:36 +0000944 if (dst_metrics_read_only(&ort->dst))
945 new->_metrics = ort->dst._metrics;
946 else
947 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -0700948 rt->rt6i_idev = ort->rt6i_idev;
949 if (rt->rt6i_idev)
950 in6_dev_hold(rt->rt6i_idev);
David S. Millerd1918542011-12-28 20:19:20 -0500951 rt->dst.expires = 0;
David S. Miller14e50e52007-05-24 18:17:54 -0700952
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000953 rt->rt6i_gateway = ort->rt6i_gateway;
David S. Miller14e50e52007-05-24 18:17:54 -0700954 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
955 rt->rt6i_metric = 0;
956
957 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
958#ifdef CONFIG_IPV6_SUBTREES
959 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
960#endif
961
962 dst_free(new);
963 }
964
David S. Miller69ead7a2011-03-01 14:45:33 -0800965 dst_release(dst_orig);
966 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -0700967}
David S. Miller14e50e52007-05-24 18:17:54 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * Destination cache support functions
971 */
972
973static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
974{
975 struct rt6_info *rt;
976
977 rt = (struct rt6_info *) dst;
978
David S. Miller6431cbc2011-02-07 20:38:06 -0800979 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
980 if (rt->rt6i_peer_genid != rt6_peer_genid()) {
981 if (!rt->rt6i_peer)
982 rt6_bind_peer(rt, 0);
983 rt->rt6i_peer_genid = rt6_peer_genid();
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return dst;
David S. Miller6431cbc2011-02-07 20:38:06 -0800986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return NULL;
988}
989
990static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
991{
992 struct rt6_info *rt = (struct rt6_info *) dst;
993
994 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +0000995 if (rt->rt6i_flags & RTF_CACHE) {
996 if (rt6_check_expired(rt)) {
997 ip6_del_rt(rt);
998 dst = NULL;
999 }
1000 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001002 dst = NULL;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00001005 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static void ip6_link_failure(struct sk_buff *skb)
1009{
1010 struct rt6_info *rt;
1011
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001012 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Dumazetadf30902009-06-02 05:19:30 +00001014 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (rt) {
David S. Miller38308472011-12-03 18:02:47 -05001016 if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001017 dst_set_expires(&rt->dst, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 rt->rt6i_flags |= RTF_EXPIRES;
1019 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
1020 rt->rt6i_node->fn_sernum = -1;
1021 }
1022}
1023
1024static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
1025{
1026 struct rt6_info *rt6 = (struct rt6_info*)dst;
1027
1028 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
1029 rt6->rt6i_flags |= RTF_MODIFIED;
1030 if (mtu < IPV6_MIN_MTU) {
David S. Millerdefb3512010-12-08 21:16:57 -08001031 u32 features = dst_metric(dst, RTAX_FEATURES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 mtu = IPV6_MIN_MTU;
David S. Millerdefb3512010-12-08 21:16:57 -08001033 features |= RTAX_FEATURE_ALLFRAG;
1034 dst_metric_set(dst, RTAX_FEATURES, features);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
David S. Millerdefb3512010-12-08 21:16:57 -08001036 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038}
1039
David S. Miller0dbaee32010-12-13 12:52:14 -08001040static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
David S. Miller0dbaee32010-12-13 12:52:14 -08001042 struct net_device *dev = dst->dev;
1043 unsigned int mtu = dst_mtu(dst);
1044 struct net *net = dev_net(dev);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1047
Daniel Lezcano55786892008-03-04 13:47:47 -08001048 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1049 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001052 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1053 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1054 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * rely only on pmtu discovery"
1056 */
1057 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1058 mtu = IPV6_MAXPLEN;
1059 return mtu;
1060}
1061
Steffen Klassertebb762f2011-11-23 02:12:51 +00001062static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001063{
David S. Millerd33e4552010-12-14 13:01:14 -08001064 struct inet6_dev *idev;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001065 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1066
1067 if (mtu)
1068 return mtu;
1069
1070 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08001071
1072 rcu_read_lock();
1073 idev = __in6_dev_get(dst->dev);
1074 if (idev)
1075 mtu = idev->cnf.mtu6;
1076 rcu_read_unlock();
1077
1078 return mtu;
1079}
1080
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001081static struct dst_entry *icmp6_dst_gc_list;
1082static DEFINE_SPINLOCK(icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001083
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001084struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 struct neighbour *neigh,
David S. Miller87a11572011-12-06 17:04:13 -05001086 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
David S. Miller87a11572011-12-06 17:04:13 -05001088 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct rt6_info *rt;
1090 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001091 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
David S. Miller38308472011-12-03 18:02:47 -05001093 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return NULL;
1095
David S. Miller957c6652011-06-24 15:25:00 -07001096 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05001097 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05001099 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 goto out;
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (neigh)
1104 neigh_hold(neigh);
David S. Miller14deae42009-01-04 16:04:39 -08001105 else {
David S. Millerf83c7792011-12-28 15:41:23 -05001106 neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr);
David S. Millerb43faac2011-12-13 16:48:21 -05001107 if (IS_ERR(neigh)) {
RongQing.Li252c3d82012-01-12 22:33:46 +00001108 in6_dev_put(idev);
David S. Millerb43faac2011-12-13 16:48:21 -05001109 dst_free(&rt->dst);
1110 return ERR_CAST(neigh);
1111 }
David S. Miller14deae42009-01-04 16:04:39 -08001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001114 rt->dst.flags |= DST_HOST;
1115 rt->dst.output = ip6_output;
David S. Miller69cce1d2011-07-17 23:09:49 -07001116 dst_set_neighbour(&rt->dst, neigh);
Changli Gaod8d1f302010-06-10 23:31:35 -07001117 atomic_set(&rt->dst.__refcnt, 1);
David S. Miller87a11572011-12-06 17:04:13 -05001118 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001119 rt->rt6i_dst.plen = 128;
1120 rt->rt6i_idev = idev;
Gao feng70116872011-10-28 02:46:57 +00001121 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001123 spin_lock_bh(&icmp6_dst_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001124 rt->dst.next = icmp6_dst_gc_list;
1125 icmp6_dst_gc_list = &rt->dst;
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001126 spin_unlock_bh(&icmp6_dst_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Daniel Lezcano55786892008-03-04 13:47:47 -08001128 fib6_force_start_gc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
David S. Miller87a11572011-12-06 17:04:13 -05001130 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132out:
David S. Miller87a11572011-12-06 17:04:13 -05001133 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001136int icmp6_dst_gc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Hagen Paul Pfeifere9476e952011-02-25 05:45:19 +00001138 struct dst_entry *dst, **pprev;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001139 int more = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001141 spin_lock_bh(&icmp6_dst_lock);
1142 pprev = &icmp6_dst_gc_list;
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 while ((dst = *pprev) != NULL) {
1145 if (!atomic_read(&dst->__refcnt)) {
1146 *pprev = dst->next;
1147 dst_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 } else {
1149 pprev = &dst->next;
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001150 ++more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 }
1153
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001154 spin_unlock_bh(&icmp6_dst_lock);
Thomas Graf5d0bbee2006-08-04 03:37:36 -07001155
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001156 return more;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
David S. Miller1e493d12008-09-10 17:27:15 -07001159static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1160 void *arg)
1161{
1162 struct dst_entry *dst, **pprev;
1163
1164 spin_lock_bh(&icmp6_dst_lock);
1165 pprev = &icmp6_dst_gc_list;
1166 while ((dst = *pprev) != NULL) {
1167 struct rt6_info *rt = (struct rt6_info *) dst;
1168 if (func(rt, arg)) {
1169 *pprev = dst->next;
1170 dst_free(dst);
1171 } else {
1172 pprev = &dst->next;
1173 }
1174 }
1175 spin_unlock_bh(&icmp6_dst_lock);
1176}
1177
Daniel Lezcano569d3642008-01-18 03:56:57 -08001178static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 unsigned long now = jiffies;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00001181 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001182 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1183 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1184 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1185 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1186 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001187 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Eric Dumazetfc66f952010-10-08 06:37:34 +00001189 entries = dst_entries_get_fast(ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08001190 if (time_after(rt_last_gc + rt_min_interval, now) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001191 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out;
1193
Benjamin Thery6891a342008-03-04 13:49:47 -08001194 net->ipv6.ip6_rt_gc_expire++;
1195 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1196 net->ipv6.ip6_rt_last_gc = now;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001197 entries = dst_entries_get_slow(ops);
1198 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08001199 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08001201 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001202 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205/* Clean host part of a prefix. Not necessary in radix tree,
1206 but results in cleaner routing tables.
1207
1208 Remove it only when all the things will work!
1209 */
1210
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001211int ip6_dst_hoplimit(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
David S. Miller5170ae82010-12-12 21:35:57 -08001213 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
David S. Millera02e4b72010-12-12 21:39:02 -08001214 if (hoplimit == 0) {
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001215 struct net_device *dev = dst->dev;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001216 struct inet6_dev *idev;
1217
1218 rcu_read_lock();
1219 idev = __in6_dev_get(dev);
1220 if (idev)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001221 hoplimit = idev->cnf.hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001222 else
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001223 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
Eric Dumazetc68f24c2010-06-14 04:46:20 +00001224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 return hoplimit;
1227}
David S. Millerabbf46a2010-12-12 21:14:46 -08001228EXPORT_SYMBOL(ip6_dst_hoplimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230/*
1231 *
1232 */
1233
Thomas Graf86872cb2006-08-22 00:01:08 -07001234int ip6_route_add(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 int err;
Daniel Lezcano55786892008-03-04 13:47:47 -08001237 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct rt6_info *rt = NULL;
1239 struct net_device *dev = NULL;
1240 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001241 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int addr_type;
1243
Thomas Graf86872cb2006-08-22 00:01:08 -07001244 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EINVAL;
1246#ifndef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001247 if (cfg->fc_src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EINVAL;
1249#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07001250 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08001252 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!dev)
1254 goto out;
1255 idev = in6_dev_get(dev);
1256 if (!idev)
1257 goto out;
1258 }
1259
Thomas Graf86872cb2006-08-22 00:01:08 -07001260 if (cfg->fc_metric == 0)
1261 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Matti Vaittinend71314b2011-11-14 00:14:49 +00001263 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05001264 if (cfg->fc_nlinfo.nlh &&
1265 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001266 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001267 if (!table) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00001268 printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n");
1269 table = fib6_new_table(net, cfg->fc_table);
1270 }
1271 } else {
1272 table = fib6_new_table(net, cfg->fc_table);
1273 }
David S. Miller38308472011-12-03 18:02:47 -05001274
1275 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001276 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07001277
David S. Miller957c6652011-06-24 15:25:00 -07001278 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
David S. Miller38308472011-12-03 18:02:47 -05001280 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 err = -ENOMEM;
1282 goto out;
1283 }
1284
Changli Gaod8d1f302010-06-10 23:31:35 -07001285 rt->dst.obsolete = -1;
David S. Millerd1918542011-12-28 20:19:20 -05001286 rt->dst.expires = (cfg->fc_flags & RTF_EXPIRES) ?
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001287 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1288 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Thomas Graf86872cb2006-08-22 00:01:08 -07001290 if (cfg->fc_protocol == RTPROT_UNSPEC)
1291 cfg->fc_protocol = RTPROT_BOOT;
1292 rt->rt6i_protocol = cfg->fc_protocol;
1293
1294 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (addr_type & IPV6_ADDR_MULTICAST)
Changli Gaod8d1f302010-06-10 23:31:35 -07001297 rt->dst.input = ip6_mc_input;
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00001298 else if (cfg->fc_flags & RTF_LOCAL)
1299 rt->dst.input = ip6_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001301 rt->dst.input = ip6_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Changli Gaod8d1f302010-06-10 23:31:35 -07001303 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Graf86872cb2006-08-22 00:01:08 -07001305 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1306 rt->rt6i_dst.plen = cfg->fc_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (rt->rt6i_dst.plen == 128)
David S. Miller11d53b42011-06-24 15:23:34 -07001308 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001310 if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1311 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1312 if (!metrics) {
1313 err = -ENOMEM;
1314 goto out;
1315 }
1316 dst_init_metrics(&rt->dst, metrics, 0);
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07001319 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1320 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321#endif
1322
Thomas Graf86872cb2006-08-22 00:01:08 -07001323 rt->rt6i_metric = cfg->fc_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* We cannot add true routes via loopback here,
1326 they would result in kernel looping; promote them to reject routes
1327 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001328 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05001329 (dev && (dev->flags & IFF_LOOPBACK) &&
1330 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1331 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08001333 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (dev) {
1335 dev_put(dev);
1336 in6_dev_put(idev);
1337 }
Daniel Lezcano55786892008-03-04 13:47:47 -08001338 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 dev_hold(dev);
1340 idev = in6_dev_get(dev);
1341 if (!idev) {
1342 err = -ENODEV;
1343 goto out;
1344 }
1345 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001346 rt->dst.output = ip6_pkt_discard_out;
1347 rt->dst.input = ip6_pkt_discard;
1348 rt->dst.error = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1350 goto install_route;
1351 }
1352
Thomas Graf86872cb2006-08-22 00:01:08 -07001353 if (cfg->fc_flags & RTF_GATEWAY) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001354 const struct in6_addr *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int gwa_type;
1356
Thomas Graf86872cb2006-08-22 00:01:08 -07001357 gw_addr = &cfg->fc_gateway;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001358 rt->rt6i_gateway = *gw_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 gwa_type = ipv6_addr_type(gw_addr);
1360
1361 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1362 struct rt6_info *grt;
1363
1364 /* IPv6 strictly inhibits using not link-local
1365 addresses as nexthop address.
1366 Otherwise, router will not able to send redirects.
1367 It is very good, but in some (rare!) circumstances
1368 (SIT, PtP, NBMA NOARP links) it is handy to allow
1369 some exceptions. --ANK
1370 */
1371 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001372 if (!(gwa_type & IPV6_ADDR_UNICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 goto out;
1374
Daniel Lezcano55786892008-03-04 13:47:47 -08001375 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 err = -EHOSTUNREACH;
David S. Miller38308472011-12-03 18:02:47 -05001378 if (!grt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out;
1380 if (dev) {
David S. Millerd1918542011-12-28 20:19:20 -05001381 if (dev != grt->dst.dev) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001382 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 goto out;
1384 }
1385 } else {
David S. Millerd1918542011-12-28 20:19:20 -05001386 dev = grt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 idev = grt->rt6i_idev;
1388 dev_hold(dev);
1389 in6_dev_hold(grt->rt6i_idev);
1390 }
David S. Miller38308472011-12-03 18:02:47 -05001391 if (!(grt->rt6i_flags & RTF_GATEWAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 err = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001393 dst_release(&grt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (err)
1396 goto out;
1397 }
1398 err = -EINVAL;
David S. Miller38308472011-12-03 18:02:47 -05001399 if (!dev || (dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out;
1401 }
1402
1403 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05001404 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 goto out;
1406
Daniel Walterc3968a82011-04-13 21:10:57 +00001407 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1408 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1409 err = -EINVAL;
1410 goto out;
1411 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001412 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00001413 rt->rt6i_prefsrc.plen = 128;
1414 } else
1415 rt->rt6i_prefsrc.plen = 0;
1416
Thomas Graf86872cb2006-08-22 00:01:08 -07001417 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
David S. Miller8ade06c2011-12-29 18:51:57 -05001418 err = rt6_bind_neighbour(rt, dev);
David S. Millerf83c7792011-12-28 15:41:23 -05001419 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
Thomas Graf86872cb2006-08-22 00:01:08 -07001423 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425install_route:
Thomas Graf86872cb2006-08-22 00:01:08 -07001426 if (cfg->fc_mx) {
1427 struct nlattr *nla;
1428 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Thomas Graf86872cb2006-08-22 00:01:08 -07001430 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +02001431 int type = nla_type(nla);
Thomas Graf86872cb2006-08-22 00:01:08 -07001432
1433 if (type) {
1434 if (type > RTAX_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 err = -EINVAL;
1436 goto out;
1437 }
Thomas Graf86872cb2006-08-22 00:01:08 -07001438
David S. Millerdefb3512010-12-08 21:16:57 -08001439 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 }
1443
Changli Gaod8d1f302010-06-10 23:31:35 -07001444 rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07001446 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001447
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001448 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001449
Thomas Graf86872cb2006-08-22 00:01:08 -07001450 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452out:
1453 if (dev)
1454 dev_put(dev);
1455 if (idev)
1456 in6_dev_put(idev);
1457 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001458 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return err;
1460}
1461
Thomas Graf86872cb2006-08-22 00:01:08 -07001462static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001465 struct fib6_table *table;
David S. Millerd1918542011-12-28 20:19:20 -05001466 struct net *net = dev_net(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001468 if (rt == net->ipv6.ip6_null_entry)
Patrick McHardy6c813a72006-08-06 22:22:47 -07001469 return -ENOENT;
1470
Thomas Grafc71099a2006-08-04 23:20:06 -07001471 table = rt->rt6i_table;
1472 write_lock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Thomas Graf86872cb2006-08-22 00:01:08 -07001474 err = fib6_del(rt, info);
Changli Gaod8d1f302010-06-10 23:31:35 -07001475 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Thomas Grafc71099a2006-08-04 23:20:06 -07001477 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 return err;
1480}
1481
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001482int ip6_del_rt(struct rt6_info *rt)
1483{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001484 struct nl_info info = {
David S. Millerd1918542011-12-28 20:19:20 -05001485 .nl_net = dev_net(rt->dst.dev),
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001486 };
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001487 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001488}
1489
Thomas Graf86872cb2006-08-22 00:01:08 -07001490static int ip6_route_del(struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Thomas Grafc71099a2006-08-04 23:20:06 -07001492 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct fib6_node *fn;
1494 struct rt6_info *rt;
1495 int err = -ESRCH;
1496
Daniel Lezcano55786892008-03-04 13:47:47 -08001497 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05001498 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Thomas Grafc71099a2006-08-04 23:20:06 -07001501 read_lock_bh(&table->tb6_lock);
1502
1503 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07001504 &cfg->fc_dst, cfg->fc_dst_len,
1505 &cfg->fc_src, cfg->fc_src_len);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (fn) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001508 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001509 if (cfg->fc_ifindex &&
David S. Millerd1918542011-12-28 20:19:20 -05001510 (!rt->dst.dev ||
1511 rt->dst.dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001513 if (cfg->fc_flags & RTF_GATEWAY &&
1514 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07001516 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001518 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001519 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Thomas Graf86872cb2006-08-22 00:01:08 -07001521 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001524 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 return err;
1527}
1528
1529/*
1530 * Handle redirects
1531 */
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001532struct ip6rd_flowi {
David S. Miller4c9483b2011-03-12 16:22:43 -05001533 struct flowi6 fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001534 struct in6_addr gateway;
1535};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001537static struct rt6_info *__ip6_route_redirect(struct net *net,
1538 struct fib6_table *table,
David S. Miller4c9483b2011-03-12 16:22:43 -05001539 struct flowi6 *fl6,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001540 int flags)
1541{
David S. Miller4c9483b2011-03-12 16:22:43 -05001542 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001543 struct rt6_info *rt;
1544 struct fib6_node *fn;
Thomas Grafc71099a2006-08-04 23:20:06 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001547 * Get the "current" route for this destination and
1548 * check if the redirect has come from approriate router.
1549 *
1550 * RFC 2461 specifies that redirects should only be
1551 * accepted if they come from the nexthop to the target.
1552 * Due to the way the routes are chosen, this notion
1553 * is a bit fuzzy and one might need to check all possible
1554 * routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Thomas Grafc71099a2006-08-04 23:20:06 -07001557 read_lock_bh(&table->tb6_lock);
David S. Miller4c9483b2011-03-12 16:22:43 -05001558 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001559restart:
Changli Gaod8d1f302010-06-10 23:31:35 -07001560 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001561 /*
1562 * Current route is on-link; redirect is always invalid.
1563 *
1564 * Seems, previous statement is not true. It could
1565 * be node, which looks for us as on-link (f.e. proxy ndisc)
1566 * But then router serving it might decide, that we should
1567 * know truth 8)8) --ANK (980726).
1568 */
1569 if (rt6_check_expired(rt))
1570 continue;
1571 if (!(rt->rt6i_flags & RTF_GATEWAY))
1572 continue;
David S. Millerd1918542011-12-28 20:19:20 -05001573 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001574 continue;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001575 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001576 continue;
1577 break;
1578 }
YOSHIFUJI Hideakie843b9e2006-03-20 17:07:49 -08001579
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001580 if (!rt)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001581 rt = net->ipv6.ip6_null_entry;
David S. Miller4c9483b2011-03-12 16:22:43 -05001582 BACKTRACK(net, &fl6->saddr);
YOSHIFUJI Hideakicb15d9c2006-08-23 17:23:11 -07001583out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001584 dst_hold(&rt->dst);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001585
1586 read_unlock_bh(&table->tb6_lock);
1587
1588 return rt;
1589};
1590
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001591static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1592 const struct in6_addr *src,
1593 const struct in6_addr *gateway,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001594 struct net_device *dev)
1595{
Thomas Grafadaa70b2006-10-13 15:01:03 -07001596 int flags = RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001597 struct net *net = dev_net(dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001598 struct ip6rd_flowi rdfl = {
David S. Miller4c9483b2011-03-12 16:22:43 -05001599 .fl6 = {
1600 .flowi6_oif = dev->ifindex,
1601 .daddr = *dest,
1602 .saddr = *src,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001603 },
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001604 };
Thomas Grafadaa70b2006-10-13 15:01:03 -07001605
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001606 rdfl.gateway = *gateway;
Brian Haley86c36ce2009-10-07 13:58:01 -07001607
Thomas Grafadaa70b2006-10-13 15:01:03 -07001608 if (rt6_need_strict(dest))
1609 flags |= RT6_LOOKUP_F_IFACE;
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001610
David S. Miller4c9483b2011-03-12 16:22:43 -05001611 return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001612 flags, __ip6_route_redirect);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001613}
1614
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001615void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1616 const struct in6_addr *saddr,
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001617 struct neighbour *neigh, u8 *lladdr, int on_link)
1618{
1619 struct rt6_info *rt, *nrt = NULL;
1620 struct netevent_redirect netevent;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001621 struct net *net = dev_net(neigh->dev);
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001622
1623 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1624
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001625 if (rt == net->ipv6.ip6_null_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (net_ratelimit())
1627 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1628 "for redirect target\n");
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07001629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * We have finally decided to accept it.
1634 */
1635
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001636 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1638 NEIGH_UPDATE_F_OVERRIDE|
1639 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1640 NEIGH_UPDATE_F_ISROUTER))
1641 );
1642
1643 /*
1644 * Redirect received -> path was valid.
1645 * Look, redirects are sent only in response to data packets,
1646 * so that this nexthop apparently is reachable. --ANK
1647 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001648 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* Duplicate redirect: silently ignore. */
David Miller27217452011-12-02 16:52:08 +00001651 if (neigh == dst_get_neighbour_noref_raw(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto out;
1653
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001654 nrt = ip6_rt_copy(rt, dest);
David S. Miller38308472011-12-03 18:02:47 -05001655 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 goto out;
1657
1658 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1659 if (on_link)
1660 nrt->rt6i_flags &= ~RTF_GATEWAY;
1661
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001662 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
David S. Miller69cce1d2011-07-17 23:09:49 -07001663 dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Thomas Graf40e22e82006-08-22 00:00:45 -07001665 if (ip6_ins_rt(nrt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto out;
1667
Changli Gaod8d1f302010-06-10 23:31:35 -07001668 netevent.old = &rt->dst;
1669 netevent.new = &nrt->dst;
Tom Tucker8d717402006-07-30 20:43:36 -07001670 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1671
David S. Miller38308472011-12-03 18:02:47 -05001672 if (rt->rt6i_flags & RTF_CACHE) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001673 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return;
1675 }
1676
1677out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001678 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681/*
1682 * Handle ICMP "packet too big" messages
1683 * i.e. Path MTU discovery
1684 */
1685
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001686static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001687 struct net *net, u32 pmtu, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct rt6_info *rt, *nrt;
1690 int allfrag = 0;
Andrey Vagind3052b52010-12-11 15:20:11 +00001691again:
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001692 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
David S. Miller38308472011-12-03 18:02:47 -05001693 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return;
1695
Andrey Vagind3052b52010-12-11 15:20:11 +00001696 if (rt6_check_expired(rt)) {
1697 ip6_del_rt(rt);
1698 goto again;
1699 }
1700
Changli Gaod8d1f302010-06-10 23:31:35 -07001701 if (pmtu >= dst_mtu(&rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto out;
1703
1704 if (pmtu < IPV6_MIN_MTU) {
1705 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001706 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * MTU (1280) and a fragment header should always be included
1708 * after a node receiving Too Big message reporting PMTU is
1709 * less than the IPv6 Minimum Link MTU.
1710 */
1711 pmtu = IPV6_MIN_MTU;
1712 allfrag = 1;
1713 }
1714
1715 /* New mtu received -> path was valid.
1716 They are sent only in response to data packets,
1717 so that this nexthop apparently is reachable. --ANK
1718 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 dst_confirm(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 /* Host route. If it is static, it would be better
1722 not to override it, but add new one, so that
1723 when cache entry will expire old pmtu
1724 would return automatically.
1725 */
1726 if (rt->rt6i_flags & RTF_CACHE) {
David S. Millerdefb3512010-12-08 21:16:57 -08001727 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1728 if (allfrag) {
1729 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1730 features |= RTAX_FEATURE_ALLFRAG;
1731 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1732 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001733 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1735 goto out;
1736 }
1737
1738 /* Network route.
1739 Two cases are possible:
1740 1. It is connected route. Action: COW
1741 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1742 */
David Miller27217452011-12-02 16:52:08 +00001743 if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001744 nrt = rt6_alloc_cow(rt, daddr, saddr);
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001745 else
1746 nrt = rt6_alloc_clone(rt, daddr);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001747
YOSHIFUJI Hideakid5315b502006-03-20 16:58:48 -08001748 if (nrt) {
David S. Millerdefb3512010-12-08 21:16:57 -08001749 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1750 if (allfrag) {
1751 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1752 features |= RTAX_FEATURE_ALLFRAG;
1753 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1754 }
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001755
1756 /* According to RFC 1981, detecting PMTU increase shouldn't be
1757 * happened within 5 mins, the recommended timer is 10 mins.
1758 * Here this route expiration time is set to ip6_rt_mtu_expires
1759 * which is 10 mins. After 10 mins the decreased pmtu is expired
1760 * and detecting PMTU increase will be automatically happened.
1761 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001762 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
YOSHIFUJI Hideakia1e78362006-03-20 16:56:32 -08001763 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1764
Thomas Graf40e22e82006-08-22 00:00:45 -07001765 ip6_ins_rt(nrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767out:
Changli Gaod8d1f302010-06-10 23:31:35 -07001768 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001771void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
Maciej Żenczykowskiae878ae2010-10-03 14:49:00 -07001772 struct net_device *dev, u32 pmtu)
1773{
1774 struct net *net = dev_net(dev);
1775
1776 /*
1777 * RFC 1981 states that a node "MUST reduce the size of the packets it
1778 * is sending along the path" that caused the Packet Too Big message.
1779 * Since it's not possible in the general case to determine which
1780 * interface was used to send the original packet, we update the MTU
1781 * on the interface that will be used to send future packets. We also
1782 * update the MTU on the interface that received the Packet Too Big in
1783 * case the original packet was forced out that interface with
1784 * SO_BINDTODEVICE or similar. This is the next best thing to the
1785 * correct behaviour, which would be to update the MTU on all
1786 * interfaces.
1787 */
1788 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1789 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
1793 * Misc support functions
1794 */
1795
Eric Dumazet21efcfa2011-07-19 20:18:36 +00001796static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
1797 const struct in6_addr *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
David S. Millerd1918542011-12-28 20:19:20 -05001799 struct net *net = dev_net(ort->dst.dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001800 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
David S. Miller957c6652011-06-24 15:25:00 -07001801 ort->dst.dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001804 rt->dst.input = ort->dst.input;
1805 rt->dst.output = ort->dst.output;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001806 rt->dst.flags |= DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001808 rt->rt6i_dst.addr = *dest;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00001809 rt->rt6i_dst.plen = 128;
David S. Millerdefb3512010-12-08 21:16:57 -08001810 dst_copy_metrics(&rt->dst, &ort->dst);
Changli Gaod8d1f302010-06-10 23:31:35 -07001811 rt->dst.error = ort->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 rt->rt6i_idev = ort->rt6i_idev;
1813 if (rt->rt6i_idev)
1814 in6_dev_hold(rt->rt6i_idev);
Changli Gaod8d1f302010-06-10 23:31:35 -07001815 rt->dst.lastuse = jiffies;
David S. Millerd1918542011-12-28 20:19:20 -05001816 rt->dst.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001818 rt->rt6i_gateway = ort->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1820 rt->rt6i_metric = 0;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822#ifdef CONFIG_IPV6_SUBTREES
1823 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1824#endif
Florian Westphal0f6c6392011-05-20 11:27:24 +00001825 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
Thomas Grafc71099a2006-08-04 23:20:06 -07001826 rt->rt6i_table = ort->rt6i_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828 return rt;
1829}
1830
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001831#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001832static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001833 const struct in6_addr *prefix, int prefixlen,
1834 const struct in6_addr *gwaddr, int ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001835{
1836 struct fib6_node *fn;
1837 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07001838 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001839
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001840 table = fib6_get_table(net, RT6_TABLE_INFO);
David S. Miller38308472011-12-03 18:02:47 -05001841 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001842 return NULL;
1843
1844 write_lock_bh(&table->tb6_lock);
1845 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001846 if (!fn)
1847 goto out;
1848
Changli Gaod8d1f302010-06-10 23:31:35 -07001849 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001850 if (rt->dst.dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001851 continue;
1852 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1853 continue;
1854 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1855 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001856 dst_hold(&rt->dst);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001857 break;
1858 }
1859out:
Thomas Grafc71099a2006-08-04 23:20:06 -07001860 write_unlock_bh(&table->tb6_lock);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001861 return rt;
1862}
1863
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001864static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001865 const struct in6_addr *prefix, int prefixlen,
1866 const struct in6_addr *gwaddr, int ifindex,
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001867 unsigned pref)
1868{
Thomas Graf86872cb2006-08-22 00:01:08 -07001869 struct fib6_config cfg = {
1870 .fc_table = RT6_TABLE_INFO,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001871 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001872 .fc_ifindex = ifindex,
1873 .fc_dst_len = prefixlen,
1874 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1875 RTF_UP | RTF_PREF(pref),
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001876 .fc_nlinfo.pid = 0,
1877 .fc_nlinfo.nlh = NULL,
1878 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001879 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001880
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001881 cfg.fc_dst = *prefix;
1882 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07001883
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08001884 /* We should treat it as a default route if prefix length is 0. */
1885 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07001886 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001887
Thomas Graf86872cb2006-08-22 00:01:08 -07001888 ip6_route_add(&cfg);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001889
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08001890 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001891}
1892#endif
1893
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001894struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001897 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001899 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05001900 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001901 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Thomas Grafc71099a2006-08-04 23:20:06 -07001903 write_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001904 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001905 if (dev == rt->dst.dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08001906 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1908 break;
1909 }
1910 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001911 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001912 write_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return rt;
1914}
1915
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001916struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001917 struct net_device *dev,
1918 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Thomas Graf86872cb2006-08-22 00:01:08 -07001920 struct fib6_config cfg = {
1921 .fc_table = RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08001922 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07001923 .fc_ifindex = dev->ifindex,
1924 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1925 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Daniel Lezcano55786892008-03-04 13:47:47 -08001926 .fc_nlinfo.pid = 0,
1927 .fc_nlinfo.nlh = NULL,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001928 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001929 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001931 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Thomas Graf86872cb2006-08-22 00:01:08 -07001933 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return rt6_get_dflt_router(gwaddr, dev);
1936}
1937
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001938void rt6_purge_dflt_routers(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001941 struct fib6_table *table;
1942
1943 /* NOTE: Keep consistent with rt6_get_dflt_router */
Daniel Lezcano7b4da532008-03-04 13:47:14 -08001944 table = fib6_get_table(net, RT6_TABLE_DFLT);
David S. Miller38308472011-12-03 18:02:47 -05001945 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07001946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948restart:
Thomas Grafc71099a2006-08-04 23:20:06 -07001949 read_lock_bh(&table->tb6_lock);
Changli Gaod8d1f302010-06-10 23:31:35 -07001950 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001952 dst_hold(&rt->dst);
Thomas Grafc71099a2006-08-04 23:20:06 -07001953 read_unlock_bh(&table->tb6_lock);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001954 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 goto restart;
1956 }
1957 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001958 read_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Daniel Lezcano55786892008-03-04 13:47:47 -08001961static void rtmsg_to_fib6_config(struct net *net,
1962 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07001963 struct fib6_config *cfg)
1964{
1965 memset(cfg, 0, sizeof(*cfg));
1966
1967 cfg->fc_table = RT6_TABLE_MAIN;
1968 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1969 cfg->fc_metric = rtmsg->rtmsg_metric;
1970 cfg->fc_expires = rtmsg->rtmsg_info;
1971 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1972 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1973 cfg->fc_flags = rtmsg->rtmsg_flags;
1974
Daniel Lezcano55786892008-03-04 13:47:47 -08001975 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08001976
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001977 cfg->fc_dst = rtmsg->rtmsg_dst;
1978 cfg->fc_src = rtmsg->rtmsg_src;
1979 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07001980}
1981
Daniel Lezcano55786892008-03-04 13:47:47 -08001982int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Thomas Graf86872cb2006-08-22 00:01:08 -07001984 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct in6_rtmsg rtmsg;
1986 int err;
1987
1988 switch(cmd) {
1989 case SIOCADDRT: /* Add a route */
1990 case SIOCDELRT: /* Delete a route */
1991 if (!capable(CAP_NET_ADMIN))
1992 return -EPERM;
1993 err = copy_from_user(&rtmsg, arg,
1994 sizeof(struct in6_rtmsg));
1995 if (err)
1996 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07001997
Daniel Lezcano55786892008-03-04 13:47:47 -08001998 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 rtnl_lock();
2001 switch (cmd) {
2002 case SIOCADDRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002003 err = ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
2005 case SIOCDELRT:
Thomas Graf86872cb2006-08-22 00:01:08 -07002006 err = ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 break;
2008 default:
2009 err = -EINVAL;
2010 }
2011 rtnl_unlock();
2012
2013 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 return -EINVAL;
2017}
2018
2019/*
2020 * Drop the packet on the floor
2021 */
2022
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07002023static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002025 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00002026 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002027 switch (ipstats_mib_noroutes) {
2028 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07002029 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00002030 if (type == IPV6_ADDR_ANY) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002031 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2032 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002033 break;
2034 }
2035 /* FALLTHROUGH */
2036 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002037 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
2038 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002039 break;
2040 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002041 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 kfree_skb(skb);
2043 return 0;
2044}
2045
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002046static int ip6_pkt_discard(struct sk_buff *skb)
2047{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002048 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002049}
2050
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002051static int ip6_pkt_discard_out(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Eric Dumazetadf30902009-06-02 05:19:30 +00002053 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002054 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
David S. Miller6723ab52006-10-18 21:20:57 -07002057#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2058
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002059static int ip6_pkt_prohibit(struct sk_buff *skb)
2060{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002061 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002062}
2063
2064static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2065{
Eric Dumazetadf30902009-06-02 05:19:30 +00002066 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07002067 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07002068}
2069
David S. Miller6723ab52006-10-18 21:20:57 -07002070#endif
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072/*
2073 * Allocate a dst for local (unicast / anycast) address.
2074 */
2075
2076struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2077 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05002078 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002080 struct net *net = dev_net(idev->dev);
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002081 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
David S. Miller957c6652011-06-24 15:25:00 -07002082 net->loopback_dev, 0);
David S. Millerf83c7792011-12-28 15:41:23 -05002083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
David S. Miller38308472011-12-03 18:02:47 -05002085 if (!rt) {
Ben Greear40385652010-11-08 12:33:48 +00002086 if (net_ratelimit())
2087 pr_warning("IPv6: Maximum number of routes reached,"
2088 " consider increasing route/max_size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return ERR_PTR(-ENOMEM);
Ben Greear40385652010-11-08 12:33:48 +00002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 in6_dev_hold(idev);
2093
David S. Miller11d53b42011-06-24 15:23:34 -07002094 rt->dst.flags |= DST_HOST;
Changli Gaod8d1f302010-06-10 23:31:35 -07002095 rt->dst.input = ip6_input;
2096 rt->dst.output = ip6_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 rt->rt6i_idev = idev;
Changli Gaod8d1f302010-06-10 23:31:35 -07002098 rt->dst.obsolete = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09002101 if (anycast)
2102 rt->rt6i_flags |= RTF_ANYCAST;
2103 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 rt->rt6i_flags |= RTF_LOCAL;
David S. Miller8ade06c2011-12-29 18:51:57 -05002105 err = rt6_bind_neighbour(rt, rt->dst.dev);
David S. Millerf83c7792011-12-28 15:41:23 -05002106 if (err) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002107 dst_free(&rt->dst);
David S. Millerf83c7792011-12-28 15:41:23 -05002108 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002111 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 rt->rt6i_dst.plen = 128;
Daniel Lezcano55786892008-03-04 13:47:47 -08002113 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Changli Gaod8d1f302010-06-10 23:31:35 -07002115 atomic_set(&rt->dst.__refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 return rt;
2118}
2119
Daniel Walterc3968a82011-04-13 21:10:57 +00002120int ip6_route_get_saddr(struct net *net,
2121 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002122 const struct in6_addr *daddr,
Daniel Walterc3968a82011-04-13 21:10:57 +00002123 unsigned int prefs,
2124 struct in6_addr *saddr)
2125{
2126 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2127 int err = 0;
2128 if (rt->rt6i_prefsrc.plen)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002129 *saddr = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002130 else
2131 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2132 daddr, prefs, saddr);
2133 return err;
2134}
2135
2136/* remove deleted ip from prefsrc entries */
2137struct arg_dev_net_ip {
2138 struct net_device *dev;
2139 struct net *net;
2140 struct in6_addr *addr;
2141};
2142
2143static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2144{
2145 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2146 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2147 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2148
David S. Millerd1918542011-12-28 20:19:20 -05002149 if (((void *)rt->dst.dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00002150 rt != net->ipv6.ip6_null_entry &&
2151 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2152 /* remove prefsrc entry */
2153 rt->rt6i_prefsrc.plen = 0;
2154 }
2155 return 0;
2156}
2157
2158void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2159{
2160 struct net *net = dev_net(ifp->idev->dev);
2161 struct arg_dev_net_ip adni = {
2162 .dev = ifp->idev->dev,
2163 .net = net,
2164 .addr = &ifp->addr,
2165 };
2166 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2167}
2168
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002169struct arg_dev_net {
2170 struct net_device *dev;
2171 struct net *net;
2172};
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int fib6_ifdown(struct rt6_info *rt, void *arg)
2175{
stephen hemmingerbc3ef662010-12-16 17:42:40 +00002176 const struct arg_dev_net *adn = arg;
2177 const struct net_device *dev = adn->dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002178
David S. Millerd1918542011-12-28 20:19:20 -05002179 if ((rt->dst.dev == dev || !dev) &&
David S. Millerc159d302011-12-26 15:24:36 -05002180 rt != adn->net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -1;
David S. Millerc159d302011-12-26 15:24:36 -05002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184}
2185
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002186void rt6_ifdown(struct net *net, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002188 struct arg_dev_net adn = {
2189 .dev = dev,
2190 .net = net,
2191 };
2192
2193 fib6_clean_all(net, fib6_ifdown, 0, &adn);
David S. Miller1e493d12008-09-10 17:27:15 -07002194 icmp6_clean_all(fib6_ifdown, &adn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197struct rt6_mtu_change_arg
2198{
2199 struct net_device *dev;
2200 unsigned mtu;
2201};
2202
2203static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2204{
2205 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2206 struct inet6_dev *idev;
2207
2208 /* In IPv6 pmtu discovery is not optional,
2209 so that RTAX_MTU lock cannot disable it.
2210 We still use this lock to block changes
2211 caused by addrconf/ndisc.
2212 */
2213
2214 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05002215 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return 0;
2217
2218 /* For administrative MTU increase, there is no way to discover
2219 IPv6 PMTU increase, so PMTU increase should be updated here.
2220 Since RFC 1981 doesn't include administrative MTU increase
2221 update PMTU increase is a MUST. (i.e. jumbo frame)
2222 */
2223 /*
2224 If new MTU is less than route PMTU, this new MTU will be the
2225 lowest MTU in the path, update the route PMTU to reflect PMTU
2226 decreases; if new MTU is greater than route PMTU, and the
2227 old MTU is the lowest MTU in the path, update the route PMTU
2228 to reflect the increase. In this case if the other nodes' MTU
2229 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2230 PMTU discouvery.
2231 */
David S. Millerd1918542011-12-28 20:19:20 -05002232 if (rt->dst.dev == arg->dev &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002233 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2234 (dst_mtu(&rt->dst) >= arg->mtu ||
2235 (dst_mtu(&rt->dst) < arg->mtu &&
2236 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
David S. Millerdefb3512010-12-08 21:16:57 -08002237 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
Simon Arlott566cfd82007-07-26 00:09:55 -07002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 0;
2240}
2241
2242void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2243{
Thomas Grafc71099a2006-08-04 23:20:06 -07002244 struct rt6_mtu_change_arg arg = {
2245 .dev = dev,
2246 .mtu = mtu,
2247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002249 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002252static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07002253 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07002254 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07002255 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07002256 [RTA_PRIORITY] = { .type = NLA_U32 },
2257 [RTA_METRICS] = { .type = NLA_NESTED },
2258};
2259
2260static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2261 struct fib6_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Thomas Graf86872cb2006-08-22 00:01:08 -07002263 struct rtmsg *rtm;
2264 struct nlattr *tb[RTA_MAX+1];
2265 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Thomas Graf86872cb2006-08-22 00:01:08 -07002267 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2268 if (err < 0)
2269 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Thomas Graf86872cb2006-08-22 00:01:08 -07002271 err = -EINVAL;
2272 rtm = nlmsg_data(nlh);
2273 memset(cfg, 0, sizeof(*cfg));
2274
2275 cfg->fc_table = rtm->rtm_table;
2276 cfg->fc_dst_len = rtm->rtm_dst_len;
2277 cfg->fc_src_len = rtm->rtm_src_len;
2278 cfg->fc_flags = RTF_UP;
2279 cfg->fc_protocol = rtm->rtm_protocol;
2280
2281 if (rtm->rtm_type == RTN_UNREACHABLE)
2282 cfg->fc_flags |= RTF_REJECT;
2283
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002284 if (rtm->rtm_type == RTN_LOCAL)
2285 cfg->fc_flags |= RTF_LOCAL;
2286
Thomas Graf86872cb2006-08-22 00:01:08 -07002287 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2288 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002289 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07002290
2291 if (tb[RTA_GATEWAY]) {
2292 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2293 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002295
2296 if (tb[RTA_DST]) {
2297 int plen = (rtm->rtm_dst_len + 7) >> 3;
2298
2299 if (nla_len(tb[RTA_DST]) < plen)
2300 goto errout;
2301
2302 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002304
2305 if (tb[RTA_SRC]) {
2306 int plen = (rtm->rtm_src_len + 7) >> 3;
2307
2308 if (nla_len(tb[RTA_SRC]) < plen)
2309 goto errout;
2310
2311 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002313
Daniel Walterc3968a82011-04-13 21:10:57 +00002314 if (tb[RTA_PREFSRC])
2315 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2316
Thomas Graf86872cb2006-08-22 00:01:08 -07002317 if (tb[RTA_OIF])
2318 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2319
2320 if (tb[RTA_PRIORITY])
2321 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2322
2323 if (tb[RTA_METRICS]) {
2324 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2325 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Thomas Graf86872cb2006-08-22 00:01:08 -07002327
2328 if (tb[RTA_TABLE])
2329 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2330
2331 err = 0;
2332errout:
2333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
Thomas Grafc127ea22007-03-22 11:58:32 -07002336static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Thomas Graf86872cb2006-08-22 00:01:08 -07002338 struct fib6_config cfg;
2339 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Thomas Graf86872cb2006-08-22 00:01:08 -07002341 err = rtm_to_fib6_config(skb, nlh, &cfg);
2342 if (err < 0)
2343 return err;
2344
2345 return ip6_route_del(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Thomas Grafc127ea22007-03-22 11:58:32 -07002348static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Thomas Graf86872cb2006-08-22 00:01:08 -07002350 struct fib6_config cfg;
2351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Thomas Graf86872cb2006-08-22 00:01:08 -07002353 err = rtm_to_fib6_config(skb, nlh, &cfg);
2354 if (err < 0)
2355 return err;
2356
2357 return ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Thomas Graf339bf982006-11-10 14:10:15 -08002360static inline size_t rt6_nlmsg_size(void)
2361{
2362 return NLMSG_ALIGN(sizeof(struct rtmsg))
2363 + nla_total_size(16) /* RTA_SRC */
2364 + nla_total_size(16) /* RTA_DST */
2365 + nla_total_size(16) /* RTA_GATEWAY */
2366 + nla_total_size(16) /* RTA_PREFSRC */
2367 + nla_total_size(4) /* RTA_TABLE */
2368 + nla_total_size(4) /* RTA_IIF */
2369 + nla_total_size(4) /* RTA_OIF */
2370 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08002371 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Thomas Graf339bf982006-11-10 14:10:15 -08002372 + nla_total_size(sizeof(struct rta_cacheinfo));
2373}
2374
Brian Haley191cd582008-08-14 15:33:21 -07002375static int rt6_fill_node(struct net *net,
2376 struct sk_buff *skb, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07002377 struct in6_addr *dst, struct in6_addr *src,
2378 int iif, int type, u32 pid, u32 seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002379 int prefix, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
David S. Miller346f8702011-12-29 15:22:33 -05002381 const struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002383 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08002384 long expires;
Patrick McHardy9e762a42006-08-10 23:09:48 -07002385 u32 table;
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002386 struct neighbour *n;
David S. Miller346f8702011-12-29 15:22:33 -05002387 u32 ts, tsage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 if (prefix) { /* user wants prefix routes only */
2390 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2391 /* success since this is not a prefix route */
2392 return 1;
2393 }
2394 }
2395
Thomas Graf2d7202b2006-08-22 00:01:27 -07002396 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05002397 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08002398 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002399
2400 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 rtm->rtm_family = AF_INET6;
2402 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2403 rtm->rtm_src_len = rt->rt6i_src.plen;
2404 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07002405 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07002406 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07002407 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07002408 table = RT6_TABLE_UNSPEC;
2409 rtm->rtm_table = table;
Thomas Graf2d7202b2006-08-22 00:01:27 -07002410 NLA_PUT_U32(skb, RTA_TABLE, table);
David S. Miller38308472011-12-03 18:02:47 -05002411 if (rt->rt6i_flags & RTF_REJECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 rtm->rtm_type = RTN_UNREACHABLE;
David S. Miller38308472011-12-03 18:02:47 -05002413 else if (rt->rt6i_flags & RTF_LOCAL)
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00002414 rtm->rtm_type = RTN_LOCAL;
David S. Millerd1918542011-12-28 20:19:20 -05002415 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 rtm->rtm_type = RTN_LOCAL;
2417 else
2418 rtm->rtm_type = RTN_UNICAST;
2419 rtm->rtm_flags = 0;
2420 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2421 rtm->rtm_protocol = rt->rt6i_protocol;
David S. Miller38308472011-12-03 18:02:47 -05002422 if (rt->rt6i_flags & RTF_DYNAMIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 rtm->rtm_protocol = RTPROT_REDIRECT;
2424 else if (rt->rt6i_flags & RTF_ADDRCONF)
2425 rtm->rtm_protocol = RTPROT_KERNEL;
David S. Miller38308472011-12-03 18:02:47 -05002426 else if (rt->rt6i_flags & RTF_DEFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 rtm->rtm_protocol = RTPROT_RA;
2428
David S. Miller38308472011-12-03 18:02:47 -05002429 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 rtm->rtm_flags |= RTM_F_CLONED;
2431
2432 if (dst) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002433 NLA_PUT(skb, RTA_DST, 16, dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002434 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 } else if (rtm->rtm_dst_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002436 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437#ifdef CONFIG_IPV6_SUBTREES
2438 if (src) {
Thomas Graf2d7202b2006-08-22 00:01:27 -07002439 NLA_PUT(skb, RTA_SRC, 16, src);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002440 rtm->rtm_src_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 } else if (rtm->rtm_src_len)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002442 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002444 if (iif) {
2445#ifdef CONFIG_IPV6_MROUTE
2446 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08002447 int err = ip6mr_get_route(net, skb, rtm, nowait);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002448 if (err <= 0) {
2449 if (!nowait) {
2450 if (err == 0)
2451 return 0;
2452 goto nla_put_failure;
2453 } else {
2454 if (err == -EMSGSIZE)
2455 goto nla_put_failure;
2456 }
2457 }
2458 } else
2459#endif
2460 NLA_PUT_U32(skb, RTA_IIF, iif);
2461 } else if (dst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct in6_addr saddr_buf;
Daniel Walterc3968a82011-04-13 21:10:57 +00002463 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002464 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07002466
Daniel Walterc3968a82011-04-13 21:10:57 +00002467 if (rt->rt6i_prefsrc.plen) {
2468 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002469 saddr_buf = rt->rt6i_prefsrc.addr;
Daniel Walterc3968a82011-04-13 21:10:57 +00002470 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2471 }
2472
David S. Millerdefb3512010-12-08 21:16:57 -08002473 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07002474 goto nla_put_failure;
2475
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002476 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +00002477 n = dst_get_neighbour_noref(&rt->dst);
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002478 if (n)
2479 NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2480 rcu_read_unlock();
Thomas Graf2d7202b2006-08-22 00:01:27 -07002481
Changli Gaod8d1f302010-06-10 23:31:35 -07002482 if (rt->dst.dev)
David S. Millerd1918542011-12-28 20:19:20 -05002483 NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex);
Thomas Graf2d7202b2006-08-22 00:01:27 -07002484
2485 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
Thomas Grafe3703b32006-11-27 09:27:07 -08002486
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002487 if (!(rt->rt6i_flags & RTF_EXPIRES))
2488 expires = 0;
David S. Millerd1918542011-12-28 20:19:20 -05002489 else if (rt->dst.expires - jiffies < INT_MAX)
2490 expires = rt->dst.expires - jiffies;
YOSHIFUJI Hideaki36e3dea2008-05-13 02:52:55 +09002491 else
2492 expires = INT_MAX;
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07002493
David S. Miller346f8702011-12-29 15:22:33 -05002494 peer = rt->rt6i_peer;
2495 ts = tsage = 0;
2496 if (peer && peer->tcp_ts_stamp) {
2497 ts = peer->tcp_ts;
2498 tsage = get_seconds() - peer->tcp_ts_stamp;
2499 }
2500
2501 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
Changli Gaod8d1f302010-06-10 23:31:35 -07002502 expires, rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08002503 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Thomas Graf2d7202b2006-08-22 00:01:27 -07002505 return nlmsg_end(skb, nlh);
2506
2507nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002508 nlmsg_cancel(skb, nlh);
2509 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Patrick McHardy1b43af52006-08-10 23:11:17 -07002512int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2515 int prefix;
2516
Thomas Graf2d7202b2006-08-22 00:01:27 -07002517 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2518 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2520 } else
2521 prefix = 0;
2522
Brian Haley191cd582008-08-14 15:33:21 -07002523 return rt6_fill_node(arg->net,
2524 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002526 prefix, 0, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Thomas Grafc127ea22007-03-22 11:58:32 -07002529static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002531 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07002532 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07002534 struct sk_buff *skb;
2535 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05002536 struct flowi6 fl6;
Thomas Grafab364a62006-08-22 00:01:47 -07002537 int err, iif = 0;
2538
2539 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2540 if (err < 0)
2541 goto errout;
2542
2543 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05002544 memset(&fl6, 0, sizeof(fl6));
Thomas Grafab364a62006-08-22 00:01:47 -07002545
2546 if (tb[RTA_SRC]) {
2547 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2548 goto errout;
2549
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002550 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07002551 }
2552
2553 if (tb[RTA_DST]) {
2554 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2555 goto errout;
2556
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002557 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07002558 }
2559
2560 if (tb[RTA_IIF])
2561 iif = nla_get_u32(tb[RTA_IIF]);
2562
2563 if (tb[RTA_OIF])
David S. Miller4c9483b2011-03-12 16:22:43 -05002564 fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07002565
2566 if (iif) {
2567 struct net_device *dev;
Daniel Lezcano55786892008-03-04 13:47:47 -08002568 dev = __dev_get_by_index(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07002569 if (!dev) {
2570 err = -ENODEV;
2571 goto errout;
2572 }
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05002576 if (!skb) {
Thomas Grafab364a62006-08-22 00:01:47 -07002577 err = -ENOBUFS;
2578 goto errout;
2579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* Reserve room for dummy headers, this skb can pass
2582 through good chunk of routing engine.
2583 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002584 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2586
David S. Miller4c9483b2011-03-12 16:22:43 -05002587 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
Changli Gaod8d1f302010-06-10 23:31:35 -07002588 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
David S. Miller4c9483b2011-03-12 16:22:43 -05002590 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002592 nlh->nlmsg_seq, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07002594 kfree_skb(skb);
2595 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597
Daniel Lezcano55786892008-03-04 13:47:47 -08002598 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafab364a62006-08-22 00:01:47 -07002599errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Thomas Graf86872cb2006-08-22 00:01:08 -07002603void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08002606 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002607 u32 seq;
2608 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002610 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002611 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07002612
Thomas Graf339bf982006-11-10 14:10:15 -08002613 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05002614 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07002615 goto errout;
2616
Brian Haley191cd582008-08-14 15:33:21 -07002617 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002618 event, info->pid, seq, 0, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002619 if (err < 0) {
2620 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2621 WARN_ON(err == -EMSGSIZE);
2622 kfree_skb(skb);
2623 goto errout;
2624 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002625 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2626 info->nlh, gfp_any());
2627 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07002628errout:
2629 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08002630 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002633static int ip6_route_dev_notify(struct notifier_block *this,
2634 unsigned long event, void *data)
2635{
2636 struct net_device *dev = (struct net_device *)data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002637 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002638
2639 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002640 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002641 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2642#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002643 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002644 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002645 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002646 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2647#endif
2648 }
2649
2650 return NOTIFY_OK;
2651}
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
2654 * /proc
2655 */
2656
2657#ifdef CONFIG_PROC_FS
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659struct rt6_proc_arg
2660{
2661 char *buffer;
2662 int offset;
2663 int length;
2664 int skip;
2665 int len;
2666};
2667
2668static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2669{
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002670 struct seq_file *m = p_arg;
David S. Miller69cce1d2011-07-17 23:09:49 -07002671 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002673 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675#ifdef CONFIG_IPV6_SUBTREES
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002676 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677#else
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002678 seq_puts(m, "00000000000000000000000000000000 00 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679#endif
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002680 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +00002681 n = dst_get_neighbour_noref(&rt->dst);
David S. Miller69cce1d2011-07-17 23:09:49 -07002682 if (n) {
2683 seq_printf(m, "%pi6", n->primary_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 } else {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002685 seq_puts(m, "00000000000000000000000000000000");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +00002687 rcu_read_unlock();
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002688 seq_printf(m, " %08x %08x %08x %08x %8s\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07002689 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2690 rt->dst.__use, rt->rt6i_flags,
David S. Millerd1918542011-12-28 20:19:20 -05002691 rt->dst.dev ? rt->dst.dev->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return 0;
2693}
2694
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002695static int ipv6_route_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002697 struct net *net = (struct net *)m->private;
Josh Hunt32b293a2011-12-28 13:23:07 +00002698 fib6_clean_all_ro(net, rt6_info_route, 0, m);
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002702static int ipv6_route_open(struct inode *inode, struct file *file)
2703{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002704 return single_open_net(inode, file, ipv6_route_show);
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002705}
2706
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002707static const struct file_operations ipv6_route_proc_fops = {
2708 .owner = THIS_MODULE,
2709 .open = ipv6_route_open,
2710 .read = seq_read,
2711 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002712 .release = single_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08002713};
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2716{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002717 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002719 net->ipv6.rt6_stats->fib_nodes,
2720 net->ipv6.rt6_stats->fib_route_nodes,
2721 net->ipv6.rt6_stats->fib_rt_alloc,
2722 net->ipv6.rt6_stats->fib_rt_entries,
2723 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00002724 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002725 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 return 0;
2728}
2729
2730static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2731{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002732 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08002733}
2734
Arjan van de Ven9a321442007-02-12 00:55:35 -08002735static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 .owner = THIS_MODULE,
2737 .open = rt6_stats_seq_open,
2738 .read = seq_read,
2739 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002740 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741};
2742#endif /* CONFIG_PROC_FS */
2743
2744#ifdef CONFIG_SYSCTL
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002747int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 void __user *buffer, size_t *lenp, loff_t *ppos)
2749{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002750 struct net *net;
2751 int delay;
2752 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002754
2755 net = (struct net *)ctl->extra1;
2756 delay = net->ipv6.sysctl.flush_delay;
2757 proc_dointvec(ctl, write, buffer, lenp, ppos);
2758 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760}
2761
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002762ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002763 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08002765 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07002767 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002768 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 },
2770 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002772 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 .maxlen = sizeof(int),
2774 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002775 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 },
2777 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08002779 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 .maxlen = sizeof(int),
2781 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002782 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 },
2784 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002786 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 .maxlen = sizeof(int),
2788 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002789 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 },
2791 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08002793 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 .maxlen = sizeof(int),
2795 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002796 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 },
2798 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08002800 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 .maxlen = sizeof(int),
2802 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002803 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 },
2805 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08002807 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .maxlen = sizeof(int),
2809 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002810 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 },
2812 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08002814 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 .maxlen = sizeof(int),
2816 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002817 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 },
2819 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08002821 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .maxlen = sizeof(int),
2823 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07002824 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 },
2826 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08002828 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 .maxlen = sizeof(int),
2830 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08002831 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002833 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834};
2835
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002836struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002837{
2838 struct ctl_table *table;
2839
2840 table = kmemdup(ipv6_route_table_template,
2841 sizeof(ipv6_route_table_template),
2842 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002843
2844 if (table) {
2845 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00002846 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002847 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002848 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2849 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2850 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2851 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2852 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2853 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2854 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08002855 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09002856 }
2857
Daniel Lezcano760f2d02008-01-10 02:53:43 -08002858 return table;
2859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860#endif
2861
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002862static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002863{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07002864 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002865
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002866 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2867 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002868
Eric Dumazetfc66f952010-10-08 06:37:34 +00002869 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2870 goto out_ip6_dst_ops;
2871
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002872 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2873 sizeof(*net->ipv6.ip6_null_entry),
2874 GFP_KERNEL);
2875 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00002876 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07002877 net->ipv6.ip6_null_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002878 (struct dst_entry *)net->ipv6.ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002879 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002880 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2881 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002882
2883#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2884 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2885 sizeof(*net->ipv6.ip6_prohibit_entry),
2886 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002887 if (!net->ipv6.ip6_prohibit_entry)
2888 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002889 net->ipv6.ip6_prohibit_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002890 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002891 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002892 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2893 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002894
2895 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2896 sizeof(*net->ipv6.ip6_blk_hole_entry),
2897 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002898 if (!net->ipv6.ip6_blk_hole_entry)
2899 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002900 net->ipv6.ip6_blk_hole_entry->dst.path =
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002901 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07002902 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08002903 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2904 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002905#endif
2906
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07002907 net->ipv6.sysctl.flush_delay = 0;
2908 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2909 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2910 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2911 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2912 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2913 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2914 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2915
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002916#ifdef CONFIG_PROC_FS
2917 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2918 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2919#endif
Benjamin Thery6891a342008-03-04 13:49:47 -08002920 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2921
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002922 ret = 0;
2923out:
2924 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002925
Peter Zijlstra68fffc62008-10-07 14:12:10 -07002926#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2927out_ip6_prohibit_entry:
2928 kfree(net->ipv6.ip6_prohibit_entry);
2929out_ip6_null_entry:
2930 kfree(net->ipv6.ip6_null_entry);
2931#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00002932out_ip6_dst_entries:
2933 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002934out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08002935 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002936}
2937
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002938static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002939{
2940#ifdef CONFIG_PROC_FS
2941 proc_net_remove(net, "ipv6_route");
2942 proc_net_remove(net, "rt6_stats");
2943#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002944 kfree(net->ipv6.ip6_null_entry);
2945#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2946 kfree(net->ipv6.ip6_prohibit_entry);
2947 kfree(net->ipv6.ip6_blk_hole_entry);
2948#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00002949 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08002950}
2951
2952static struct pernet_operations ip6_route_net_ops = {
2953 .init = ip6_route_net_init,
2954 .exit = ip6_route_net_exit,
2955};
2956
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002957static struct notifier_block ip6_route_dev_notifier = {
2958 .notifier_call = ip6_route_dev_notify,
2959 .priority = 0,
2960};
2961
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002962int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002964 int ret;
2965
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08002966 ret = -ENOMEM;
2967 ip6_dst_ops_template.kmem_cachep =
2968 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2969 SLAB_HWCACHE_ALIGN, NULL);
2970 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08002971 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07002972
Eric Dumazetfc66f952010-10-08 06:37:34 +00002973 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002974 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002975 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08002976
Eric Dumazetfc66f952010-10-08 06:37:34 +00002977 ret = register_pernet_subsys(&ip6_route_net_ops);
2978 if (ret)
2979 goto out_dst_entries;
2980
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07002981 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2982
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002983 /* Registering of the loopback is done before this portion of code,
2984 * the loopback reference in rt6_info will not be taken, do it
2985 * manually for init_net */
Changli Gaod8d1f302010-06-10 23:31:35 -07002986 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002987 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2988 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07002989 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002990 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07002991 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002992 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2993 #endif
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002994 ret = fib6_init();
2995 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002996 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002997
Daniel Lezcano433d49c2007-12-07 00:43:48 -08002998 ret = xfrm6_init();
2999 if (ret)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003000 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08003001
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003002 ret = fib6_rules_init();
3003 if (ret)
3004 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08003005
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003006 ret = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003007 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3008 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3009 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003010 goto fib6_rules_init;
3011
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003012 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08003013 if (ret)
3014 goto fib6_rules_init;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003015
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003016out:
3017 return ret;
3018
3019fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003020 fib6_rules_cleanup();
3021xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003022 xfrm6_fini();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003023out_fib6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003024 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003025out_register_subsys:
3026 unregister_pernet_subsys(&ip6_route_net_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003027out_dst_entries:
3028 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003029out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003030 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08003031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
3034void ip6_route_cleanup(void)
3035{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003036 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Graf101367c2006-08-04 03:39:02 -07003037 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003040 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00003041 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08003042 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}