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