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