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