blob: 8060524f425667d008470c2826f2ac835c8e25d2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Robert Olsson19baf832005-06-21 12:43:18 -07002/*
Robert Olsson19baf832005-06-21 12:43:18 -07003 *
4 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
5 * & Swedish University of Agricultural Sciences.
6 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -07008 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09009 *
Robert Olsson19baf832005-06-21 12:43:18 -070010 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
11 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 *
Robert Olsson19baf832005-06-21 12:43:18 -070014 * An experimental study of compression methods for dynamic tries
15 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Alexander A. Klimov7a6498e2020-07-06 19:38:50 +020016 * https://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070017 *
Robert Olsson19baf832005-06-21 12:43:18 -070018 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
19 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
20 *
Robert Olsson19baf832005-06-21 12:43:18 -070021 * Code from fib_hash has been reused which includes the following header:
22 *
Robert Olsson19baf832005-06-21 12:43:18 -070023 * INET An implementation of the TCP/IP protocol suite for the LINUX
24 * operating system. INET is implemented using the BSD Socket
25 * interface as the means of communication with the user level.
26 *
27 * IPv4 FIB: lookup engine and maintenance routines.
28 *
Robert Olsson19baf832005-06-21 12:43:18 -070029 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
30 *
Robert Olssonfd966252005-12-22 11:25:10 -080031 * Substantial contributions to this work comes from:
32 *
33 * David S. Miller, <davem@davemloft.net>
34 * Stephen Hemminger <shemminger@osdl.org>
35 * Paul E. McKenney <paulmck@us.ibm.com>
36 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070037 */
Alexey Dobriyan08009a72018-02-24 21:20:33 +030038#include <linux/cache.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070040#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070041#include <linux/types.h>
42#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070043#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
47#include <linux/errno.h>
48#include <linux/in.h>
49#include <linux/inet.h>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080050#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070051#include <linux/netdevice.h>
52#include <linux/if_arp.h>
53#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070054#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070055#include <linux/skbuff.h>
56#include <linux/netlink.h>
57#include <linux/init.h>
58#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040060#include <linux/export.h>
David S. Millerffa915d2015-05-27 00:19:03 -040061#include <linux/vmalloc.h>
Jiri Pirkob90eb752016-09-26 12:52:29 +020062#include <linux/notifier.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020063#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070064#include <net/ip.h>
65#include <net/protocol.h>
66#include <net/route.h>
67#include <net/tcp.h>
68#include <net/sock.h>
69#include <net/ip_fib.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020070#include <net/fib_notifier.h>
David Ahernf6d3c192015-08-28 08:42:09 -070071#include <trace/events/fib.h>
Robert Olsson19baf832005-06-21 12:43:18 -070072#include "fib_lookup.h"
73
Jiri Pirko7c550da2019-10-03 11:49:27 +020074static int call_fib_entry_notifier(struct notifier_block *nb,
Ido Schimmelc3852ef2016-12-03 16:45:07 +010075 enum fib_event_type event_type, u32 dst,
Jiri Pirkob7a59552019-10-03 11:49:30 +020076 int dst_len, struct fib_alias *fa,
77 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +010078{
79 struct fib_entry_notifier_info info = {
Jiri Pirkob7a59552019-10-03 11:49:30 +020080 .info.extack = extack,
Ido Schimmelc3852ef2016-12-03 16:45:07 +010081 .dst = dst,
82 .dst_len = dst_len,
David Ahern6eba87c2017-10-18 11:39:13 -070083 .fi = fa->fa_info,
84 .tos = fa->fa_tos,
85 .type = fa->fa_type,
86 .tb_id = fa->tb_id,
Ido Schimmelc3852ef2016-12-03 16:45:07 +010087 };
Jiri Pirko7c550da2019-10-03 11:49:27 +020088 return call_fib4_notifier(nb, event_type, &info.info);
Ido Schimmelc3852ef2016-12-03 16:45:07 +010089}
90
Jiri Pirkob90eb752016-09-26 12:52:29 +020091static int call_fib_entry_notifiers(struct net *net,
92 enum fib_event_type event_type, u32 dst,
David Ahern6c31e5a2017-10-27 17:37:13 -070093 int dst_len, struct fib_alias *fa,
94 struct netlink_ext_ack *extack)
Jiri Pirkob90eb752016-09-26 12:52:29 +020095{
96 struct fib_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -070097 .info.extack = extack,
Jiri Pirkob90eb752016-09-26 12:52:29 +020098 .dst = dst,
99 .dst_len = dst_len,
David Ahern6eba87c2017-10-18 11:39:13 -0700100 .fi = fa->fa_info,
101 .tos = fa->fa_tos,
102 .type = fa->fa_type,
103 .tb_id = fa->tb_id,
Jiri Pirkob90eb752016-09-26 12:52:29 +0200104 };
Ido Schimmel04b1d4e2017-08-03 13:28:11 +0200105 return call_fib4_notifiers(net, event_type, &info.info);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200106}
107
Robert Olsson06ef9212006-03-20 21:35:01 -0800108#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -0700109
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800110#define KEYLENGTH (8*sizeof(t_key))
111#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -0700112
Robert Olsson19baf832005-06-21 12:43:18 -0700113typedef unsigned int t_key;
114
Alexander Duyck88bae712015-03-06 09:54:52 -0800115#define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
116#define IS_TNODE(n) ((n)->bits)
117#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700118
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800119struct key_vector {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800120 t_key key;
121 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
122 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
123 unsigned char slen;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800124 union {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800125 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800126 struct hlist_head leaf;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800127 /* This array is valid if (pos | bits) > 0 (TNODE) */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800128 struct key_vector __rcu *tnode[0];
Alexander Duyckadaf9812014-12-31 10:55:47 -0800129 };
Robert Olsson19baf832005-06-21 12:43:18 -0700130};
131
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800132struct tnode {
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800133 struct rcu_head rcu;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800134 t_key empty_children; /* KEYLENGTH bits needed */
135 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800136 struct key_vector __rcu *parent;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800137 struct key_vector kv[1];
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800138#define tn_bits kv[0].bits
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800139};
140
141#define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
Alexander Duyck41b489f2015-03-04 15:02:33 -0800142#define LEAF_SIZE TNODE_SIZE(1)
143
Robert Olsson19baf832005-06-21 12:43:18 -0700144#ifdef CONFIG_IP_FIB_TRIE_STATS
145struct trie_use_stats {
146 unsigned int gets;
147 unsigned int backtrack;
148 unsigned int semantic_match_passed;
149 unsigned int semantic_match_miss;
150 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700151 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700152};
153#endif
154
155struct trie_stat {
156 unsigned int totdepth;
157 unsigned int maxdepth;
158 unsigned int tnodes;
159 unsigned int leaves;
160 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800161 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800162 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700163};
Robert Olsson19baf832005-06-21 12:43:18 -0700164
165struct trie {
Alexander Duyck88bae712015-03-06 09:54:52 -0800166 struct key_vector kv[1];
Robert Olsson19baf832005-06-21 12:43:18 -0700167#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800168 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700169#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700170};
171
Alexander Duyck88bae712015-03-06 09:54:52 -0800172static struct key_vector *resize(struct trie *t, struct key_vector *tn);
David Ahern9ab948a2019-03-20 09:18:59 -0700173static unsigned int tnode_free_size;
Jarek Poplawskic3059472009-07-14 08:33:08 +0000174
175/*
David Ahern9ab948a2019-03-20 09:18:59 -0700176 * synchronize_rcu after call_rcu for outstanding dirty memory; it should be
177 * especially useful before resizing the root node with PREEMPT_NONE configs;
178 * the value was obtained experimentally, aiming to avoid visible slowdown.
Jarek Poplawskic3059472009-07-14 08:33:08 +0000179 */
David Ahern9ab948a2019-03-20 09:18:59 -0700180unsigned int sysctl_fib_sync_mem = 512 * 1024;
181unsigned int sysctl_fib_sync_mem_min = 64 * 1024;
182unsigned int sysctl_fib_sync_mem_max = 64 * 1024 * 1024;
Robert Olsson19baf832005-06-21 12:43:18 -0700183
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300184static struct kmem_cache *fn_alias_kmem __ro_after_init;
185static struct kmem_cache *trie_leaf_kmem __ro_after_init;
Robert Olsson19baf832005-06-21 12:43:18 -0700186
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800187static inline struct tnode *tn_info(struct key_vector *kv)
188{
189 return container_of(kv, struct tnode, kv[0]);
190}
191
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800192/* caller must hold RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800193#define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800194#define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700195
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800196/* caller must hold RCU read lock or RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800197#define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800198#define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700199
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800200/* wrapper for rcu_assign_pointer */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800201static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700202{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800203 if (n)
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800204 rcu_assign_pointer(tn_info(n)->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800205}
206
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800207#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800208
209/* This provides us with the number of children in this node, in the case of a
210 * leaf this will return 0 meaning none of the children are accessible.
211 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800212static inline unsigned long child_length(const struct key_vector *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800213{
214 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700215}
Robert Olsson2373ce12005-08-25 13:01:29 -0700216
Alexander Duyck88bae712015-03-06 09:54:52 -0800217#define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
218
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800219static inline unsigned long get_index(t_key key, struct key_vector *kv)
220{
221 unsigned long index = key ^ kv->key;
222
Alexander Duyck88bae712015-03-06 09:54:52 -0800223 if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
224 return 0;
225
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800226 return index >> kv->pos;
227}
228
Alexander Duycke9b44012014-12-31 10:56:12 -0800229/* To understand this stuff, an understanding of keys and all their bits is
230 * necessary. Every node in the trie has a key associated with it, but not
231 * all of the bits in that key are significant.
232 *
233 * Consider a node 'n' and its parent 'tp'.
234 *
235 * If n is a leaf, every bit in its key is significant. Its presence is
236 * necessitated by path compression, since during a tree traversal (when
237 * searching for a leaf - unless we are doing an insertion) we will completely
238 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
239 * a potentially successful search, that we have indeed been walking the
240 * correct key path.
241 *
242 * Note that we can never "miss" the correct key in the tree if present by
243 * following the wrong path. Path compression ensures that segments of the key
244 * that are the same for all keys with a given prefix are skipped, but the
245 * skipped part *is* identical for each node in the subtrie below the skipped
246 * bit! trie_insert() in this implementation takes care of that.
247 *
248 * if n is an internal node - a 'tnode' here, the various parts of its key
249 * have many different meanings.
250 *
251 * Example:
252 * _________________________________________________________________
253 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
254 * -----------------------------------------------------------------
255 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
256 *
257 * _________________________________________________________________
258 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
259 * -----------------------------------------------------------------
260 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
261 *
262 * tp->pos = 22
263 * tp->bits = 3
264 * n->pos = 13
265 * n->bits = 4
266 *
267 * First, let's just ignore the bits that come before the parent tp, that is
268 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
269 * point we do not use them for anything.
270 *
271 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
272 * index into the parent's child array. That is, they will be used to find
273 * 'n' among tp's children.
274 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800275 * The bits from (n->pos + n->bits) to (tp->pos - 1) - "S" - are skipped bits
Alexander Duycke9b44012014-12-31 10:56:12 -0800276 * for the node n.
277 *
278 * All the bits we have seen so far are significant to the node n. The rest
279 * of the bits are really not needed or indeed known in n->key.
280 *
281 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
282 * n's child array, and will of course be different for each child.
283 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800284 * The rest of the bits, from 0 to (n->pos -1) - "u" - are completely unknown
Alexander Duycke9b44012014-12-31 10:56:12 -0800285 * at this point.
286 */
Robert Olsson19baf832005-06-21 12:43:18 -0700287
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800288static const int halve_threshold = 25;
289static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700290static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700291static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700292
293static void __alias_free_mem(struct rcu_head *head)
294{
295 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
296 kmem_cache_free(fn_alias_kmem, fa);
297}
298
299static inline void alias_free_mem_rcu(struct fib_alias *fa)
300{
301 call_rcu(&fa->rcu, __alias_free_mem);
302}
303
Alexander Duyck1de3d872015-03-04 15:04:46 -0800304#define TNODE_VMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800305 ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800306
307static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700308{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800309 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800310
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800311 if (!n->tn_bits)
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800312 kmem_cache_free(trie_leaf_kmem, n);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800313 else
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800314 kvfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700315}
316
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800317#define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700318
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800319static struct tnode *tnode_alloc(int bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700320{
Alexander Duyck1de3d872015-03-04 15:04:46 -0800321 size_t size;
322
323 /* verify bits is within bounds */
324 if (bits > TNODE_VMALLOC_MAX)
325 return NULL;
326
327 /* determine size and verify it is non-zero and didn't overflow */
328 size = TNODE_SIZE(1ul << bits);
329
Robert Olsson2373ce12005-08-25 13:01:29 -0700330 if (size <= PAGE_SIZE)
Eric Dumazet8d9654442008-01-13 22:31:44 -0800331 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700332 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000333 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700334}
Robert Olsson2373ce12005-08-25 13:01:29 -0700335
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800336static inline void empty_child_inc(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800337{
Matthias Kaehlcke25cec752019-06-18 14:14:40 -0700338 tn_info(n)->empty_children++;
339
340 if (!tn_info(n)->empty_children)
341 tn_info(n)->full_children++;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800342}
343
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800344static inline void empty_child_dec(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800345{
Matthias Kaehlcke25cec752019-06-18 14:14:40 -0700346 if (!tn_info(n)->empty_children)
347 tn_info(n)->full_children--;
348
349 tn_info(n)->empty_children--;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800350}
351
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800352static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
Robert Olsson19baf832005-06-21 12:43:18 -0700353{
Firo Yangf38b24c2015-06-08 11:54:51 +0800354 struct key_vector *l;
355 struct tnode *kv;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800356
Firo Yangf38b24c2015-06-08 11:54:51 +0800357 kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800358 if (!kv)
359 return NULL;
360
361 /* initialize key vector */
Firo Yangf38b24c2015-06-08 11:54:51 +0800362 l = kv->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800363 l->key = key;
364 l->pos = 0;
365 l->bits = 0;
366 l->slen = fa->fa_slen;
367
368 /* link leaf to fib alias */
369 INIT_HLIST_HEAD(&l->leaf);
370 hlist_add_head(&fa->fa_list, &l->leaf);
371
Robert Olsson19baf832005-06-21 12:43:18 -0700372 return l;
373}
374
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800375static struct key_vector *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700376{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800377 unsigned int shift = pos + bits;
Firo Yangf38b24c2015-06-08 11:54:51 +0800378 struct key_vector *tn;
379 struct tnode *tnode;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800380
381 /* verify bits and pos their msb bits clear and values are valid */
382 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700383
Firo Yangf38b24c2015-06-08 11:54:51 +0800384 tnode = tnode_alloc(bits);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800385 if (!tnode)
386 return NULL;
387
Firo Yangf38b24c2015-06-08 11:54:51 +0800388 pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
389 sizeof(struct key_vector *) << bits);
390
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800391 if (bits == KEYLENGTH)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800392 tnode->full_children = 1;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800393 else
Alexander Duyck6e22d172015-03-06 09:54:39 -0800394 tnode->empty_children = 1ul << bits;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800395
Firo Yangf38b24c2015-06-08 11:54:51 +0800396 tn = tnode->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800397 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
398 tn->pos = pos;
399 tn->bits = bits;
400 tn->slen = pos;
401
Robert Olsson19baf832005-06-21 12:43:18 -0700402 return tn;
403}
404
Alexander Duycke9b44012014-12-31 10:56:12 -0800405/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700406 * and no bits are skipped. See discussion in dyntree paper p. 6
407 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800408static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700409{
Alexander Duycke9b44012014-12-31 10:56:12 -0800410 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700411}
412
Alexander Duyckff181ed2014-12-31 10:56:43 -0800413/* Add a child at position i overwriting the old value.
414 * Update the value of full_children and empty_children.
415 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800416static void put_child(struct key_vector *tn, unsigned long i,
417 struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700418{
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800419 struct key_vector *chi = get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800420 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700421
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800422 BUG_ON(i >= child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700423
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800424 /* update emptyChildren, overflow into fullChildren */
Ian Morris00db4122015-04-03 09:17:27 +0100425 if (!n && chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800426 empty_child_inc(tn);
Ian Morris00db4122015-04-03 09:17:27 +0100427 if (n && !chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800428 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700429
Robert Olsson19baf832005-06-21 12:43:18 -0700430 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800431 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700432 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800433
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700434 if (wasfull && !isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800435 tn_info(tn)->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700436 else if (!wasfull && isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800437 tn_info(tn)->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700438
Alexander Duyck5405afd2014-12-31 10:57:08 -0800439 if (n && (tn->slen < n->slen))
440 tn->slen = n->slen;
441
Alexander Duyck41b489f2015-03-04 15:02:33 -0800442 rcu_assign_pointer(tn->tnode[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700443}
444
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800445static void update_children(struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800446{
447 unsigned long i;
448
449 /* update all of the child parent pointers */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800450 for (i = child_length(tn); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800451 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800452
453 if (!inode)
454 continue;
455
456 /* Either update the children of a tnode that
457 * already belongs to us or update the child
458 * to point to ourselves.
459 */
460 if (node_parent(inode) == tn)
461 update_children(inode);
462 else
463 node_set_parent(inode, tn);
464 }
465}
466
Alexander Duyck88bae712015-03-06 09:54:52 -0800467static inline void put_child_root(struct key_vector *tp, t_key key,
468 struct key_vector *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800469{
Alexander Duyck88bae712015-03-06 09:54:52 -0800470 if (IS_TRIE(tp))
471 rcu_assign_pointer(tp->tnode[0], n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800472 else
Alexander Duyck88bae712015-03-06 09:54:52 -0800473 put_child(tp, get_index(key, tp), n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800474}
475
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800476static inline void tnode_free_init(struct key_vector *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700477{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800478 tn_info(tn)->rcu.next = NULL;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800479}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700480
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800481static inline void tnode_free_append(struct key_vector *tn,
482 struct key_vector *n)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800483{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800484 tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
485 tn_info(tn)->rcu.next = &tn_info(n)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800486}
487
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800488static void tnode_free(struct key_vector *tn)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800489{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800490 struct callback_head *head = &tn_info(tn)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800491
492 while (head) {
493 head = head->next;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800494 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800495 node_free(tn);
496
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800497 tn = container_of(head, struct tnode, rcu)->kv;
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700498 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800499
David Ahern9ab948a2019-03-20 09:18:59 -0700500 if (tnode_free_size >= sysctl_fib_sync_mem) {
Alexander Duyckfc86a932014-12-31 10:56:49 -0800501 tnode_free_size = 0;
502 synchronize_rcu();
503 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700504}
505
Alexander Duyck88bae712015-03-06 09:54:52 -0800506static struct key_vector *replace(struct trie *t,
507 struct key_vector *oldtnode,
508 struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800509{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800510 struct key_vector *tp = node_parent(oldtnode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800511 unsigned long i;
512
513 /* setup the parent pointer out of and back into this node */
514 NODE_INIT_PARENT(tn, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -0800515 put_child_root(tp, tn->key, tn);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800516
517 /* update all of the child parent pointers */
518 update_children(tn);
519
520 /* all pointers should be clean so we are done */
521 tnode_free(oldtnode);
522
523 /* resize children now that oldtnode is freed */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800524 for (i = child_length(tn); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800525 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800526
527 /* resize child node */
528 if (tnode_full(tn, inode))
Alexander Duyck88bae712015-03-06 09:54:52 -0800529 tn = resize(t, inode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800530 }
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800531
Alexander Duyck88bae712015-03-06 09:54:52 -0800532 return tp;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800533}
534
Alexander Duyck88bae712015-03-06 09:54:52 -0800535static struct key_vector *inflate(struct trie *t,
536 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700537{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800538 struct key_vector *tn;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800539 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800540 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700541
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700542 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700543
Alexander Duycke9b44012014-12-31 10:56:12 -0800544 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700545 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800546 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700547
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800548 /* prepare oldtnode to be freed */
549 tnode_free_init(oldtnode);
550
Alexander Duyck12c081a2014-12-31 10:56:55 -0800551 /* Assemble all of the pointers in our cluster, in this case that
552 * represents all of the pointers out of our allocated nodes that
553 * point to existing tnodes and the links between our allocated
554 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700555 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800556 for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800557 struct key_vector *inode = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800558 struct key_vector *node0, *node1;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800559 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700560
Robert Olsson19baf832005-06-21 12:43:18 -0700561 /* An empty child */
Ian Morris51456b22015-04-03 09:17:26 +0100562 if (!inode)
Robert Olsson19baf832005-06-21 12:43:18 -0700563 continue;
564
565 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800566 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800567 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700568 continue;
569 }
570
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800571 /* drop the node in the old tnode free list */
572 tnode_free_append(oldtnode, inode);
573
Robert Olsson19baf832005-06-21 12:43:18 -0700574 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700575 if (inode->bits == 1) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800576 put_child(tn, 2 * i + 1, get_child(inode, 1));
577 put_child(tn, 2 * i, get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700578 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700579 }
580
Olof Johansson91b9a272005-08-09 20:24:39 -0700581 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800582 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700583 * original children. The two new nodes will have
584 * a position one bit further down the key and this
585 * means that the "significant" part of their keys
586 * (see the discussion near the top of this file)
587 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800588 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700589 * moving the key position by one step, the bit that
590 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800591 * (tn->pos) - is the one that will differ between
592 * node0 and node1. So... we synthesize that bit in the
593 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700594 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800595 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
596 if (!node1)
597 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800598 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700599
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800600 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800601 if (!node0)
602 goto nomem;
603 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700604
Alexander Duyck12c081a2014-12-31 10:56:55 -0800605 /* populate child pointers in new nodes */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800606 for (k = child_length(inode), j = k / 2; j;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800607 put_child(node1, --j, get_child(inode, --k));
608 put_child(node0, j, get_child(inode, j));
609 put_child(node1, --j, get_child(inode, --k));
610 put_child(node0, j, get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700611 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800612
Alexander Duyck12c081a2014-12-31 10:56:55 -0800613 /* link new nodes to parent */
614 NODE_INIT_PARENT(node1, tn);
615 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700616
Alexander Duyck12c081a2014-12-31 10:56:55 -0800617 /* link parent to nodes */
618 put_child(tn, 2 * i + 1, node1);
619 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700620 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800621
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800622 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800623 return replace(t, oldtnode, tn);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700624nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800625 /* all pointers should be clean so we are done */
626 tnode_free(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800627notnode:
628 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700629}
630
Alexander Duyck88bae712015-03-06 09:54:52 -0800631static struct key_vector *halve(struct trie *t,
632 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700633{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800634 struct key_vector *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800635 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700636
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700637 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700638
Alexander Duycke9b44012014-12-31 10:56:12 -0800639 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700640 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800641 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700642
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800643 /* prepare oldtnode to be freed */
644 tnode_free_init(oldtnode);
645
Alexander Duyck12c081a2014-12-31 10:56:55 -0800646 /* Assemble all of the pointers in our cluster, in this case that
647 * represents all of the pointers out of our allocated nodes that
648 * point to existing tnodes and the links between our allocated
649 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700650 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800651 for (i = child_length(oldtnode); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800652 struct key_vector *node1 = get_child(oldtnode, --i);
653 struct key_vector *node0 = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800654 struct key_vector *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700655
Alexander Duyck12c081a2014-12-31 10:56:55 -0800656 /* At least one of the children is empty */
657 if (!node1 || !node0) {
658 put_child(tn, i / 2, node1 ? : node0);
659 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700660 }
Robert Olsson2f368952005-07-05 15:02:40 -0700661
Alexander Duyck12c081a2014-12-31 10:56:55 -0800662 /* Two nonempty children */
663 inode = tnode_new(node0->key, oldtnode->pos, 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800664 if (!inode)
665 goto nomem;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800666 tnode_free_append(tn, inode);
667
668 /* initialize pointers out of node */
669 put_child(inode, 1, node1);
670 put_child(inode, 0, node0);
671 NODE_INIT_PARENT(inode, tn);
672
673 /* link parent to node */
674 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700675 }
Robert Olsson19baf832005-06-21 12:43:18 -0700676
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800677 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800678 return replace(t, oldtnode, tn);
679nomem:
680 /* all pointers should be clean so we are done */
681 tnode_free(tn);
682notnode:
683 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700684}
685
Alexander Duyck88bae712015-03-06 09:54:52 -0800686static struct key_vector *collapse(struct trie *t,
687 struct key_vector *oldtnode)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800688{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800689 struct key_vector *n, *tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800690 unsigned long i;
691
692 /* scan the tnode looking for that one child that might still exist */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800693 for (n = NULL, i = child_length(oldtnode); !n && i;)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800694 n = get_child(oldtnode, --i);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800695
696 /* compress one level */
697 tp = node_parent(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800698 put_child_root(tp, oldtnode->key, n);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800699 node_set_parent(n, tp);
700
701 /* drop dead node */
702 node_free(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800703
704 return tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800705}
706
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800707static unsigned char update_suffix(struct key_vector *tn)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800708{
709 unsigned char slen = tn->pos;
710 unsigned long stride, i;
Alexander Duycka52ca622016-12-01 07:27:57 -0500711 unsigned char slen_max;
712
713 /* only vector 0 can have a suffix length greater than or equal to
714 * tn->pos + tn->bits, the second highest node will have a suffix
715 * length at most of tn->pos + tn->bits - 1
716 */
717 slen_max = min_t(unsigned char, tn->pos + tn->bits - 1, tn->slen);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800718
719 /* search though the list of children looking for nodes that might
720 * have a suffix greater than the one we currently have. This is
721 * why we start with a stride of 2 since a stride of 1 would
722 * represent the nodes with suffix length equal to tn->pos
723 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800724 for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800725 struct key_vector *n = get_child(tn, i);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800726
727 if (!n || (n->slen <= slen))
728 continue;
729
730 /* update stride and slen based on new value */
731 stride <<= (n->slen - slen);
732 slen = n->slen;
733 i &= ~(stride - 1);
734
Alexander Duycka52ca622016-12-01 07:27:57 -0500735 /* stop searching if we have hit the maximum possible value */
736 if (slen >= slen_max)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800737 break;
738 }
739
740 tn->slen = slen;
741
742 return slen;
743}
744
Alexander Duyckf05a4812014-12-31 10:56:37 -0800745/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
746 * the Helsinki University of Technology and Matti Tikkanen of Nokia
747 * Telecommunications, page 6:
748 * "A node is doubled if the ratio of non-empty children to all
749 * children in the *doubled* node is at least 'high'."
750 *
751 * 'high' in this instance is the variable 'inflate_threshold'. It
752 * is expressed as a percentage, so we multiply it with
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800753 * child_length() and instead of multiplying by 2 (since the
Alexander Duyckf05a4812014-12-31 10:56:37 -0800754 * child array will be doubled by inflate()) and multiplying
755 * the left-hand side by 100 (to handle the percentage thing) we
756 * multiply the left-hand side by 50.
757 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800758 * The left-hand side may look a bit weird: child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800759 * - tn->empty_children is of course the number of non-null children
760 * in the current node. tn->full_children is the number of "full"
761 * children, that is non-null tnodes with a skip value of 0.
762 * All of those will be doubled in the resulting inflated tnode, so
763 * we just count them one extra time here.
764 *
765 * A clearer way to write this would be:
766 *
767 * to_be_doubled = tn->full_children;
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800768 * not_to_be_doubled = child_length(tn) - tn->empty_children -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800769 * tn->full_children;
770 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800771 * new_child_length = child_length(tn) * 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800772 *
773 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
774 * new_child_length;
775 * if (new_fill_factor >= inflate_threshold)
776 *
777 * ...and so on, tho it would mess up the while () loop.
778 *
779 * anyway,
780 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
781 * inflate_threshold
782 *
783 * avoid a division:
784 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
785 * inflate_threshold * new_child_length
786 *
787 * expand not_to_be_doubled and to_be_doubled, and shorten:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800788 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800789 * tn->full_children) >= inflate_threshold * new_child_length
790 *
791 * expand new_child_length:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800792 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800793 * tn->full_children) >=
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800794 * inflate_threshold * child_length(tn) * 2
Alexander Duyckf05a4812014-12-31 10:56:37 -0800795 *
796 * shorten again:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800797 * 50 * (tn->full_children + child_length(tn) -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800798 * tn->empty_children) >= inflate_threshold *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800799 * child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800800 *
801 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800802static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800803{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800804 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800805 unsigned long threshold = used;
806
807 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800808 threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800809 used -= tn_info(tn)->empty_children;
810 used += tn_info(tn)->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800811
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800812 /* if bits == KEYLENGTH then pos = 0, and will fail below */
813
814 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800815}
816
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800817static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800818{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800819 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800820 unsigned long threshold = used;
821
822 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800823 threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800824 used -= tn_info(tn)->empty_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800825
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800826 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
827
828 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
829}
830
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800831static inline bool should_collapse(struct key_vector *tn)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800832{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800833 unsigned long used = child_length(tn);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800834
Alexander Duyck6e22d172015-03-06 09:54:39 -0800835 used -= tn_info(tn)->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800836
837 /* account for bits == KEYLENGTH case */
Alexander Duyck6e22d172015-03-06 09:54:39 -0800838 if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800839 used -= KEY_MAX;
840
841 /* One child or none, time to drop us from the trie */
842 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800843}
844
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800845#define MAX_WORK 10
Alexander Duyck88bae712015-03-06 09:54:52 -0800846static struct key_vector *resize(struct trie *t, struct key_vector *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800847{
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800848#ifdef CONFIG_IP_FIB_TRIE_STATS
849 struct trie_use_stats __percpu *stats = t->stats;
850#endif
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800851 struct key_vector *tp = node_parent(tn);
Alexander Duyck88bae712015-03-06 09:54:52 -0800852 unsigned long cindex = get_index(tn->key, tp);
Alexander Duycka80e89d2015-01-22 15:51:20 -0800853 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800854
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800855 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
856 tn, inflate_threshold, halve_threshold);
857
Alexander Duyckff181ed2014-12-31 10:56:43 -0800858 /* track the tnode via the pointer from the parent instead of
859 * doing it ourselves. This way we can let RCU fully do its
860 * thing without us interfering
861 */
Alexander Duyck88bae712015-03-06 09:54:52 -0800862 BUG_ON(tn != get_child(tp, cindex));
Alexander Duyckff181ed2014-12-31 10:56:43 -0800863
Alexander Duyckf05a4812014-12-31 10:56:37 -0800864 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800865 * nonempty nodes that are above the threshold.
866 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700867 while (should_inflate(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800868 tp = inflate(t, tn);
869 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800870#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800871 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800872#endif
873 break;
874 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800875
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700876 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -0800877 tn = get_child(tp, cindex);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800878 }
879
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700880 /* update parent in case inflate failed */
881 tp = node_parent(tn);
882
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800883 /* Return if at least one inflate is run */
884 if (max_work != MAX_WORK)
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700885 return tp;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800886
Alexander Duyckf05a4812014-12-31 10:56:37 -0800887 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800888 * node is above threshold.
889 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700890 while (should_halve(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800891 tp = halve(t, tn);
892 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800893#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800894 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800895#endif
896 break;
897 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800898
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700899 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -0800900 tn = get_child(tp, cindex);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800901 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800902
903 /* Only one child remains */
Alexander Duyck88bae712015-03-06 09:54:52 -0800904 if (should_collapse(tn))
905 return collapse(t, tn);
906
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700907 /* update parent in case halve failed */
Alexander Duycka52ca622016-12-01 07:27:57 -0500908 return node_parent(tn);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800909}
910
Alexander Duyck1a239172016-12-01 07:27:52 -0500911static void node_pull_suffix(struct key_vector *tn, unsigned char slen)
Robert Olsson19baf832005-06-21 12:43:18 -0700912{
Alexander Duyck1a239172016-12-01 07:27:52 -0500913 unsigned char node_slen = tn->slen;
914
915 while ((node_slen > tn->pos) && (node_slen > slen)) {
916 slen = update_suffix(tn);
917 if (node_slen == slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800918 break;
Alexander Duyck1a239172016-12-01 07:27:52 -0500919
920 tn = node_parent(tn);
921 node_slen = tn->slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800922 }
923}
924
Alexander Duyck1a239172016-12-01 07:27:52 -0500925static void node_push_suffix(struct key_vector *tn, unsigned char slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800926{
Alexander Duyck1a239172016-12-01 07:27:52 -0500927 while (tn->slen < slen) {
928 tn->slen = slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800929 tn = node_parent(tn);
930 }
931}
932
Robert Olsson2373ce12005-08-25 13:01:29 -0700933/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800934static struct key_vector *fib_find_node(struct trie *t,
935 struct key_vector **tp, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700936{
Alexander Duyck88bae712015-03-06 09:54:52 -0800937 struct key_vector *pn, *n = t->kv;
938 unsigned long index = 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700939
Alexander Duyck88bae712015-03-06 09:54:52 -0800940 do {
941 pn = n;
942 n = get_child_rcu(n, index);
943
944 if (!n)
945 break;
946
947 index = get_cindex(key, n);
Alexander Duyck939afb02014-12-31 10:56:00 -0800948
949 /* This bit of code is a bit tricky but it combines multiple
950 * checks into a single check. The prefix consists of the
951 * prefix plus zeros for the bits in the cindex. The index
952 * is the difference between the key and this value. From
953 * this we can actually derive several pieces of data.
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800954 * if (index >= (1ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800955 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800956 * else
957 * we know the value is cindex
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800958 *
959 * This check is safe even if bits == KEYLENGTH due to the
960 * fact that we can only allocate a node with 32 bits if a
961 * long is greater than 32 bits.
Alexander Duyck939afb02014-12-31 10:56:00 -0800962 */
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800963 if (index >= (1ul << n->bits)) {
964 n = NULL;
965 break;
966 }
Alexander Duyck939afb02014-12-31 10:56:00 -0800967
Alexander Duyck88bae712015-03-06 09:54:52 -0800968 /* keep searching until we find a perfect match leaf or NULL */
969 } while (IS_TNODE(n));
Robert Olsson19baf832005-06-21 12:43:18 -0700970
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800971 *tp = pn;
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800972
Alexander Duyck939afb02014-12-31 10:56:00 -0800973 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700974}
975
Alexander Duyck02525362015-01-22 15:51:39 -0800976/* Return the first fib alias matching TOS with
977 * priority less than or equal to PRIO.
Ido Schimmelb5fc0432019-12-14 17:53:08 +0200978 * If 'find_first' is set, return the first matching
979 * fib alias, regardless of TOS and priority.
Alexander Duyck02525362015-01-22 15:51:39 -0800980 */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800981static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
Ido Schimmelb5fc0432019-12-14 17:53:08 +0200982 u8 tos, u32 prio, u32 tb_id,
983 bool find_first)
Alexander Duyck02525362015-01-22 15:51:39 -0800984{
985 struct fib_alias *fa;
986
987 if (!fah)
988 return NULL;
989
Alexander Duyck56315f92015-02-25 15:31:31 -0800990 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800991 if (fa->fa_slen < slen)
992 continue;
993 if (fa->fa_slen != slen)
994 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -0700995 if (fa->tb_id > tb_id)
996 continue;
997 if (fa->tb_id != tb_id)
998 break;
Ido Schimmelb5fc0432019-12-14 17:53:08 +0200999 if (find_first)
1000 return fa;
Alexander Duyck02525362015-01-22 15:51:39 -08001001 if (fa->fa_tos > tos)
1002 continue;
1003 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
1004 return fa;
1005 }
1006
1007 return NULL;
1008}
1009
Ido Schimmel90b93f12020-01-14 13:23:11 +02001010static struct fib_alias *
1011fib_find_matching_alias(struct net *net, const struct fib_rt_info *fri)
1012{
1013 u8 slen = KEYLENGTH - fri->dst_len;
1014 struct key_vector *l, *tp;
1015 struct fib_table *tb;
1016 struct fib_alias *fa;
1017 struct trie *t;
1018
1019 tb = fib_get_table(net, fri->tb_id);
1020 if (!tb)
1021 return NULL;
1022
1023 t = (struct trie *)tb->tb_data;
1024 l = fib_find_node(t, &tp, be32_to_cpu(fri->dst));
1025 if (!l)
1026 return NULL;
1027
1028 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1029 if (fa->fa_slen == slen && fa->tb_id == fri->tb_id &&
1030 fa->fa_tos == fri->tos && fa->fa_info == fri->fi &&
1031 fa->fa_type == fri->type)
1032 return fa;
1033 }
1034
1035 return NULL;
1036}
1037
1038void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri)
1039{
1040 struct fib_alias *fa_match;
Amit Cohen680aea02021-02-01 21:47:52 +02001041 struct sk_buff *skb;
1042 int err;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001043
1044 rcu_read_lock();
1045
1046 fa_match = fib_find_matching_alias(net, fri);
1047 if (!fa_match)
1048 goto out;
1049
Amit Cohen36c51002021-02-07 10:22:50 +02001050 if (fa_match->offload == fri->offload && fa_match->trap == fri->trap &&
1051 fa_match->offload_failed == fri->offload_failed)
Amit Cohen680aea02021-02-01 21:47:52 +02001052 goto out;
1053
Ido Schimmel90b93f12020-01-14 13:23:11 +02001054 fa_match->offload = fri->offload;
1055 fa_match->trap = fri->trap;
Amit Cohen648106c2021-02-07 10:22:51 +02001056
1057 /* 2 means send notifications only if offload_failed was changed. */
1058 if (net->ipv4.sysctl_fib_notify_on_flag_change == 2 &&
1059 fa_match->offload_failed == fri->offload_failed)
1060 goto out;
1061
Amit Cohen36c51002021-02-07 10:22:50 +02001062 fa_match->offload_failed = fri->offload_failed;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001063
Amit Cohen680aea02021-02-01 21:47:52 +02001064 if (!net->ipv4.sysctl_fib_notify_on_flag_change)
1065 goto out;
1066
1067 skb = nlmsg_new(fib_nlmsg_size(fa_match->fa_info), GFP_ATOMIC);
1068 if (!skb) {
1069 err = -ENOBUFS;
1070 goto errout;
1071 }
1072
1073 err = fib_dump_info(skb, 0, 0, RTM_NEWROUTE, fri, 0);
1074 if (err < 0) {
1075 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
1076 WARN_ON(err == -EMSGSIZE);
1077 kfree_skb(skb);
1078 goto errout;
1079 }
1080
1081 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_ROUTE, NULL, GFP_ATOMIC);
1082 goto out;
1083
1084errout:
1085 rtnl_set_sk_err(net, RTNLGRP_IPV4_ROUTE, err);
Ido Schimmel90b93f12020-01-14 13:23:11 +02001086out:
1087 rcu_read_unlock();
1088}
1089EXPORT_SYMBOL_GPL(fib_alias_hw_flags_set);
1090
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001091static void trie_rebalance(struct trie *t, struct key_vector *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001092{
Alexander Duyck88bae712015-03-06 09:54:52 -08001093 while (!IS_TRIE(tn))
1094 tn = resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001095}
1096
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001097static int fib_insert_node(struct trie *t, struct key_vector *tp,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001098 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001099{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001100 struct key_vector *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -08001101
Alexander Duyckd5d64872015-03-04 15:02:18 -08001102 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001103 if (!l)
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001104 goto noleaf;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001105
1106 /* retrieve child from parent node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001107 n = get_child(tp, get_index(key, tp));
Alexander Duyck836a0122014-12-31 10:56:06 -08001108
1109 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1110 *
1111 * Add a new tnode here
1112 * first tnode need some special handling
1113 * leaves us in position for handling as case 3
1114 */
1115 if (n) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001116 struct key_vector *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001117
Alexander Duycke9b44012014-12-31 10:56:12 -08001118 tn = tnode_new(key, __fls(key ^ n->key), 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001119 if (!tn)
1120 goto notnode;
Olof Johansson91b9a272005-08-09 20:24:39 -07001121
Alexander Duyck836a0122014-12-31 10:56:06 -08001122 /* initialize routes out of node */
1123 NODE_INIT_PARENT(tn, tp);
1124 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001125
Alexander Duyck836a0122014-12-31 10:56:06 -08001126 /* start adding routes into the node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001127 put_child_root(tp, key, tn);
Alexander Duyck836a0122014-12-31 10:56:06 -08001128 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001129
Alexander Duyck836a0122014-12-31 10:56:06 -08001130 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001131 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001132 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001133
Alexander Duyck836a0122014-12-31 10:56:06 -08001134 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duycka52ca622016-12-01 07:27:57 -05001135 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001136 NODE_INIT_PARENT(l, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -08001137 put_child_root(tp, key, l);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001138 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001139
Alexander Duyckd5d64872015-03-04 15:02:18 -08001140 return 0;
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001141notnode:
1142 node_free(l);
1143noleaf:
1144 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07001145}
1146
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001147static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1148 struct key_vector *l, struct fib_alias *new,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001149 struct fib_alias *fa, t_key key)
1150{
1151 if (!l)
1152 return fib_insert_node(t, tp, new, key);
1153
1154 if (fa) {
1155 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1156 } else {
1157 struct fib_alias *last;
1158
1159 hlist_for_each_entry(last, &l->leaf, fa_list) {
1160 if (new->fa_slen < last->fa_slen)
1161 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001162 if ((new->fa_slen == last->fa_slen) &&
1163 (new->tb_id > last->tb_id))
1164 break;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001165 fa = last;
1166 }
1167
1168 if (fa)
1169 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1170 else
1171 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1172 }
1173
1174 /* if we added to the tail node then we need to update slen */
1175 if (l->slen < new->fa_slen) {
1176 l->slen = new->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001177 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001178 }
1179
1180 return 0;
1181}
1182
David Ahern78055992017-05-27 16:19:26 -06001183static bool fib_valid_key_len(u32 key, u8 plen, struct netlink_ext_ack *extack)
David Ahernba277e82017-05-27 16:19:25 -06001184{
David Ahern78055992017-05-27 16:19:26 -06001185 if (plen > KEYLENGTH) {
1186 NL_SET_ERR_MSG(extack, "Invalid prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001187 return false;
David Ahern78055992017-05-27 16:19:26 -06001188 }
David Ahernba277e82017-05-27 16:19:25 -06001189
David Ahern78055992017-05-27 16:19:26 -06001190 if ((plen < KEYLENGTH) && (key << plen)) {
1191 NL_SET_ERR_MSG(extack,
1192 "Invalid prefix for given prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001193 return false;
David Ahern78055992017-05-27 16:19:26 -06001194 }
David Ahernba277e82017-05-27 16:19:25 -06001195
1196 return true;
1197}
1198
Ido Schimmela6c76c12019-12-14 17:53:07 +02001199static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1200 struct key_vector *l, struct fib_alias *old);
1201
Alexander Duyckd5d64872015-03-04 15:02:18 -08001202/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001203int fib_table_insert(struct net *net, struct fib_table *tb,
David Ahern6d8422a12017-05-21 10:12:02 -06001204 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001205{
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001206 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001207 struct fib_alias *fa, *new_fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001208 struct key_vector *l, *tp;
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001209 u16 nlflags = NLM_F_EXCL;
Robert Olsson19baf832005-06-21 12:43:18 -07001210 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001211 u8 plen = cfg->fc_dst_len;
1212 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001213 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001214 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001215 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001216
Thomas Graf4e902c52006-08-17 18:14:52 -07001217 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001218
David Ahern78055992017-05-27 16:19:26 -06001219 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001220 return -EINVAL;
1221
David Ahernba277e82017-05-27 16:19:25 -06001222 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
1223
David Ahern6d8422a12017-05-21 10:12:02 -06001224 fi = fib_create_info(cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001225 if (IS_ERR(fi)) {
1226 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001227 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001228 }
Robert Olsson19baf832005-06-21 12:43:18 -07001229
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001230 l = fib_find_node(t, &tp, key);
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001231 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
Ido Schimmelb5fc0432019-12-14 17:53:08 +02001232 tb->tb_id, false) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001233
1234 /* Now fa, if non-NULL, points to the first fib alias
1235 * with the same keys [prefix,tos,priority], if such key already
1236 * exists or to the node before which we will insert new one.
1237 *
1238 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001239 * insert to the tail of the section matching the suffix length
1240 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001241 */
1242
Julian Anastasov936f6f82008-01-28 21:18:06 -08001243 if (fa && fa->fa_tos == tos &&
1244 fa->fa_info->fib_priority == fi->fib_priority) {
1245 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001246
1247 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001248 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001249 goto out;
1250
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001251 nlflags &= ~NLM_F_EXCL;
1252
Julian Anastasov936f6f82008-01-28 21:18:06 -08001253 /* We have 2 goals:
1254 * 1. Find exact match for type, scope, fib_info to avoid
1255 * duplicate routes
1256 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1257 */
1258 fa_match = NULL;
1259 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001260 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001261 if ((fa->fa_slen != slen) ||
1262 (fa->tb_id != tb->tb_id) ||
1263 (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001264 break;
1265 if (fa->fa_info->fib_priority != fi->fib_priority)
1266 break;
1267 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001268 fa->fa_info == fi) {
1269 fa_match = fa;
1270 break;
1271 }
1272 }
1273
Thomas Graf4e902c52006-08-17 18:14:52 -07001274 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001275 struct fib_info *fi_drop;
1276 u8 state;
1277
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001278 nlflags |= NLM_F_REPLACE;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001279 fa = fa_first;
1280 if (fa_match) {
1281 if (fa == fa_match)
1282 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001283 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001284 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001285 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001286 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001287 if (!new_fa)
Robert Olsson2373ce12005-08-25 13:01:29 -07001288 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001289
1290 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001291 new_fa->fa_tos = fa->fa_tos;
1292 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001293 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001294 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001295 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001296 new_fa->fa_slen = fa->fa_slen;
Michal Kubečekd4e64c22015-05-22 13:40:09 +02001297 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001298 new_fa->fa_default = -1;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001299 new_fa->offload = 0;
1300 new_fa->trap = 0;
Amit Cohen36c51002021-02-07 10:22:50 +02001301 new_fa->offload_failed = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001302
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001303 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1304
Ido Schimmelee3936d2019-12-14 17:53:09 +02001305 if (fib_find_alias(&l->leaf, fa->fa_slen, 0, 0,
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001306 tb->tb_id, true) == new_fa) {
Ido Schimmelee3936d2019-12-14 17:53:09 +02001307 enum fib_event_type fib_event;
1308
Ido Schimmel446f7392019-12-14 17:53:15 +02001309 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmelee3936d2019-12-14 17:53:09 +02001310 err = call_fib_entry_notifiers(net, fib_event,
1311 key, plen,
1312 new_fa, extack);
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001313 if (err) {
1314 hlist_replace_rcu(&new_fa->fa_list,
1315 &fa->fa_list);
Ido Schimmelee3936d2019-12-14 17:53:09 +02001316 goto out_free_new_fa;
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001317 }
Ido Schimmelee3936d2019-12-14 17:53:09 +02001318 }
David Ahernc1d7ee62018-03-27 18:21:58 -07001319
Ido Schimmel5b7d6162017-02-09 10:28:40 +01001320 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1321 tb->tb_id, &cfg->fc_nlinfo, nlflags);
1322
Robert Olsson2373ce12005-08-25 13:01:29 -07001323 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001324
1325 fib_release_info(fi_drop);
1326 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001327 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Jiri Pirkob90eb752016-09-26 12:52:29 +02001328
Olof Johansson91b9a272005-08-09 20:24:39 -07001329 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001330 }
1331 /* Error if we find a perfect match which
1332 * uses the same scope, type, and nexthop
1333 * information.
1334 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001335 if (fa_match)
1336 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001337
Ido Schimmel446f7392019-12-14 17:53:15 +02001338 if (cfg->fc_nlflags & NLM_F_APPEND)
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001339 nlflags |= NLM_F_APPEND;
Ido Schimmel446f7392019-12-14 17:53:15 +02001340 else
Julian Anastasov936f6f82008-01-28 21:18:06 -08001341 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001342 }
1343 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001344 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001345 goto out;
1346
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001347 nlflags |= NLM_F_CREATE;
Robert Olsson19baf832005-06-21 12:43:18 -07001348 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001349 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001350 if (!new_fa)
Robert Olsson19baf832005-06-21 12:43:18 -07001351 goto out;
1352
1353 new_fa->fa_info = fi;
1354 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001355 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001356 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001357 new_fa->fa_slen = slen;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001358 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001359 new_fa->fa_default = -1;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001360 new_fa->offload = 0;
1361 new_fa->trap = 0;
Amit Cohen36c51002021-02-07 10:22:50 +02001362 new_fa->offload_failed = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001363
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001364 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001365 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1366 if (err)
Ido Schimmela6c76c12019-12-14 17:53:07 +02001367 goto out_free_new_fa;
1368
1369 /* The alias was already inserted, so the node must exist. */
1370 l = l ? l : fib_find_node(t, &tp, key);
1371 if (WARN_ON_ONCE(!l))
1372 goto out_free_new_fa;
1373
Ido Schimmela8674f72019-12-14 17:53:10 +02001374 if (fib_find_alias(&l->leaf, new_fa->fa_slen, 0, 0, tb->tb_id, true) ==
1375 new_fa) {
1376 enum fib_event_type fib_event;
1377
Ido Schimmel446f7392019-12-14 17:53:15 +02001378 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmela8674f72019-12-14 17:53:10 +02001379 err = call_fib_entry_notifiers(net, fib_event, key, plen,
1380 new_fa, extack);
1381 if (err)
1382 goto out_remove_new_fa;
1383 }
Robert Olsson19baf832005-06-21 12:43:18 -07001384
David S. Miller21d8c492011-04-14 14:49:37 -07001385 if (!plen)
1386 tb->tb_num_default++;
1387
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001388 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001389 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001390 &cfg->fc_nlinfo, nlflags);
Robert Olsson19baf832005-06-21 12:43:18 -07001391succeeded:
1392 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001393
Ido Schimmela6c76c12019-12-14 17:53:07 +02001394out_remove_new_fa:
1395 fib_remove_alias(t, tp, l, new_fa);
Robert Olssonf835e472005-06-28 15:00:39 -07001396out_free_new_fa:
1397 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001398out:
1399 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001400err:
Robert Olsson19baf832005-06-21 12:43:18 -07001401 return err;
1402}
1403
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001404static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001405{
1406 t_key prefix = n->key;
1407
1408 return (key ^ prefix) & (prefix | -prefix);
1409}
1410
David Ahernaf7888a2020-05-26 12:56:17 -06001411bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
1412 const struct flowi4 *flp)
1413{
1414 if (nhc->nhc_flags & RTNH_F_DEAD)
1415 return false;
1416
1417 if (ip_ignore_linkdown(nhc->nhc_dev) &&
1418 nhc->nhc_flags & RTNH_F_LINKDOWN &&
1419 !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
1420 return false;
1421
1422 if (!(flp->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) {
1423 if (flp->flowi4_oif &&
1424 flp->flowi4_oif != nhc->nhc_oif)
1425 return false;
1426 }
1427
1428 return true;
1429}
1430
Alexander Duyck345e9b52014-12-31 10:56:24 -08001431/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001432int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001433 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001434{
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001435 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001436#ifdef CONFIG_IP_FIB_TRIE_STATS
1437 struct trie_use_stats __percpu *stats = t->stats;
1438#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001439 const t_key key = ntohl(flp->daddr);
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001440 struct key_vector *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001441 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001442 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001443 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001444
Alexander Duyck88bae712015-03-06 09:54:52 -08001445 pn = t->kv;
1446 cindex = 0;
1447
1448 n = get_child_rcu(pn, cindex);
David Ahern9f323972018-05-23 17:08:47 -07001449 if (!n) {
1450 trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001451 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001452 }
Robert Olsson19baf832005-06-21 12:43:18 -07001453
1454#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001455 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001456#endif
1457
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001458 /* Step 1: Travel to the longest prefix match in the trie */
1459 for (;;) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001460 index = get_cindex(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001461
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001462 /* This bit of code is a bit tricky but it combines multiple
1463 * checks into a single check. The prefix consists of the
1464 * prefix plus zeros for the "bits" in the prefix. The index
1465 * is the difference between the key and this value. From
1466 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001467 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001468 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001469 * else
1470 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001471 *
1472 * This check is safe even if bits == KEYLENGTH due to the
1473 * fact that we can only allocate a node with 32 bits if a
1474 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001475 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001476 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001477 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001478
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001479 /* we have found a leaf. Prefixes have already been compared */
1480 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001481 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001482
1483 /* only record pn and cindex if we are going to be chopping
1484 * bits later. Otherwise we are just wasting cycles.
1485 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001486 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001487 pn = n;
1488 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001489 }
1490
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001491 n = get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001492 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001493 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001494 }
1495
1496 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1497 for (;;) {
1498 /* record the pointer where our next node pointer is stored */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001499 struct key_vector __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001500
1501 /* This test verifies that none of the bits that differ
1502 * between the key and the prefix exist in the region of
1503 * the lsb and higher in the prefix.
1504 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001505 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001506 goto backtrace;
1507
1508 /* exit out and process leaf */
1509 if (unlikely(IS_LEAF(n)))
1510 break;
1511
1512 /* Don't bother recording parent info. Since we are in
1513 * prefix match mode we will have to come back to wherever
1514 * we started this traversal anyway
1515 */
1516
1517 while ((n = rcu_dereference(*cptr)) == NULL) {
1518backtrace:
1519#ifdef CONFIG_IP_FIB_TRIE_STATS
1520 if (!n)
1521 this_cpu_inc(stats->null_node_hit);
1522#endif
1523 /* If we are at cindex 0 there are no more bits for
1524 * us to strip at this level so we must ascend back
1525 * up one level to see if there are any more bits to
1526 * be stripped there.
1527 */
1528 while (!cindex) {
1529 t_key pkey = pn->key;
1530
Alexander Duyck88bae712015-03-06 09:54:52 -08001531 /* If we don't have a parent then there is
1532 * nothing for us to do as we do not have any
1533 * further nodes to parse.
1534 */
David Ahern9f323972018-05-23 17:08:47 -07001535 if (IS_TRIE(pn)) {
1536 trace_fib_table_lookup(tb->tb_id, flp,
1537 NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001538 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001539 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001540#ifdef CONFIG_IP_FIB_TRIE_STATS
1541 this_cpu_inc(stats->backtrack);
1542#endif
1543 /* Get Child's index */
Alexander Duyck88bae712015-03-06 09:54:52 -08001544 pn = node_parent_rcu(pn);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001545 cindex = get_index(pkey, pn);
1546 }
1547
1548 /* strip the least significant bit from the cindex */
1549 cindex &= cindex - 1;
1550
1551 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001552 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001553 }
Robert Olsson19baf832005-06-21 12:43:18 -07001554 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001555
Robert Olsson19baf832005-06-21 12:43:18 -07001556found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001557 /* this line carries forward the xor from earlier in the function */
1558 index = key ^ n->key;
1559
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001560 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001561 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1562 struct fib_info *fi = fa->fa_info;
David Ahernaf7888a2020-05-26 12:56:17 -06001563 struct fib_nh_common *nhc;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001564 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001565
Alexander Duycka5829f52016-01-28 13:42:24 -08001566 if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
1567 if (index >= (1ul << fa->fa_slen))
1568 continue;
1569 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001570 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1571 continue;
1572 if (fi->fib_dead)
1573 continue;
1574 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1575 continue;
1576 fib_alias_accessed(fa);
1577 err = fib_props[fa->fa_type].error;
1578 if (unlikely(err < 0)) {
David Ahern4c7e8082019-06-03 20:19:51 -07001579out_reject:
Alexander Duyck345e9b52014-12-31 10:56:24 -08001580#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001581 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001582#endif
David Ahern9f323972018-05-23 17:08:47 -07001583 trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001584 return err;
1585 }
1586 if (fi->fib_flags & RTNH_F_DEAD)
1587 continue;
David Ahern4c7e8082019-06-03 20:19:51 -07001588
David Ahernaf7888a2020-05-26 12:56:17 -06001589 if (unlikely(fi->nh)) {
1590 if (nexthop_is_blackhole(fi->nh)) {
1591 err = fib_props[RTN_BLACKHOLE].error;
1592 goto out_reject;
1593 }
1594
1595 nhc = nexthop_get_nhc_lookup(fi->nh, fib_flags, flp,
1596 &nhsel);
1597 if (nhc)
1598 goto set_result;
1599 goto miss;
David Ahern4c7e8082019-06-03 20:19:51 -07001600 }
1601
David Ahern5481d732019-06-03 20:19:49 -07001602 for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
David Ahernaf7888a2020-05-26 12:56:17 -06001603 nhc = fib_info_nhc(fi, nhsel);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001604
David Ahernaf7888a2020-05-26 12:56:17 -06001605 if (!fib_lookup_good_nhc(nhc, fib_flags, flp))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001606 continue;
David Ahernaf7888a2020-05-26 12:56:17 -06001607set_result:
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001608 if (!(fib_flags & FIB_LOOKUP_NOREF))
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001609 refcount_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001610
David Ahern6ffd9032017-05-25 10:42:37 -07001611 res->prefix = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001612 res->prefixlen = KEYLENGTH - fa->fa_slen;
1613 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001614 res->nhc = nhc;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001615 res->type = fa->fa_type;
1616 res->scope = fi->fib_scope;
1617 res->fi = fi;
1618 res->table = tb;
1619 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001620#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001621 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001622#endif
David Aherneba618a2019-04-02 14:11:55 -07001623 trace_fib_table_lookup(tb->tb_id, flp, nhc, err);
David Ahernf6d3c192015-08-28 08:42:09 -07001624
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001625 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001626 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001627 }
David Ahernaf7888a2020-05-26 12:56:17 -06001628miss:
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001629#ifdef CONFIG_IP_FIB_TRIE_STATS
1630 this_cpu_inc(stats->semantic_match_miss);
1631#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001632 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001633}
Florian Westphal6fc01432011-08-25 13:46:12 +02001634EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001635
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001636static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1637 struct key_vector *l, struct fib_alias *old)
Alexander Duyckd5d64872015-03-04 15:02:18 -08001638{
1639 /* record the location of the previous list_info entry */
1640 struct hlist_node **pprev = old->fa_list.pprev;
1641 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1642
1643 /* remove the fib_alias from the list */
1644 hlist_del_rcu(&old->fa_list);
1645
1646 /* if we emptied the list this leaf will be freed and we can sort
1647 * out parent suffix lengths as a part of trie_rebalance
1648 */
1649 if (hlist_empty(&l->leaf)) {
Alexander Duycka52ca622016-12-01 07:27:57 -05001650 if (tp->slen == l->slen)
1651 node_pull_suffix(tp, tp->pos);
Alexander Duyck88bae712015-03-06 09:54:52 -08001652 put_child_root(tp, l->key, NULL);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001653 node_free(l);
1654 trie_rebalance(t, tp);
1655 return;
1656 }
1657
1658 /* only access fa if it is pointing at the last valid hlist_node */
1659 if (*pprev)
1660 return;
1661
1662 /* update the trie with the latest suffix length */
1663 l->slen = fa->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001664 node_pull_suffix(tp, fa->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001665}
1666
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001667static void fib_notify_alias_delete(struct net *net, u32 key,
1668 struct hlist_head *fah,
1669 struct fib_alias *fa_to_delete,
1670 struct netlink_ext_ack *extack)
1671{
1672 struct fib_alias *fa_next, *fa_to_notify;
1673 u32 tb_id = fa_to_delete->tb_id;
1674 u8 slen = fa_to_delete->fa_slen;
1675 enum fib_event_type fib_event;
1676
1677 /* Do not notify if we do not care about the route. */
1678 if (fib_find_alias(fah, slen, 0, 0, tb_id, true) != fa_to_delete)
1679 return;
1680
1681 /* Determine if the route should be replaced by the next route in the
1682 * list.
1683 */
1684 fa_next = hlist_entry_safe(fa_to_delete->fa_list.next,
1685 struct fib_alias, fa_list);
1686 if (fa_next && fa_next->fa_slen == slen && fa_next->tb_id == tb_id) {
Ido Schimmel446f7392019-12-14 17:53:15 +02001687 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001688 fa_to_notify = fa_next;
1689 } else {
Ido Schimmel446f7392019-12-14 17:53:15 +02001690 fib_event = FIB_EVENT_ENTRY_DEL;
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001691 fa_to_notify = fa_to_delete;
1692 }
1693 call_fib_entry_notifiers(net, fib_event, key, KEYLENGTH - slen,
1694 fa_to_notify, extack);
1695}
1696
Alexander Duyckd5d64872015-03-04 15:02:18 -08001697/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001698int fib_table_delete(struct net *net, struct fib_table *tb,
David Ahern78055992017-05-27 16:19:26 -06001699 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001700{
1701 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001702 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001703 struct key_vector *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001704 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001705 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001706 u8 tos = cfg->fc_tos;
1707 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001708
Thomas Graf4e902c52006-08-17 18:14:52 -07001709 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001710
David Ahern78055992017-05-27 16:19:26 -06001711 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001712 return -EINVAL;
1713
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001714 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001715 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001716 return -ESRCH;
1717
Ido Schimmelb5fc0432019-12-14 17:53:08 +02001718 fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id, false);
Robert Olsson19baf832005-06-21 12:43:18 -07001719 if (!fa)
1720 return -ESRCH;
1721
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001722 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001723
1724 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001725 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001726 struct fib_info *fi = fa->fa_info;
1727
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001728 if ((fa->fa_slen != slen) ||
1729 (fa->tb_id != tb->tb_id) ||
1730 (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001731 break;
1732
Thomas Graf4e902c52006-08-17 18:14:52 -07001733 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1734 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001735 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001736 (!cfg->fc_prefsrc ||
1737 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001738 (!cfg->fc_protocol ||
1739 fi->fib_protocol == cfg->fc_protocol) &&
Alexander Aringfaee6762020-03-27 18:00:21 -04001740 fib_nh_match(net, cfg, fi, extack) == 0 &&
Xin Long5f9ae3d2017-08-23 10:07:26 +08001741 fib_metrics_match(cfg, fi)) {
Robert Olsson19baf832005-06-21 12:43:18 -07001742 fa_to_delete = fa;
1743 break;
1744 }
1745 }
1746
Olof Johansson91b9a272005-08-09 20:24:39 -07001747 if (!fa_to_delete)
1748 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001749
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001750 fib_notify_alias_delete(net, key, &l->leaf, fa_to_delete, extack);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001751 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001752 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001753
David S. Miller21d8c492011-04-14 14:49:37 -07001754 if (!plen)
1755 tb->tb_num_default--;
1756
Alexander Duyckd5d64872015-03-04 15:02:18 -08001757 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001758
Alexander Duyckd5d64872015-03-04 15:02:18 -08001759 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001760 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001761
Alexander Duyckd5d64872015-03-04 15:02:18 -08001762 fib_release_info(fa_to_delete->fa_info);
1763 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001764 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001765}
1766
Alexander Duyck8be33e92015-03-04 14:59:19 -08001767/* Scan for the next leaf starting at the provided key value */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001768static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001769{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001770 struct key_vector *pn, *n = *tn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001771 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001772
Alexander Duyck8be33e92015-03-04 14:59:19 -08001773 /* this loop is meant to try and find the key in the trie */
Alexander Duyck88bae712015-03-06 09:54:52 -08001774 do {
1775 /* record parent and next child index */
1776 pn = n;
Alexander Duyckc2229fe2015-10-27 15:06:45 -07001777 cindex = (key > pn->key) ? get_index(key, pn) : 0;
Alexander Duyck88bae712015-03-06 09:54:52 -08001778
1779 if (cindex >> pn->bits)
1780 break;
1781
1782 /* descend into the next child */
1783 n = get_child_rcu(pn, cindex++);
1784 if (!n)
1785 break;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001786
Alexander Duyck8be33e92015-03-04 14:59:19 -08001787 /* guarantee forward progress on the keys */
1788 if (IS_LEAF(n) && (n->key >= key))
1789 goto found;
Alexander Duyck88bae712015-03-06 09:54:52 -08001790 } while (IS_TNODE(n));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001791
1792 /* this loop will search for the next leaf with a greater key */
Alexander Duyck88bae712015-03-06 09:54:52 -08001793 while (!IS_TRIE(pn)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001794 /* if we exhausted the parent node we will need to climb */
1795 if (cindex >= (1ul << pn->bits)) {
1796 t_key pkey = pn->key;
1797
1798 pn = node_parent_rcu(pn);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001799 cindex = get_index(pkey, pn) + 1;
1800 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001801 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001802
Alexander Duyck8be33e92015-03-04 14:59:19 -08001803 /* grab the next available node */
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001804 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001805 if (!n)
1806 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001807
Alexander Duyck8be33e92015-03-04 14:59:19 -08001808 /* no need to compare keys since we bumped the index */
1809 if (IS_LEAF(n))
1810 goto found;
1811
1812 /* Rescan start scanning in new node */
1813 pn = n;
1814 cindex = 0;
1815 }
1816
1817 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001818 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001819found:
1820 /* if we are at the limit for keys just return NULL for the tnode */
Alexander Duyck88bae712015-03-06 09:54:52 -08001821 *tn = pn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001822 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001823}
1824
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001825static void fib_trie_free(struct fib_table *tb)
1826{
1827 struct trie *t = (struct trie *)tb->tb_data;
1828 struct key_vector *pn = t->kv;
1829 unsigned long cindex = 1;
1830 struct hlist_node *tmp;
1831 struct fib_alias *fa;
1832
1833 /* walk trie in reverse order and free everything */
1834 for (;;) {
1835 struct key_vector *n;
1836
1837 if (!(cindex--)) {
1838 t_key pkey = pn->key;
1839
1840 if (IS_TRIE(pn))
1841 break;
1842
1843 n = pn;
1844 pn = node_parent(pn);
1845
1846 /* drop emptied tnode */
1847 put_child_root(pn, n->key, NULL);
1848 node_free(n);
1849
1850 cindex = get_index(pkey, pn);
1851
1852 continue;
1853 }
1854
1855 /* grab the next available node */
1856 n = get_child(pn, cindex);
1857 if (!n)
1858 continue;
1859
1860 if (IS_TNODE(n)) {
1861 /* record pn and cindex for leaf walking */
1862 pn = n;
1863 cindex = 1ul << n->bits;
1864
1865 continue;
1866 }
1867
1868 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1869 hlist_del_rcu(&fa->fa_list);
1870 alias_free_mem_rcu(fa);
1871 }
1872
1873 put_child_root(pn, n->key, NULL);
1874 node_free(n);
1875 }
1876
1877#ifdef CONFIG_IP_FIB_TRIE_STATS
1878 free_percpu(t->stats);
1879#endif
1880 kfree(tb);
1881}
1882
1883struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
1884{
1885 struct trie *ot = (struct trie *)oldtb->tb_data;
1886 struct key_vector *l, *tp = ot->kv;
1887 struct fib_table *local_tb;
1888 struct fib_alias *fa;
1889 struct trie *lt;
1890 t_key key = 0;
1891
1892 if (oldtb->tb_data == oldtb->__data)
1893 return oldtb;
1894
1895 local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
1896 if (!local_tb)
1897 return NULL;
1898
1899 lt = (struct trie *)local_tb->tb_data;
1900
1901 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1902 struct key_vector *local_l = NULL, *local_tp;
1903
Ido Schimmel83f35222020-07-29 11:37:13 +03001904 hlist_for_each_entry(fa, &l->leaf, fa_list) {
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001905 struct fib_alias *new_fa;
1906
1907 if (local_tb->tb_id != fa->tb_id)
1908 continue;
1909
1910 /* clone fa for new local table */
1911 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1912 if (!new_fa)
1913 goto out;
1914
1915 memcpy(new_fa, fa, sizeof(*fa));
1916
1917 /* insert clone into table */
1918 if (!local_l)
1919 local_l = fib_find_node(lt, &local_tp, l->key);
1920
1921 if (fib_insert_alias(lt, local_tp, local_l, new_fa,
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001922 NULL, l->key)) {
1923 kmem_cache_free(fn_alias_kmem, new_fa);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001924 goto out;
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001925 }
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001926 }
1927
1928 /* stop loop if key wrapped back to 0 */
1929 key = l->key + 1;
1930 if (key < l->key)
1931 break;
1932 }
1933
1934 return local_tb;
1935out:
1936 fib_trie_free(local_tb);
1937
1938 return NULL;
1939}
1940
Alexander Duyck3b709332016-11-15 05:46:06 -05001941/* Caller must hold RTNL */
1942void fib_table_flush_external(struct fib_table *tb)
1943{
1944 struct trie *t = (struct trie *)tb->tb_data;
1945 struct key_vector *pn = t->kv;
1946 unsigned long cindex = 1;
1947 struct hlist_node *tmp;
1948 struct fib_alias *fa;
1949
1950 /* walk trie in reverse order */
1951 for (;;) {
1952 unsigned char slen = 0;
1953 struct key_vector *n;
1954
1955 if (!(cindex--)) {
1956 t_key pkey = pn->key;
1957
1958 /* cannot resize the trie vector */
1959 if (IS_TRIE(pn))
1960 break;
1961
Alexander Duycka52ca622016-12-01 07:27:57 -05001962 /* update the suffix to address pulled leaves */
1963 if (pn->slen > pn->pos)
1964 update_suffix(pn);
1965
Alexander Duyck3b709332016-11-15 05:46:06 -05001966 /* resize completed node */
1967 pn = resize(t, pn);
1968 cindex = get_index(pkey, pn);
1969
1970 continue;
1971 }
1972
1973 /* grab the next available node */
1974 n = get_child(pn, cindex);
1975 if (!n)
1976 continue;
1977
1978 if (IS_TNODE(n)) {
1979 /* record pn and cindex for leaf walking */
1980 pn = n;
1981 cindex = 1ul << n->bits;
1982
1983 continue;
1984 }
1985
1986 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1987 /* if alias was cloned to local then we just
1988 * need to remove the local copy from main
1989 */
1990 if (tb->tb_id != fa->tb_id) {
1991 hlist_del_rcu(&fa->fa_list);
1992 alias_free_mem_rcu(fa);
1993 continue;
1994 }
1995
1996 /* record local slen */
1997 slen = fa->fa_slen;
1998 }
1999
2000 /* update leaf slen */
2001 n->slen = slen;
2002
2003 if (hlist_empty(&n->leaf)) {
2004 put_child_root(pn, n->key, NULL);
2005 node_free(n);
2006 }
2007 }
2008}
2009
Alexander Duyck8be33e92015-03-04 14:59:19 -08002010/* Caller must hold RTNL. */
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00002011int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
Robert Olsson19baf832005-06-21 12:43:18 -07002012{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002013 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002014 struct key_vector *pn = t->kv;
2015 unsigned long cindex = 1;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002016 struct hlist_node *tmp;
2017 struct fib_alias *fa;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08002018 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002019
Alexander Duyck88bae712015-03-06 09:54:52 -08002020 /* walk trie in reverse order */
2021 for (;;) {
2022 unsigned char slen = 0;
2023 struct key_vector *n;
Robert Olsson19baf832005-06-21 12:43:18 -07002024
Alexander Duyck88bae712015-03-06 09:54:52 -08002025 if (!(cindex--)) {
2026 t_key pkey = pn->key;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002027
Alexander Duyck88bae712015-03-06 09:54:52 -08002028 /* cannot resize the trie vector */
2029 if (IS_TRIE(pn))
2030 break;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002031
Alexander Duycka52ca622016-12-01 07:27:57 -05002032 /* update the suffix to address pulled leaves */
2033 if (pn->slen > pn->pos)
2034 update_suffix(pn);
2035
Alexander Duyck88bae712015-03-06 09:54:52 -08002036 /* resize completed node */
2037 pn = resize(t, pn);
2038 cindex = get_index(pkey, pn);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002039
Alexander Duyck88bae712015-03-06 09:54:52 -08002040 continue;
2041 }
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002042
Alexander Duyck88bae712015-03-06 09:54:52 -08002043 /* grab the next available node */
2044 n = get_child(pn, cindex);
2045 if (!n)
2046 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002047
Alexander Duyck88bae712015-03-06 09:54:52 -08002048 if (IS_TNODE(n)) {
2049 /* record pn and cindex for leaf walking */
2050 pn = n;
2051 cindex = 1ul << n->bits;
2052
2053 continue;
2054 }
2055
2056 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
2057 struct fib_info *fi = fa->fa_info;
2058
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00002059 if (!fi || tb->tb_id != fa->tb_id ||
2060 (!(fi->fib_flags & RTNH_F_DEAD) &&
2061 !fib_props[fa->fa_type].error)) {
2062 slen = fa->fa_slen;
2063 continue;
2064 }
2065
2066 /* Do not flush error routes if network namespace is
2067 * not being dismantled
2068 */
2069 if (!flush_all && fib_props[fa->fa_type].error) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002070 slen = fa->fa_slen;
2071 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002072 }
2073
Ido Schimmel525bc342019-12-14 17:53:12 +02002074 fib_notify_alias_delete(net, n->key, &n->leaf, fa,
2075 NULL);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002076 hlist_del_rcu(&fa->fa_list);
2077 fib_release_info(fa->fa_info);
2078 alias_free_mem_rcu(fa);
2079 found++;
Alexander Duyck64c62722015-01-22 15:51:45 -08002080 }
2081
Alexander Duyck88bae712015-03-06 09:54:52 -08002082 /* update leaf slen */
2083 n->slen = slen;
2084
2085 if (hlist_empty(&n->leaf)) {
2086 put_child_root(pn, n->key, NULL);
2087 node_free(n);
Alexander Duyck88bae712015-03-06 09:54:52 -08002088 }
Robert Olsson19baf832005-06-21 12:43:18 -07002089 }
2090
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07002091 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07002092 return found;
2093}
2094
David Ahern1bff1a02019-05-22 12:04:42 -07002095/* derived from fib_trie_free */
2096static void __fib_info_notify_update(struct net *net, struct fib_table *tb,
2097 struct nl_info *info)
2098{
2099 struct trie *t = (struct trie *)tb->tb_data;
2100 struct key_vector *pn = t->kv;
2101 unsigned long cindex = 1;
2102 struct fib_alias *fa;
2103
2104 for (;;) {
2105 struct key_vector *n;
2106
2107 if (!(cindex--)) {
2108 t_key pkey = pn->key;
2109
2110 if (IS_TRIE(pn))
2111 break;
2112
David Ahern1bff1a02019-05-22 12:04:42 -07002113 pn = node_parent(pn);
2114 cindex = get_index(pkey, pn);
2115 continue;
2116 }
2117
2118 /* grab the next available node */
2119 n = get_child(pn, cindex);
2120 if (!n)
2121 continue;
2122
2123 if (IS_TNODE(n)) {
2124 /* record pn and cindex for leaf walking */
2125 pn = n;
2126 cindex = 1ul << n->bits;
2127
2128 continue;
2129 }
2130
2131 hlist_for_each_entry(fa, &n->leaf, fa_list) {
2132 struct fib_info *fi = fa->fa_info;
2133
2134 if (!fi || !fi->nh_updated || fa->tb_id != tb->tb_id)
2135 continue;
2136
2137 rtmsg_fib(RTM_NEWROUTE, htonl(n->key), fa,
2138 KEYLENGTH - fa->fa_slen, tb->tb_id,
2139 info, NLM_F_REPLACE);
David Ahern1bff1a02019-05-22 12:04:42 -07002140 }
2141 }
2142}
2143
2144void fib_info_notify_update(struct net *net, struct nl_info *info)
2145{
2146 unsigned int h;
2147
2148 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2149 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2150 struct fib_table *tb;
2151
Ido Schimmel7f6f32b2020-08-26 19:48:10 +03002152 hlist_for_each_entry_rcu(tb, head, tb_hlist,
2153 lockdep_rtnl_is_held())
David Ahern1bff1a02019-05-22 12:04:42 -07002154 __fib_info_notify_update(net, tb, info);
2155 }
2156}
2157
Jiri Pirko55c894f2019-10-03 11:49:28 +02002158static int fib_leaf_notify(struct key_vector *l, struct fib_table *tb,
Jiri Pirkob7a59552019-10-03 11:49:30 +02002159 struct notifier_block *nb,
2160 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002161{
2162 struct fib_alias *fa;
Ido Schimmel20d15652019-12-14 17:53:13 +02002163 int last_slen = -1;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002164 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002165
2166 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2167 struct fib_info *fi = fa->fa_info;
2168
2169 if (!fi)
2170 continue;
2171
2172 /* local and main table can share the same trie,
2173 * so don't notify twice for the same entry.
2174 */
2175 if (tb->tb_id != fa->tb_id)
2176 continue;
2177
Ido Schimmel20d15652019-12-14 17:53:13 +02002178 if (fa->fa_slen == last_slen)
2179 continue;
2180
2181 last_slen = fa->fa_slen;
Ido Schimmel446f7392019-12-14 17:53:15 +02002182 err = call_fib_entry_notifier(nb, FIB_EVENT_ENTRY_REPLACE,
Ido Schimmel20d15652019-12-14 17:53:13 +02002183 l->key, KEYLENGTH - fa->fa_slen,
2184 fa, extack);
2185 if (err)
2186 return err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002187 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002188 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002189}
2190
Jiri Pirkob7a59552019-10-03 11:49:30 +02002191static int fib_table_notify(struct fib_table *tb, struct notifier_block *nb,
2192 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002193{
2194 struct trie *t = (struct trie *)tb->tb_data;
2195 struct key_vector *l, *tp = t->kv;
2196 t_key key = 0;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002197 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002198
2199 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Jiri Pirkob7a59552019-10-03 11:49:30 +02002200 err = fib_leaf_notify(l, tb, nb, extack);
Jiri Pirko55c894f2019-10-03 11:49:28 +02002201 if (err)
2202 return err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002203
2204 key = l->key + 1;
2205 /* stop in case of wrap around */
2206 if (key < l->key)
2207 break;
2208 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002209 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002210}
2211
Jiri Pirkob7a59552019-10-03 11:49:30 +02002212int fib_notify(struct net *net, struct notifier_block *nb,
2213 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002214{
2215 unsigned int h;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002216 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002217
2218 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2219 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2220 struct fib_table *tb;
2221
Jiri Pirko55c894f2019-10-03 11:49:28 +02002222 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Jiri Pirkob7a59552019-10-03 11:49:30 +02002223 err = fib_table_notify(tb, nb, extack);
Jiri Pirko55c894f2019-10-03 11:49:28 +02002224 if (err)
2225 return err;
2226 }
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002227 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002228 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002229}
2230
Alexander Duycka7e53532015-03-04 15:02:44 -08002231static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002232{
Alexander Duycka7e53532015-03-04 15:02:44 -08002233 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08002234#ifdef CONFIG_IP_FIB_TRIE_STATS
2235 struct trie *t = (struct trie *)tb->tb_data;
2236
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002237 if (tb->tb_data == tb->__data)
2238 free_percpu(t->stats);
Alexander Duyck8274a972014-12-31 10:55:29 -08002239#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002240 kfree(tb);
2241}
2242
Alexander Duycka7e53532015-03-04 15:02:44 -08002243void fib_free_table(struct fib_table *tb)
2244{
2245 call_rcu(&tb->rcu, __trie_free_rcu);
2246}
2247
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002248static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
David Ahern18a80212018-10-15 18:56:43 -07002249 struct sk_buff *skb, struct netlink_callback *cb,
2250 struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002251{
David Ahern18a80212018-10-15 18:56:43 -07002252 unsigned int flags = NLM_F_MULTI;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002253 __be32 xkey = htonl(l->key);
Stefano Brivioee289062019-06-21 17:45:23 +02002254 int i, s_i, i_fa, s_fa, err;
Robert Olsson19baf832005-06-21 12:43:18 -07002255 struct fib_alias *fa;
Robert Olsson19baf832005-06-21 12:43:18 -07002256
Stefano Brivioee289062019-06-21 17:45:23 +02002257 if (filter->filter_set ||
2258 !filter->dump_exceptions || !filter->dump_routes)
David Ahern18a80212018-10-15 18:56:43 -07002259 flags |= NLM_F_DUMP_FILTERED;
2260
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002261 s_i = cb->args[4];
Stefano Brivioee289062019-06-21 17:45:23 +02002262 s_fa = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07002263 i = 0;
2264
Robert Olsson2373ce12005-08-25 13:01:29 -07002265 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002266 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
Stefano Brivioee289062019-06-21 17:45:23 +02002267 struct fib_info *fi = fa->fa_info;
David Ahernf6c57752017-05-15 23:19:17 -07002268
David Ahern18a80212018-10-15 18:56:43 -07002269 if (i < s_i)
2270 goto next;
Robert Olsson19baf832005-06-21 12:43:18 -07002271
Stefano Brivioee289062019-06-21 17:45:23 +02002272 i_fa = 0;
2273
David Ahern18a80212018-10-15 18:56:43 -07002274 if (tb->tb_id != fa->tb_id)
2275 goto next;
2276
2277 if (filter->filter_set) {
2278 if (filter->rt_type && fa->fa_type != filter->rt_type)
2279 goto next;
2280
2281 if ((filter->protocol &&
Stefano Brivioee289062019-06-21 17:45:23 +02002282 fi->fib_protocol != filter->protocol))
David Ahern18a80212018-10-15 18:56:43 -07002283 goto next;
2284
2285 if (filter->dev &&
Stefano Brivioee289062019-06-21 17:45:23 +02002286 !fib_info_nh_uses_dev(fi, filter->dev))
David Ahern18a80212018-10-15 18:56:43 -07002287 goto next;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002288 }
2289
Stefano Brivio885b8b42019-06-29 19:55:08 +02002290 if (filter->dump_routes) {
2291 if (!s_fa) {
Ido Schimmel1e301fd2020-01-14 13:23:10 +02002292 struct fib_rt_info fri;
2293
2294 fri.fi = fi;
2295 fri.tb_id = tb->tb_id;
2296 fri.dst = xkey;
2297 fri.dst_len = KEYLENGTH - fa->fa_slen;
2298 fri.tos = fa->fa_tos;
2299 fri.type = fa->fa_type;
Ido Schimmel90b93f12020-01-14 13:23:11 +02002300 fri.offload = fa->offload;
2301 fri.trap = fa->trap;
Amit Cohen36c51002021-02-07 10:22:50 +02002302 fri.offload_failed = fa->offload_failed;
Stefano Brivio885b8b42019-06-29 19:55:08 +02002303 err = fib_dump_info(skb,
2304 NETLINK_CB(cb->skb).portid,
2305 cb->nlh->nlmsg_seq,
Ido Schimmel1e301fd2020-01-14 13:23:10 +02002306 RTM_NEWROUTE, &fri, flags);
Stefano Brivio885b8b42019-06-29 19:55:08 +02002307 if (err < 0)
2308 goto stop;
2309 }
2310
Stefano Brivioee289062019-06-21 17:45:23 +02002311 i_fa++;
Robert Olsson19baf832005-06-21 12:43:18 -07002312 }
Stefano Brivioee289062019-06-21 17:45:23 +02002313
2314 if (filter->dump_exceptions) {
2315 err = fib_dump_info_fnhe(skb, cb, tb->tb_id, fi,
John Fastabende93fb3e2019-08-23 17:11:38 -07002316 &i_fa, s_fa, flags);
Stefano Brivioee289062019-06-21 17:45:23 +02002317 if (err < 0)
2318 goto stop;
2319 }
2320
David Ahern18a80212018-10-15 18:56:43 -07002321next:
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002322 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07002323 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002324
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002325 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07002326 return skb->len;
Stefano Brivioee289062019-06-21 17:45:23 +02002327
2328stop:
2329 cb->args[4] = i;
2330 cb->args[5] = i_fa;
2331 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002332}
2333
Alexander Duycka7e53532015-03-04 15:02:44 -08002334/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00002335int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
David Ahern18a80212018-10-15 18:56:43 -07002336 struct netlink_callback *cb, struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002337{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002338 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002339 struct key_vector *l, *tp = t->kv;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002340 /* Dump starting at last key.
2341 * Note: 0.0.0.0/0 (ie default) is first key.
2342 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002343 int count = cb->args[2];
2344 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002345
David Ahern9827c062020-01-10 09:03:58 -08002346 /* First time here, count and key are both always 0. Count > 0
2347 * and key == 0 means the dump has wrapped around and we are done.
2348 */
2349 if (count && !key)
2350 return skb->len;
2351
Alexander Duyck8be33e92015-03-04 14:59:19 -08002352 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
David Ahernf6c57752017-05-15 23:19:17 -07002353 int err;
2354
David Ahern18a80212018-10-15 18:56:43 -07002355 err = fn_trie_dump_leaf(l, tb, skb, cb, filter);
David Ahernf6c57752017-05-15 23:19:17 -07002356 if (err < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002357 cb->args[3] = key;
2358 cb->args[2] = count;
David Ahernf6c57752017-05-15 23:19:17 -07002359 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002360 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002361
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002362 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002363 key = l->key + 1;
2364
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002365 memset(&cb->args[4], 0,
2366 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08002367
2368 /* stop loop if key wrapped back to 0 */
2369 if (key < l->key)
2370 break;
Robert Olsson19baf832005-06-21 12:43:18 -07002371 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08002372
Alexander Duyck8be33e92015-03-04 14:59:19 -08002373 cb->args[3] = key;
2374 cb->args[2] = count;
2375
Robert Olsson19baf832005-06-21 12:43:18 -07002376 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07002377}
2378
David S. Miller5348ba82011-02-01 15:30:56 -08002379void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002380{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002381 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
2382 sizeof(struct fib_alias),
Vasily Averin61268912021-07-19 13:44:31 +03002383 0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002384
2385 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002386 LEAF_SIZE,
Vasily Averin61268912021-07-19 13:44:31 +03002387 0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002388}
Robert Olsson19baf832005-06-21 12:43:18 -07002389
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002390struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
Robert Olsson19baf832005-06-21 12:43:18 -07002391{
2392 struct fib_table *tb;
2393 struct trie *t;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002394 size_t sz = sizeof(*tb);
Robert Olsson19baf832005-06-21 12:43:18 -07002395
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002396 if (!alias)
2397 sz += sizeof(struct trie);
2398
2399 tb = kzalloc(sz, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002400 if (!tb)
Robert Olsson19baf832005-06-21 12:43:18 -07002401 return NULL;
2402
2403 tb->tb_id = id;
David S. Miller21d8c492011-04-14 14:49:37 -07002404 tb->tb_num_default = 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002405 tb->tb_data = (alias ? alias->__data : tb->__data);
2406
2407 if (alias)
2408 return tb;
Robert Olsson19baf832005-06-21 12:43:18 -07002409
2410 t = (struct trie *) tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002411 t->kv[0].pos = KEYLENGTH;
2412 t->kv[0].slen = KEYLENGTH;
Alexander Duyck8274a972014-12-31 10:55:29 -08002413#ifdef CONFIG_IP_FIB_TRIE_STATS
2414 t->stats = alloc_percpu(struct trie_use_stats);
2415 if (!t->stats) {
2416 kfree(tb);
2417 tb = NULL;
2418 }
2419#endif
Robert Olsson19baf832005-06-21 12:43:18 -07002420
Robert Olsson19baf832005-06-21 12:43:18 -07002421 return tb;
2422}
2423
Robert Olsson19baf832005-06-21 12:43:18 -07002424#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002425/* Depth first Trie walk iterator */
2426struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002427 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002428 struct fib_table *tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002429 struct key_vector *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002430 unsigned int index;
2431 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002432};
Robert Olsson19baf832005-06-21 12:43:18 -07002433
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002434static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07002435{
Alexander Duyck98293e82014-12-31 10:56:18 -08002436 unsigned long cindex = iter->index;
Alexander Duyck88bae712015-03-06 09:54:52 -08002437 struct key_vector *pn = iter->tnode;
2438 t_key pkey;
Eric W. Biederman6640e692007-01-24 14:42:04 -08002439
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002440 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
2441 iter->tnode, iter->index, iter->depth);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002442
Alexander Duyck88bae712015-03-06 09:54:52 -08002443 while (!IS_TRIE(pn)) {
2444 while (cindex < child_length(pn)) {
2445 struct key_vector *n = get_child_rcu(pn, cindex++);
2446
2447 if (!n)
2448 continue;
2449
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002450 if (IS_LEAF(n)) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002451 iter->tnode = pn;
2452 iter->index = cindex;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002453 } else {
2454 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08002455 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002456 iter->index = 0;
2457 ++iter->depth;
2458 }
Alexander Duyck88bae712015-03-06 09:54:52 -08002459
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002460 return n;
2461 }
2462
Alexander Duyck88bae712015-03-06 09:54:52 -08002463 /* Current node exhausted, pop back up */
2464 pkey = pn->key;
2465 pn = node_parent_rcu(pn);
2466 cindex = get_index(pkey, pn) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002467 --iter->depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002468 }
2469
Alexander Duyck88bae712015-03-06 09:54:52 -08002470 /* record root node so further searches know we are done */
2471 iter->tnode = pn;
2472 iter->index = 0;
2473
Robert Olsson19baf832005-06-21 12:43:18 -07002474 return NULL;
2475}
2476
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002477static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
2478 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07002479{
Firo Yangf38b24c2015-06-08 11:54:51 +08002480 struct key_vector *n, *pn;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002481
Stephen Hemminger132adf52007-03-08 20:44:43 -08002482 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002483 return NULL;
2484
Firo Yangf38b24c2015-06-08 11:54:51 +08002485 pn = t->kv;
Alexander Duyck88bae712015-03-06 09:54:52 -08002486 n = rcu_dereference(pn->tnode[0]);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002487 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002488 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002489
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002490 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002491 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002492 iter->index = 0;
2493 iter->depth = 1;
2494 } else {
Alexander Duyck88bae712015-03-06 09:54:52 -08002495 iter->tnode = pn;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002496 iter->index = 0;
2497 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002498 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002499
2500 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002501}
2502
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002503static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07002504{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002505 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002506 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07002507
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002508 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07002509
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002510 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002511 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002512 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002513 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08002514
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002515 s->leaves++;
2516 s->totdepth += iter.depth;
2517 if (iter.depth > s->maxdepth)
2518 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08002519
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002520 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08002521 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002522 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002523 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002524 if (n->bits < MAX_STAT_DEPTH)
2525 s->nodesizes[n->bits]++;
Alexander Duyck6e22d172015-03-06 09:54:39 -08002526 s->nullpointers += tn_info(n)->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002527 }
2528 }
2529 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002530}
2531
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002532/*
Robert Olsson19baf832005-06-21 12:43:18 -07002533 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002534 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002535static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002536{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002537 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002538
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002539 if (stat->leaves)
2540 avdepth = stat->totdepth*100 / stat->leaves;
2541 else
2542 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002543
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002544 seq_printf(seq, "\tAver depth: %u.%02d\n",
2545 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002546 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002547
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002548 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002549 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002550
2551 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002552 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08002553
Stephen Hemminger187b5182008-01-12 20:55:55 -08002554 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002555 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002556
Robert Olsson06ef9212006-03-20 21:35:01 -08002557 max = MAX_STAT_DEPTH;
2558 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002559 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002560
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002561 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002562 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002563 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002564 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002565 pointers += (1<<i) * stat->nodesizes[i];
2566 }
2567 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002568 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002569
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002570 bytes += sizeof(struct key_vector *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002571 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2572 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002573}
Robert Olsson19baf832005-06-21 12:43:18 -07002574
2575#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002576static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002577 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002578{
Alexander Duyck8274a972014-12-31 10:55:29 -08002579 struct trie_use_stats s = { 0 };
2580 int cpu;
2581
2582 /* loop through all of the CPUs and gather up the stats */
2583 for_each_possible_cpu(cpu) {
2584 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2585
2586 s.gets += pcpu->gets;
2587 s.backtrack += pcpu->backtrack;
2588 s.semantic_match_passed += pcpu->semantic_match_passed;
2589 s.semantic_match_miss += pcpu->semantic_match_miss;
2590 s.null_node_hit += pcpu->null_node_hit;
2591 s.resize_node_skipped += pcpu->resize_node_skipped;
2592 }
2593
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002594 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002595 seq_printf(seq, "gets = %u\n", s.gets);
2596 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002597 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002598 s.semantic_match_passed);
2599 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2600 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2601 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002602}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002603#endif /* CONFIG_IP_FIB_TRIE_STATS */
2604
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002605static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002606{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002607 if (tb->tb_id == RT_TABLE_LOCAL)
2608 seq_puts(seq, "Local:\n");
2609 else if (tb->tb_id == RT_TABLE_MAIN)
2610 seq_puts(seq, "Main:\n");
2611 else
2612 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002613}
Robert Olsson19baf832005-06-21 12:43:18 -07002614
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002615
Robert Olsson19baf832005-06-21 12:43:18 -07002616static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2617{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002618 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002619 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002620
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002621 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002622 "Basic info: size of leaf:"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002623 " %zd bytes, size of tnode: %zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002624 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07002625
Qian Caifbe4e0c2020-03-25 18:01:00 -04002626 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002627 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2628 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002629 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002630
Sasha Levinb67bfe02013-02-27 17:06:00 -08002631 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002632 struct trie *t = (struct trie *) tb->tb_data;
2633 struct trie_stat stat;
2634
2635 if (!t)
2636 continue;
2637
2638 fib_table_print(seq, tb);
2639
2640 trie_collect_stats(t, &stat);
2641 trie_show_stats(seq, &stat);
2642#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002643 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002644#endif
2645 }
Qian Caifbe4e0c2020-03-25 18:01:00 -04002646 cond_resched_rcu();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002647 }
Qian Caifbe4e0c2020-03-25 18:01:00 -04002648 rcu_read_unlock();
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002649
Robert Olsson19baf832005-06-21 12:43:18 -07002650 return 0;
2651}
2652
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002653static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002654{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002655 struct fib_trie_iter *iter = seq->private;
2656 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002657 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002658 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002659
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002660 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2661 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002662 struct fib_table *tb;
2663
Sasha Levinb67bfe02013-02-27 17:06:00 -08002664 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002665 struct key_vector *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002666
2667 for (n = fib_trie_get_first(iter,
2668 (struct trie *) tb->tb_data);
2669 n; n = fib_trie_get_next(iter))
2670 if (pos == idx++) {
2671 iter->tb = tb;
2672 return n;
2673 }
2674 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002675 }
Robert Olsson19baf832005-06-21 12:43:18 -07002676
Robert Olsson19baf832005-06-21 12:43:18 -07002677 return NULL;
2678}
2679
2680static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002681 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002682{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002683 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002684 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002685}
2686
2687static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2688{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002689 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002690 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002691 struct fib_table *tb = iter->tb;
2692 struct hlist_node *tb_node;
2693 unsigned int h;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002694 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002695
Robert Olsson19baf832005-06-21 12:43:18 -07002696 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002697 /* next node in same table */
2698 n = fib_trie_get_next(iter);
2699 if (n)
2700 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002701
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002702 /* walk rest of this hash chain */
2703 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002704 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002705 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2706 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2707 if (n)
2708 goto found;
2709 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002710
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002711 /* new hash chain */
2712 while (++h < FIB_TABLE_HASHSZ) {
2713 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002714 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002715 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2716 if (n)
2717 goto found;
2718 }
2719 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002720 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002721
2722found:
2723 iter->tb = tb;
2724 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002725}
2726
2727static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002728 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002729{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002730 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002731}
2732
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002733static void seq_indent(struct seq_file *seq, int n)
2734{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002735 while (n-- > 0)
2736 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002737}
Robert Olsson19baf832005-06-21 12:43:18 -07002738
Eric Dumazet28d36e32008-01-14 23:09:56 -08002739static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002740{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002741 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002742 case RT_SCOPE_UNIVERSE: return "universe";
2743 case RT_SCOPE_SITE: return "site";
2744 case RT_SCOPE_LINK: return "link";
2745 case RT_SCOPE_HOST: return "host";
2746 case RT_SCOPE_NOWHERE: return "nowhere";
2747 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002748 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002749 return buf;
2750 }
2751}
2752
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002753static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002754 [RTN_UNSPEC] = "UNSPEC",
2755 [RTN_UNICAST] = "UNICAST",
2756 [RTN_LOCAL] = "LOCAL",
2757 [RTN_BROADCAST] = "BROADCAST",
2758 [RTN_ANYCAST] = "ANYCAST",
2759 [RTN_MULTICAST] = "MULTICAST",
2760 [RTN_BLACKHOLE] = "BLACKHOLE",
2761 [RTN_UNREACHABLE] = "UNREACHABLE",
2762 [RTN_PROHIBIT] = "PROHIBIT",
2763 [RTN_THROW] = "THROW",
2764 [RTN_NAT] = "NAT",
2765 [RTN_XRESOLVE] = "XRESOLVE",
2766};
2767
Eric Dumazeta034ee32010-09-09 23:32:28 +00002768static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002769{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002770 if (t < __RTN_MAX && rtn_type_names[t])
2771 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002772 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002773 return buf;
2774}
2775
2776/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002777static int fib_trie_seq_show(struct seq_file *seq, void *v)
2778{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002779 const struct fib_trie_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002780 struct key_vector *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002781
Alexander Duyck88bae712015-03-06 09:54:52 -08002782 if (IS_TRIE(node_parent_rcu(n)))
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002783 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002784
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002785 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002786 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002787
Alexander Duycke9b44012014-12-31 10:56:12 -08002788 seq_indent(seq, iter->depth-1);
2789 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2790 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
Alexander Duyck6e22d172015-03-06 09:54:39 -08002791 tn_info(n)->full_children,
2792 tn_info(n)->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002793 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002794 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002795 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002796
2797 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002798 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002799
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002800 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2801 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002802
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002803 seq_indent(seq, iter->depth + 1);
2804 seq_printf(seq, " /%zu %s %s",
2805 KEYLENGTH - fa->fa_slen,
2806 rtn_scope(buf1, sizeof(buf1),
2807 fa->fa_info->fib_scope),
2808 rtn_type(buf2, sizeof(buf2),
2809 fa->fa_type));
2810 if (fa->fa_tos)
2811 seq_printf(seq, " tos=%d", fa->fa_tos);
2812 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002813 }
Robert Olsson19baf832005-06-21 12:43:18 -07002814 }
2815
2816 return 0;
2817}
2818
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002819static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002820 .start = fib_trie_seq_start,
2821 .next = fib_trie_seq_next,
2822 .stop = fib_trie_seq_stop,
2823 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002824};
2825
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002826struct fib_route_iter {
2827 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002828 struct fib_table *main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002829 struct key_vector *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002830 loff_t pos;
2831 t_key key;
2832};
2833
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002834static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2835 loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002836{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002837 struct key_vector *l, **tp = &iter->tnode;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002838 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002839
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002840 /* use cached location of previously found key */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002841 if (iter->pos > 0 && pos >= iter->pos) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002842 key = iter->key;
2843 } else {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002844 iter->pos = 1;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002845 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002846 }
2847
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002848 pos -= iter->pos;
2849
2850 while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002851 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002852 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002853 l = NULL;
2854
2855 /* handle unlikely case of a key wrap */
2856 if (!key)
2857 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002858 }
2859
2860 if (l)
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002861 iter->key = l->key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002862 else
2863 iter->pos = 0; /* forget it */
2864
2865 return l;
2866}
2867
2868static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2869 __acquires(RCU)
2870{
2871 struct fib_route_iter *iter = seq->private;
2872 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002873 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002874
2875 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002876
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002877 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002878 if (!tb)
2879 return NULL;
2880
Alexander Duyck8be33e92015-03-04 14:59:19 -08002881 iter->main_tb = tb;
David Forster94d9f1c2016-08-03 15:13:01 +01002882 t = (struct trie *)tb->tb_data;
2883 iter->tnode = t->kv;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002884
2885 if (*pos != 0)
2886 return fib_route_get_idx(iter, *pos);
2887
Alexander Duyck8be33e92015-03-04 14:59:19 -08002888 iter->pos = 0;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002889 iter->key = KEY_MAX;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002890
2891 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002892}
2893
2894static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2895{
2896 struct fib_route_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002897 struct key_vector *l = NULL;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002898 t_key key = iter->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002899
2900 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002901
2902 /* only allow key of 0 for start of sequence */
2903 if ((v == SEQ_START_TOKEN) || key)
2904 l = leaf_walk_rcu(&iter->tnode, key);
2905
2906 if (l) {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002907 iter->key = l->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002908 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002909 } else {
2910 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002911 }
2912
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002913 return l;
2914}
2915
2916static void fib_route_seq_stop(struct seq_file *seq, void *v)
2917 __releases(RCU)
2918{
2919 rcu_read_unlock();
2920}
2921
David Ahern5481d732019-06-03 20:19:49 -07002922static unsigned int fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002923{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002924 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002925
Eric Dumazeta034ee32010-09-09 23:32:28 +00002926 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2927 flags = RTF_REJECT;
David Ahern5481d732019-06-03 20:19:49 -07002928 if (fi) {
David Aherndcb1ecb2019-06-03 20:19:50 -07002929 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
David Ahern5481d732019-06-03 20:19:49 -07002930
David Aherndcb1ecb2019-06-03 20:19:50 -07002931 if (nhc->nhc_gw.ipv4)
David Ahern5481d732019-06-03 20:19:49 -07002932 flags |= RTF_GATEWAY;
2933 }
Al Viro32ab5f82006-09-26 22:21:45 -07002934 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002935 flags |= RTF_HOST;
2936 flags |= RTF_UP;
2937 return flags;
2938}
2939
2940/*
2941 * This outputs /proc/net/route.
2942 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002943 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002944 * legacy utilities
2945 */
2946static int fib_route_seq_show(struct seq_file *seq, void *v)
2947{
Alexander Duyck654eff42015-03-11 16:36:08 -07002948 struct fib_route_iter *iter = seq->private;
2949 struct fib_table *tb = iter->main_tb;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002950 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002951 struct key_vector *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002952 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002953
2954 if (v == SEQ_START_TOKEN) {
2955 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2956 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2957 "\tWindow\tIRTT");
2958 return 0;
2959 }
2960
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002961 prefix = htonl(l->key);
2962
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002963 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
David Ahern5481d732019-06-03 20:19:49 -07002964 struct fib_info *fi = fa->fa_info;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002965 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2966 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002967
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002968 if ((fa->fa_type == RTN_BROADCAST) ||
2969 (fa->fa_type == RTN_MULTICAST))
2970 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002971
Alexander Duyck654eff42015-03-11 16:36:08 -07002972 if (fa->tb_id != tb->tb_id)
2973 continue;
2974
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002975 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002976
David Ahern5481d732019-06-03 20:19:49 -07002977 if (fi) {
David Aherndcb1ecb2019-06-03 20:19:50 -07002978 struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
2979 __be32 gw = 0;
2980
2981 if (nhc->nhc_gw_family == AF_INET)
2982 gw = nhc->nhc_gw.ipv4;
David Ahern5481d732019-06-03 20:19:49 -07002983
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002984 seq_printf(seq,
2985 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2986 "%d\t%08X\t%d\t%u\t%u",
David Aherndcb1ecb2019-06-03 20:19:50 -07002987 nhc->nhc_dev ? nhc->nhc_dev->name : "*",
2988 prefix, gw, flags, 0, 0,
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002989 fi->fib_priority,
2990 mask,
2991 (fi->fib_advmss ?
2992 fi->fib_advmss + 40 : 0),
2993 fi->fib_window,
2994 fi->fib_rtt >> 3);
David Ahern5481d732019-06-03 20:19:49 -07002995 } else {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002996 seq_printf(seq,
2997 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2998 "%d\t%08X\t%d\t%u\t%u",
2999 prefix, 0, flags, 0, 0, 0,
3000 mask, 0, 0, 0);
David Ahern5481d732019-06-03 20:19:49 -07003001 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08003002 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003003 }
3004
3005 return 0;
3006}
3007
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003008static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08003009 .start = fib_route_seq_start,
3010 .next = fib_route_seq_next,
3011 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003012 .show = fib_route_seq_show,
3013};
3014
Denis V. Lunev61a02652008-01-10 03:21:09 -08003015int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07003016{
Christoph Hellwigc3506372018-04-10 19:42:55 +02003017 if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
3018 sizeof(struct fib_trie_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003019 goto out1;
3020
Christoph Hellwig3617d942018-04-13 20:38:35 +02003021 if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
3022 fib_triestat_seq_show, NULL))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003023 goto out2;
3024
Christoph Hellwigc3506372018-04-10 19:42:55 +02003025 if (!proc_create_net("route", 0444, net->proc_net, &fib_route_seq_ops,
3026 sizeof(struct fib_route_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003027 goto out3;
3028
Robert Olsson19baf832005-06-21 12:43:18 -07003029 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003030
3031out3:
Gao fengece31ff2013-02-18 01:34:56 +00003032 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003033out2:
Gao fengece31ff2013-02-18 01:34:56 +00003034 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003035out1:
3036 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07003037}
3038
Denis V. Lunev61a02652008-01-10 03:21:09 -08003039void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07003040{
Gao fengece31ff2013-02-18 01:34:56 +00003041 remove_proc_entry("fib_trie", net->proc_net);
3042 remove_proc_entry("fib_triestat", net->proc_net);
3043 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07003044}
3045
3046#endif /* CONFIG_PROC_FS */