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