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