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