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