Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Forwarding Information Database |
| 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. |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 12 | * |
| 13 | * Changes: |
| 14 | * Yuji SEKIYA @USAGI: Support default route on router node; |
| 15 | * remove ip6_null_entry from the top of |
| 16 | * routing table. |
| 17 | * Ville Nuorvala: Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 19 | |
| 20 | #define pr_fmt(fmt) "IPv6: " fmt |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/errno.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/route.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/in6.h> |
| 28 | #include <linux/init.h> |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/ipv6.h> |
| 33 | #include <net/ndisc.h> |
| 34 | #include <net/addrconf.h> |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 35 | #include <net/lwtunnel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <net/ip6_fib.h> |
| 38 | #include <net/ip6_route.h> |
| 39 | |
| 40 | #define RT6_DEBUG 2 |
| 41 | |
| 42 | #if RT6_DEBUG >= 3 |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 43 | #define RT6_TRACE(x...) pr_debug(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #else |
| 45 | #define RT6_TRACE(x...) do { ; } while (0) |
| 46 | #endif |
| 47 | |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 48 | static struct kmem_cache *fib6_node_kmem __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 50 | struct fib6_cleaner { |
| 51 | struct fib6_walker w; |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 52 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int (*func)(struct rt6_info *, void *arg); |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 54 | int sernum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | void *arg; |
| 56 | }; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifdef CONFIG_IPV6_SUBTREES |
| 59 | #define FWS_INIT FWS_S |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #else |
| 61 | #define FWS_INIT FWS_L |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
Duan Jiong | 163cd4e | 2014-05-09 13:31:43 +0800 | [diff] [blame] | 64 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 65 | static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn); |
| 66 | static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn); |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 67 | static int fib6_walk(struct net *net, struct fib6_walker *w); |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 68 | static int fib6_walk_continue(struct fib6_walker *w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * A routing update causes an increase of the serial number on the |
| 72 | * affected subtree. This allows for cached routes to be asynchronously |
| 73 | * tested when modifications are made to the destination cache as a |
| 74 | * result of redirects, path MTU changes, etc. |
| 75 | */ |
| 76 | |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 77 | static void fib6_gc_timer_cb(unsigned long arg); |
| 78 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 79 | #define FOR_WALKERS(net, w) \ |
| 80 | list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 82 | static void fib6_walker_link(struct net *net, struct fib6_walker *w) |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 83 | { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 84 | write_lock_bh(&net->ipv6.fib6_walker_lock); |
| 85 | list_add(&w->lh, &net->ipv6.fib6_walkers); |
| 86 | write_unlock_bh(&net->ipv6.fib6_walker_lock); |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 89 | static void fib6_walker_unlink(struct net *net, struct fib6_walker *w) |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 90 | { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 91 | write_lock_bh(&net->ipv6.fib6_walker_lock); |
Alexey Dobriyan | bbef49d | 2010-02-18 08:13:30 +0000 | [diff] [blame] | 92 | list_del(&w->lh); |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 93 | write_unlock_bh(&net->ipv6.fib6_walker_lock); |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 94 | } |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 95 | |
Hannes Frederic Sowa | 812918c | 2014-10-06 19:58:37 +0200 | [diff] [blame] | 96 | static int fib6_new_sernum(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Hannes Frederic Sowa | 42b1870 | 2014-10-06 19:58:35 +0200 | [diff] [blame] | 98 | int new, old; |
| 99 | |
| 100 | do { |
Hannes Frederic Sowa | 812918c | 2014-10-06 19:58:37 +0200 | [diff] [blame] | 101 | old = atomic_read(&net->ipv6.fib6_sernum); |
Hannes Frederic Sowa | 42b1870 | 2014-10-06 19:58:35 +0200 | [diff] [blame] | 102 | new = old < INT_MAX ? old + 1 : 1; |
Hannes Frederic Sowa | 812918c | 2014-10-06 19:58:37 +0200 | [diff] [blame] | 103 | } while (atomic_cmpxchg(&net->ipv6.fib6_sernum, |
| 104 | old, new) != old); |
Hannes Frederic Sowa | 42b1870 | 2014-10-06 19:58:35 +0200 | [diff] [blame] | 105 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 108 | enum { |
| 109 | FIB6_NO_SERNUM_CHANGE = 0, |
| 110 | }; |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
| 113 | * Auxiliary address test functions for the radix tree. |
| 114 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 115 | * These assume a 32bit processor (although it will work on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * 64bit processors) |
| 117 | */ |
| 118 | |
| 119 | /* |
| 120 | * test bit |
| 121 | */ |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 122 | #if defined(__LITTLE_ENDIAN) |
| 123 | # define BITOP_BE32_SWIZZLE (0x1F & ~7) |
| 124 | #else |
| 125 | # define BITOP_BE32_SWIZZLE 0 |
| 126 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 128 | static __be32 addr_bit_set(const void *token, int fn_bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 130 | const __be32 *addr = token; |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Here, |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 133 | * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f) |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 134 | * is optimized version of |
| 135 | * htonl(1 << ((~fn_bit)&0x1F)) |
| 136 | * See include/asm-generic/bitops/le.h. |
| 137 | */ |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 138 | return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) & |
| 139 | addr[fn_bit >> 5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 142 | static struct fib6_node *node_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct fib6_node *fn; |
| 145 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 146 | fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | return fn; |
| 149 | } |
| 150 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 151 | static void node_free(struct fib6_node *fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
| 153 | kmem_cache_free(fib6_node_kmem, fn); |
| 154 | } |
| 155 | |
Martin KaFai Lau | 70da5b5 | 2015-09-15 14:30:09 -0700 | [diff] [blame] | 156 | static void rt6_rcu_free(struct rt6_info *rt) |
| 157 | { |
| 158 | call_rcu(&rt->dst.rcu_head, dst_rcu_free); |
| 159 | } |
| 160 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 161 | static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt) |
| 162 | { |
| 163 | int cpu; |
| 164 | |
| 165 | if (!non_pcpu_rt->rt6i_pcpu) |
| 166 | return; |
| 167 | |
| 168 | for_each_possible_cpu(cpu) { |
| 169 | struct rt6_info **ppcpu_rt; |
| 170 | struct rt6_info *pcpu_rt; |
| 171 | |
| 172 | ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu); |
| 173 | pcpu_rt = *ppcpu_rt; |
| 174 | if (pcpu_rt) { |
Martin KaFai Lau | 70da5b5 | 2015-09-15 14:30:09 -0700 | [diff] [blame] | 175 | rt6_rcu_free(pcpu_rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 176 | *ppcpu_rt = NULL; |
| 177 | } |
| 178 | } |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 179 | |
Martin KaFai Lau | 903ce4a | 2016-07-05 12:10:23 -0700 | [diff] [blame] | 180 | free_percpu(non_pcpu_rt->rt6i_pcpu); |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 181 | non_pcpu_rt->rt6i_pcpu = NULL; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 184 | static void rt6_release(struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 186 | if (atomic_dec_and_test(&rt->rt6i_ref)) { |
| 187 | rt6_free_pcpu(rt); |
Martin KaFai Lau | 70da5b5 | 2015-09-15 14:30:09 -0700 | [diff] [blame] | 188 | rt6_rcu_free(rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 192 | static void fib6_link_table(struct net *net, struct fib6_table *tb) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 193 | { |
| 194 | unsigned int h; |
| 195 | |
Thomas Graf | 375216a | 2006-10-21 20:20:54 -0700 | [diff] [blame] | 196 | /* |
| 197 | * Initialize table lock at a single place to give lockdep a key, |
| 198 | * tables aren't visible prior to being linked to the list. |
| 199 | */ |
| 200 | rwlock_init(&tb->tb6_lock); |
| 201 | |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 202 | h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * No protection necessary, this is the only list mutatation |
| 206 | * operation, tables never disappear once they exist. |
| 207 | */ |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 208 | hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 212 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 213 | static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 214 | { |
| 215 | struct fib6_table *table; |
| 216 | |
| 217 | table = kzalloc(sizeof(*table), GFP_ATOMIC); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 218 | if (table) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 219 | table->tb6_id = id; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 220 | table->tb6_root.leaf = net->ipv6.ip6_null_entry; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 221 | table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 222 | inet_peer_base_init(&table->tb6_peers); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return table; |
| 226 | } |
| 227 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 228 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 229 | { |
| 230 | struct fib6_table *tb; |
| 231 | |
| 232 | if (id == 0) |
| 233 | id = RT6_TABLE_MAIN; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 234 | tb = fib6_get_table(net, id); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 235 | if (tb) |
| 236 | return tb; |
| 237 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 238 | tb = fib6_alloc_table(net, id); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 239 | if (tb) |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 240 | fib6_link_table(net, tb); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 241 | |
| 242 | return tb; |
| 243 | } |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(fib6_new_table); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 245 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 246 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 247 | { |
| 248 | struct fib6_table *tb; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 249 | struct hlist_head *head; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 250 | unsigned int h; |
| 251 | |
| 252 | if (id == 0) |
| 253 | id = RT6_TABLE_MAIN; |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 254 | h = id & (FIB6_TABLE_HASHSZ - 1); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 255 | rcu_read_lock(); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 256 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 257 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 258 | if (tb->tb6_id == id) { |
| 259 | rcu_read_unlock(); |
| 260 | return tb; |
| 261 | } |
| 262 | } |
| 263 | rcu_read_unlock(); |
| 264 | |
| 265 | return NULL; |
| 266 | } |
David Ahern | c485068 | 2015-10-12 11:47:08 -0700 | [diff] [blame] | 267 | EXPORT_SYMBOL_GPL(fib6_get_table); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 268 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 269 | static void __net_init fib6_tables_init(struct net *net) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 270 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 271 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
| 272 | fib6_link_table(net, net->ipv6.fib6_local_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 273 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 274 | #else |
| 275 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 276 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 277 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 278 | return fib6_get_table(net, id); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 281 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 282 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 283 | return net->ipv6.fib6_main_tbl; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 284 | } |
| 285 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 286 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 287 | int flags, pol_lookup_t lookup) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 288 | { |
lucien | ab997ad | 2015-10-23 15:36:53 +0800 | [diff] [blame] | 289 | struct rt6_info *rt; |
| 290 | |
| 291 | rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags); |
| 292 | if (rt->rt6i_flags & RTF_REJECT && |
| 293 | rt->dst.error == -EAGAIN) { |
| 294 | ip6_rt_put(rt); |
| 295 | rt = net->ipv6.ip6_null_entry; |
| 296 | dst_hold(&rt->dst); |
| 297 | } |
| 298 | |
| 299 | return &rt->dst; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 302 | static void __net_init fib6_tables_init(struct net *net) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 303 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 304 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | #endif |
| 308 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 309 | static int fib6_dump_node(struct fib6_walker *w) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 310 | { |
| 311 | int res; |
| 312 | struct rt6_info *rt; |
| 313 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 314 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 315 | res = rt6_dump_route(rt, w->args); |
| 316 | if (res < 0) { |
| 317 | /* Frame is full, suspend walking */ |
| 318 | w->leaf = rt; |
| 319 | return 1; |
| 320 | } |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 321 | |
| 322 | /* Multipath routes are dumped in one route with the |
| 323 | * RTA_MULTIPATH attribute. Jump 'rt' to point to the |
| 324 | * last sibling of this route (no need to dump the |
| 325 | * sibling routes again) |
| 326 | */ |
| 327 | if (rt->rt6i_nsiblings) |
| 328 | rt = list_last_entry(&rt->rt6i_siblings, |
| 329 | struct rt6_info, |
| 330 | rt6i_siblings); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 331 | } |
| 332 | w->leaf = NULL; |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static void fib6_dump_end(struct netlink_callback *cb) |
| 337 | { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 338 | struct net *net = sock_net(cb->skb->sk); |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 339 | struct fib6_walker *w = (void *)cb->args[2]; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 340 | |
| 341 | if (w) { |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 342 | if (cb->args[4]) { |
| 343 | cb->args[4] = 0; |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 344 | fib6_walker_unlink(net, w); |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 345 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 346 | cb->args[2] = 0; |
| 347 | kfree(w); |
| 348 | } |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 349 | cb->done = (void *)cb->args[3]; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 350 | cb->args[1] = 3; |
| 351 | } |
| 352 | |
| 353 | static int fib6_dump_done(struct netlink_callback *cb) |
| 354 | { |
| 355 | fib6_dump_end(cb); |
| 356 | return cb->done ? cb->done(cb) : 0; |
| 357 | } |
| 358 | |
| 359 | static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, |
| 360 | struct netlink_callback *cb) |
| 361 | { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 362 | struct net *net = sock_net(skb->sk); |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 363 | struct fib6_walker *w; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 364 | int res; |
| 365 | |
| 366 | w = (void *)cb->args[2]; |
| 367 | w->root = &table->tb6_root; |
| 368 | |
| 369 | if (cb->args[4] == 0) { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 370 | w->count = 0; |
| 371 | w->skip = 0; |
| 372 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 373 | read_lock_bh(&table->tb6_lock); |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 374 | res = fib6_walk(net, w); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 375 | read_unlock_bh(&table->tb6_lock); |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 376 | if (res > 0) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 377 | cb->args[4] = 1; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 378 | cb->args[5] = w->root->fn_sernum; |
| 379 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 380 | } else { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 381 | if (cb->args[5] != w->root->fn_sernum) { |
| 382 | /* Begin at the root if the tree changed */ |
| 383 | cb->args[5] = w->root->fn_sernum; |
| 384 | w->state = FWS_INIT; |
| 385 | w->node = w->root; |
| 386 | w->skip = w->count; |
| 387 | } else |
| 388 | w->skip = 0; |
| 389 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 390 | read_lock_bh(&table->tb6_lock); |
| 391 | res = fib6_walk_continue(w); |
| 392 | read_unlock_bh(&table->tb6_lock); |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 393 | if (res <= 0) { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 394 | fib6_walker_unlink(net, w); |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 395 | cb->args[4] = 0; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 396 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 397 | } |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 398 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 399 | return res; |
| 400 | } |
| 401 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 402 | static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 403 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 404 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 405 | unsigned int h, s_h; |
| 406 | unsigned int e = 0, s_e; |
| 407 | struct rt6_rtnl_dump_arg arg; |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 408 | struct fib6_walker *w; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 409 | struct fib6_table *tb; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 410 | struct hlist_head *head; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 411 | int res = 0; |
| 412 | |
| 413 | s_h = cb->args[0]; |
| 414 | s_e = cb->args[1]; |
| 415 | |
| 416 | w = (void *)cb->args[2]; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 417 | if (!w) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 418 | /* New dump: |
| 419 | * |
| 420 | * 1. hook callback destructor. |
| 421 | */ |
| 422 | cb->args[3] = (long)cb->done; |
| 423 | cb->done = fib6_dump_done; |
| 424 | |
| 425 | /* |
| 426 | * 2. allocate and initialize walker. |
| 427 | */ |
| 428 | w = kzalloc(sizeof(*w), GFP_ATOMIC); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 429 | if (!w) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 430 | return -ENOMEM; |
| 431 | w->func = fib6_dump_node; |
| 432 | cb->args[2] = (long)w; |
| 433 | } |
| 434 | |
| 435 | arg.skb = skb; |
| 436 | arg.cb = cb; |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 437 | arg.net = net; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 438 | w->args = &arg; |
| 439 | |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 440 | rcu_read_lock(); |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 441 | for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 442 | e = 0; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 443 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 444 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 445 | if (e < s_e) |
| 446 | goto next; |
| 447 | res = fib6_dump_table(tb, skb, cb); |
| 448 | if (res != 0) |
| 449 | goto out; |
| 450 | next: |
| 451 | e++; |
| 452 | } |
| 453 | } |
| 454 | out: |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 455 | rcu_read_unlock(); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 456 | cb->args[1] = e; |
| 457 | cb->args[0] = h; |
| 458 | |
| 459 | res = res < 0 ? res : skb->len; |
| 460 | if (res <= 0) |
| 461 | fib6_dump_end(cb); |
| 462 | return res; |
| 463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * Routing Table |
| 467 | * |
| 468 | * return the appropriate node for a routing tree "add" operation |
| 469 | * by either creating and inserting or by returning an existing |
| 470 | * node. |
| 471 | */ |
| 472 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 473 | static struct fib6_node *fib6_add_1(struct fib6_node *root, |
| 474 | struct in6_addr *addr, int plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 475 | int offset, int allow_create, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame^] | 476 | int replace_required, int sernum, |
| 477 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
| 479 | struct fib6_node *fn, *in, *ln; |
| 480 | struct fib6_node *pn = NULL; |
| 481 | struct rt6key *key; |
| 482 | int bit; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 483 | __be32 dir = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | RT6_TRACE("fib6_add_1\n"); |
| 486 | |
| 487 | /* insert node in tree */ |
| 488 | |
| 489 | fn = root; |
| 490 | |
| 491 | do { |
| 492 | key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 493 | |
| 494 | /* |
| 495 | * Prefix match |
| 496 | */ |
| 497 | if (plen < fn->fn_bit || |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 498 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) { |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 499 | if (!allow_create) { |
| 500 | if (replace_required) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 501 | pr_warn("Can't replace route, no match found\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 502 | return ERR_PTR(-ENOENT); |
| 503 | } |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 504 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto insert_above; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 507 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* |
| 510 | * Exact match ? |
| 511 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (plen == fn->fn_bit) { |
| 514 | /* clean up an intermediate node */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 515 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | rt6_release(fn->leaf); |
| 517 | fn->leaf = NULL; |
| 518 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | fn->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return fn; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * We have more bits to go |
| 527 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* Try to walk down on tree. */ |
| 530 | fn->fn_sernum = sernum; |
| 531 | dir = addr_bit_set(addr, fn->fn_bit); |
| 532 | pn = fn; |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 533 | fn = dir ? fn->right : fn->left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } while (fn); |
| 535 | |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 536 | if (!allow_create) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 537 | /* We should not create new node because |
| 538 | * NLM_F_REPLACE was specified without NLM_F_CREATE |
| 539 | * I assume it is safe to require NLM_F_CREATE when |
| 540 | * REPLACE flag is used! Later we may want to remove the |
| 541 | * check for replace_required, because according |
| 542 | * to netlink specification, NLM_F_CREATE |
| 543 | * MUST be specified if new route is created. |
| 544 | * That would keep IPv6 consistent with IPv4 |
| 545 | */ |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 546 | if (replace_required) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 547 | pr_warn("Can't replace route, no match found\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 548 | return ERR_PTR(-ENOENT); |
| 549 | } |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 550 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* |
| 553 | * We walked to the bottom of tree. |
| 554 | * Create new leaf node without children. |
| 555 | */ |
| 556 | |
| 557 | ln = node_alloc(); |
| 558 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 559 | if (!ln) |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 560 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | ln->fn_bit = plen; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | ln->parent = pn; |
| 564 | ln->fn_sernum = sernum; |
| 565 | |
| 566 | if (dir) |
| 567 | pn->right = ln; |
| 568 | else |
| 569 | pn->left = ln; |
| 570 | |
| 571 | return ln; |
| 572 | |
| 573 | |
| 574 | insert_above: |
| 575 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 576 | * split since we don't have a common prefix anymore or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | * we have a less significant route. |
| 578 | * we've to insert an intermediate node on the list |
| 579 | * this new node will point to the one we need to create |
| 580 | * and the current |
| 581 | */ |
| 582 | |
| 583 | pn = fn->parent; |
| 584 | |
| 585 | /* find 1st bit in difference between the 2 addrs. |
| 586 | |
YOSHIFUJI Hideaki | 971f359 | 2005-11-08 09:37:56 -0800 | [diff] [blame] | 587 | See comment in __ipv6_addr_diff: bit may be an invalid value, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | but if it is >= plen, the value is ignored in any case. |
| 589 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 590 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 591 | bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 593 | /* |
| 594 | * (intermediate)[in] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | * / \ |
| 596 | * (new leaf node)[ln] (old node)[fn] |
| 597 | */ |
| 598 | if (plen > bit) { |
| 599 | in = node_alloc(); |
| 600 | ln = node_alloc(); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 601 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 602 | if (!in || !ln) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (in) |
| 604 | node_free(in); |
| 605 | if (ln) |
| 606 | node_free(ln); |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 607 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 610 | /* |
| 611 | * new intermediate node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | * RTN_RTINFO will |
| 613 | * be off since that an address that chooses one of |
| 614 | * the branches would not match less specific routes |
| 615 | * in the other branch |
| 616 | */ |
| 617 | |
| 618 | in->fn_bit = bit; |
| 619 | |
| 620 | in->parent = pn; |
| 621 | in->leaf = fn->leaf; |
| 622 | atomic_inc(&in->leaf->rt6i_ref); |
| 623 | |
| 624 | in->fn_sernum = sernum; |
| 625 | |
| 626 | /* update parent pointer */ |
| 627 | if (dir) |
| 628 | pn->right = in; |
| 629 | else |
| 630 | pn->left = in; |
| 631 | |
| 632 | ln->fn_bit = plen; |
| 633 | |
| 634 | ln->parent = in; |
| 635 | fn->parent = in; |
| 636 | |
| 637 | ln->fn_sernum = sernum; |
| 638 | |
| 639 | if (addr_bit_set(addr, bit)) { |
| 640 | in->right = ln; |
| 641 | in->left = fn; |
| 642 | } else { |
| 643 | in->left = ln; |
| 644 | in->right = fn; |
| 645 | } |
| 646 | } else { /* plen <= bit */ |
| 647 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 648 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | * (new leaf node)[ln] |
| 650 | * / \ |
| 651 | * (old node)[fn] NULL |
| 652 | */ |
| 653 | |
| 654 | ln = node_alloc(); |
| 655 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 656 | if (!ln) |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 657 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | ln->fn_bit = plen; |
| 660 | |
| 661 | ln->parent = pn; |
| 662 | |
| 663 | ln->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | if (dir) |
| 666 | pn->right = ln; |
| 667 | else |
| 668 | pn->left = ln; |
| 669 | |
| 670 | if (addr_bit_set(&key->addr, plen)) |
| 671 | ln->right = fn; |
| 672 | else |
| 673 | ln->left = fn; |
| 674 | |
| 675 | fn->parent = ln; |
| 676 | } |
| 677 | return ln; |
| 678 | } |
| 679 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 680 | static bool rt6_qualify_for_ecmp(struct rt6_info *rt) |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 681 | { |
| 682 | return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) == |
| 683 | RTF_GATEWAY; |
| 684 | } |
| 685 | |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 686 | static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc) |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 687 | { |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 688 | int i; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 689 | |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 690 | for (i = 0; i < RTAX_MAX; i++) { |
| 691 | if (test_bit(i, mxc->mx_valid)) |
| 692 | mp[i] = mxc->mx[i]; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 693 | } |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc) |
| 697 | { |
| 698 | if (!mxc->mx) |
| 699 | return 0; |
| 700 | |
| 701 | if (dst->flags & DST_HOST) { |
| 702 | u32 *mp = dst_metrics_write_ptr(dst); |
| 703 | |
| 704 | if (unlikely(!mp)) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | fib6_copy_metrics(mp, mxc); |
| 708 | } else { |
| 709 | dst_init_metrics(dst, mxc->mx, false); |
| 710 | |
| 711 | /* We've stolen mx now. */ |
| 712 | mxc->mx = NULL; |
| 713 | } |
| 714 | |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Hannes Frederic Sowa | 6e9e16e6 | 2015-01-26 15:11:17 +0100 | [diff] [blame] | 718 | static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn, |
| 719 | struct net *net) |
| 720 | { |
| 721 | if (atomic_read(&rt->rt6i_ref) != 1) { |
| 722 | /* This route is used as dummy address holder in some split |
| 723 | * nodes. It is not leaked, but it still holds other resources, |
| 724 | * which must be released in time. So, scan ascendant nodes |
| 725 | * and replace dummy references to this route with references |
| 726 | * to still alive ones. |
| 727 | */ |
| 728 | while (fn) { |
| 729 | if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) { |
| 730 | fn->leaf = fib6_find_prefix(net, fn); |
| 731 | atomic_inc(&fn->leaf->rt6i_ref); |
| 732 | rt6_release(rt); |
| 733 | } |
| 734 | fn = fn->parent; |
| 735 | } |
| 736 | /* No more references are possible at this point. */ |
| 737 | BUG_ON(atomic_read(&rt->rt6i_ref) != 1); |
| 738 | } |
| 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* |
| 742 | * Insert routing information in a node. |
| 743 | */ |
| 744 | |
| 745 | static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 746 | struct nl_info *info, struct mx6_config *mxc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
| 748 | struct rt6_info *iter = NULL; |
| 749 | struct rt6_info **ins; |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 750 | struct rt6_info **fallback_ins = NULL; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 751 | int replace = (info->nlh && |
| 752 | (info->nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 753 | int add = (!info->nlh || |
| 754 | (info->nlh->nlmsg_flags & NLM_F_CREATE)); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 755 | int found = 0; |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 756 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); |
Guillaume Nault | 73483c1 | 2016-09-07 17:21:40 +0200 | [diff] [blame] | 757 | u16 nlflags = NLM_F_EXCL; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 758 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
David Ahern | 1f5e29c | 2017-01-31 16:51:37 -0800 | [diff] [blame] | 760 | if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND)) |
| 761 | nlflags |= NLM_F_APPEND; |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | ins = &fn->leaf; |
| 764 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 765 | for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Search for duplicates |
| 768 | */ |
| 769 | |
| 770 | if (iter->rt6i_metric == rt->rt6i_metric) { |
| 771 | /* |
| 772 | * Same priority level |
| 773 | */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 774 | if (info->nlh && |
| 775 | (info->nlh->nlmsg_flags & NLM_F_EXCL)) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 776 | return -EEXIST; |
Guillaume Nault | 73483c1 | 2016-09-07 17:21:40 +0200 | [diff] [blame] | 777 | |
| 778 | nlflags &= ~NLM_F_EXCL; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 779 | if (replace) { |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 780 | if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) { |
| 781 | found++; |
| 782 | break; |
| 783 | } |
| 784 | if (rt_can_ecmp) |
| 785 | fallback_ins = fallback_ins ?: ins; |
| 786 | goto next_iter; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 789 | if (iter->dst.dev == rt->dst.dev && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | iter->rt6i_idev == rt->rt6i_idev && |
| 791 | ipv6_addr_equal(&iter->rt6i_gateway, |
| 792 | &rt->rt6i_gateway)) { |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 793 | if (rt->rt6i_nsiblings) |
| 794 | rt->rt6i_nsiblings = 0; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 795 | if (!(iter->rt6i_flags & RTF_EXPIRES)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | return -EEXIST; |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 797 | if (!(rt->rt6i_flags & RTF_EXPIRES)) |
| 798 | rt6_clean_expires(iter); |
| 799 | else |
| 800 | rt6_set_expires(iter, rt->dst.expires); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 801 | iter->rt6i_pmtu = rt->rt6i_pmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return -EEXIST; |
| 803 | } |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 804 | /* If we have the same destination and the same metric, |
| 805 | * but not the same gateway, then the route we try to |
| 806 | * add is sibling to this route, increment our counter |
| 807 | * of siblings, and later we will add our route to the |
| 808 | * list. |
| 809 | * Only static routes (which don't have flag |
| 810 | * RTF_EXPIRES) are used for ECMPv6. |
| 811 | * |
| 812 | * To avoid long list, we only had siblings if the |
| 813 | * route have a gateway. |
| 814 | */ |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 815 | if (rt_can_ecmp && |
| 816 | rt6_qualify_for_ecmp(iter)) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 817 | rt->rt6i_nsiblings++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if (iter->rt6i_metric > rt->rt6i_metric) |
| 821 | break; |
| 822 | |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 823 | next_iter: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 824 | ins = &iter->dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 827 | if (fallback_ins && !found) { |
| 828 | /* No ECMP-able route found, replace first non-ECMP one */ |
| 829 | ins = fallback_ins; |
| 830 | iter = *ins; |
| 831 | found++; |
| 832 | } |
| 833 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 834 | /* Reset round-robin state, if necessary */ |
| 835 | if (ins == &fn->leaf) |
| 836 | fn->rr_ptr = NULL; |
| 837 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 838 | /* Link this route to others same route. */ |
| 839 | if (rt->rt6i_nsiblings) { |
| 840 | unsigned int rt6i_nsiblings; |
| 841 | struct rt6_info *sibling, *temp_sibling; |
| 842 | |
| 843 | /* Find the first route that have the same metric */ |
| 844 | sibling = fn->leaf; |
| 845 | while (sibling) { |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 846 | if (sibling->rt6i_metric == rt->rt6i_metric && |
| 847 | rt6_qualify_for_ecmp(sibling)) { |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 848 | list_add_tail(&rt->rt6i_siblings, |
| 849 | &sibling->rt6i_siblings); |
| 850 | break; |
| 851 | } |
| 852 | sibling = sibling->dst.rt6_next; |
| 853 | } |
| 854 | /* For each sibling in the list, increment the counter of |
| 855 | * siblings. BUG() if counters does not match, list of siblings |
| 856 | * is broken! |
| 857 | */ |
| 858 | rt6i_nsiblings = 0; |
| 859 | list_for_each_entry_safe(sibling, temp_sibling, |
| 860 | &rt->rt6i_siblings, rt6i_siblings) { |
| 861 | sibling->rt6i_nsiblings++; |
| 862 | BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings); |
| 863 | rt6i_nsiblings++; |
| 864 | } |
| 865 | BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings); |
| 866 | } |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | /* |
| 869 | * insert node |
| 870 | */ |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 871 | if (!replace) { |
| 872 | if (!add) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 873 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 875 | add: |
Guillaume Nault | 73483c1 | 2016-09-07 17:21:40 +0200 | [diff] [blame] | 876 | nlflags |= NLM_F_CREATE; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 877 | err = fib6_commit_metrics(&rt->dst, mxc); |
| 878 | if (err) |
| 879 | return err; |
| 880 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 881 | rt->dst.rt6_next = iter; |
| 882 | *ins = rt; |
| 883 | rt->rt6i_node = fn; |
| 884 | atomic_inc(&rt->rt6i_ref); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 885 | if (!info->skip_notify) |
| 886 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 887 | info->nl_net->ipv6.rt6_stats->fib_rt_entries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 889 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 890 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
| 891 | fn->fn_flags |= RTN_RTINFO; |
| 892 | } |
| 893 | |
| 894 | } else { |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 895 | int nsiblings; |
| 896 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 897 | if (!found) { |
| 898 | if (add) |
| 899 | goto add; |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 900 | pr_warn("NLM_F_REPLACE set, but no existing node found!\n"); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 901 | return -ENOENT; |
| 902 | } |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 903 | |
| 904 | err = fib6_commit_metrics(&rt->dst, mxc); |
| 905 | if (err) |
| 906 | return err; |
| 907 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 908 | *ins = rt; |
| 909 | rt->rt6i_node = fn; |
| 910 | rt->dst.rt6_next = iter->dst.rt6_next; |
| 911 | atomic_inc(&rt->rt6i_ref); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 912 | if (!info->skip_notify) |
| 913 | inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 914 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 915 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
| 916 | fn->fn_flags |= RTN_RTINFO; |
| 917 | } |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 918 | nsiblings = iter->rt6i_nsiblings; |
Hannes Frederic Sowa | 6e9e16e6 | 2015-01-26 15:11:17 +0100 | [diff] [blame] | 919 | fib6_purge_rt(iter, fn, info->nl_net); |
| 920 | rt6_release(iter); |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 921 | |
| 922 | if (nsiblings) { |
| 923 | /* Replacing an ECMP route, remove all siblings */ |
| 924 | ins = &rt->dst.rt6_next; |
| 925 | iter = *ins; |
| 926 | while (iter) { |
Sabrina Dubroca | 67e1940 | 2017-03-13 13:28:09 +0100 | [diff] [blame] | 927 | if (iter->rt6i_metric > rt->rt6i_metric) |
| 928 | break; |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 929 | if (rt6_qualify_for_ecmp(iter)) { |
| 930 | *ins = iter->dst.rt6_next; |
| 931 | fib6_purge_rt(iter, fn, info->nl_net); |
| 932 | rt6_release(iter); |
| 933 | nsiblings--; |
| 934 | } else { |
| 935 | ins = &iter->dst.rt6_next; |
| 936 | } |
| 937 | iter = *ins; |
| 938 | } |
| 939 | WARN_ON(nsiblings != 0); |
| 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 946 | static void fib6_start_gc(struct net *net, struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 948 | if (!timer_pending(&net->ipv6.ip6_fib_timer) && |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 949 | (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE))) |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 950 | mod_timer(&net->ipv6.ip6_fib_timer, |
Stephen Hemminger | 847499c | 2008-07-21 13:21:35 -0700 | [diff] [blame] | 951 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 954 | void fib6_force_start_gc(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 956 | if (!timer_pending(&net->ipv6.ip6_fib_timer)) |
| 957 | mod_timer(&net->ipv6.ip6_fib_timer, |
Stephen Hemminger | 847499c | 2008-07-21 13:21:35 -0700 | [diff] [blame] | 958 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Add routing information to the routing tree. |
| 963 | * <destination addr>/<source addr> |
| 964 | * with source addr info in sub-trees |
| 965 | */ |
| 966 | |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 967 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame^] | 968 | struct nl_info *info, struct mx6_config *mxc, |
| 969 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | { |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 971 | struct fib6_node *fn, *pn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int err = -ENOMEM; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 973 | int allow_create = 1; |
| 974 | int replace_required = 0; |
Hannes Frederic Sowa | 812918c | 2014-10-06 19:58:37 +0200 | [diff] [blame] | 975 | int sernum = fib6_new_sernum(info->nl_net); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 976 | |
Martin KaFai Lau | 8e3d5be | 2015-09-15 14:30:08 -0700 | [diff] [blame] | 977 | if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) && |
| 978 | !atomic_read(&rt->dst.__refcnt))) |
| 979 | return -EINVAL; |
| 980 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 981 | if (info->nlh) { |
| 982 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 983 | allow_create = 0; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 984 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 985 | replace_required = 1; |
| 986 | } |
| 987 | if (!allow_create && !replace_required) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 988 | pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 990 | fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen, |
| 991 | offsetof(struct rt6_info, rt6i_dst), allow_create, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame^] | 992 | replace_required, sernum, extack); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 993 | if (IS_ERR(fn)) { |
| 994 | err = PTR_ERR(fn); |
Daniel Borkmann | ae7b4e1 | 2013-09-07 15:13:20 +0200 | [diff] [blame] | 995 | fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | goto out; |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 999 | pn = fn; |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | #ifdef CONFIG_IPV6_SUBTREES |
| 1002 | if (rt->rt6i_src.plen) { |
| 1003 | struct fib6_node *sn; |
| 1004 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1005 | if (!fn->subtree) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | struct fib6_node *sfn; |
| 1007 | |
| 1008 | /* |
| 1009 | * Create subtree. |
| 1010 | * |
| 1011 | * fn[main tree] |
| 1012 | * | |
| 1013 | * sfn[subtree root] |
| 1014 | * \ |
| 1015 | * sn[new leaf node] |
| 1016 | */ |
| 1017 | |
| 1018 | /* Create subtree root node */ |
| 1019 | sfn = node_alloc(); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1020 | if (!sfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | goto st_failure; |
| 1022 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1023 | sfn->leaf = info->nl_net->ipv6.ip6_null_entry; |
| 1024 | atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | sfn->fn_flags = RTN_ROOT; |
Hannes Frederic Sowa | c8c4d42 | 2014-10-06 19:58:36 +0200 | [diff] [blame] | 1026 | sfn->fn_sernum = sernum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* Now add the first leaf node to new subtree */ |
| 1029 | |
| 1030 | sn = fib6_add_1(sfn, &rt->rt6i_src.addr, |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 1031 | rt->rt6i_src.plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 1032 | offsetof(struct rt6_info, rt6i_src), |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame^] | 1033 | allow_create, replace_required, sernum, |
| 1034 | extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
Wei Yongjun | f950c0e | 2012-09-20 18:29:56 +0000 | [diff] [blame] | 1036 | if (IS_ERR(sn)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | /* If it is failed, discard just allocated |
| 1038 | root, and then (in st_failure) stale node |
| 1039 | in main tree. |
| 1040 | */ |
| 1041 | node_free(sfn); |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 1042 | err = PTR_ERR(sn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | goto st_failure; |
| 1044 | } |
| 1045 | |
| 1046 | /* Now link new subtree to main tree */ |
| 1047 | sfn->parent = fn; |
| 1048 | fn->subtree = sfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } else { |
| 1050 | sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 1051 | rt->rt6i_src.plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 1052 | offsetof(struct rt6_info, rt6i_src), |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame^] | 1053 | allow_create, replace_required, sernum, |
| 1054 | extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 1056 | if (IS_ERR(sn)) { |
| 1057 | err = PTR_ERR(sn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | goto st_failure; |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1062 | if (!fn->leaf) { |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1063 | fn->leaf = rt; |
| 1064 | atomic_inc(&rt->rt6i_ref); |
| 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | fn = sn; |
| 1067 | } |
| 1068 | #endif |
| 1069 | |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1070 | err = fib6_add_rt2node(fn, rt, info, mxc); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1071 | if (!err) { |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1072 | fib6_start_gc(info->nl_net, rt); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1073 | if (!(rt->rt6i_flags & RTF_CACHE)) |
Duan Jiong | 163cd4e | 2014-05-09 13:31:43 +0800 | [diff] [blame] | 1074 | fib6_prune_clones(info->nl_net, pn); |
Martin KaFai Lau | 8e3d5be | 2015-09-15 14:30:08 -0700 | [diff] [blame] | 1075 | rt->dst.flags &= ~DST_NOCACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | out: |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1079 | if (err) { |
| 1080 | #ifdef CONFIG_IPV6_SUBTREES |
| 1081 | /* |
| 1082 | * If fib6_add_1 has cleared the old leaf pointer in the |
| 1083 | * super-tree leaf node we have to find a new one for it. |
| 1084 | */ |
David S. Miller | 3c05123 | 2008-04-18 01:46:19 -0700 | [diff] [blame] | 1085 | if (pn != fn && pn->leaf == rt) { |
| 1086 | pn->leaf = NULL; |
| 1087 | atomic_dec(&rt->rt6i_ref); |
| 1088 | } |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1089 | if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1090 | pn->leaf = fib6_find_prefix(info->nl_net, pn); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1091 | #if RT6_DEBUG >= 2 |
| 1092 | if (!pn->leaf) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1093 | WARN_ON(pn->leaf == NULL); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1094 | pn->leaf = info->nl_net->ipv6.ip6_null_entry; |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1095 | } |
| 1096 | #endif |
| 1097 | atomic_inc(&pn->leaf->rt6i_ref); |
| 1098 | } |
| 1099 | #endif |
Martin KaFai Lau | 8e3d5be | 2015-09-15 14:30:08 -0700 | [diff] [blame] | 1100 | if (!(rt->dst.flags & DST_NOCACHE)) |
| 1101 | dst_free(&rt->dst); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 1102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return err; |
| 1104 | |
| 1105 | #ifdef CONFIG_IPV6_SUBTREES |
| 1106 | /* Subtree creation failed, probably main tree node |
| 1107 | is orphan. If it is, shoot it. |
| 1108 | */ |
| 1109 | st_failure: |
| 1110 | if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT))) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1111 | fib6_repair_tree(info->nl_net, fn); |
Martin KaFai Lau | 8e3d5be | 2015-09-15 14:30:08 -0700 | [diff] [blame] | 1112 | if (!(rt->dst.flags & DST_NOCACHE)) |
| 1113 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | return err; |
| 1115 | #endif |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * Routing tree lookup |
| 1120 | * |
| 1121 | */ |
| 1122 | |
| 1123 | struct lookup_args { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1124 | int offset; /* key offset on rt6_info */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1125 | const struct in6_addr *addr; /* search key */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | }; |
| 1127 | |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 1128 | static struct fib6_node *fib6_lookup_1(struct fib6_node *root, |
| 1129 | struct lookup_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
| 1131 | struct fib6_node *fn; |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 1132 | __be32 dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1134 | if (unlikely(args->offset == 0)) |
| 1135 | return NULL; |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /* |
| 1138 | * Descend on a tree |
| 1139 | */ |
| 1140 | |
| 1141 | fn = root; |
| 1142 | |
| 1143 | for (;;) { |
| 1144 | struct fib6_node *next; |
| 1145 | |
| 1146 | dir = addr_bit_set(args->addr, fn->fn_bit); |
| 1147 | |
| 1148 | next = dir ? fn->right : fn->left; |
| 1149 | |
| 1150 | if (next) { |
| 1151 | fn = next; |
| 1152 | continue; |
| 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | } |
| 1156 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1157 | while (fn) { |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1158 | if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | struct rt6key *key; |
| 1160 | |
| 1161 | key = (struct rt6key *) ((u8 *) fn->leaf + |
| 1162 | args->offset); |
| 1163 | |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1164 | if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { |
| 1165 | #ifdef CONFIG_IPV6_SUBTREES |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1166 | if (fn->subtree) { |
| 1167 | struct fib6_node *sfn; |
| 1168 | sfn = fib6_lookup_1(fn->subtree, |
| 1169 | args + 1); |
| 1170 | if (!sfn) |
| 1171 | goto backtrack; |
| 1172 | fn = sfn; |
| 1173 | } |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1174 | #endif |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1175 | if (fn->fn_flags & RTN_RTINFO) |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1176 | return fn; |
| 1177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1179 | #ifdef CONFIG_IPV6_SUBTREES |
| 1180 | backtrack: |
| 1181 | #endif |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1182 | if (fn->fn_flags & RTN_ROOT) |
| 1183 | break; |
| 1184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | fn = fn->parent; |
| 1186 | } |
| 1187 | |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 1191 | struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr, |
| 1192 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | struct fib6_node *fn; |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1195 | struct lookup_args args[] = { |
| 1196 | { |
| 1197 | .offset = offsetof(struct rt6_info, rt6i_dst), |
| 1198 | .addr = daddr, |
| 1199 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1201 | { |
| 1202 | .offset = offsetof(struct rt6_info, rt6i_src), |
| 1203 | .addr = saddr, |
| 1204 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | #endif |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1206 | { |
| 1207 | .offset = 0, /* sentinel */ |
| 1208 | } |
| 1209 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
YOSHIFUJI Hideaki | fefc2a6 | 2006-08-23 17:21:50 -0700 | [diff] [blame] | 1211 | fn = fib6_lookup_1(root, daddr ? args : args + 1); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1212 | if (!fn || fn->fn_flags & RTN_TL_ROOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | fn = root; |
| 1214 | |
| 1215 | return fn; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Get node with specified destination prefix (and source prefix, |
| 1220 | * if subtrees are used) |
| 1221 | */ |
| 1222 | |
| 1223 | |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 1224 | static struct fib6_node *fib6_locate_1(struct fib6_node *root, |
| 1225 | const struct in6_addr *addr, |
| 1226 | int plen, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | { |
| 1228 | struct fib6_node *fn; |
| 1229 | |
| 1230 | for (fn = root; fn ; ) { |
| 1231 | struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 1232 | |
| 1233 | /* |
| 1234 | * Prefix match |
| 1235 | */ |
| 1236 | if (plen < fn->fn_bit || |
| 1237 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) |
| 1238 | return NULL; |
| 1239 | |
| 1240 | if (plen == fn->fn_bit) |
| 1241 | return fn; |
| 1242 | |
| 1243 | /* |
| 1244 | * We have more bits to go |
| 1245 | */ |
| 1246 | if (addr_bit_set(addr, fn->fn_bit)) |
| 1247 | fn = fn->right; |
| 1248 | else |
| 1249 | fn = fn->left; |
| 1250 | } |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | |
Wang Yufen | 437de07 | 2014-03-28 12:07:04 +0800 | [diff] [blame] | 1254 | struct fib6_node *fib6_locate(struct fib6_node *root, |
| 1255 | const struct in6_addr *daddr, int dst_len, |
| 1256 | const struct in6_addr *saddr, int src_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | { |
| 1258 | struct fib6_node *fn; |
| 1259 | |
| 1260 | fn = fib6_locate_1(root, daddr, dst_len, |
| 1261 | offsetof(struct rt6_info, rt6i_dst)); |
| 1262 | |
| 1263 | #ifdef CONFIG_IPV6_SUBTREES |
| 1264 | if (src_len) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1265 | WARN_ON(saddr == NULL); |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1266 | if (fn && fn->subtree) |
| 1267 | fn = fib6_locate_1(fn->subtree, saddr, src_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | offsetof(struct rt6_info, rt6i_src)); |
| 1269 | } |
| 1270 | #endif |
| 1271 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1272 | if (fn && fn->fn_flags & RTN_RTINFO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | return fn; |
| 1274 | |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | /* |
| 1280 | * Deletion |
| 1281 | * |
| 1282 | */ |
| 1283 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1284 | static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1286 | if (fn->fn_flags & RTN_ROOT) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1287 | return net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1289 | while (fn) { |
| 1290 | if (fn->left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | return fn->left->leaf; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1292 | if (fn->right) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | return fn->right->leaf; |
| 1294 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1295 | fn = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | } |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Called to trim the tree of intermediate nodes when possible. "fn" |
| 1302 | * is the node we want to try and remove. |
| 1303 | */ |
| 1304 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1305 | static struct fib6_node *fib6_repair_tree(struct net *net, |
| 1306 | struct fib6_node *fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | { |
| 1308 | int children; |
| 1309 | int nstate; |
| 1310 | struct fib6_node *child, *pn; |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1311 | struct fib6_walker *w; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | int iter = 0; |
| 1313 | |
| 1314 | for (;;) { |
| 1315 | RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); |
| 1316 | iter++; |
| 1317 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1318 | WARN_ON(fn->fn_flags & RTN_RTINFO); |
| 1319 | WARN_ON(fn->fn_flags & RTN_TL_ROOT); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1320 | WARN_ON(fn->leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | children = 0; |
| 1323 | child = NULL; |
Wang Yufen | 49e253e | 2014-03-28 12:07:03 +0800 | [diff] [blame] | 1324 | if (fn->right) |
| 1325 | child = fn->right, children |= 1; |
| 1326 | if (fn->left) |
| 1327 | child = fn->left, children |= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1329 | if (children == 3 || FIB6_SUBTREE(fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | #ifdef CONFIG_IPV6_SUBTREES |
| 1331 | /* Subtree root (i.e. fn) may have one child */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1332 | || (children && fn->fn_flags & RTN_ROOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | #endif |
| 1334 | ) { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1335 | fn->leaf = fib6_find_prefix(net, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | #if RT6_DEBUG >= 2 |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1337 | if (!fn->leaf) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1338 | WARN_ON(!fn->leaf); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1339 | fn->leaf = net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | } |
| 1341 | #endif |
| 1342 | atomic_inc(&fn->leaf->rt6i_ref); |
| 1343 | return fn->parent; |
| 1344 | } |
| 1345 | |
| 1346 | pn = fn->parent; |
| 1347 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1348 | if (FIB6_SUBTREE(pn) == fn) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1349 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1350 | FIB6_SUBTREE(pn) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | nstate = FWS_L; |
| 1352 | } else { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1353 | WARN_ON(fn->fn_flags & RTN_ROOT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | #endif |
Wang Yufen | 49e253e | 2014-03-28 12:07:03 +0800 | [diff] [blame] | 1355 | if (pn->right == fn) |
| 1356 | pn->right = child; |
| 1357 | else if (pn->left == fn) |
| 1358 | pn->left = child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | #if RT6_DEBUG >= 2 |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1360 | else |
| 1361 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | #endif |
| 1363 | if (child) |
| 1364 | child->parent = pn; |
| 1365 | nstate = FWS_R; |
| 1366 | #ifdef CONFIG_IPV6_SUBTREES |
| 1367 | } |
| 1368 | #endif |
| 1369 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1370 | read_lock(&net->ipv6.fib6_walker_lock); |
| 1371 | FOR_WALKERS(net, w) { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1372 | if (!child) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | if (w->root == fn) { |
| 1374 | w->root = w->node = NULL; |
| 1375 | RT6_TRACE("W %p adjusted by delroot 1\n", w); |
| 1376 | } else if (w->node == fn) { |
| 1377 | RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); |
| 1378 | w->node = pn; |
| 1379 | w->state = nstate; |
| 1380 | } |
| 1381 | } else { |
| 1382 | if (w->root == fn) { |
| 1383 | w->root = child; |
| 1384 | RT6_TRACE("W %p adjusted by delroot 2\n", w); |
| 1385 | } |
| 1386 | if (w->node == fn) { |
| 1387 | w->node = child; |
| 1388 | if (children&2) { |
| 1389 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 1390 | w->state = w->state >= FWS_R ? FWS_U : FWS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } else { |
| 1392 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 1393 | w->state = w->state >= FWS_C ? FWS_U : FWS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | } |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1398 | read_unlock(&net->ipv6.fib6_walker_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | node_free(fn); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1401 | if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | return pn; |
| 1403 | |
| 1404 | rt6_release(pn->leaf); |
| 1405 | pn->leaf = NULL; |
| 1406 | fn = pn; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1411 | struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | { |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1413 | struct fib6_walker *w; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | struct rt6_info *rt = *rtp; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1415 | struct net *net = info->nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | RT6_TRACE("fib6_del_route\n"); |
| 1418 | |
| 1419 | /* Unlink it */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1420 | *rtp = rt->dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | rt->rt6i_node = NULL; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1422 | net->ipv6.rt6_stats->fib_rt_entries--; |
| 1423 | net->ipv6.rt6_stats->fib_discarded_routes++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 1425 | /* Reset round-robin state, if necessary */ |
| 1426 | if (fn->rr_ptr == rt) |
| 1427 | fn->rr_ptr = NULL; |
| 1428 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 1429 | /* Remove this entry from other siblings */ |
| 1430 | if (rt->rt6i_nsiblings) { |
| 1431 | struct rt6_info *sibling, *next_sibling; |
| 1432 | |
| 1433 | list_for_each_entry_safe(sibling, next_sibling, |
| 1434 | &rt->rt6i_siblings, rt6i_siblings) |
| 1435 | sibling->rt6i_nsiblings--; |
| 1436 | rt->rt6i_nsiblings = 0; |
| 1437 | list_del_init(&rt->rt6i_siblings); |
| 1438 | } |
| 1439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | /* Adjust walkers */ |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1441 | read_lock(&net->ipv6.fib6_walker_lock); |
| 1442 | FOR_WALKERS(net, w) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | if (w->state == FWS_C && w->leaf == rt) { |
| 1444 | RT6_TRACE("walker %p adjusted by delroute\n", w); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1445 | w->leaf = rt->dst.rt6_next; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1446 | if (!w->leaf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | w->state = FWS_U; |
| 1448 | } |
| 1449 | } |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1450 | read_unlock(&net->ipv6.fib6_walker_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1452 | rt->dst.rt6_next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | /* If it was last route, expunge its radix tree node */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1455 | if (!fn->leaf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | fn->fn_flags &= ~RTN_RTINFO; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1457 | net->ipv6.rt6_stats->fib_route_nodes--; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1458 | fn = fib6_repair_tree(net, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Hannes Frederic Sowa | 6e9e16e6 | 2015-01-26 15:11:17 +0100 | [diff] [blame] | 1461 | fib6_purge_rt(rt, fn, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 1463 | if (!info->skip_notify) |
| 1464 | inet6_rt_notify(RTM_DELROUTE, rt, info, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | rt6_release(rt); |
| 1466 | } |
| 1467 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1468 | int fib6_del(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1470 | struct net *net = info->nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | struct fib6_node *fn = rt->rt6i_node; |
| 1472 | struct rt6_info **rtp; |
| 1473 | |
| 1474 | #if RT6_DEBUG >= 2 |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 1475 | if (rt->dst.obsolete > 0) { |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1476 | WARN_ON(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | return -ENOENT; |
| 1478 | } |
| 1479 | #endif |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1480 | if (!fn || rt == net->ipv6.ip6_null_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | return -ENOENT; |
| 1482 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1483 | WARN_ON(!(fn->fn_flags & RTN_RTINFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1485 | if (!(rt->rt6i_flags & RTF_CACHE)) { |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1486 | struct fib6_node *pn = fn; |
| 1487 | #ifdef CONFIG_IPV6_SUBTREES |
| 1488 | /* clones of this route might be in another subtree */ |
| 1489 | if (rt->rt6i_src.plen) { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1490 | while (!(pn->fn_flags & RTN_ROOT)) |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1491 | pn = pn->parent; |
| 1492 | pn = pn->parent; |
| 1493 | } |
| 1494 | #endif |
Duan Jiong | 163cd4e | 2014-05-09 13:31:43 +0800 | [diff] [blame] | 1495 | fib6_prune_clones(info->nl_net, pn); |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
| 1498 | /* |
| 1499 | * Walk the leaf entries looking for ourself |
| 1500 | */ |
| 1501 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1502 | for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (*rtp == rt) { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1504 | fib6_del_route(fn, rtp, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | return 0; |
| 1506 | } |
| 1507 | } |
| 1508 | return -ENOENT; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Tree traversal function. |
| 1513 | * |
| 1514 | * Certainly, it is not interrupt safe. |
| 1515 | * However, it is internally reenterable wrt itself and fib6_add/fib6_del. |
| 1516 | * It means, that we can modify tree during walking |
| 1517 | * and use this function for garbage collection, clone pruning, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1518 | * cleaning tree when a device goes down etc. etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | * |
| 1520 | * It guarantees that every node will be traversed, |
| 1521 | * and that it will be traversed only once. |
| 1522 | * |
| 1523 | * Callback function w->func may return: |
| 1524 | * 0 -> continue walking. |
| 1525 | * positive value -> walking is suspended (used by tree dumps, |
| 1526 | * and probably by gc, if it will be split to several slices) |
| 1527 | * negative value -> terminate walking. |
| 1528 | * |
| 1529 | * The function itself returns: |
| 1530 | * 0 -> walk is complete. |
| 1531 | * >0 -> walk is incomplete (i.e. suspended) |
| 1532 | * <0 -> walk is terminated by an error. |
| 1533 | */ |
| 1534 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1535 | static int fib6_walk_continue(struct fib6_walker *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | { |
| 1537 | struct fib6_node *fn, *pn; |
| 1538 | |
| 1539 | for (;;) { |
| 1540 | fn = w->node; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1541 | if (!fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return 0; |
| 1543 | |
| 1544 | if (w->prune && fn != w->root && |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1545 | fn->fn_flags & RTN_RTINFO && w->state < FWS_C) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | w->state = FWS_C; |
| 1547 | w->leaf = fn->leaf; |
| 1548 | } |
| 1549 | switch (w->state) { |
| 1550 | #ifdef CONFIG_IPV6_SUBTREES |
| 1551 | case FWS_S: |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1552 | if (FIB6_SUBTREE(fn)) { |
| 1553 | w->node = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | continue; |
| 1555 | } |
| 1556 | w->state = FWS_L; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1557 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | case FWS_L: |
| 1559 | if (fn->left) { |
| 1560 | w->node = fn->left; |
| 1561 | w->state = FWS_INIT; |
| 1562 | continue; |
| 1563 | } |
| 1564 | w->state = FWS_R; |
| 1565 | case FWS_R: |
| 1566 | if (fn->right) { |
| 1567 | w->node = fn->right; |
| 1568 | w->state = FWS_INIT; |
| 1569 | continue; |
| 1570 | } |
| 1571 | w->state = FWS_C; |
| 1572 | w->leaf = fn->leaf; |
| 1573 | case FWS_C: |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1574 | if (w->leaf && fn->fn_flags & RTN_RTINFO) { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1575 | int err; |
| 1576 | |
Eric Dumazet | fa809e2 | 2012-06-25 15:37:19 -0700 | [diff] [blame] | 1577 | if (w->skip) { |
| 1578 | w->skip--; |
Kumar Sundararajan | 1c26585 | 2014-04-24 09:48:53 -0400 | [diff] [blame] | 1579 | goto skip; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | err = w->func(w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (err) |
| 1584 | return err; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1585 | |
| 1586 | w->count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | continue; |
| 1588 | } |
Kumar Sundararajan | 1c26585 | 2014-04-24 09:48:53 -0400 | [diff] [blame] | 1589 | skip: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | w->state = FWS_U; |
| 1591 | case FWS_U: |
| 1592 | if (fn == w->root) |
| 1593 | return 0; |
| 1594 | pn = fn->parent; |
| 1595 | w->node = pn; |
| 1596 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1597 | if (FIB6_SUBTREE(pn) == fn) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1598 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | w->state = FWS_L; |
| 1600 | continue; |
| 1601 | } |
| 1602 | #endif |
| 1603 | if (pn->left == fn) { |
| 1604 | w->state = FWS_R; |
| 1605 | continue; |
| 1606 | } |
| 1607 | if (pn->right == fn) { |
| 1608 | w->state = FWS_C; |
| 1609 | w->leaf = w->node->leaf; |
| 1610 | continue; |
| 1611 | } |
| 1612 | #if RT6_DEBUG >= 2 |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1613 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | #endif |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1619 | static int fib6_walk(struct net *net, struct fib6_walker *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | { |
| 1621 | int res; |
| 1622 | |
| 1623 | w->state = FWS_INIT; |
| 1624 | w->node = w->root; |
| 1625 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1626 | fib6_walker_link(net, w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | res = fib6_walk_continue(w); |
| 1628 | if (res <= 0) |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1629 | fib6_walker_unlink(net, w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | return res; |
| 1631 | } |
| 1632 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1633 | static int fib6_clean_node(struct fib6_walker *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | { |
| 1635 | int res; |
| 1636 | struct rt6_info *rt; |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1637 | struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w); |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1638 | struct nl_info info = { |
| 1639 | .nl_net = c->net, |
| 1640 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1642 | if (c->sernum != FIB6_NO_SERNUM_CHANGE && |
| 1643 | w->node->fn_sernum != c->sernum) |
| 1644 | w->node->fn_sernum = c->sernum; |
| 1645 | |
| 1646 | if (!c->func) { |
| 1647 | WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE); |
| 1648 | w->leaf = NULL; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1652 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | res = c->func(rt, c->arg); |
| 1654 | if (res < 0) { |
| 1655 | w->leaf = rt; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 1656 | res = fib6_del(rt, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | if (res) { |
| 1658 | #if RT6_DEBUG >= 2 |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 1659 | pr_debug("%s: del failed: rt=%p@%p err=%d\n", |
| 1660 | __func__, rt, rt->rt6i_node, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | #endif |
| 1662 | continue; |
| 1663 | } |
| 1664 | return 0; |
| 1665 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1666 | WARN_ON(res != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | } |
| 1668 | w->leaf = rt; |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * Convenient frontend to tree walker. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1674 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | * func is called on each route. |
| 1676 | * It may return -1 -> delete this route. |
| 1677 | * 0 -> continue walking |
| 1678 | * |
| 1679 | * prune==1 -> only immediate children of node (certainly, |
| 1680 | * ignoring pure split nodes) will be scanned. |
| 1681 | */ |
| 1682 | |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1683 | static void fib6_clean_tree(struct net *net, struct fib6_node *root, |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 1684 | int (*func)(struct rt6_info *, void *arg), |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1685 | bool prune, int sernum, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1687 | struct fib6_cleaner c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | |
| 1689 | c.w.root = root; |
| 1690 | c.w.func = fib6_clean_node; |
| 1691 | c.w.prune = prune; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1692 | c.w.count = 0; |
| 1693 | c.w.skip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | c.func = func; |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1695 | c.sernum = sernum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | c.arg = arg; |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1697 | c.net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1699 | fib6_walk(net, &c.w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1702 | static void __fib6_clean_all(struct net *net, |
| 1703 | int (*func)(struct rt6_info *, void *), |
| 1704 | int sernum, void *arg) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1705 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1706 | struct fib6_table *table; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1707 | struct hlist_head *head; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1708 | unsigned int h; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1709 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1710 | rcu_read_lock(); |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 1711 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 1712 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1713 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1714 | write_lock_bh(&table->tb6_lock); |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1715 | fib6_clean_tree(net, &table->tb6_root, |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1716 | func, false, sernum, arg); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1717 | write_unlock_bh(&table->tb6_lock); |
| 1718 | } |
| 1719 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1720 | rcu_read_unlock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1723 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *), |
| 1724 | void *arg) |
| 1725 | { |
| 1726 | __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg); |
| 1727 | } |
| 1728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | static int fib6_prune_clone(struct rt6_info *rt, void *arg) |
| 1730 | { |
| 1731 | if (rt->rt6i_flags & RTF_CACHE) { |
| 1732 | RT6_TRACE("pruning clone %p\n", rt); |
| 1733 | return -1; |
| 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Duan Jiong | 163cd4e | 2014-05-09 13:31:43 +0800 | [diff] [blame] | 1739 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | { |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1741 | fib6_clean_tree(net, fn, fib6_prune_clone, true, |
| 1742 | FIB6_NO_SERNUM_CHANGE, NULL); |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void fib6_flush_trees(struct net *net) |
| 1746 | { |
Hannes Frederic Sowa | 812918c | 2014-10-06 19:58:37 +0200 | [diff] [blame] | 1747 | int new_sernum = fib6_new_sernum(net); |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 1748 | |
Hannes Frederic Sowa | 327571c | 2014-10-06 19:58:38 +0200 | [diff] [blame] | 1749 | __fib6_clean_all(net, NULL, new_sernum, NULL); |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | /* |
| 1753 | * Garbage collection |
| 1754 | */ |
| 1755 | |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1756 | struct fib6_gc_args |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | { |
| 1758 | int timeout; |
| 1759 | int more; |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1760 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
| 1762 | static int fib6_age(struct rt6_info *rt, void *arg) |
| 1763 | { |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1764 | struct fib6_gc_args *gc_args = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | unsigned long now = jiffies; |
| 1766 | |
| 1767 | /* |
| 1768 | * check addrconf expiration here. |
| 1769 | * Routes are expired even if they are in use. |
| 1770 | * |
| 1771 | * Also age clones. Note, that clones are aged out |
| 1772 | * only if they are not in use now. |
| 1773 | */ |
| 1774 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 1775 | if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) { |
| 1776 | if (time_after(now, rt->dst.expires)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | RT6_TRACE("expiring %p\n", rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | return -1; |
| 1779 | } |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1780 | gc_args->more++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | } else if (rt->rt6i_flags & RTF_CACHE) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1782 | if (atomic_read(&rt->dst.__refcnt) == 0 && |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1783 | time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | RT6_TRACE("aging clone %p\n", rt); |
| 1785 | return -1; |
David S. Miller | 5339ab8 | 2012-01-27 15:14:01 -0800 | [diff] [blame] | 1786 | } else if (rt->rt6i_flags & RTF_GATEWAY) { |
| 1787 | struct neighbour *neigh; |
| 1788 | __u8 neigh_flags = 0; |
| 1789 | |
| 1790 | neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); |
| 1791 | if (neigh) { |
| 1792 | neigh_flags = neigh->flags; |
| 1793 | neigh_release(neigh); |
| 1794 | } |
Thomas Graf | 8bd7451 | 2012-06-07 06:51:04 +0000 | [diff] [blame] | 1795 | if (!(neigh_flags & NTF_ROUTER)) { |
David S. Miller | 5339ab8 | 2012-01-27 15:14:01 -0800 | [diff] [blame] | 1796 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1797 | rt); |
| 1798 | return -1; |
| 1799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1801 | gc_args->more++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1807 | void fib6_run_gc(unsigned long expires, struct net *net, bool force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | { |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1809 | struct fib6_gc_args gc_args; |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1810 | unsigned long now; |
| 1811 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1812 | if (force) { |
Michal Kubeček | 3dc94f9 | 2016-03-08 14:44:45 +0100 | [diff] [blame] | 1813 | spin_lock_bh(&net->ipv6.fib6_gc_lock); |
| 1814 | } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) { |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1815 | mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); |
| 1816 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1818 | gc_args.timeout = expires ? (int)expires : |
| 1819 | net->ipv6.sysctl.ip6_rt_gc_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1821 | gc_args.more = icmp6_dst_gc(); |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 1822 | |
Michal Kubeček | 3570df9 | 2016-03-08 14:44:25 +0100 | [diff] [blame] | 1823 | fib6_clean_all(net, fib6_age, &gc_args); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1824 | now = jiffies; |
| 1825 | net->ipv6.ip6_rt_last_gc = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
| 1827 | if (gc_args.more) |
Stephen Hemminger | c8a4522 | 2008-07-22 14:34:09 -0700 | [diff] [blame] | 1828 | mod_timer(&net->ipv6.ip6_fib_timer, |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1829 | round_jiffies(now |
Stephen Hemminger | c8a4522 | 2008-07-22 14:34:09 -0700 | [diff] [blame] | 1830 | + net->ipv6.sysctl.ip6_rt_gc_interval)); |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1831 | else |
| 1832 | del_timer(&net->ipv6.ip6_fib_timer); |
Michal Kubeček | 3dc94f9 | 2016-03-08 14:44:45 +0100 | [diff] [blame] | 1833 | spin_unlock_bh(&net->ipv6.fib6_gc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1836 | static void fib6_gc_timer_cb(unsigned long arg) |
| 1837 | { |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1838 | fib6_run_gc(0, (struct net *)arg, true); |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1841 | static int __net_init fib6_net_init(struct net *net) |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1842 | { |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1843 | size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ; |
| 1844 | |
Michal Kubeček | 3dc94f9 | 2016-03-08 14:44:45 +0100 | [diff] [blame] | 1845 | spin_lock_init(&net->ipv6.fib6_gc_lock); |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 1846 | rwlock_init(&net->ipv6.fib6_walker_lock); |
| 1847 | INIT_LIST_HEAD(&net->ipv6.fib6_walkers); |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1848 | setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1849 | |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1850 | net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); |
| 1851 | if (!net->ipv6.rt6_stats) |
| 1852 | goto out_timer; |
| 1853 | |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1854 | /* Avoid false sharing : Use at least a full cache line */ |
| 1855 | size = max_t(size_t, size, L1_CACHE_BYTES); |
| 1856 | |
| 1857 | net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1858 | if (!net->ipv6.fib_table_hash) |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1859 | goto out_rt6_stats; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1860 | |
| 1861 | net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), |
| 1862 | GFP_KERNEL); |
| 1863 | if (!net->ipv6.fib6_main_tbl) |
| 1864 | goto out_fib_table_hash; |
| 1865 | |
| 1866 | net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1867 | net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1868 | net->ipv6.fib6_main_tbl->tb6_root.fn_flags = |
| 1869 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1870 | inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1871 | |
| 1872 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1873 | net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), |
| 1874 | GFP_KERNEL); |
| 1875 | if (!net->ipv6.fib6_local_tbl) |
| 1876 | goto out_fib6_main_tbl; |
| 1877 | net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1878 | net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1879 | net->ipv6.fib6_local_tbl->tb6_root.fn_flags = |
| 1880 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1881 | inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1882 | #endif |
| 1883 | fib6_tables_init(net); |
| 1884 | |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1885 | return 0; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1886 | |
| 1887 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1888 | out_fib6_main_tbl: |
| 1889 | kfree(net->ipv6.fib6_main_tbl); |
| 1890 | #endif |
| 1891 | out_fib_table_hash: |
| 1892 | kfree(net->ipv6.fib_table_hash); |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1893 | out_rt6_stats: |
| 1894 | kfree(net->ipv6.rt6_stats); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1895 | out_timer: |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1896 | return -ENOMEM; |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame] | 1897 | } |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1898 | |
| 1899 | static void fib6_net_exit(struct net *net) |
| 1900 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1901 | rt6_ifdown(net, NULL); |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1902 | del_timer_sync(&net->ipv6.ip6_fib_timer); |
| 1903 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1904 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1905 | inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1906 | kfree(net->ipv6.fib6_local_tbl); |
| 1907 | #endif |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1908 | inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1909 | kfree(net->ipv6.fib6_main_tbl); |
| 1910 | kfree(net->ipv6.fib_table_hash); |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1911 | kfree(net->ipv6.rt6_stats); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | static struct pernet_operations fib6_net_ops = { |
| 1915 | .init = fib6_net_init, |
| 1916 | .exit = fib6_net_exit, |
| 1917 | }; |
| 1918 | |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1919 | int __init fib6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 1921 | int ret = -ENOMEM; |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | fib6_node_kmem = kmem_cache_create("fib6_nodes", |
| 1924 | sizeof(struct fib6_node), |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1925 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1926 | NULL); |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1927 | if (!fib6_node_kmem) |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 1928 | goto out; |
| 1929 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1930 | ret = register_pernet_subsys(&fib6_net_ops); |
| 1931 | if (ret) |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1932 | goto out_kmem_cache_create; |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 1933 | |
| 1934 | ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib, |
| 1935 | NULL); |
| 1936 | if (ret) |
| 1937 | goto out_unregister_subsys; |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 1938 | |
| 1939 | __fib6_flush_trees = fib6_flush_trees; |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1940 | out: |
| 1941 | return ret; |
| 1942 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 1943 | out_unregister_subsys: |
| 1944 | unregister_pernet_subsys(&fib6_net_ops); |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1945 | out_kmem_cache_create: |
| 1946 | kmem_cache_destroy(fib6_node_kmem); |
| 1947 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | void fib6_gc_cleanup(void) |
| 1951 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1952 | unregister_pernet_subsys(&fib6_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | kmem_cache_destroy(fib6_node_kmem); |
| 1954 | } |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1955 | |
| 1956 | #ifdef CONFIG_PROC_FS |
| 1957 | |
| 1958 | struct ipv6_route_iter { |
| 1959 | struct seq_net_private p; |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1960 | struct fib6_walker w; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1961 | loff_t skip; |
| 1962 | struct fib6_table *tbl; |
Hannes Frederic Sowa | 42b1870 | 2014-10-06 19:58:35 +0200 | [diff] [blame] | 1963 | int sernum; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1964 | }; |
| 1965 | |
| 1966 | static int ipv6_route_seq_show(struct seq_file *seq, void *v) |
| 1967 | { |
| 1968 | struct rt6_info *rt = v; |
| 1969 | struct ipv6_route_iter *iter = seq->private; |
| 1970 | |
| 1971 | seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); |
| 1972 | |
| 1973 | #ifdef CONFIG_IPV6_SUBTREES |
| 1974 | seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); |
| 1975 | #else |
| 1976 | seq_puts(seq, "00000000000000000000000000000000 00 "); |
| 1977 | #endif |
| 1978 | if (rt->rt6i_flags & RTF_GATEWAY) |
| 1979 | seq_printf(seq, "%pi6", &rt->rt6i_gateway); |
| 1980 | else |
| 1981 | seq_puts(seq, "00000000000000000000000000000000"); |
| 1982 | |
| 1983 | seq_printf(seq, " %08x %08x %08x %08x %8s\n", |
| 1984 | rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), |
| 1985 | rt->dst.__use, rt->rt6i_flags, |
| 1986 | rt->dst.dev ? rt->dst.dev->name : ""); |
| 1987 | iter->w.leaf = NULL; |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 1991 | static int ipv6_route_yield(struct fib6_walker *w) |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1992 | { |
| 1993 | struct ipv6_route_iter *iter = w->args; |
| 1994 | |
| 1995 | if (!iter->skip) |
| 1996 | return 1; |
| 1997 | |
| 1998 | do { |
| 1999 | iter->w.leaf = iter->w.leaf->dst.rt6_next; |
| 2000 | iter->skip--; |
| 2001 | if (!iter->skip && iter->w.leaf) |
| 2002 | return 1; |
| 2003 | } while (iter->w.leaf); |
| 2004 | |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2008 | static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter, |
| 2009 | struct net *net) |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2010 | { |
| 2011 | memset(&iter->w, 0, sizeof(iter->w)); |
| 2012 | iter->w.func = ipv6_route_yield; |
| 2013 | iter->w.root = &iter->tbl->tb6_root; |
| 2014 | iter->w.state = FWS_INIT; |
| 2015 | iter->w.node = iter->w.root; |
| 2016 | iter->w.args = iter; |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 2017 | iter->sernum = iter->w.root->fn_sernum; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2018 | INIT_LIST_HEAD(&iter->w.lh); |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2019 | fib6_walker_link(net, &iter->w); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl, |
| 2023 | struct net *net) |
| 2024 | { |
| 2025 | unsigned int h; |
| 2026 | struct hlist_node *node; |
| 2027 | |
| 2028 | if (tbl) { |
| 2029 | h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1; |
| 2030 | node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist)); |
| 2031 | } else { |
| 2032 | h = 0; |
| 2033 | node = NULL; |
| 2034 | } |
| 2035 | |
| 2036 | while (!node && h < FIB6_TABLE_HASHSZ) { |
| 2037 | node = rcu_dereference_bh( |
| 2038 | hlist_first_rcu(&net->ipv6.fib_table_hash[h++])); |
| 2039 | } |
| 2040 | return hlist_entry_safe(node, struct fib6_table, tb6_hlist); |
| 2041 | } |
| 2042 | |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 2043 | static void ipv6_route_check_sernum(struct ipv6_route_iter *iter) |
| 2044 | { |
| 2045 | if (iter->sernum != iter->w.root->fn_sernum) { |
| 2046 | iter->sernum = iter->w.root->fn_sernum; |
| 2047 | iter->w.state = FWS_INIT; |
| 2048 | iter->w.node = iter->w.root; |
| 2049 | WARN_ON(iter->w.skip); |
| 2050 | iter->w.skip = iter->w.count; |
| 2051 | } |
| 2052 | } |
| 2053 | |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2054 | static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2055 | { |
| 2056 | int r; |
| 2057 | struct rt6_info *n; |
| 2058 | struct net *net = seq_file_net(seq); |
| 2059 | struct ipv6_route_iter *iter = seq->private; |
| 2060 | |
| 2061 | if (!v) |
| 2062 | goto iter_table; |
| 2063 | |
| 2064 | n = ((struct rt6_info *)v)->dst.rt6_next; |
| 2065 | if (n) { |
| 2066 | ++*pos; |
| 2067 | return n; |
| 2068 | } |
| 2069 | |
| 2070 | iter_table: |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 2071 | ipv6_route_check_sernum(iter); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2072 | read_lock(&iter->tbl->tb6_lock); |
| 2073 | r = fib6_walk_continue(&iter->w); |
| 2074 | read_unlock(&iter->tbl->tb6_lock); |
| 2075 | if (r > 0) { |
| 2076 | if (v) |
| 2077 | ++*pos; |
| 2078 | return iter->w.leaf; |
| 2079 | } else if (r < 0) { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2080 | fib6_walker_unlink(net, &iter->w); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2081 | return NULL; |
| 2082 | } |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2083 | fib6_walker_unlink(net, &iter->w); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2084 | |
| 2085 | iter->tbl = ipv6_route_seq_next_table(iter->tbl, net); |
| 2086 | if (!iter->tbl) |
| 2087 | return NULL; |
| 2088 | |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2089 | ipv6_route_seq_setup_walk(iter, net); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2090 | goto iter_table; |
| 2091 | } |
| 2092 | |
| 2093 | static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos) |
| 2094 | __acquires(RCU_BH) |
| 2095 | { |
| 2096 | struct net *net = seq_file_net(seq); |
| 2097 | struct ipv6_route_iter *iter = seq->private; |
| 2098 | |
| 2099 | rcu_read_lock_bh(); |
| 2100 | iter->tbl = ipv6_route_seq_next_table(NULL, net); |
| 2101 | iter->skip = *pos; |
| 2102 | |
| 2103 | if (iter->tbl) { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2104 | ipv6_route_seq_setup_walk(iter, net); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2105 | return ipv6_route_seq_next(seq, NULL, pos); |
| 2106 | } else { |
| 2107 | return NULL; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | static bool ipv6_route_iter_active(struct ipv6_route_iter *iter) |
| 2112 | { |
Hannes Frederic Sowa | 94b2cfe | 2014-10-06 19:58:34 +0200 | [diff] [blame] | 2113 | struct fib6_walker *w = &iter->w; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2114 | return w->node && !(w->state == FWS_U && w->node == w->root); |
| 2115 | } |
| 2116 | |
| 2117 | static void ipv6_route_seq_stop(struct seq_file *seq, void *v) |
| 2118 | __releases(RCU_BH) |
| 2119 | { |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2120 | struct net *net = seq_file_net(seq); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2121 | struct ipv6_route_iter *iter = seq->private; |
| 2122 | |
| 2123 | if (ipv6_route_iter_active(iter)) |
Michal Kubeček | 9a03cd8 | 2016-03-08 14:44:35 +0100 | [diff] [blame] | 2124 | fib6_walker_unlink(net, &iter->w); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 2125 | |
| 2126 | rcu_read_unlock_bh(); |
| 2127 | } |
| 2128 | |
| 2129 | static const struct seq_operations ipv6_route_seq_ops = { |
| 2130 | .start = ipv6_route_seq_start, |
| 2131 | .next = ipv6_route_seq_next, |
| 2132 | .stop = ipv6_route_seq_stop, |
| 2133 | .show = ipv6_route_seq_show |
| 2134 | }; |
| 2135 | |
| 2136 | int ipv6_route_open(struct inode *inode, struct file *file) |
| 2137 | { |
| 2138 | return seq_open_net(inode, file, &ipv6_route_seq_ops, |
| 2139 | sizeof(struct ipv6_route_iter)); |
| 2140 | } |
| 2141 | |
| 2142 | #endif /* CONFIG_PROC_FS */ |