Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux INET6 implementation |
| 3 | * FIB front-end. |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * 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 Hideaki | c0bece9 | 2006-08-23 17:23:25 -0700 | [diff] [blame] | 23 | * Ville Nuorvala |
| 24 | * Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/times.h> |
| 31 | #include <linux/socket.h> |
| 32 | #include <linux/sockios.h> |
| 33 | #include <linux/net.h> |
| 34 | #include <linux/route.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/in6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 37 | #include <linux/mroute6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
| 41 | #include <linux/seq_file.h> |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 42 | #include <linux/nsproxy.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 44 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <net/snmp.h> |
| 46 | #include <net/ipv6.h> |
| 47 | #include <net/ip6_fib.h> |
| 48 | #include <net/ip6_route.h> |
| 49 | #include <net/ndisc.h> |
| 50 | #include <net/addrconf.h> |
| 51 | #include <net/tcp.h> |
| 52 | #include <linux/rtnetlink.h> |
| 53 | #include <net/dst.h> |
| 54 | #include <net/xfrm.h> |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 55 | #include <net/netevent.h> |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 56 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #include <asm/uaccess.h> |
| 59 | |
| 60 | #ifdef CONFIG_SYSCTL |
| 61 | #include <linux/sysctl.h> |
| 62 | #endif |
| 63 | |
| 64 | /* Set to 3 to get tracing. */ |
| 65 | #define RT6_DEBUG 2 |
| 66 | |
| 67 | #if RT6_DEBUG >= 3 |
| 68 | #define RDBG(x) printk x |
| 69 | #define RT6_TRACE(x...) printk(KERN_DEBUG x) |
| 70 | #else |
| 71 | #define RDBG(x) |
| 72 | #define RT6_TRACE(x...) do { ; } while (0) |
| 73 | #endif |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static struct rt6_info * ip6_rt_copy(struct rt6_info *ort); |
| 76 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 77 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 78 | static unsigned int ip6_default_mtu(const struct dst_entry *dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); |
| 80 | static void ip6_dst_destroy(struct dst_entry *); |
| 81 | static void ip6_dst_ifdown(struct dst_entry *, |
| 82 | struct net_device *dev, int how); |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 83 | static int ip6_dst_gc(struct dst_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | static int ip6_pkt_discard(struct sk_buff *skb); |
| 86 | static int ip6_pkt_discard_out(struct sk_buff *skb); |
| 87 | static void ip6_link_failure(struct sk_buff *skb); |
| 88 | static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); |
| 89 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 91 | static struct rt6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 92 | const struct in6_addr *prefix, int prefixlen, |
| 93 | const struct in6_addr *gwaddr, int ifindex, |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 94 | unsigned pref); |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 95 | static struct rt6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 96 | const struct in6_addr *prefix, int prefixlen, |
| 97 | const struct in6_addr *gwaddr, int ifindex); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 98 | #endif |
| 99 | |
David S. Miller | 0658254 | 2011-01-27 14:58:42 -0800 | [diff] [blame] | 100 | static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) |
| 101 | { |
| 102 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 103 | struct inet_peer *peer; |
| 104 | u32 *p = NULL; |
| 105 | |
| 106 | if (!rt->rt6i_peer) |
| 107 | rt6_bind_peer(rt, 1); |
| 108 | |
| 109 | peer = rt->rt6i_peer; |
| 110 | if (peer) { |
| 111 | u32 *old_p = __DST_METRICS_PTR(old); |
| 112 | unsigned long prev, new; |
| 113 | |
| 114 | p = peer->metrics; |
| 115 | if (inet_metrics_new(peer)) |
| 116 | memcpy(p, old_p, sizeof(u32) * RTAX_MAX); |
| 117 | |
| 118 | new = (unsigned long) p; |
| 119 | prev = cmpxchg(&dst->_metrics, old, new); |
| 120 | |
| 121 | if (prev != old) { |
| 122 | p = __DST_METRICS_PTR(prev); |
| 123 | if (prev & DST_METRICS_READ_ONLY) |
| 124 | p = NULL; |
| 125 | } |
| 126 | } |
| 127 | return p; |
| 128 | } |
| 129 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 130 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .family = AF_INET6, |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 132 | .protocol = cpu_to_be16(ETH_P_IPV6), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .gc = ip6_dst_gc, |
| 134 | .gc_thresh = 1024, |
| 135 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 136 | .default_advmss = ip6_default_advmss, |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 137 | .default_mtu = ip6_default_mtu, |
David S. Miller | 0658254 | 2011-01-27 14:58:42 -0800 | [diff] [blame] | 138 | .cow_metrics = ipv6_cow_metrics, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .destroy = ip6_dst_destroy, |
| 140 | .ifdown = ip6_dst_ifdown, |
| 141 | .negative_advice = ip6_negative_advice, |
| 142 | .link_failure = ip6_link_failure, |
| 143 | .update_pmtu = ip6_rt_update_pmtu, |
Herbert Xu | 1ac06e0 | 2008-05-20 14:32:14 -0700 | [diff] [blame] | 144 | .local_out = __ip6_local_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 147 | static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 152 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) |
| 153 | { |
| 154 | } |
| 155 | |
Held Bernhard | 0972ddb | 2011-04-24 22:07:32 +0000 | [diff] [blame] | 156 | static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, |
| 157 | unsigned long old) |
| 158 | { |
| 159 | return NULL; |
| 160 | } |
| 161 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 162 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 163 | .family = AF_INET6, |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 164 | .protocol = cpu_to_be16(ETH_P_IPV6), |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 165 | .destroy = ip6_dst_destroy, |
| 166 | .check = ip6_dst_check, |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 167 | .default_mtu = ip6_blackhole_default_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 168 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 169 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
Held Bernhard | 0972ddb | 2011-04-24 22:07:32 +0000 | [diff] [blame] | 170 | .cow_metrics = ip6_rt_blackhole_cow_metrics, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 173 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
| 174 | [RTAX_HOPLIMIT - 1] = 255, |
| 175 | }; |
| 176 | |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 177 | static struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 178 | .dst = { |
| 179 | .__refcnt = ATOMIC_INIT(1), |
| 180 | .__use = 1, |
| 181 | .obsolete = -1, |
| 182 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 183 | .input = ip6_pkt_discard, |
| 184 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | }, |
| 186 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 187 | .rt6i_protocol = RTPROT_KERNEL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | .rt6i_metric = ~(u32) 0, |
| 189 | .rt6i_ref = ATOMIC_INIT(1), |
| 190 | }; |
| 191 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 193 | |
David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 194 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
| 195 | static int ip6_pkt_prohibit_out(struct sk_buff *skb); |
David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 196 | |
Adrian Bunk | 280a34c | 2008-04-21 02:29:32 -0700 | [diff] [blame] | 197 | static struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 198 | .dst = { |
| 199 | .__refcnt = ATOMIC_INIT(1), |
| 200 | .__use = 1, |
| 201 | .obsolete = -1, |
| 202 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 203 | .input = ip6_pkt_prohibit, |
| 204 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 205 | }, |
| 206 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 207 | .rt6i_protocol = RTPROT_KERNEL, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 208 | .rt6i_metric = ~(u32) 0, |
| 209 | .rt6i_ref = ATOMIC_INIT(1), |
| 210 | }; |
| 211 | |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 212 | static struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 213 | .dst = { |
| 214 | .__refcnt = ATOMIC_INIT(1), |
| 215 | .__use = 1, |
| 216 | .obsolete = -1, |
| 217 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 218 | .input = dst_discard, |
| 219 | .output = dst_discard, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 220 | }, |
| 221 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 222 | .rt6i_protocol = RTPROT_KERNEL, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 223 | .rt6i_metric = ~(u32) 0, |
| 224 | .rt6i_ref = ATOMIC_INIT(1), |
| 225 | }; |
| 226 | |
| 227 | #endif |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* allocate dst with ip6_dst_ops */ |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 230 | static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 231 | struct net_device *dev, |
| 232 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 234 | struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 235 | |
| 236 | memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); |
| 237 | |
| 238 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 242 | { |
| 243 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 244 | struct inet6_dev *idev = rt->rt6i_idev; |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 245 | struct inet_peer *peer = rt->rt6i_peer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | if (idev != NULL) { |
| 248 | rt->rt6i_idev = NULL; |
| 249 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 250 | } |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 251 | if (peer) { |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 252 | rt->rt6i_peer = NULL; |
| 253 | inet_putpeer(peer); |
| 254 | } |
| 255 | } |
| 256 | |
David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 257 | static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); |
| 258 | |
| 259 | static u32 rt6_peer_genid(void) |
| 260 | { |
| 261 | return atomic_read(&__rt6_peer_genid); |
| 262 | } |
| 263 | |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 264 | void rt6_bind_peer(struct rt6_info *rt, int create) |
| 265 | { |
| 266 | struct inet_peer *peer; |
| 267 | |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 268 | peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); |
| 269 | if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) |
| 270 | inet_putpeer(peer); |
David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 271 | else |
| 272 | rt->rt6i_peer_genid = rt6_peer_genid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 276 | int how) |
| 277 | { |
| 278 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 279 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 280 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 281 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 283 | if (dev != loopback_dev && idev != NULL && idev->dev == dev) { |
| 284 | struct inet6_dev *loopback_idev = |
| 285 | in6_dev_get(loopback_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (loopback_idev != NULL) { |
| 287 | rt->rt6i_idev = loopback_idev; |
| 288 | in6_dev_put(idev); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static __inline__ int rt6_check_expired(const struct rt6_info *rt) |
| 294 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 295 | return (rt->rt6i_flags & RTF_EXPIRES) && |
| 296 | time_after(jiffies, rt->rt6i_expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 299 | static inline int rt6_need_strict(const struct in6_addr *daddr) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 300 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 301 | return ipv6_addr_type(daddr) & |
| 302 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 306 | * Route lookup. Any table->tb6_lock is implied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ |
| 308 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 309 | static inline struct rt6_info *rt6_device_match(struct net *net, |
| 310 | struct rt6_info *rt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 311 | const struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | int oif, |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 313 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct rt6_info *local = NULL; |
| 316 | struct rt6_info *sprt; |
| 317 | |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 318 | if (!oif && ipv6_addr_any(saddr)) |
| 319 | goto out; |
| 320 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 321 | for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 322 | struct net_device *dev = sprt->rt6i_dev; |
| 323 | |
| 324 | if (oif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (dev->ifindex == oif) |
| 326 | return sprt; |
| 327 | if (dev->flags & IFF_LOOPBACK) { |
| 328 | if (sprt->rt6i_idev == NULL || |
| 329 | sprt->rt6i_idev->dev->ifindex != oif) { |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 330 | if (flags & RT6_LOOKUP_F_IFACE && oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | continue; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 332 | if (local && (!oif || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | local->rt6i_idev->dev->ifindex == oif)) |
| 334 | continue; |
| 335 | } |
| 336 | local = sprt; |
| 337 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 338 | } else { |
| 339 | if (ipv6_chk_addr(net, saddr, dev, |
| 340 | flags & RT6_LOOKUP_F_IFACE)) |
| 341 | return sprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 345 | if (oif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (local) |
| 347 | return local; |
| 348 | |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 349 | if (flags & RT6_LOOKUP_F_IFACE) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 350 | return net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 352 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return rt; |
| 354 | } |
| 355 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 356 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 357 | static void rt6_probe(struct rt6_info *rt) |
| 358 | { |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 359 | struct neighbour *neigh = rt ? dst_get_neighbour(&rt->dst) : NULL; |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 360 | /* |
| 361 | * Okay, this does not seem to be appropriate |
| 362 | * for now, however, we need to check if it |
| 363 | * is really so; aka Router Reachability Probing. |
| 364 | * |
| 365 | * Router Reachability Probe MUST be rate-limited |
| 366 | * to no more than one per minute. |
| 367 | */ |
| 368 | if (!neigh || (neigh->nud_state & NUD_VALID)) |
| 369 | return; |
| 370 | read_lock_bh(&neigh->lock); |
| 371 | if (!(neigh->nud_state & NUD_VALID) && |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 372 | time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 373 | struct in6_addr mcaddr; |
| 374 | struct in6_addr *target; |
| 375 | |
| 376 | neigh->updated = jiffies; |
| 377 | read_unlock_bh(&neigh->lock); |
| 378 | |
| 379 | target = (struct in6_addr *)&neigh->primary_key; |
| 380 | addrconf_addr_solict_mult(target, &mcaddr); |
| 381 | ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL); |
| 382 | } else |
| 383 | read_unlock_bh(&neigh->lock); |
| 384 | } |
| 385 | #else |
| 386 | static inline void rt6_probe(struct rt6_info *rt) |
| 387 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 388 | } |
| 389 | #endif |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 392 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | */ |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 394 | static inline int rt6_check_dev(struct rt6_info *rt, int oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 396 | struct net_device *dev = rt->rt6i_dev; |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 397 | if (!oif || dev->ifindex == oif) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 398 | return 2; |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 399 | if ((dev->flags & IFF_LOOPBACK) && |
| 400 | rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) |
| 401 | return 1; |
| 402 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 405 | static inline int rt6_check_neigh(struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 407 | struct neighbour *neigh = dst_get_neighbour(&rt->dst); |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 408 | int m; |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 409 | if (rt->rt6i_flags & RTF_NONEXTHOP || |
| 410 | !(rt->rt6i_flags & RTF_GATEWAY)) |
| 411 | m = 1; |
| 412 | else if (neigh) { |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 413 | read_lock_bh(&neigh->lock); |
| 414 | if (neigh->nud_state & NUD_VALID) |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 415 | m = 2; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 416 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 417 | else if (neigh->nud_state & NUD_FAILED) |
| 418 | m = 0; |
| 419 | #endif |
| 420 | else |
YOSHIFUJI Hideaki | ea73ee2 | 2006-11-06 09:45:44 -0800 | [diff] [blame] | 421 | m = 1; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 422 | read_unlock_bh(&neigh->lock); |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 423 | } else |
| 424 | m = 0; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 425 | return m; |
| 426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 428 | static int rt6_score_route(struct rt6_info *rt, int oif, |
| 429 | int strict) |
| 430 | { |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 431 | int m, n; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 432 | |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 433 | m = rt6_check_dev(rt, oif); |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 434 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 435 | return -1; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 436 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 437 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; |
| 438 | #endif |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 439 | n = rt6_check_neigh(rt); |
YOSHIFUJI Hideaki | 557e92e | 2006-11-06 09:45:45 -0800 | [diff] [blame] | 440 | if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 441 | return -1; |
| 442 | return m; |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 445 | static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, |
| 446 | int *mpri, struct rt6_info *match) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 447 | { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 448 | int m; |
| 449 | |
| 450 | if (rt6_check_expired(rt)) |
| 451 | goto out; |
| 452 | |
| 453 | m = rt6_score_route(rt, oif, strict); |
| 454 | if (m < 0) |
| 455 | goto out; |
| 456 | |
| 457 | if (m > *mpri) { |
| 458 | if (strict & RT6_LOOKUP_F_REACHABLE) |
| 459 | rt6_probe(match); |
| 460 | *mpri = m; |
| 461 | match = rt; |
| 462 | } else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 463 | rt6_probe(rt); |
| 464 | } |
| 465 | |
| 466 | out: |
| 467 | return match; |
| 468 | } |
| 469 | |
| 470 | static struct rt6_info *find_rr_leaf(struct fib6_node *fn, |
| 471 | struct rt6_info *rr_head, |
| 472 | u32 metric, int oif, int strict) |
| 473 | { |
| 474 | struct rt6_info *rt, *match; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 475 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 477 | match = NULL; |
| 478 | for (rt = rr_head; rt && rt->rt6i_metric == metric; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 479 | rt = rt->dst.rt6_next) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 480 | match = find_match(rt, oif, strict, &mpri, match); |
| 481 | for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 482 | rt = rt->dst.rt6_next) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 483 | match = find_match(rt, oif, strict, &mpri, match); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 484 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 485 | return match; |
| 486 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 487 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 488 | static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) |
| 489 | { |
| 490 | struct rt6_info *match, *rt0; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 491 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 493 | RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 494 | __func__, fn->leaf, oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 496 | rt0 = fn->rr_ptr; |
| 497 | if (!rt0) |
| 498 | fn->rr_ptr = rt0 = fn->leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 500 | match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 502 | if (!match && |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 503 | (strict & RT6_LOOKUP_F_REACHABLE)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 504 | struct rt6_info *next = rt0->dst.rt6_next; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 505 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 506 | /* no entries matched; do round-robin */ |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 507 | if (!next || next->rt6i_metric != rt0->rt6i_metric) |
| 508 | next = fn->leaf; |
| 509 | |
| 510 | if (next != rt0) |
| 511 | fn->rr_ptr = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 514 | RT6_TRACE("%s() => %p\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 515 | __func__, match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 517 | net = dev_net(rt0->rt6i_dev); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 518 | return match ? match : net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 521 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 522 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 523 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 524 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 525 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 526 | struct route_info *rinfo = (struct route_info *) opt; |
| 527 | struct in6_addr prefix_buf, *prefix; |
| 528 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 529 | unsigned long lifetime; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 530 | struct rt6_info *rt; |
| 531 | |
| 532 | if (len < sizeof(struct route_info)) { |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | /* Sanity check for prefix_len and length */ |
| 537 | if (rinfo->length > 3) { |
| 538 | return -EINVAL; |
| 539 | } else if (rinfo->prefix_len > 128) { |
| 540 | return -EINVAL; |
| 541 | } else if (rinfo->prefix_len > 64) { |
| 542 | if (rinfo->length < 2) { |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | } else if (rinfo->prefix_len > 0) { |
| 546 | if (rinfo->length < 1) { |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | pref = rinfo->route_pref; |
| 552 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 553 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 554 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 555 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 556 | |
| 557 | if (rinfo->length == 3) |
| 558 | prefix = (struct in6_addr *)rinfo->prefix; |
| 559 | else { |
| 560 | /* this function is safe */ |
| 561 | ipv6_addr_prefix(&prefix_buf, |
| 562 | (struct in6_addr *)rinfo->prefix, |
| 563 | rinfo->prefix_len); |
| 564 | prefix = &prefix_buf; |
| 565 | } |
| 566 | |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 567 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 568 | dev->ifindex); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 569 | |
| 570 | if (rt && !lifetime) { |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 571 | ip6_del_rt(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 572 | rt = NULL; |
| 573 | } |
| 574 | |
| 575 | if (!rt && lifetime) |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 576 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 577 | pref); |
| 578 | else if (rt) |
| 579 | rt->rt6i_flags = RTF_ROUTEINFO | |
| 580 | (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
| 581 | |
| 582 | if (rt) { |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 583 | if (!addrconf_finite_timeout(lifetime)) { |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 584 | rt->rt6i_flags &= ~RTF_EXPIRES; |
| 585 | } else { |
| 586 | rt->rt6i_expires = jiffies + HZ * lifetime; |
| 587 | rt->rt6i_flags |= RTF_EXPIRES; |
| 588 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 589 | dst_release(&rt->dst); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | #endif |
| 594 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 595 | #define BACKTRACK(__net, saddr) \ |
YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 596 | do { \ |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 597 | if (rt == __net->ipv6.ip6_null_entry) { \ |
YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 598 | struct fib6_node *pn; \ |
Ville Nuorvala | e0eda7b | 2006-10-16 22:11:11 -0700 | [diff] [blame] | 599 | while (1) { \ |
YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 600 | if (fn->fn_flags & RTN_TL_ROOT) \ |
| 601 | goto out; \ |
| 602 | pn = fn->parent; \ |
| 603 | if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ |
Kim Nordlund | 8bce65b | 2006-12-13 16:38:29 -0800 | [diff] [blame] | 604 | fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ |
YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 605 | else \ |
| 606 | fn = pn; \ |
| 607 | if (fn->fn_flags & RTN_RTINFO) \ |
| 608 | goto restart; \ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 609 | } \ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 610 | } \ |
YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 611 | } while(0) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 612 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 613 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 614 | struct fib6_table *table, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 615 | struct flowi6 *fl6, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
| 617 | struct fib6_node *fn; |
| 618 | struct rt6_info *rt; |
| 619 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 620 | read_lock_bh(&table->tb6_lock); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 621 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 622 | restart: |
| 623 | rt = fn->leaf; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 624 | rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); |
| 625 | BACKTRACK(net, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 626 | out: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 627 | dst_use(&rt->dst, jiffies); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 628 | read_unlock_bh(&table->tb6_lock); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 629 | return rt; |
| 630 | |
| 631 | } |
| 632 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 633 | struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, |
| 634 | const struct in6_addr *saddr, int oif, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 635 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 636 | struct flowi6 fl6 = { |
| 637 | .flowi6_oif = oif, |
| 638 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 639 | }; |
| 640 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 641 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 642 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 643 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 644 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 645 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 646 | } |
| 647 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 648 | dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 649 | if (dst->error == 0) |
| 650 | return (struct rt6_info *) dst; |
| 651 | |
| 652 | dst_release(dst); |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 657 | EXPORT_SYMBOL(rt6_lookup); |
| 658 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 659 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | It takes new route entry, the addition fails by any reason the |
| 661 | route is freed. In any case, if caller does not hold it, it may |
| 662 | be destroyed. |
| 663 | */ |
| 664 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 665 | static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
| 667 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 668 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 670 | table = rt->rt6i_table; |
| 671 | write_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 672 | err = fib6_add(&table->tb6_root, rt, info); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 673 | write_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | return err; |
| 676 | } |
| 677 | |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 678 | int ip6_ins_rt(struct rt6_info *rt) |
| 679 | { |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 680 | struct nl_info info = { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 681 | .nl_net = dev_net(rt->rt6i_dev), |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 682 | }; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 683 | return __ip6_ins_rt(rt, &info); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 686 | static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr, |
| 687 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | struct rt6_info *rt; |
| 690 | |
| 691 | /* |
| 692 | * Clone the route. |
| 693 | */ |
| 694 | |
| 695 | rt = ip6_rt_copy(ort); |
| 696 | |
| 697 | if (rt) { |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 698 | struct neighbour *neigh; |
| 699 | int attempts = !in_softirq(); |
| 700 | |
YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 701 | if (!(rt->rt6i_flags&RTF_GATEWAY)) { |
| 702 | if (rt->rt6i_dst.plen != 128 && |
| 703 | ipv6_addr_equal(&rt->rt6i_dst.addr, daddr)) |
| 704 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | ipv6_addr_copy(&rt->rt6i_gateway, daddr); |
YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 708 | ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | rt->rt6i_dst.plen = 128; |
| 710 | rt->rt6i_flags |= RTF_CACHE; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 711 | rt->dst.flags |= DST_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | #ifdef CONFIG_IPV6_SUBTREES |
| 714 | if (rt->rt6i_src.plen && saddr) { |
| 715 | ipv6_addr_copy(&rt->rt6i_src.addr, saddr); |
| 716 | rt->rt6i_src.plen = 128; |
| 717 | } |
| 718 | #endif |
| 719 | |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 720 | retry: |
| 721 | neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); |
| 722 | if (IS_ERR(neigh)) { |
| 723 | struct net *net = dev_net(rt->rt6i_dev); |
| 724 | int saved_rt_min_interval = |
| 725 | net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 726 | int saved_rt_elasticity = |
| 727 | net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 728 | |
| 729 | if (attempts-- > 0) { |
| 730 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; |
| 731 | net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; |
| 732 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 733 | ip6_dst_gc(&net->ipv6.ip6_dst_ops); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 734 | |
| 735 | net->ipv6.sysctl.ip6_rt_gc_elasticity = |
| 736 | saved_rt_elasticity; |
| 737 | net->ipv6.sysctl.ip6_rt_gc_min_interval = |
| 738 | saved_rt_min_interval; |
| 739 | goto retry; |
| 740 | } |
| 741 | |
| 742 | if (net_ratelimit()) |
| 743 | printk(KERN_WARNING |
Ulrich Weber | 7e1b33e | 2010-09-27 15:02:18 -0700 | [diff] [blame] | 744 | "ipv6: Neighbour table overflow.\n"); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 745 | dst_free(&rt->dst); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 746 | return NULL; |
| 747 | } |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 748 | dst_set_neighbour(&rt->dst, neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 752 | return rt; |
| 753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 755 | static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr) |
YOSHIFUJI Hideaki | 299d993 | 2006-03-20 16:58:32 -0800 | [diff] [blame] | 756 | { |
| 757 | struct rt6_info *rt = ip6_rt_copy(ort); |
| 758 | if (rt) { |
| 759 | ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); |
| 760 | rt->rt6i_dst.plen = 128; |
| 761 | rt->rt6i_flags |= RTF_CACHE; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 762 | rt->dst.flags |= DST_HOST; |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 763 | dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour(&ort->dst))); |
YOSHIFUJI Hideaki | 299d993 | 2006-03-20 16:58:32 -0800 | [diff] [blame] | 764 | } |
| 765 | return rt; |
| 766 | } |
| 767 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 768 | static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 769 | struct flowi6 *fl6, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
| 771 | struct fib6_node *fn; |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 772 | struct rt6_info *rt, *nrt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 773 | int strict = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | int attempts = 3; |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 775 | int err; |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 776 | int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 778 | strict |= flags & RT6_LOOKUP_F_IFACE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | relookup: |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 781 | read_lock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 783 | restart_2: |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 784 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
| 786 | restart: |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 787 | rt = rt6_select(fn, oif, strict | reachable); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 788 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 789 | BACKTRACK(net, &fl6->saddr); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 790 | if (rt == net->ipv6.ip6_null_entry || |
YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 791 | rt->rt6i_flags & RTF_CACHE) |
YOSHIFUJI Hideaki | 1ddef044 | 2006-03-20 17:01:24 -0800 | [diff] [blame] | 792 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 794 | dst_hold(&rt->dst); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 795 | read_unlock_bh(&table->tb6_lock); |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 796 | |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 797 | if (!dst_get_neighbour(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 798 | nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); |
David S. Miller | 7343ff3 | 2011-03-09 19:55:25 -0800 | [diff] [blame] | 799 | else if (!(rt->dst.flags & DST_HOST)) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 800 | nrt = rt6_alloc_clone(rt, &fl6->daddr); |
David S. Miller | 7343ff3 | 2011-03-09 19:55:25 -0800 | [diff] [blame] | 801 | else |
| 802 | goto out2; |
YOSHIFUJI Hideaki | e40cf35 | 2006-03-20 16:59:27 -0800 | [diff] [blame] | 803 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 804 | dst_release(&rt->dst); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 805 | rt = nrt ? : net->ipv6.ip6_null_entry; |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 806 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 807 | dst_hold(&rt->dst); |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 808 | if (nrt) { |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 809 | err = ip6_ins_rt(nrt); |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 810 | if (!err) |
| 811 | goto out2; |
| 812 | } |
| 813 | |
| 814 | if (--attempts <= 0) |
| 815 | goto out2; |
| 816 | |
| 817 | /* |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 818 | * Race condition! In the gap, when table->tb6_lock was |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 819 | * released someone could insert this route. Relookup. |
| 820 | */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 821 | dst_release(&rt->dst); |
YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 822 | goto relookup; |
| 823 | |
| 824 | out: |
YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 825 | if (reachable) { |
| 826 | reachable = 0; |
| 827 | goto restart_2; |
| 828 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 829 | dst_hold(&rt->dst); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 830 | read_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | out2: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 832 | rt->dst.lastuse = jiffies; |
| 833 | rt->dst.__use++; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 834 | |
| 835 | return rt; |
| 836 | } |
| 837 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 838 | static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 839 | struct flowi6 *fl6, int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 840 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 841 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 844 | void ip6_route_input(struct sk_buff *skb) |
| 845 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 846 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 847 | struct net *net = dev_net(skb->dev); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 848 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 849 | struct flowi6 fl6 = { |
| 850 | .flowi6_iif = skb->dev->ifindex, |
| 851 | .daddr = iph->daddr, |
| 852 | .saddr = iph->saddr, |
| 853 | .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK, |
| 854 | .flowi6_mark = skb->mark, |
| 855 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 856 | }; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 857 | |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 858 | if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 859 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 860 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 861 | skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 864 | static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 865 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 866 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 867 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Florian Westphal | 9c7a4f9c | 2011-03-22 19:17:36 -0700 | [diff] [blame] | 870 | struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 871 | struct flowi6 *fl6) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 872 | { |
| 873 | int flags = 0; |
| 874 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 875 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 876 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 877 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 878 | if (!ipv6_addr_any(&fl6->saddr)) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 879 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 880 | else if (sk) |
| 881 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 882 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 883 | return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 886 | EXPORT_SYMBOL(ip6_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 888 | struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 889 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 890 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 891 | struct dst_entry *new = NULL; |
| 892 | |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 893 | rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 894 | if (rt) { |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 895 | memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); |
| 896 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 897 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 898 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 899 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 900 | new->input = dst_discard; |
| 901 | new->output = dst_discard; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 902 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 903 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 904 | rt->rt6i_idev = ort->rt6i_idev; |
| 905 | if (rt->rt6i_idev) |
| 906 | in6_dev_hold(rt->rt6i_idev); |
| 907 | rt->rt6i_expires = 0; |
| 908 | |
| 909 | ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); |
| 910 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; |
| 911 | rt->rt6i_metric = 0; |
| 912 | |
| 913 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 914 | #ifdef CONFIG_IPV6_SUBTREES |
| 915 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 916 | #endif |
| 917 | |
| 918 | dst_free(new); |
| 919 | } |
| 920 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 921 | dst_release(dst_orig); |
| 922 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 923 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | /* |
| 926 | * Destination cache support functions |
| 927 | */ |
| 928 | |
| 929 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 930 | { |
| 931 | struct rt6_info *rt; |
| 932 | |
| 933 | rt = (struct rt6_info *) dst; |
| 934 | |
David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 935 | if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { |
| 936 | if (rt->rt6i_peer_genid != rt6_peer_genid()) { |
| 937 | if (!rt->rt6i_peer) |
| 938 | rt6_bind_peer(rt, 0); |
| 939 | rt->rt6i_peer_genid = rt6_peer_genid(); |
| 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return dst; |
David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 947 | { |
| 948 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 949 | |
| 950 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 951 | if (rt->rt6i_flags & RTF_CACHE) { |
| 952 | if (rt6_check_expired(rt)) { |
| 953 | ip6_del_rt(rt); |
| 954 | dst = NULL; |
| 955 | } |
| 956 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 958 | dst = NULL; |
| 959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 961 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static void ip6_link_failure(struct sk_buff *skb) |
| 965 | { |
| 966 | struct rt6_info *rt; |
| 967 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 968 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 970 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | if (rt) { |
| 972 | if (rt->rt6i_flags&RTF_CACHE) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 973 | dst_set_expires(&rt->dst, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | rt->rt6i_flags |= RTF_EXPIRES; |
| 975 | } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) |
| 976 | rt->rt6i_node->fn_sernum = -1; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) |
| 981 | { |
| 982 | struct rt6_info *rt6 = (struct rt6_info*)dst; |
| 983 | |
| 984 | if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { |
| 985 | rt6->rt6i_flags |= RTF_MODIFIED; |
| 986 | if (mtu < IPV6_MIN_MTU) { |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 987 | u32 features = dst_metric(dst, RTAX_FEATURES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | mtu = IPV6_MIN_MTU; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 989 | features |= RTAX_FEATURE_ALLFRAG; |
| 990 | dst_metric_set(dst, RTAX_FEATURES, features); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 992 | dst_metric_set(dst, RTAX_MTU, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 996 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 998 | struct net_device *dev = dst->dev; |
| 999 | unsigned int mtu = dst_mtu(dst); |
| 1000 | struct net *net = dev_net(dev); |
| 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 1003 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1004 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 1005 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1008 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 1009 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 1010 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | * rely only on pmtu discovery" |
| 1012 | */ |
| 1013 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 1014 | mtu = IPV6_MAXPLEN; |
| 1015 | return mtu; |
| 1016 | } |
| 1017 | |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 1018 | static unsigned int ip6_default_mtu(const struct dst_entry *dst) |
| 1019 | { |
| 1020 | unsigned int mtu = IPV6_MIN_MTU; |
| 1021 | struct inet6_dev *idev; |
| 1022 | |
| 1023 | rcu_read_lock(); |
| 1024 | idev = __in6_dev_get(dst->dev); |
| 1025 | if (idev) |
| 1026 | mtu = idev->cnf.mtu6; |
| 1027 | rcu_read_unlock(); |
| 1028 | |
| 1029 | return mtu; |
| 1030 | } |
| 1031 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1032 | static struct dst_entry *icmp6_dst_gc_list; |
| 1033 | static DEFINE_SPINLOCK(icmp6_dst_lock); |
Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1034 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1035 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | struct neighbour *neigh, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1037 | const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | struct rt6_info *rt; |
| 1040 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1041 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | if (unlikely(idev == NULL)) |
| 1044 | return NULL; |
| 1045 | |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1046 | rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | if (unlikely(rt == NULL)) { |
| 1048 | in6_dev_put(idev); |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (neigh) |
| 1053 | neigh_hold(neigh); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 1054 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | neigh = ndisc_get_neigh(dev, addr); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 1056 | if (IS_ERR(neigh)) |
| 1057 | neigh = NULL; |
| 1058 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | rt->rt6i_idev = idev; |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1061 | dst_set_neighbour(&rt->dst, neigh); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1062 | atomic_set(&rt->dst.__refcnt, 1); |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1063 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1064 | rt->dst.output = ip6_output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1066 | spin_lock_bh(&icmp6_dst_lock); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1067 | rt->dst.next = icmp6_dst_gc_list; |
| 1068 | icmp6_dst_gc_list = &rt->dst; |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1069 | spin_unlock_bh(&icmp6_dst_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1071 | fib6_force_start_gc(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | out: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1074 | return &rt->dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1077 | int icmp6_dst_gc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | { |
Hagen Paul Pfeifer | e9476e95 | 2011-02-25 05:45:19 +0000 | [diff] [blame] | 1079 | struct dst_entry *dst, **pprev; |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1080 | int more = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1082 | spin_lock_bh(&icmp6_dst_lock); |
| 1083 | pprev = &icmp6_dst_gc_list; |
Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | while ((dst = *pprev) != NULL) { |
| 1086 | if (!atomic_read(&dst->__refcnt)) { |
| 1087 | *pprev = dst->next; |
| 1088 | dst_free(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | pprev = &dst->next; |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1091 | ++more; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1095 | spin_unlock_bh(&icmp6_dst_lock); |
Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1096 | |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1097 | return more; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
David S. Miller | 1e493d1 | 2008-09-10 17:27:15 -0700 | [diff] [blame] | 1100 | static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), |
| 1101 | void *arg) |
| 1102 | { |
| 1103 | struct dst_entry *dst, **pprev; |
| 1104 | |
| 1105 | spin_lock_bh(&icmp6_dst_lock); |
| 1106 | pprev = &icmp6_dst_gc_list; |
| 1107 | while ((dst = *pprev) != NULL) { |
| 1108 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 1109 | if (func(rt, arg)) { |
| 1110 | *pprev = dst->next; |
| 1111 | dst_free(dst); |
| 1112 | } else { |
| 1113 | pprev = &dst->next; |
| 1114 | } |
| 1115 | } |
| 1116 | spin_unlock_bh(&icmp6_dst_lock); |
| 1117 | } |
| 1118 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 1119 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | unsigned long now = jiffies; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 1122 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1123 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 1124 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 1125 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 1126 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 1127 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1128 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1130 | entries = dst_entries_get_fast(ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1131 | if (time_after(rt_last_gc + rt_min_interval, now) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1132 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | goto out; |
| 1134 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 1135 | net->ipv6.ip6_rt_gc_expire++; |
| 1136 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); |
| 1137 | net->ipv6.ip6_rt_last_gc = now; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1138 | entries = dst_entries_get_slow(ops); |
| 1139 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1140 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1142 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1143 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* Clean host part of a prefix. Not necessary in radix tree, |
| 1147 | but results in cleaner routing tables. |
| 1148 | |
| 1149 | Remove it only when all the things will work! |
| 1150 | */ |
| 1151 | |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1152 | int ip6_dst_hoplimit(struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | { |
David S. Miller | 5170ae8 | 2010-12-12 21:35:57 -0800 | [diff] [blame] | 1154 | int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); |
David S. Miller | a02e4b7 | 2010-12-12 21:39:02 -0800 | [diff] [blame] | 1155 | if (hoplimit == 0) { |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1156 | struct net_device *dev = dst->dev; |
Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1157 | struct inet6_dev *idev; |
| 1158 | |
| 1159 | rcu_read_lock(); |
| 1160 | idev = __in6_dev_get(dev); |
| 1161 | if (idev) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1162 | hoplimit = idev->cnf.hop_limit; |
Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1163 | else |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 1164 | hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; |
Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1165 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | } |
| 1167 | return hoplimit; |
| 1168 | } |
David S. Miller | abbf46a | 2010-12-12 21:14:46 -0800 | [diff] [blame] | 1169 | EXPORT_SYMBOL(ip6_dst_hoplimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
| 1171 | /* |
| 1172 | * |
| 1173 | */ |
| 1174 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1175 | int ip6_route_add(struct fib6_config *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
| 1177 | int err; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1178 | struct net *net = cfg->fc_nlinfo.nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | struct rt6_info *rt = NULL; |
| 1180 | struct net_device *dev = NULL; |
| 1181 | struct inet6_dev *idev = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1182 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | int addr_type; |
| 1184 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1185 | if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return -EINVAL; |
| 1187 | #ifndef CONFIG_IPV6_SUBTREES |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1188 | if (cfg->fc_src_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | return -EINVAL; |
| 1190 | #endif |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1191 | if (cfg->fc_ifindex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | err = -ENODEV; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1193 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | if (!dev) |
| 1195 | goto out; |
| 1196 | idev = in6_dev_get(dev); |
| 1197 | if (!idev) |
| 1198 | goto out; |
| 1199 | } |
| 1200 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1201 | if (cfg->fc_metric == 0) |
| 1202 | cfg->fc_metric = IP6_RT_PRIO_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1204 | table = fib6_new_table(net, cfg->fc_table); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1205 | if (table == NULL) { |
| 1206 | err = -ENOBUFS; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1210 | rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (rt == NULL) { |
| 1213 | err = -ENOMEM; |
| 1214 | goto out; |
| 1215 | } |
| 1216 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1217 | rt->dst.obsolete = -1; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 1218 | rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ? |
| 1219 | jiffies + clock_t_to_jiffies(cfg->fc_expires) : |
| 1220 | 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1222 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 1223 | cfg->fc_protocol = RTPROT_BOOT; |
| 1224 | rt->rt6i_protocol = cfg->fc_protocol; |
| 1225 | |
| 1226 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | if (addr_type & IPV6_ADDR_MULTICAST) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1229 | rt->dst.input = ip6_mc_input; |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 1230 | else if (cfg->fc_flags & RTF_LOCAL) |
| 1231 | rt->dst.input = ip6_input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | else |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1233 | rt->dst.input = ip6_forward; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1235 | rt->dst.output = ip6_output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1237 | ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 1238 | rt->rt6i_dst.plen = cfg->fc_dst_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (rt->rt6i_dst.plen == 128) |
David S. Miller | 11d53b4 | 2011-06-24 15:23:34 -0700 | [diff] [blame] | 1240 | rt->dst.flags |= DST_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
| 1242 | #ifdef CONFIG_IPV6_SUBTREES |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1243 | ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 1244 | rt->rt6i_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | #endif |
| 1246 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1247 | rt->rt6i_metric = cfg->fc_metric; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | /* We cannot add true routes via loopback here, |
| 1250 | they would result in kernel looping; promote them to reject routes |
| 1251 | */ |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1252 | if ((cfg->fc_flags & RTF_REJECT) || |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 1253 | (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK) |
| 1254 | && !(cfg->fc_flags&RTF_LOCAL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | /* hold loopback dev/idev if we haven't done so. */ |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1256 | if (dev != net->loopback_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | if (dev) { |
| 1258 | dev_put(dev); |
| 1259 | in6_dev_put(idev); |
| 1260 | } |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1261 | dev = net->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | dev_hold(dev); |
| 1263 | idev = in6_dev_get(dev); |
| 1264 | if (!idev) { |
| 1265 | err = -ENODEV; |
| 1266 | goto out; |
| 1267 | } |
| 1268 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1269 | rt->dst.output = ip6_pkt_discard_out; |
| 1270 | rt->dst.input = ip6_pkt_discard; |
| 1271 | rt->dst.error = -ENETUNREACH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; |
| 1273 | goto install_route; |
| 1274 | } |
| 1275 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1276 | if (cfg->fc_flags & RTF_GATEWAY) { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1277 | const struct in6_addr *gw_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | int gwa_type; |
| 1279 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1280 | gw_addr = &cfg->fc_gateway; |
| 1281 | ipv6_addr_copy(&rt->rt6i_gateway, gw_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | gwa_type = ipv6_addr_type(gw_addr); |
| 1283 | |
| 1284 | if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { |
| 1285 | struct rt6_info *grt; |
| 1286 | |
| 1287 | /* IPv6 strictly inhibits using not link-local |
| 1288 | addresses as nexthop address. |
| 1289 | Otherwise, router will not able to send redirects. |
| 1290 | It is very good, but in some (rare!) circumstances |
| 1291 | (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 1292 | some exceptions. --ANK |
| 1293 | */ |
| 1294 | err = -EINVAL; |
| 1295 | if (!(gwa_type&IPV6_ADDR_UNICAST)) |
| 1296 | goto out; |
| 1297 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1298 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
| 1300 | err = -EHOSTUNREACH; |
| 1301 | if (grt == NULL) |
| 1302 | goto out; |
| 1303 | if (dev) { |
| 1304 | if (dev != grt->rt6i_dev) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1305 | dst_release(&grt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | goto out; |
| 1307 | } |
| 1308 | } else { |
| 1309 | dev = grt->rt6i_dev; |
| 1310 | idev = grt->rt6i_idev; |
| 1311 | dev_hold(dev); |
| 1312 | in6_dev_hold(grt->rt6i_idev); |
| 1313 | } |
| 1314 | if (!(grt->rt6i_flags&RTF_GATEWAY)) |
| 1315 | err = 0; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1316 | dst_release(&grt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (err) |
| 1319 | goto out; |
| 1320 | } |
| 1321 | err = -EINVAL; |
| 1322 | if (dev == NULL || (dev->flags&IFF_LOOPBACK)) |
| 1323 | goto out; |
| 1324 | } |
| 1325 | |
| 1326 | err = -ENODEV; |
| 1327 | if (dev == NULL) |
| 1328 | goto out; |
| 1329 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 1330 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
| 1331 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
| 1332 | err = -EINVAL; |
| 1333 | goto out; |
| 1334 | } |
| 1335 | ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc); |
| 1336 | rt->rt6i_prefsrc.plen = 128; |
| 1337 | } else |
| 1338 | rt->rt6i_prefsrc.plen = 0; |
| 1339 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1340 | if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1341 | struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev); |
| 1342 | if (IS_ERR(n)) { |
| 1343 | err = PTR_ERR(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | goto out; |
| 1345 | } |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1346 | dst_set_neighbour(&rt->dst, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1349 | rt->rt6i_flags = cfg->fc_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | install_route: |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1352 | if (cfg->fc_mx) { |
| 1353 | struct nlattr *nla; |
| 1354 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1356 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 1357 | int type = nla_type(nla); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1358 | |
| 1359 | if (type) { |
| 1360 | if (type > RTAX_MAX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | err = -EINVAL; |
| 1362 | goto out; |
| 1363 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1364 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1365 | dst_metric_set(&rt->dst, type, nla_get_u32(nla)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1370 | rt->dst.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | rt->rt6i_idev = idev; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1372 | rt->rt6i_table = table; |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1373 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1374 | cfg->fc_nlinfo.nl_net = dev_net(dev); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1375 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1376 | return __ip6_ins_rt(rt, &cfg->fc_nlinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | out: |
| 1379 | if (dev) |
| 1380 | dev_put(dev); |
| 1381 | if (idev) |
| 1382 | in6_dev_put(idev); |
| 1383 | if (rt) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1384 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | return err; |
| 1386 | } |
| 1387 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1388 | static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | { |
| 1390 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1391 | struct fib6_table *table; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1392 | struct net *net = dev_net(rt->rt6i_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1394 | if (rt == net->ipv6.ip6_null_entry) |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 1395 | return -ENOENT; |
| 1396 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1397 | table = rt->rt6i_table; |
| 1398 | write_lock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1400 | err = fib6_del(rt, info); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1401 | dst_release(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1403 | write_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | return err; |
| 1406 | } |
| 1407 | |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1408 | int ip6_del_rt(struct rt6_info *rt) |
| 1409 | { |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1410 | struct nl_info info = { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1411 | .nl_net = dev_net(rt->rt6i_dev), |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1412 | }; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 1413 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1416 | static int ip6_route_del(struct fib6_config *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1418 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | struct fib6_node *fn; |
| 1420 | struct rt6_info *rt; |
| 1421 | int err = -ESRCH; |
| 1422 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1423 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1424 | if (table == NULL) |
| 1425 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1427 | read_lock_bh(&table->tb6_lock); |
| 1428 | |
| 1429 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1430 | &cfg->fc_dst, cfg->fc_dst_len, |
| 1431 | &cfg->fc_src, cfg->fc_src_len); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | if (fn) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1434 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1435 | if (cfg->fc_ifindex && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | (rt->rt6i_dev == NULL || |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1437 | rt->rt6i_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1439 | if (cfg->fc_flags & RTF_GATEWAY && |
| 1440 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1442 | if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | continue; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1444 | dst_hold(&rt->dst); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1445 | read_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1447 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | } |
| 1449 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1450 | read_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | return err; |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Handle redirects |
| 1457 | */ |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1458 | struct ip6rd_flowi { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1459 | struct flowi6 fl6; |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1460 | struct in6_addr gateway; |
| 1461 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1463 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 1464 | struct fib6_table *table, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1465 | struct flowi6 *fl6, |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1466 | int flags) |
| 1467 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1468 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1469 | struct rt6_info *rt; |
| 1470 | struct fib6_node *fn; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1473 | * Get the "current" route for this destination and |
| 1474 | * check if the redirect has come from approriate router. |
| 1475 | * |
| 1476 | * RFC 2461 specifies that redirects should only be |
| 1477 | * accepted if they come from the nexthop to the target. |
| 1478 | * Due to the way the routes are chosen, this notion |
| 1479 | * is a bit fuzzy and one might need to check all possible |
| 1480 | * routes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1483 | read_lock_bh(&table->tb6_lock); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1484 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1485 | restart: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1486 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1487 | /* |
| 1488 | * Current route is on-link; redirect is always invalid. |
| 1489 | * |
| 1490 | * Seems, previous statement is not true. It could |
| 1491 | * be node, which looks for us as on-link (f.e. proxy ndisc) |
| 1492 | * But then router serving it might decide, that we should |
| 1493 | * know truth 8)8) --ANK (980726). |
| 1494 | */ |
| 1495 | if (rt6_check_expired(rt)) |
| 1496 | continue; |
| 1497 | if (!(rt->rt6i_flags & RTF_GATEWAY)) |
| 1498 | continue; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1499 | if (fl6->flowi6_oif != rt->rt6i_dev->ifindex) |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1500 | continue; |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1501 | if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1502 | continue; |
| 1503 | break; |
| 1504 | } |
YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1505 | |
YOSHIFUJI Hideaki | cb15d9c | 2006-08-23 17:23:11 -0700 | [diff] [blame] | 1506 | if (!rt) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1507 | rt = net->ipv6.ip6_null_entry; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1508 | BACKTRACK(net, &fl6->saddr); |
YOSHIFUJI Hideaki | cb15d9c | 2006-08-23 17:23:11 -0700 | [diff] [blame] | 1509 | out: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1510 | dst_hold(&rt->dst); |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1511 | |
| 1512 | read_unlock_bh(&table->tb6_lock); |
| 1513 | |
| 1514 | return rt; |
| 1515 | }; |
| 1516 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1517 | static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, |
| 1518 | const struct in6_addr *src, |
| 1519 | const struct in6_addr *gateway, |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1520 | struct net_device *dev) |
| 1521 | { |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1522 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1523 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1524 | struct ip6rd_flowi rdfl = { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1525 | .fl6 = { |
| 1526 | .flowi6_oif = dev->ifindex, |
| 1527 | .daddr = *dest, |
| 1528 | .saddr = *src, |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1529 | }, |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1530 | }; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1531 | |
Brian Haley | 86c36ce | 2009-10-07 13:58:01 -0700 | [diff] [blame] | 1532 | ipv6_addr_copy(&rdfl.gateway, gateway); |
| 1533 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1534 | if (rt6_need_strict(dest)) |
| 1535 | flags |= RT6_LOOKUP_F_IFACE; |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1536 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1537 | return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1538 | flags, __ip6_route_redirect); |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1541 | void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, |
| 1542 | const struct in6_addr *saddr, |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1543 | struct neighbour *neigh, u8 *lladdr, int on_link) |
| 1544 | { |
| 1545 | struct rt6_info *rt, *nrt = NULL; |
| 1546 | struct netevent_redirect netevent; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1547 | struct net *net = dev_net(neigh->dev); |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1548 | |
| 1549 | rt = ip6_route_redirect(dest, src, saddr, neigh->dev); |
| 1550 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1551 | if (rt == net->ipv6.ip6_null_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | if (net_ratelimit()) |
| 1553 | printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " |
| 1554 | "for redirect target\n"); |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1555 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | /* |
| 1559 | * We have finally decided to accept it. |
| 1560 | */ |
| 1561 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1562 | neigh_update(neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 1564 | NEIGH_UPDATE_F_OVERRIDE| |
| 1565 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
| 1566 | NEIGH_UPDATE_F_ISROUTER)) |
| 1567 | ); |
| 1568 | |
| 1569 | /* |
| 1570 | * Redirect received -> path was valid. |
| 1571 | * Look, redirects are sent only in response to data packets, |
| 1572 | * so that this nexthop apparently is reachable. --ANK |
| 1573 | */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1574 | dst_confirm(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
| 1576 | /* Duplicate redirect: silently ignore. */ |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1577 | if (neigh == dst_get_neighbour(&rt->dst)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | goto out; |
| 1579 | |
| 1580 | nrt = ip6_rt_copy(rt); |
| 1581 | if (nrt == NULL) |
| 1582 | goto out; |
| 1583 | |
| 1584 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 1585 | if (on_link) |
| 1586 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 1587 | |
| 1588 | ipv6_addr_copy(&nrt->rt6i_dst.addr, dest); |
| 1589 | nrt->rt6i_dst.plen = 128; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1590 | nrt->dst.flags |= DST_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
| 1592 | ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key); |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1593 | dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1595 | if (ip6_ins_rt(nrt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | goto out; |
| 1597 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1598 | netevent.old = &rt->dst; |
| 1599 | netevent.new = &nrt->dst; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1600 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | if (rt->rt6i_flags&RTF_CACHE) { |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1603 | ip6_del_rt(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | return; |
| 1605 | } |
| 1606 | |
| 1607 | out: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1608 | dst_release(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Handle ICMP "packet too big" messages |
| 1613 | * i.e. Path MTU discovery |
| 1614 | */ |
| 1615 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1616 | static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, |
Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1617 | struct net *net, u32 pmtu, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | { |
| 1619 | struct rt6_info *rt, *nrt; |
| 1620 | int allfrag = 0; |
Andrey Vagin | d3052b5 | 2010-12-11 15:20:11 +0000 | [diff] [blame] | 1621 | again: |
Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1622 | rt = rt6_lookup(net, daddr, saddr, ifindex, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | if (rt == NULL) |
| 1624 | return; |
| 1625 | |
Andrey Vagin | d3052b5 | 2010-12-11 15:20:11 +0000 | [diff] [blame] | 1626 | if (rt6_check_expired(rt)) { |
| 1627 | ip6_del_rt(rt); |
| 1628 | goto again; |
| 1629 | } |
| 1630 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1631 | if (pmtu >= dst_mtu(&rt->dst)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | goto out; |
| 1633 | |
| 1634 | if (pmtu < IPV6_MIN_MTU) { |
| 1635 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1636 | * According to RFC2460, PMTU is set to the IPv6 Minimum Link |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | * MTU (1280) and a fragment header should always be included |
| 1638 | * after a node receiving Too Big message reporting PMTU is |
| 1639 | * less than the IPv6 Minimum Link MTU. |
| 1640 | */ |
| 1641 | pmtu = IPV6_MIN_MTU; |
| 1642 | allfrag = 1; |
| 1643 | } |
| 1644 | |
| 1645 | /* New mtu received -> path was valid. |
| 1646 | They are sent only in response to data packets, |
| 1647 | so that this nexthop apparently is reachable. --ANK |
| 1648 | */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1649 | dst_confirm(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | /* Host route. If it is static, it would be better |
| 1652 | not to override it, but add new one, so that |
| 1653 | when cache entry will expire old pmtu |
| 1654 | would return automatically. |
| 1655 | */ |
| 1656 | if (rt->rt6i_flags & RTF_CACHE) { |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1657 | dst_metric_set(&rt->dst, RTAX_MTU, pmtu); |
| 1658 | if (allfrag) { |
| 1659 | u32 features = dst_metric(&rt->dst, RTAX_FEATURES); |
| 1660 | features |= RTAX_FEATURE_ALLFRAG; |
| 1661 | dst_metric_set(&rt->dst, RTAX_FEATURES, features); |
| 1662 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1663 | dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; |
| 1665 | goto out; |
| 1666 | } |
| 1667 | |
| 1668 | /* Network route. |
| 1669 | Two cases are possible: |
| 1670 | 1. It is connected route. Action: COW |
| 1671 | 2. It is gatewayed route or NONEXTHOP route. Action: clone it. |
| 1672 | */ |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 1673 | if (!dst_get_neighbour(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) |
YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1674 | nrt = rt6_alloc_cow(rt, daddr, saddr); |
YOSHIFUJI Hideaki | d5315b50 | 2006-03-20 16:58:48 -0800 | [diff] [blame] | 1675 | else |
| 1676 | nrt = rt6_alloc_clone(rt, daddr); |
YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1677 | |
YOSHIFUJI Hideaki | d5315b50 | 2006-03-20 16:58:48 -0800 | [diff] [blame] | 1678 | if (nrt) { |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1679 | dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); |
| 1680 | if (allfrag) { |
| 1681 | u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); |
| 1682 | features |= RTAX_FEATURE_ALLFRAG; |
| 1683 | dst_metric_set(&nrt->dst, RTAX_FEATURES, features); |
| 1684 | } |
YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1685 | |
| 1686 | /* According to RFC 1981, detecting PMTU increase shouldn't be |
| 1687 | * happened within 5 mins, the recommended timer is 10 mins. |
| 1688 | * Here this route expiration time is set to ip6_rt_mtu_expires |
| 1689 | * which is 10 mins. After 10 mins the decreased pmtu is expired |
| 1690 | * and detecting PMTU increase will be automatically happened. |
| 1691 | */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1692 | dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); |
YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1693 | nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; |
| 1694 | |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1695 | ip6_ins_rt(nrt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | out: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1698 | dst_release(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1701 | void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, |
Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1702 | struct net_device *dev, u32 pmtu) |
| 1703 | { |
| 1704 | struct net *net = dev_net(dev); |
| 1705 | |
| 1706 | /* |
| 1707 | * RFC 1981 states that a node "MUST reduce the size of the packets it |
| 1708 | * is sending along the path" that caused the Packet Too Big message. |
| 1709 | * Since it's not possible in the general case to determine which |
| 1710 | * interface was used to send the original packet, we update the MTU |
| 1711 | * on the interface that will be used to send future packets. We also |
| 1712 | * update the MTU on the interface that received the Packet Too Big in |
| 1713 | * case the original packet was forced out that interface with |
| 1714 | * SO_BINDTODEVICE or similar. This is the next best thing to the |
| 1715 | * correct behaviour, which would be to update the MTU on all |
| 1716 | * interfaces. |
| 1717 | */ |
| 1718 | rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); |
| 1719 | rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); |
| 1720 | } |
| 1721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | /* |
| 1723 | * Misc support functions |
| 1724 | */ |
| 1725 | |
| 1726 | static struct rt6_info * ip6_rt_copy(struct rt6_info *ort) |
| 1727 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1728 | struct net *net = dev_net(ort->rt6i_dev); |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 1729 | struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1730 | ort->dst.dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
| 1732 | if (rt) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1733 | rt->dst.input = ort->dst.input; |
| 1734 | rt->dst.output = ort->dst.output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1736 | dst_copy_metrics(&rt->dst, &ort->dst); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1737 | rt->dst.error = ort->dst.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | rt->rt6i_idev = ort->rt6i_idev; |
| 1739 | if (rt->rt6i_idev) |
| 1740 | in6_dev_hold(rt->rt6i_idev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1741 | rt->dst.lastuse = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | rt->rt6i_expires = 0; |
| 1743 | |
| 1744 | ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); |
| 1745 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; |
| 1746 | rt->rt6i_metric = 0; |
| 1747 | |
| 1748 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 1749 | #ifdef CONFIG_IPV6_SUBTREES |
| 1750 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 1751 | #endif |
Florian Westphal | 0f6c639 | 2011-05-20 11:27:24 +0000 | [diff] [blame] | 1752 | memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1753 | rt->rt6i_table = ort->rt6i_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | } |
| 1755 | return rt; |
| 1756 | } |
| 1757 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1758 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1759 | static struct rt6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1760 | const struct in6_addr *prefix, int prefixlen, |
| 1761 | const struct in6_addr *gwaddr, int ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1762 | { |
| 1763 | struct fib6_node *fn; |
| 1764 | struct rt6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1765 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1766 | |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1767 | table = fib6_get_table(net, RT6_TABLE_INFO); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1768 | if (table == NULL) |
| 1769 | return NULL; |
| 1770 | |
| 1771 | write_lock_bh(&table->tb6_lock); |
| 1772 | fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1773 | if (!fn) |
| 1774 | goto out; |
| 1775 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1776 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1777 | if (rt->rt6i_dev->ifindex != ifindex) |
| 1778 | continue; |
| 1779 | if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) |
| 1780 | continue; |
| 1781 | if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) |
| 1782 | continue; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1783 | dst_hold(&rt->dst); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1784 | break; |
| 1785 | } |
| 1786 | out: |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1787 | write_unlock_bh(&table->tb6_lock); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1788 | return rt; |
| 1789 | } |
| 1790 | |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1791 | static struct rt6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1792 | const struct in6_addr *prefix, int prefixlen, |
| 1793 | const struct in6_addr *gwaddr, int ifindex, |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1794 | unsigned pref) |
| 1795 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1796 | struct fib6_config cfg = { |
| 1797 | .fc_table = RT6_TABLE_INFO, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 1798 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1799 | .fc_ifindex = ifindex, |
| 1800 | .fc_dst_len = prefixlen, |
| 1801 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 1802 | RTF_UP | RTF_PREF(pref), |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1803 | .fc_nlinfo.pid = 0, |
| 1804 | .fc_nlinfo.nlh = NULL, |
| 1805 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1806 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1807 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1808 | ipv6_addr_copy(&cfg.fc_dst, prefix); |
| 1809 | ipv6_addr_copy(&cfg.fc_gateway, gwaddr); |
| 1810 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 1811 | /* We should treat it as a default route if prefix length is 0. */ |
| 1812 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1813 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1814 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1815 | ip6_route_add(&cfg); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1816 | |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1817 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1818 | } |
| 1819 | #endif |
| 1820 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1821 | struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1822 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | struct rt6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1824 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1826 | table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1827 | if (table == NULL) |
| 1828 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1830 | write_lock_bh(&table->tb6_lock); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1831 | for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | if (dev == rt->rt6i_dev && |
YOSHIFUJI Hideaki | 045927f | 2006-03-20 17:00:48 -0800 | [diff] [blame] | 1833 | ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | ipv6_addr_equal(&rt->rt6i_gateway, addr)) |
| 1835 | break; |
| 1836 | } |
| 1837 | if (rt) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1838 | dst_hold(&rt->dst); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1839 | write_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | return rt; |
| 1841 | } |
| 1842 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1843 | struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1844 | struct net_device *dev, |
| 1845 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1847 | struct fib6_config cfg = { |
| 1848 | .fc_table = RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 1849 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1850 | .fc_ifindex = dev->ifindex, |
| 1851 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 1852 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1853 | .fc_nlinfo.pid = 0, |
| 1854 | .fc_nlinfo.nlh = NULL, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1855 | .fc_nlinfo.nl_net = dev_net(dev), |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1856 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1858 | ipv6_addr_copy(&cfg.fc_gateway, gwaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1860 | ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | return rt6_get_dflt_router(gwaddr, dev); |
| 1863 | } |
| 1864 | |
Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 1865 | void rt6_purge_dflt_routers(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | { |
| 1867 | struct rt6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1868 | struct fib6_table *table; |
| 1869 | |
| 1870 | /* NOTE: Keep consistent with rt6_get_dflt_router */ |
Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 1871 | table = fib6_get_table(net, RT6_TABLE_DFLT); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1872 | if (table == NULL) |
| 1873 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
| 1875 | restart: |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1876 | read_lock_bh(&table->tb6_lock); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1877 | for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1879 | dst_hold(&rt->dst); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1880 | read_unlock_bh(&table->tb6_lock); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1881 | ip6_del_rt(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | goto restart; |
| 1883 | } |
| 1884 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1885 | read_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1888 | static void rtmsg_to_fib6_config(struct net *net, |
| 1889 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1890 | struct fib6_config *cfg) |
| 1891 | { |
| 1892 | memset(cfg, 0, sizeof(*cfg)); |
| 1893 | |
| 1894 | cfg->fc_table = RT6_TABLE_MAIN; |
| 1895 | cfg->fc_ifindex = rtmsg->rtmsg_ifindex; |
| 1896 | cfg->fc_metric = rtmsg->rtmsg_metric; |
| 1897 | cfg->fc_expires = rtmsg->rtmsg_info; |
| 1898 | cfg->fc_dst_len = rtmsg->rtmsg_dst_len; |
| 1899 | cfg->fc_src_len = rtmsg->rtmsg_src_len; |
| 1900 | cfg->fc_flags = rtmsg->rtmsg_flags; |
| 1901 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1902 | cfg->fc_nlinfo.nl_net = net; |
Benjamin Thery | f1243c2 | 2008-02-26 18:10:03 -0800 | [diff] [blame] | 1903 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1904 | ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst); |
| 1905 | ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src); |
| 1906 | ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway); |
| 1907 | } |
| 1908 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1909 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1911 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | struct in6_rtmsg rtmsg; |
| 1913 | int err; |
| 1914 | |
| 1915 | switch(cmd) { |
| 1916 | case SIOCADDRT: /* Add a route */ |
| 1917 | case SIOCDELRT: /* Delete a route */ |
| 1918 | if (!capable(CAP_NET_ADMIN)) |
| 1919 | return -EPERM; |
| 1920 | err = copy_from_user(&rtmsg, arg, |
| 1921 | sizeof(struct in6_rtmsg)); |
| 1922 | if (err) |
| 1923 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1924 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1925 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | rtnl_lock(); |
| 1928 | switch (cmd) { |
| 1929 | case SIOCADDRT: |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1930 | err = ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | break; |
| 1932 | case SIOCDELRT: |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1933 | err = ip6_route_del(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | break; |
| 1935 | default: |
| 1936 | err = -EINVAL; |
| 1937 | } |
| 1938 | rtnl_unlock(); |
| 1939 | |
| 1940 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
| 1943 | return -EINVAL; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * Drop the packet on the floor |
| 1948 | */ |
| 1949 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 1950 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1952 | int type; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1953 | struct dst_entry *dst = skb_dst(skb); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1954 | switch (ipstats_mib_noroutes) { |
| 1955 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1956 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 1957 | if (type == IPV6_ADDR_ANY) { |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1958 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 1959 | IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1960 | break; |
| 1961 | } |
| 1962 | /* FALLTHROUGH */ |
| 1963 | case IPSTATS_MIB_OUTNOROUTES: |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1964 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 1965 | ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1966 | break; |
| 1967 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1968 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | kfree_skb(skb); |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1973 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 1974 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1975 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 1978 | static int ip6_pkt_discard_out(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1980 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1981 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 1984 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1985 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1986 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 1987 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1988 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static int ip6_pkt_prohibit_out(struct sk_buff *skb) |
| 1992 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1993 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1994 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 1997 | #endif |
| 1998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | /* |
| 2000 | * Allocate a dst for local (unicast / anycast) address. |
| 2001 | */ |
| 2002 | |
| 2003 | struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, |
| 2004 | const struct in6_addr *addr, |
| 2005 | int anycast) |
| 2006 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2007 | struct net *net = dev_net(idev->dev); |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2008 | struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, |
David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 2009 | net->loopback_dev, 0); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2010 | struct neighbour *neigh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | |
Ben Greear | 4038565 | 2010-11-08 12:33:48 +0000 | [diff] [blame] | 2012 | if (rt == NULL) { |
| 2013 | if (net_ratelimit()) |
| 2014 | pr_warning("IPv6: Maximum number of routes reached," |
| 2015 | " consider increasing route/max_size.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | return ERR_PTR(-ENOMEM); |
Ben Greear | 4038565 | 2010-11-08 12:33:48 +0000 | [diff] [blame] | 2017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | in6_dev_hold(idev); |
| 2020 | |
David S. Miller | 11d53b4 | 2011-06-24 15:23:34 -0700 | [diff] [blame] | 2021 | rt->dst.flags |= DST_HOST; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2022 | rt->dst.input = ip6_input; |
| 2023 | rt->dst.output = ip6_output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | rt->rt6i_idev = idev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2025 | rt->dst.obsolete = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; |
YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 2028 | if (anycast) |
| 2029 | rt->rt6i_flags |= RTF_ANYCAST; |
| 2030 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | rt->rt6i_flags |= RTF_LOCAL; |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2032 | neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); |
| 2033 | if (IS_ERR(neigh)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2034 | dst_free(&rt->dst); |
David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2035 | |
David S. Miller | 29546a6 | 2011-03-03 12:10:37 -0800 | [diff] [blame] | 2036 | return ERR_CAST(neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | } |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 2038 | dst_set_neighbour(&rt->dst, neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
| 2040 | ipv6_addr_copy(&rt->rt6i_dst.addr, addr); |
| 2041 | rt->rt6i_dst.plen = 128; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2042 | rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2044 | atomic_set(&rt->dst.__refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | |
| 2046 | return rt; |
| 2047 | } |
| 2048 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2049 | int ip6_route_get_saddr(struct net *net, |
| 2050 | struct rt6_info *rt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2051 | const struct in6_addr *daddr, |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2052 | unsigned int prefs, |
| 2053 | struct in6_addr *saddr) |
| 2054 | { |
| 2055 | struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); |
| 2056 | int err = 0; |
| 2057 | if (rt->rt6i_prefsrc.plen) |
| 2058 | ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr); |
| 2059 | else |
| 2060 | err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, |
| 2061 | daddr, prefs, saddr); |
| 2062 | return err; |
| 2063 | } |
| 2064 | |
| 2065 | /* remove deleted ip from prefsrc entries */ |
| 2066 | struct arg_dev_net_ip { |
| 2067 | struct net_device *dev; |
| 2068 | struct net *net; |
| 2069 | struct in6_addr *addr; |
| 2070 | }; |
| 2071 | |
| 2072 | static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) |
| 2073 | { |
| 2074 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 2075 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 2076 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 2077 | |
| 2078 | if (((void *)rt->rt6i_dev == dev || dev == NULL) && |
| 2079 | rt != net->ipv6.ip6_null_entry && |
| 2080 | ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { |
| 2081 | /* remove prefsrc entry */ |
| 2082 | rt->rt6i_prefsrc.plen = 0; |
| 2083 | } |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 2088 | { |
| 2089 | struct net *net = dev_net(ifp->idev->dev); |
| 2090 | struct arg_dev_net_ip adni = { |
| 2091 | .dev = ifp->idev->dev, |
| 2092 | .net = net, |
| 2093 | .addr = &ifp->addr, |
| 2094 | }; |
| 2095 | fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); |
| 2096 | } |
| 2097 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2098 | struct arg_dev_net { |
| 2099 | struct net_device *dev; |
| 2100 | struct net *net; |
| 2101 | }; |
| 2102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | static int fib6_ifdown(struct rt6_info *rt, void *arg) |
| 2104 | { |
stephen hemminger | bc3ef66 | 2010-12-16 17:42:40 +0000 | [diff] [blame] | 2105 | const struct arg_dev_net *adn = arg; |
| 2106 | const struct net_device *dev = adn->dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2107 | |
stephen hemminger | bc3ef66 | 2010-12-16 17:42:40 +0000 | [diff] [blame] | 2108 | if ((rt->rt6i_dev == dev || dev == NULL) && |
| 2109 | rt != adn->net->ipv6.ip6_null_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | RT6_TRACE("deleted by ifdown %p\n", rt); |
| 2111 | return -1; |
| 2112 | } |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2116 | void rt6_ifdown(struct net *net, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2118 | struct arg_dev_net adn = { |
| 2119 | .dev = dev, |
| 2120 | .net = net, |
| 2121 | }; |
| 2122 | |
| 2123 | fib6_clean_all(net, fib6_ifdown, 0, &adn); |
David S. Miller | 1e493d1 | 2008-09-10 17:27:15 -0700 | [diff] [blame] | 2124 | icmp6_clean_all(fib6_ifdown, &adn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | struct rt6_mtu_change_arg |
| 2128 | { |
| 2129 | struct net_device *dev; |
| 2130 | unsigned mtu; |
| 2131 | }; |
| 2132 | |
| 2133 | static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) |
| 2134 | { |
| 2135 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 2136 | struct inet6_dev *idev; |
| 2137 | |
| 2138 | /* In IPv6 pmtu discovery is not optional, |
| 2139 | so that RTAX_MTU lock cannot disable it. |
| 2140 | We still use this lock to block changes |
| 2141 | caused by addrconf/ndisc. |
| 2142 | */ |
| 2143 | |
| 2144 | idev = __in6_dev_get(arg->dev); |
| 2145 | if (idev == NULL) |
| 2146 | return 0; |
| 2147 | |
| 2148 | /* For administrative MTU increase, there is no way to discover |
| 2149 | IPv6 PMTU increase, so PMTU increase should be updated here. |
| 2150 | Since RFC 1981 doesn't include administrative MTU increase |
| 2151 | update PMTU increase is a MUST. (i.e. jumbo frame) |
| 2152 | */ |
| 2153 | /* |
| 2154 | If new MTU is less than route PMTU, this new MTU will be the |
| 2155 | lowest MTU in the path, update the route PMTU to reflect PMTU |
| 2156 | decreases; if new MTU is greater than route PMTU, and the |
| 2157 | old MTU is the lowest MTU in the path, update the route PMTU |
| 2158 | to reflect the increase. In this case if the other nodes' MTU |
| 2159 | also have the lowest MTU, TOO BIG MESSAGE will be lead to |
| 2160 | PMTU discouvery. |
| 2161 | */ |
| 2162 | if (rt->rt6i_dev == arg->dev && |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2163 | !dst_metric_locked(&rt->dst, RTAX_MTU) && |
| 2164 | (dst_mtu(&rt->dst) >= arg->mtu || |
| 2165 | (dst_mtu(&rt->dst) < arg->mtu && |
| 2166 | dst_mtu(&rt->dst) == idev->cnf.mtu6))) { |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2167 | dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); |
Simon Arlott | 566cfd8 | 2007-07-26 00:09:55 -0700 | [diff] [blame] | 2168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
| 2172 | void rt6_mtu_change(struct net_device *dev, unsigned mtu) |
| 2173 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2174 | struct rt6_mtu_change_arg arg = { |
| 2175 | .dev = dev, |
| 2176 | .mtu = mtu, |
| 2177 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2179 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 2182 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2183 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2184 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2185 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2186 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 2187 | [RTA_METRICS] = { .type = NLA_NESTED }, |
| 2188 | }; |
| 2189 | |
| 2190 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 2191 | struct fib6_config *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2193 | struct rtmsg *rtm; |
| 2194 | struct nlattr *tb[RTA_MAX+1]; |
| 2195 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2197 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); |
| 2198 | if (err < 0) |
| 2199 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2201 | err = -EINVAL; |
| 2202 | rtm = nlmsg_data(nlh); |
| 2203 | memset(cfg, 0, sizeof(*cfg)); |
| 2204 | |
| 2205 | cfg->fc_table = rtm->rtm_table; |
| 2206 | cfg->fc_dst_len = rtm->rtm_dst_len; |
| 2207 | cfg->fc_src_len = rtm->rtm_src_len; |
| 2208 | cfg->fc_flags = RTF_UP; |
| 2209 | cfg->fc_protocol = rtm->rtm_protocol; |
| 2210 | |
| 2211 | if (rtm->rtm_type == RTN_UNREACHABLE) |
| 2212 | cfg->fc_flags |= RTF_REJECT; |
| 2213 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 2214 | if (rtm->rtm_type == RTN_LOCAL) |
| 2215 | cfg->fc_flags |= RTF_LOCAL; |
| 2216 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2217 | cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; |
| 2218 | cfg->fc_nlinfo.nlh = nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2219 | cfg->fc_nlinfo.nl_net = sock_net(skb->sk); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2220 | |
| 2221 | if (tb[RTA_GATEWAY]) { |
| 2222 | nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); |
| 2223 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2225 | |
| 2226 | if (tb[RTA_DST]) { |
| 2227 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 2228 | |
| 2229 | if (nla_len(tb[RTA_DST]) < plen) |
| 2230 | goto errout; |
| 2231 | |
| 2232 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2234 | |
| 2235 | if (tb[RTA_SRC]) { |
| 2236 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 2237 | |
| 2238 | if (nla_len(tb[RTA_SRC]) < plen) |
| 2239 | goto errout; |
| 2240 | |
| 2241 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2243 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2244 | if (tb[RTA_PREFSRC]) |
| 2245 | nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); |
| 2246 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2247 | if (tb[RTA_OIF]) |
| 2248 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 2249 | |
| 2250 | if (tb[RTA_PRIORITY]) |
| 2251 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 2252 | |
| 2253 | if (tb[RTA_METRICS]) { |
| 2254 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 2255 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2257 | |
| 2258 | if (tb[RTA_TABLE]) |
| 2259 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 2260 | |
| 2261 | err = 0; |
| 2262 | errout: |
| 2263 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2266 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2268 | struct fib6_config cfg; |
| 2269 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2271 | err = rtm_to_fib6_config(skb, nlh, &cfg); |
| 2272 | if (err < 0) |
| 2273 | return err; |
| 2274 | |
| 2275 | return ip6_route_del(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2278 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2280 | struct fib6_config cfg; |
| 2281 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2283 | err = rtm_to_fib6_config(skb, nlh, &cfg); |
| 2284 | if (err < 0) |
| 2285 | return err; |
| 2286 | |
| 2287 | return ip6_route_add(&cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2290 | static inline size_t rt6_nlmsg_size(void) |
| 2291 | { |
| 2292 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 2293 | + nla_total_size(16) /* RTA_SRC */ |
| 2294 | + nla_total_size(16) /* RTA_DST */ |
| 2295 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 2296 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 2297 | + nla_total_size(4) /* RTA_TABLE */ |
| 2298 | + nla_total_size(4) /* RTA_IIF */ |
| 2299 | + nla_total_size(4) /* RTA_OIF */ |
| 2300 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 2301 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2302 | + nla_total_size(sizeof(struct rta_cacheinfo)); |
| 2303 | } |
| 2304 | |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2305 | static int rt6_fill_node(struct net *net, |
| 2306 | struct sk_buff *skb, struct rt6_info *rt, |
Jamal Hadi Salim | 0d51aa8 | 2005-06-21 13:51:04 -0700 | [diff] [blame] | 2307 | struct in6_addr *dst, struct in6_addr *src, |
| 2308 | int iif, int type, u32 pid, u32 seq, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2309 | int prefix, int nowait, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | { |
| 2311 | struct rtmsg *rtm; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2312 | struct nlmsghdr *nlh; |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2313 | long expires; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2314 | u32 table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | if (prefix) { /* user wants prefix routes only */ |
| 2317 | if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { |
| 2318 | /* success since this is not a prefix route */ |
| 2319 | return 1; |
| 2320 | } |
| 2321 | } |
| 2322 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2323 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); |
| 2324 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2325 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2326 | |
| 2327 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | rtm->rtm_family = AF_INET6; |
| 2329 | rtm->rtm_dst_len = rt->rt6i_dst.plen; |
| 2330 | rtm->rtm_src_len = rt->rt6i_src.plen; |
| 2331 | rtm->rtm_tos = 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2332 | if (rt->rt6i_table) |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2333 | table = rt->rt6i_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2334 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2335 | table = RT6_TABLE_UNSPEC; |
| 2336 | rtm->rtm_table = table; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2337 | NLA_PUT_U32(skb, RTA_TABLE, table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | if (rt->rt6i_flags&RTF_REJECT) |
| 2339 | rtm->rtm_type = RTN_UNREACHABLE; |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 2340 | else if (rt->rt6i_flags&RTF_LOCAL) |
| 2341 | rtm->rtm_type = RTN_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK)) |
| 2343 | rtm->rtm_type = RTN_LOCAL; |
| 2344 | else |
| 2345 | rtm->rtm_type = RTN_UNICAST; |
| 2346 | rtm->rtm_flags = 0; |
| 2347 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
| 2348 | rtm->rtm_protocol = rt->rt6i_protocol; |
| 2349 | if (rt->rt6i_flags&RTF_DYNAMIC) |
| 2350 | rtm->rtm_protocol = RTPROT_REDIRECT; |
| 2351 | else if (rt->rt6i_flags & RTF_ADDRCONF) |
| 2352 | rtm->rtm_protocol = RTPROT_KERNEL; |
| 2353 | else if (rt->rt6i_flags&RTF_DEFAULT) |
| 2354 | rtm->rtm_protocol = RTPROT_RA; |
| 2355 | |
| 2356 | if (rt->rt6i_flags&RTF_CACHE) |
| 2357 | rtm->rtm_flags |= RTM_F_CLONED; |
| 2358 | |
| 2359 | if (dst) { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2360 | NLA_PUT(skb, RTA_DST, 16, dst); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2361 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | } else if (rtm->rtm_dst_len) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2363 | NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | #ifdef CONFIG_IPV6_SUBTREES |
| 2365 | if (src) { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2366 | NLA_PUT(skb, RTA_SRC, 16, src); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2367 | rtm->rtm_src_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | } else if (rtm->rtm_src_len) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2369 | NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2371 | if (iif) { |
| 2372 | #ifdef CONFIG_IPV6_MROUTE |
| 2373 | if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 2374 | int err = ip6mr_get_route(net, skb, rtm, nowait); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2375 | if (err <= 0) { |
| 2376 | if (!nowait) { |
| 2377 | if (err == 0) |
| 2378 | return 0; |
| 2379 | goto nla_put_failure; |
| 2380 | } else { |
| 2381 | if (err == -EMSGSIZE) |
| 2382 | goto nla_put_failure; |
| 2383 | } |
| 2384 | } |
| 2385 | } else |
| 2386 | #endif |
| 2387 | NLA_PUT_U32(skb, RTA_IIF, iif); |
| 2388 | } else if (dst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | struct in6_addr saddr_buf; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2390 | if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2391 | NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2393 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2394 | if (rt->rt6i_prefsrc.plen) { |
| 2395 | struct in6_addr saddr_buf; |
| 2396 | ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr); |
| 2397 | NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); |
| 2398 | } |
| 2399 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2400 | if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2401 | goto nla_put_failure; |
| 2402 | |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 2403 | if (dst_get_neighbour(&rt->dst)) |
| 2404 | NLA_PUT(skb, RTA_GATEWAY, 16, &dst_get_neighbour(&rt->dst)->primary_key); |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2405 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2406 | if (rt->dst.dev) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2407 | NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex); |
| 2408 | |
| 2409 | NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2410 | |
YOSHIFUJI Hideaki | 36e3dea | 2008-05-13 02:52:55 +0900 | [diff] [blame] | 2411 | if (!(rt->rt6i_flags & RTF_EXPIRES)) |
| 2412 | expires = 0; |
| 2413 | else if (rt->rt6i_expires - jiffies < INT_MAX) |
| 2414 | expires = rt->rt6i_expires - jiffies; |
| 2415 | else |
| 2416 | expires = INT_MAX; |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 2417 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2418 | if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, |
| 2419 | expires, rt->dst.error) < 0) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2420 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2422 | return nlmsg_end(skb, nlh); |
| 2423 | |
| 2424 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2425 | nlmsg_cancel(skb, nlh); |
| 2426 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 2429 | int rt6_dump_route(struct rt6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | { |
| 2431 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; |
| 2432 | int prefix; |
| 2433 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2434 | if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { |
| 2435 | struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; |
| 2437 | } else |
| 2438 | prefix = 0; |
| 2439 | |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2440 | return rt6_fill_node(arg->net, |
| 2441 | arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2443 | prefix, 0, NLM_F_MULTI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2446 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2448 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2449 | struct nlattr *tb[RTA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2451 | struct sk_buff *skb; |
| 2452 | struct rtmsg *rtm; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2453 | struct flowi6 fl6; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2454 | int err, iif = 0; |
| 2455 | |
| 2456 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); |
| 2457 | if (err < 0) |
| 2458 | goto errout; |
| 2459 | |
| 2460 | err = -EINVAL; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2461 | memset(&fl6, 0, sizeof(fl6)); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2462 | |
| 2463 | if (tb[RTA_SRC]) { |
| 2464 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 2465 | goto errout; |
| 2466 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2467 | ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC])); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | if (tb[RTA_DST]) { |
| 2471 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 2472 | goto errout; |
| 2473 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2474 | ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST])); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | if (tb[RTA_IIF]) |
| 2478 | iif = nla_get_u32(tb[RTA_IIF]); |
| 2479 | |
| 2480 | if (tb[RTA_OIF]) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2481 | fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2482 | |
| 2483 | if (iif) { |
| 2484 | struct net_device *dev; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2485 | dev = __dev_get_by_index(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2486 | if (!dev) { |
| 2487 | err = -ENODEV; |
| 2488 | goto errout; |
| 2489 | } |
| 2490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
| 2492 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2493 | if (skb == NULL) { |
| 2494 | err = -ENOBUFS; |
| 2495 | goto errout; |
| 2496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
| 2498 | /* Reserve room for dummy headers, this skb can pass |
| 2499 | through good chunk of routing engine. |
| 2500 | */ |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 2501 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); |
| 2503 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2504 | rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2505 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2507 | err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | RTM_NEWROUTE, NETLINK_CB(in_skb).pid, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2509 | nlh->nlmsg_seq, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2511 | kfree_skb(skb); |
| 2512 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2515 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2516 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | } |
| 2519 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2520 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | { |
| 2522 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2523 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 2524 | u32 seq; |
| 2525 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 2527 | err = -ENOBUFS; |
| 2528 | seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2529 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2530 | skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 2531 | if (skb == NULL) |
| 2532 | goto errout; |
| 2533 | |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2534 | err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2535 | event, info->pid, seq, 0, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2536 | if (err < 0) { |
| 2537 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 2538 | WARN_ON(err == -EMSGSIZE); |
| 2539 | kfree_skb(skb); |
| 2540 | goto errout; |
| 2541 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2542 | rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, |
| 2543 | info->nlh, gfp_any()); |
| 2544 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 2545 | errout: |
| 2546 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2547 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2550 | static int ip6_route_dev_notify(struct notifier_block *this, |
| 2551 | unsigned long event, void *data) |
| 2552 | { |
| 2553 | struct net_device *dev = (struct net_device *)data; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2554 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2555 | |
| 2556 | if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2557 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2558 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 2559 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2560 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2561 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2562 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2563 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 2564 | #endif |
| 2565 | } |
| 2566 | |
| 2567 | return NOTIFY_OK; |
| 2568 | } |
| 2569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | /* |
| 2571 | * /proc |
| 2572 | */ |
| 2573 | |
| 2574 | #ifdef CONFIG_PROC_FS |
| 2575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | struct rt6_proc_arg |
| 2577 | { |
| 2578 | char *buffer; |
| 2579 | int offset; |
| 2580 | int length; |
| 2581 | int skip; |
| 2582 | int len; |
| 2583 | }; |
| 2584 | |
| 2585 | static int rt6_info_route(struct rt6_info *rt, void *p_arg) |
| 2586 | { |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2587 | struct seq_file *m = p_arg; |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 2588 | struct neighbour *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2590 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
| 2592 | #ifdef CONFIG_IPV6_SUBTREES |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2593 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | #else |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2595 | seq_puts(m, "00000000000000000000000000000000 00 "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | #endif |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame^] | 2597 | n = dst_get_neighbour(&rt->dst); |
| 2598 | if (n) { |
| 2599 | seq_printf(m, "%pi6", n->primary_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | } else { |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2601 | seq_puts(m, "00000000000000000000000000000000"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | } |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2603 | seq_printf(m, " %08x %08x %08x %08x %8s\n", |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2604 | rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), |
| 2605 | rt->dst.__use, rt->rt6i_flags, |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2606 | rt->rt6i_dev ? rt->rt6i_dev->name : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | return 0; |
| 2608 | } |
| 2609 | |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2610 | static int ipv6_route_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | { |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2612 | struct net *net = (struct net *)m->private; |
| 2613 | fib6_clean_all(net, rt6_info_route, 0, m); |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2614 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2617 | static int ipv6_route_open(struct inode *inode, struct file *file) |
| 2618 | { |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 2619 | return single_open_net(inode, file, ipv6_route_show); |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2620 | } |
| 2621 | |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2622 | static const struct file_operations ipv6_route_proc_fops = { |
| 2623 | .owner = THIS_MODULE, |
| 2624 | .open = ipv6_route_open, |
| 2625 | .read = seq_read, |
| 2626 | .llseek = seq_lseek, |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 2627 | .release = single_release_net, |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2628 | }; |
| 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 2631 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2632 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2634 | net->ipv6.rt6_stats->fib_nodes, |
| 2635 | net->ipv6.rt6_stats->fib_route_nodes, |
| 2636 | net->ipv6.rt6_stats->fib_rt_alloc, |
| 2637 | net->ipv6.rt6_stats->fib_rt_entries, |
| 2638 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2639 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2640 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
| 2642 | return 0; |
| 2643 | } |
| 2644 | |
| 2645 | static int rt6_stats_seq_open(struct inode *inode, struct file *file) |
| 2646 | { |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 2647 | return single_open_net(inode, file, rt6_stats_seq_show); |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2648 | } |
| 2649 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2650 | static const struct file_operations rt6_stats_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | .owner = THIS_MODULE, |
| 2652 | .open = rt6_stats_seq_open, |
| 2653 | .read = seq_read, |
| 2654 | .llseek = seq_lseek, |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 2655 | .release = single_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | }; |
| 2657 | #endif /* CONFIG_PROC_FS */ |
| 2658 | |
| 2659 | #ifdef CONFIG_SYSCTL |
| 2660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | static |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2662 | int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 2664 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2665 | struct net *net; |
| 2666 | int delay; |
| 2667 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2669 | |
| 2670 | net = (struct net *)ctl->extra1; |
| 2671 | delay = net->ipv6.sysctl.flush_delay; |
| 2672 | proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 2673 | fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); |
| 2674 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2677 | ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2678 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2680 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 2682 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2683 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | }, |
| 2685 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 2687 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | .maxlen = sizeof(int), |
| 2689 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2690 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | }, |
| 2692 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2694 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | .maxlen = sizeof(int), |
| 2696 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2697 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | }, |
| 2699 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2701 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | .maxlen = sizeof(int), |
| 2703 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2704 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | }, |
| 2706 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2708 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | .maxlen = sizeof(int), |
| 2710 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2711 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | }, |
| 2713 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2715 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | .maxlen = sizeof(int), |
| 2717 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2718 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | }, |
| 2720 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2722 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | .maxlen = sizeof(int), |
| 2724 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 2725 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | }, |
| 2727 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2729 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | .maxlen = sizeof(int), |
| 2731 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2732 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | }, |
| 2734 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2736 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | .maxlen = sizeof(int), |
| 2738 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 2739 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | }, |
| 2741 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2743 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | .maxlen = sizeof(int), |
| 2745 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2746 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2748 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | }; |
| 2750 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2751 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2752 | { |
| 2753 | struct ctl_table *table; |
| 2754 | |
| 2755 | table = kmemdup(ipv6_route_table_template, |
| 2756 | sizeof(ipv6_route_table_template), |
| 2757 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2758 | |
| 2759 | if (table) { |
| 2760 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2761 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2762 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2763 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 2764 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 2765 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 2766 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 2767 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 2768 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 2769 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 2770 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2771 | } |
| 2772 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2773 | return table; |
| 2774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | #endif |
| 2776 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2777 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2778 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 2779 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2780 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2781 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 2782 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2783 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2784 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 2785 | goto out_ip6_dst_ops; |
| 2786 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2787 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 2788 | sizeof(*net->ipv6.ip6_null_entry), |
| 2789 | GFP_KERNEL); |
| 2790 | if (!net->ipv6.ip6_null_entry) |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2791 | goto out_ip6_dst_entries; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2792 | net->ipv6.ip6_null_entry->dst.path = |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2793 | (struct dst_entry *)net->ipv6.ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2794 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2795 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 2796 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2797 | |
| 2798 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 2799 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 2800 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 2801 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2802 | if (!net->ipv6.ip6_prohibit_entry) |
| 2803 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2804 | net->ipv6.ip6_prohibit_entry->dst.path = |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2805 | (struct dst_entry *)net->ipv6.ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2806 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2807 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 2808 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2809 | |
| 2810 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 2811 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 2812 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2813 | if (!net->ipv6.ip6_blk_hole_entry) |
| 2814 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2815 | net->ipv6.ip6_blk_hole_entry->dst.path = |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2816 | (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2817 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2818 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 2819 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2820 | #endif |
| 2821 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 2822 | net->ipv6.sysctl.flush_delay = 0; |
| 2823 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 2824 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 2825 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 2826 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 2827 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 2828 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 2829 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
| 2830 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2831 | #ifdef CONFIG_PROC_FS |
| 2832 | proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); |
| 2833 | proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); |
| 2834 | #endif |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2835 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 2836 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2837 | ret = 0; |
| 2838 | out: |
| 2839 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2840 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2841 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 2842 | out_ip6_prohibit_entry: |
| 2843 | kfree(net->ipv6.ip6_prohibit_entry); |
| 2844 | out_ip6_null_entry: |
| 2845 | kfree(net->ipv6.ip6_null_entry); |
| 2846 | #endif |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2847 | out_ip6_dst_entries: |
| 2848 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2849 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2850 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2851 | } |
| 2852 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2853 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2854 | { |
| 2855 | #ifdef CONFIG_PROC_FS |
| 2856 | proc_net_remove(net, "ipv6_route"); |
| 2857 | proc_net_remove(net, "rt6_stats"); |
| 2858 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2859 | kfree(net->ipv6.ip6_null_entry); |
| 2860 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 2861 | kfree(net->ipv6.ip6_prohibit_entry); |
| 2862 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 2863 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 2864 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | static struct pernet_operations ip6_route_net_ops = { |
| 2868 | .init = ip6_route_net_init, |
| 2869 | .exit = ip6_route_net_exit, |
| 2870 | }; |
| 2871 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2872 | static struct notifier_block ip6_route_dev_notifier = { |
| 2873 | .notifier_call = ip6_route_dev_notify, |
| 2874 | .priority = 0, |
| 2875 | }; |
| 2876 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2877 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2879 | int ret; |
| 2880 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 2881 | ret = -ENOMEM; |
| 2882 | ip6_dst_ops_template.kmem_cachep = |
| 2883 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 2884 | SLAB_HWCACHE_ALIGN, NULL); |
| 2885 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 2886 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2887 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2888 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2889 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 2890 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 2891 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2892 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 2893 | if (ret) |
| 2894 | goto out_dst_entries; |
| 2895 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 2896 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 2897 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2898 | /* Registering of the loopback is done before this portion of code, |
| 2899 | * the loopback reference in rt6_info will not be taken, do it |
| 2900 | * manually for init_net */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2901 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2902 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 2903 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2904 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2905 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2906 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2907 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 2908 | #endif |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2909 | ret = fib6_init(); |
| 2910 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2911 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2912 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2913 | ret = xfrm6_init(); |
| 2914 | if (ret) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2915 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 2916 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2917 | ret = fib6_rules_init(); |
| 2918 | if (ret) |
| 2919 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 2920 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2921 | ret = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2922 | if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || |
| 2923 | __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || |
| 2924 | __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2925 | goto fib6_rules_init; |
| 2926 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2927 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2928 | if (ret) |
| 2929 | goto fib6_rules_init; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2930 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2931 | out: |
| 2932 | return ret; |
| 2933 | |
| 2934 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2935 | fib6_rules_cleanup(); |
| 2936 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2937 | xfrm6_fini(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2938 | out_fib6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2939 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2940 | out_register_subsys: |
| 2941 | unregister_pernet_subsys(&ip6_route_net_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2942 | out_dst_entries: |
| 2943 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2944 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2945 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2946 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | void ip6_route_cleanup(void) |
| 2950 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2951 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 2952 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2955 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 2956 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2957 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | } |