blob: ffc5332f139062525e92e1f03b197524842a02f1 [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;
1041
1042 rcu_read_lock();
1043
1044 fa_match = fib_find_matching_alias(net, fri);
1045 if (!fa_match)
1046 goto out;
1047
1048 fa_match->offload = fri->offload;
1049 fa_match->trap = fri->trap;
1050
1051out:
1052 rcu_read_unlock();
1053}
1054EXPORT_SYMBOL_GPL(fib_alias_hw_flags_set);
1055
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001056static void trie_rebalance(struct trie *t, struct key_vector *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001057{
Alexander Duyck88bae712015-03-06 09:54:52 -08001058 while (!IS_TRIE(tn))
1059 tn = resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001060}
1061
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001062static int fib_insert_node(struct trie *t, struct key_vector *tp,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001063 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001064{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001065 struct key_vector *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -08001066
Alexander Duyckd5d64872015-03-04 15:02:18 -08001067 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001068 if (!l)
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001069 goto noleaf;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001070
1071 /* retrieve child from parent node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001072 n = get_child(tp, get_index(key, tp));
Alexander Duyck836a0122014-12-31 10:56:06 -08001073
1074 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1075 *
1076 * Add a new tnode here
1077 * first tnode need some special handling
1078 * leaves us in position for handling as case 3
1079 */
1080 if (n) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001081 struct key_vector *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001082
Alexander Duycke9b44012014-12-31 10:56:12 -08001083 tn = tnode_new(key, __fls(key ^ n->key), 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001084 if (!tn)
1085 goto notnode;
Olof Johansson91b9a272005-08-09 20:24:39 -07001086
Alexander Duyck836a0122014-12-31 10:56:06 -08001087 /* initialize routes out of node */
1088 NODE_INIT_PARENT(tn, tp);
1089 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001090
Alexander Duyck836a0122014-12-31 10:56:06 -08001091 /* start adding routes into the node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001092 put_child_root(tp, key, tn);
Alexander Duyck836a0122014-12-31 10:56:06 -08001093 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001094
Alexander Duyck836a0122014-12-31 10:56:06 -08001095 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001096 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001097 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001098
Alexander Duyck836a0122014-12-31 10:56:06 -08001099 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duycka52ca622016-12-01 07:27:57 -05001100 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001101 NODE_INIT_PARENT(l, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -08001102 put_child_root(tp, key, l);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001103 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001104
Alexander Duyckd5d64872015-03-04 15:02:18 -08001105 return 0;
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001106notnode:
1107 node_free(l);
1108noleaf:
1109 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07001110}
1111
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001112static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1113 struct key_vector *l, struct fib_alias *new,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001114 struct fib_alias *fa, t_key key)
1115{
1116 if (!l)
1117 return fib_insert_node(t, tp, new, key);
1118
1119 if (fa) {
1120 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1121 } else {
1122 struct fib_alias *last;
1123
1124 hlist_for_each_entry(last, &l->leaf, fa_list) {
1125 if (new->fa_slen < last->fa_slen)
1126 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001127 if ((new->fa_slen == last->fa_slen) &&
1128 (new->tb_id > last->tb_id))
1129 break;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001130 fa = last;
1131 }
1132
1133 if (fa)
1134 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1135 else
1136 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1137 }
1138
1139 /* if we added to the tail node then we need to update slen */
1140 if (l->slen < new->fa_slen) {
1141 l->slen = new->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001142 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001143 }
1144
1145 return 0;
1146}
1147
David Ahern78055992017-05-27 16:19:26 -06001148static bool fib_valid_key_len(u32 key, u8 plen, struct netlink_ext_ack *extack)
David Ahernba277e82017-05-27 16:19:25 -06001149{
David Ahern78055992017-05-27 16:19:26 -06001150 if (plen > KEYLENGTH) {
1151 NL_SET_ERR_MSG(extack, "Invalid prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001152 return false;
David Ahern78055992017-05-27 16:19:26 -06001153 }
David Ahernba277e82017-05-27 16:19:25 -06001154
David Ahern78055992017-05-27 16:19:26 -06001155 if ((plen < KEYLENGTH) && (key << plen)) {
1156 NL_SET_ERR_MSG(extack,
1157 "Invalid prefix for given prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001158 return false;
David Ahern78055992017-05-27 16:19:26 -06001159 }
David Ahernba277e82017-05-27 16:19:25 -06001160
1161 return true;
1162}
1163
Ido Schimmela6c76c12019-12-14 17:53:07 +02001164static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1165 struct key_vector *l, struct fib_alias *old);
1166
Alexander Duyckd5d64872015-03-04 15:02:18 -08001167/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001168int fib_table_insert(struct net *net, struct fib_table *tb,
David Ahern6d8422a12017-05-21 10:12:02 -06001169 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001170{
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001171 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001172 struct fib_alias *fa, *new_fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001173 struct key_vector *l, *tp;
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001174 u16 nlflags = NLM_F_EXCL;
Robert Olsson19baf832005-06-21 12:43:18 -07001175 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001176 u8 plen = cfg->fc_dst_len;
1177 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001178 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001179 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001180 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001181
Thomas Graf4e902c52006-08-17 18:14:52 -07001182 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001183
David Ahern78055992017-05-27 16:19:26 -06001184 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001185 return -EINVAL;
1186
David Ahernba277e82017-05-27 16:19:25 -06001187 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
1188
David Ahern6d8422a12017-05-21 10:12:02 -06001189 fi = fib_create_info(cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001190 if (IS_ERR(fi)) {
1191 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001192 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001193 }
Robert Olsson19baf832005-06-21 12:43:18 -07001194
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001195 l = fib_find_node(t, &tp, key);
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001196 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
Ido Schimmelb5fc0432019-12-14 17:53:08 +02001197 tb->tb_id, false) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001198
1199 /* Now fa, if non-NULL, points to the first fib alias
1200 * with the same keys [prefix,tos,priority], if such key already
1201 * exists or to the node before which we will insert new one.
1202 *
1203 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001204 * insert to the tail of the section matching the suffix length
1205 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001206 */
1207
Julian Anastasov936f6f82008-01-28 21:18:06 -08001208 if (fa && fa->fa_tos == tos &&
1209 fa->fa_info->fib_priority == fi->fib_priority) {
1210 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001211
1212 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001213 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001214 goto out;
1215
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001216 nlflags &= ~NLM_F_EXCL;
1217
Julian Anastasov936f6f82008-01-28 21:18:06 -08001218 /* We have 2 goals:
1219 * 1. Find exact match for type, scope, fib_info to avoid
1220 * duplicate routes
1221 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1222 */
1223 fa_match = NULL;
1224 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001225 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001226 if ((fa->fa_slen != slen) ||
1227 (fa->tb_id != tb->tb_id) ||
1228 (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001229 break;
1230 if (fa->fa_info->fib_priority != fi->fib_priority)
1231 break;
1232 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001233 fa->fa_info == fi) {
1234 fa_match = fa;
1235 break;
1236 }
1237 }
1238
Thomas Graf4e902c52006-08-17 18:14:52 -07001239 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001240 struct fib_info *fi_drop;
1241 u8 state;
1242
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001243 nlflags |= NLM_F_REPLACE;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001244 fa = fa_first;
1245 if (fa_match) {
1246 if (fa == fa_match)
1247 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001248 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001249 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001250 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001251 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001252 if (!new_fa)
Robert Olsson2373ce12005-08-25 13:01:29 -07001253 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001254
1255 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001256 new_fa->fa_tos = fa->fa_tos;
1257 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001258 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001259 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001260 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001261 new_fa->fa_slen = fa->fa_slen;
Michal Kubečekd4e64c22015-05-22 13:40:09 +02001262 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001263 new_fa->fa_default = -1;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001264 new_fa->offload = 0;
1265 new_fa->trap = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001266
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001267 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1268
Ido Schimmelee3936d2019-12-14 17:53:09 +02001269 if (fib_find_alias(&l->leaf, fa->fa_slen, 0, 0,
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001270 tb->tb_id, true) == new_fa) {
Ido Schimmelee3936d2019-12-14 17:53:09 +02001271 enum fib_event_type fib_event;
1272
Ido Schimmel446f7392019-12-14 17:53:15 +02001273 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmelee3936d2019-12-14 17:53:09 +02001274 err = call_fib_entry_notifiers(net, fib_event,
1275 key, plen,
1276 new_fa, extack);
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001277 if (err) {
1278 hlist_replace_rcu(&new_fa->fa_list,
1279 &fa->fa_list);
Ido Schimmelee3936d2019-12-14 17:53:09 +02001280 goto out_free_new_fa;
Ido Schimmel6324d0f2020-01-14 13:23:09 +02001281 }
Ido Schimmelee3936d2019-12-14 17:53:09 +02001282 }
David Ahernc1d7ee62018-03-27 18:21:58 -07001283
Ido Schimmel5b7d6162017-02-09 10:28:40 +01001284 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1285 tb->tb_id, &cfg->fc_nlinfo, nlflags);
1286
Robert Olsson2373ce12005-08-25 13:01:29 -07001287 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001288
1289 fib_release_info(fi_drop);
1290 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001291 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Jiri Pirkob90eb752016-09-26 12:52:29 +02001292
Olof Johansson91b9a272005-08-09 20:24:39 -07001293 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001294 }
1295 /* Error if we find a perfect match which
1296 * uses the same scope, type, and nexthop
1297 * information.
1298 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001299 if (fa_match)
1300 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001301
Ido Schimmel446f7392019-12-14 17:53:15 +02001302 if (cfg->fc_nlflags & NLM_F_APPEND)
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001303 nlflags |= NLM_F_APPEND;
Ido Schimmel446f7392019-12-14 17:53:15 +02001304 else
Julian Anastasov936f6f82008-01-28 21:18:06 -08001305 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001306 }
1307 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001308 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001309 goto out;
1310
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001311 nlflags |= NLM_F_CREATE;
Robert Olsson19baf832005-06-21 12:43:18 -07001312 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001313 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001314 if (!new_fa)
Robert Olsson19baf832005-06-21 12:43:18 -07001315 goto out;
1316
1317 new_fa->fa_info = fi;
1318 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001319 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001320 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001321 new_fa->fa_slen = slen;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001322 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001323 new_fa->fa_default = -1;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001324 new_fa->offload = 0;
1325 new_fa->trap = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001326
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001327 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001328 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1329 if (err)
Ido Schimmela6c76c12019-12-14 17:53:07 +02001330 goto out_free_new_fa;
1331
1332 /* The alias was already inserted, so the node must exist. */
1333 l = l ? l : fib_find_node(t, &tp, key);
1334 if (WARN_ON_ONCE(!l))
1335 goto out_free_new_fa;
1336
Ido Schimmela8674f72019-12-14 17:53:10 +02001337 if (fib_find_alias(&l->leaf, new_fa->fa_slen, 0, 0, tb->tb_id, true) ==
1338 new_fa) {
1339 enum fib_event_type fib_event;
1340
Ido Schimmel446f7392019-12-14 17:53:15 +02001341 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmela8674f72019-12-14 17:53:10 +02001342 err = call_fib_entry_notifiers(net, fib_event, key, plen,
1343 new_fa, extack);
1344 if (err)
1345 goto out_remove_new_fa;
1346 }
Robert Olsson19baf832005-06-21 12:43:18 -07001347
David S. Miller21d8c492011-04-14 14:49:37 -07001348 if (!plen)
1349 tb->tb_num_default++;
1350
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001351 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001352 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001353 &cfg->fc_nlinfo, nlflags);
Robert Olsson19baf832005-06-21 12:43:18 -07001354succeeded:
1355 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001356
Ido Schimmela6c76c12019-12-14 17:53:07 +02001357out_remove_new_fa:
1358 fib_remove_alias(t, tp, l, new_fa);
Robert Olssonf835e472005-06-28 15:00:39 -07001359out_free_new_fa:
1360 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001361out:
1362 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001363err:
Robert Olsson19baf832005-06-21 12:43:18 -07001364 return err;
1365}
1366
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001367static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001368{
1369 t_key prefix = n->key;
1370
1371 return (key ^ prefix) & (prefix | -prefix);
1372}
1373
David Ahernaf7888a2020-05-26 12:56:17 -06001374bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
1375 const struct flowi4 *flp)
1376{
1377 if (nhc->nhc_flags & RTNH_F_DEAD)
1378 return false;
1379
1380 if (ip_ignore_linkdown(nhc->nhc_dev) &&
1381 nhc->nhc_flags & RTNH_F_LINKDOWN &&
1382 !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
1383 return false;
1384
1385 if (!(flp->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) {
1386 if (flp->flowi4_oif &&
1387 flp->flowi4_oif != nhc->nhc_oif)
1388 return false;
1389 }
1390
1391 return true;
1392}
1393
Alexander Duyck345e9b52014-12-31 10:56:24 -08001394/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001395int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001396 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001397{
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001398 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001399#ifdef CONFIG_IP_FIB_TRIE_STATS
1400 struct trie_use_stats __percpu *stats = t->stats;
1401#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001402 const t_key key = ntohl(flp->daddr);
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001403 struct key_vector *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001404 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001405 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001406 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001407
Alexander Duyck88bae712015-03-06 09:54:52 -08001408 pn = t->kv;
1409 cindex = 0;
1410
1411 n = get_child_rcu(pn, cindex);
David Ahern9f323972018-05-23 17:08:47 -07001412 if (!n) {
1413 trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001414 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001415 }
Robert Olsson19baf832005-06-21 12:43:18 -07001416
1417#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001418 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001419#endif
1420
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001421 /* Step 1: Travel to the longest prefix match in the trie */
1422 for (;;) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001423 index = get_cindex(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001424
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001425 /* This bit of code is a bit tricky but it combines multiple
1426 * checks into a single check. The prefix consists of the
1427 * prefix plus zeros for the "bits" in the prefix. The index
1428 * is the difference between the key and this value. From
1429 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001430 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001431 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001432 * else
1433 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001434 *
1435 * This check is safe even if bits == KEYLENGTH due to the
1436 * fact that we can only allocate a node with 32 bits if a
1437 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001438 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001439 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001440 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001441
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001442 /* we have found a leaf. Prefixes have already been compared */
1443 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001444 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001445
1446 /* only record pn and cindex if we are going to be chopping
1447 * bits later. Otherwise we are just wasting cycles.
1448 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001449 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001450 pn = n;
1451 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001452 }
1453
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001454 n = get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001455 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001456 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001457 }
1458
1459 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1460 for (;;) {
1461 /* record the pointer where our next node pointer is stored */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001462 struct key_vector __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001463
1464 /* This test verifies that none of the bits that differ
1465 * between the key and the prefix exist in the region of
1466 * the lsb and higher in the prefix.
1467 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001468 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001469 goto backtrace;
1470
1471 /* exit out and process leaf */
1472 if (unlikely(IS_LEAF(n)))
1473 break;
1474
1475 /* Don't bother recording parent info. Since we are in
1476 * prefix match mode we will have to come back to wherever
1477 * we started this traversal anyway
1478 */
1479
1480 while ((n = rcu_dereference(*cptr)) == NULL) {
1481backtrace:
1482#ifdef CONFIG_IP_FIB_TRIE_STATS
1483 if (!n)
1484 this_cpu_inc(stats->null_node_hit);
1485#endif
1486 /* If we are at cindex 0 there are no more bits for
1487 * us to strip at this level so we must ascend back
1488 * up one level to see if there are any more bits to
1489 * be stripped there.
1490 */
1491 while (!cindex) {
1492 t_key pkey = pn->key;
1493
Alexander Duyck88bae712015-03-06 09:54:52 -08001494 /* If we don't have a parent then there is
1495 * nothing for us to do as we do not have any
1496 * further nodes to parse.
1497 */
David Ahern9f323972018-05-23 17:08:47 -07001498 if (IS_TRIE(pn)) {
1499 trace_fib_table_lookup(tb->tb_id, flp,
1500 NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001501 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001502 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001503#ifdef CONFIG_IP_FIB_TRIE_STATS
1504 this_cpu_inc(stats->backtrack);
1505#endif
1506 /* Get Child's index */
Alexander Duyck88bae712015-03-06 09:54:52 -08001507 pn = node_parent_rcu(pn);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001508 cindex = get_index(pkey, pn);
1509 }
1510
1511 /* strip the least significant bit from the cindex */
1512 cindex &= cindex - 1;
1513
1514 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001515 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001516 }
Robert Olsson19baf832005-06-21 12:43:18 -07001517 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001518
Robert Olsson19baf832005-06-21 12:43:18 -07001519found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001520 /* this line carries forward the xor from earlier in the function */
1521 index = key ^ n->key;
1522
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001523 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001524 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1525 struct fib_info *fi = fa->fa_info;
David Ahernaf7888a2020-05-26 12:56:17 -06001526 struct fib_nh_common *nhc;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001527 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001528
Alexander Duycka5829f52016-01-28 13:42:24 -08001529 if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
1530 if (index >= (1ul << fa->fa_slen))
1531 continue;
1532 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001533 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1534 continue;
1535 if (fi->fib_dead)
1536 continue;
1537 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1538 continue;
1539 fib_alias_accessed(fa);
1540 err = fib_props[fa->fa_type].error;
1541 if (unlikely(err < 0)) {
David Ahern4c7e8082019-06-03 20:19:51 -07001542out_reject:
Alexander Duyck345e9b52014-12-31 10:56:24 -08001543#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001544 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001545#endif
David Ahern9f323972018-05-23 17:08:47 -07001546 trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001547 return err;
1548 }
1549 if (fi->fib_flags & RTNH_F_DEAD)
1550 continue;
David Ahern4c7e8082019-06-03 20:19:51 -07001551
David Ahernaf7888a2020-05-26 12:56:17 -06001552 if (unlikely(fi->nh)) {
1553 if (nexthop_is_blackhole(fi->nh)) {
1554 err = fib_props[RTN_BLACKHOLE].error;
1555 goto out_reject;
1556 }
1557
1558 nhc = nexthop_get_nhc_lookup(fi->nh, fib_flags, flp,
1559 &nhsel);
1560 if (nhc)
1561 goto set_result;
1562 goto miss;
David Ahern4c7e8082019-06-03 20:19:51 -07001563 }
1564
David Ahern5481d732019-06-03 20:19:49 -07001565 for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
David Ahernaf7888a2020-05-26 12:56:17 -06001566 nhc = fib_info_nhc(fi, nhsel);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001567
David Ahernaf7888a2020-05-26 12:56:17 -06001568 if (!fib_lookup_good_nhc(nhc, fib_flags, flp))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001569 continue;
David Ahernaf7888a2020-05-26 12:56:17 -06001570set_result:
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001571 if (!(fib_flags & FIB_LOOKUP_NOREF))
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001572 refcount_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001573
David Ahern6ffd9032017-05-25 10:42:37 -07001574 res->prefix = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001575 res->prefixlen = KEYLENGTH - fa->fa_slen;
1576 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001577 res->nhc = nhc;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001578 res->type = fa->fa_type;
1579 res->scope = fi->fib_scope;
1580 res->fi = fi;
1581 res->table = tb;
1582 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001583#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001584 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001585#endif
David Aherneba618a2019-04-02 14:11:55 -07001586 trace_fib_table_lookup(tb->tb_id, flp, nhc, err);
David Ahernf6d3c192015-08-28 08:42:09 -07001587
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001588 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001589 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001590 }
David Ahernaf7888a2020-05-26 12:56:17 -06001591miss:
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001592#ifdef CONFIG_IP_FIB_TRIE_STATS
1593 this_cpu_inc(stats->semantic_match_miss);
1594#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001595 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001596}
Florian Westphal6fc01432011-08-25 13:46:12 +02001597EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001598
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001599static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1600 struct key_vector *l, struct fib_alias *old)
Alexander Duyckd5d64872015-03-04 15:02:18 -08001601{
1602 /* record the location of the previous list_info entry */
1603 struct hlist_node **pprev = old->fa_list.pprev;
1604 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1605
1606 /* remove the fib_alias from the list */
1607 hlist_del_rcu(&old->fa_list);
1608
1609 /* if we emptied the list this leaf will be freed and we can sort
1610 * out parent suffix lengths as a part of trie_rebalance
1611 */
1612 if (hlist_empty(&l->leaf)) {
Alexander Duycka52ca622016-12-01 07:27:57 -05001613 if (tp->slen == l->slen)
1614 node_pull_suffix(tp, tp->pos);
Alexander Duyck88bae712015-03-06 09:54:52 -08001615 put_child_root(tp, l->key, NULL);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001616 node_free(l);
1617 trie_rebalance(t, tp);
1618 return;
1619 }
1620
1621 /* only access fa if it is pointing at the last valid hlist_node */
1622 if (*pprev)
1623 return;
1624
1625 /* update the trie with the latest suffix length */
1626 l->slen = fa->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001627 node_pull_suffix(tp, fa->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001628}
1629
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001630static void fib_notify_alias_delete(struct net *net, u32 key,
1631 struct hlist_head *fah,
1632 struct fib_alias *fa_to_delete,
1633 struct netlink_ext_ack *extack)
1634{
1635 struct fib_alias *fa_next, *fa_to_notify;
1636 u32 tb_id = fa_to_delete->tb_id;
1637 u8 slen = fa_to_delete->fa_slen;
1638 enum fib_event_type fib_event;
1639
1640 /* Do not notify if we do not care about the route. */
1641 if (fib_find_alias(fah, slen, 0, 0, tb_id, true) != fa_to_delete)
1642 return;
1643
1644 /* Determine if the route should be replaced by the next route in the
1645 * list.
1646 */
1647 fa_next = hlist_entry_safe(fa_to_delete->fa_list.next,
1648 struct fib_alias, fa_list);
1649 if (fa_next && fa_next->fa_slen == slen && fa_next->tb_id == tb_id) {
Ido Schimmel446f7392019-12-14 17:53:15 +02001650 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001651 fa_to_notify = fa_next;
1652 } else {
Ido Schimmel446f7392019-12-14 17:53:15 +02001653 fib_event = FIB_EVENT_ENTRY_DEL;
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001654 fa_to_notify = fa_to_delete;
1655 }
1656 call_fib_entry_notifiers(net, fib_event, key, KEYLENGTH - slen,
1657 fa_to_notify, extack);
1658}
1659
Alexander Duyckd5d64872015-03-04 15:02:18 -08001660/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001661int fib_table_delete(struct net *net, struct fib_table *tb,
David Ahern78055992017-05-27 16:19:26 -06001662 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001663{
1664 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001665 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001666 struct key_vector *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001667 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001668 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001669 u8 tos = cfg->fc_tos;
1670 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001671
Thomas Graf4e902c52006-08-17 18:14:52 -07001672 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001673
David Ahern78055992017-05-27 16:19:26 -06001674 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001675 return -EINVAL;
1676
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001677 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001678 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001679 return -ESRCH;
1680
Ido Schimmelb5fc0432019-12-14 17:53:08 +02001681 fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id, false);
Robert Olsson19baf832005-06-21 12:43:18 -07001682 if (!fa)
1683 return -ESRCH;
1684
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001685 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001686
1687 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001688 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001689 struct fib_info *fi = fa->fa_info;
1690
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001691 if ((fa->fa_slen != slen) ||
1692 (fa->tb_id != tb->tb_id) ||
1693 (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001694 break;
1695
Thomas Graf4e902c52006-08-17 18:14:52 -07001696 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1697 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001698 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001699 (!cfg->fc_prefsrc ||
1700 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001701 (!cfg->fc_protocol ||
1702 fi->fib_protocol == cfg->fc_protocol) &&
Alexander Aringfaee6762020-03-27 18:00:21 -04001703 fib_nh_match(net, cfg, fi, extack) == 0 &&
Xin Long5f9ae3d2017-08-23 10:07:26 +08001704 fib_metrics_match(cfg, fi)) {
Robert Olsson19baf832005-06-21 12:43:18 -07001705 fa_to_delete = fa;
1706 break;
1707 }
1708 }
1709
Olof Johansson91b9a272005-08-09 20:24:39 -07001710 if (!fa_to_delete)
1711 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001712
Ido Schimmelf613b6e2019-12-14 17:53:11 +02001713 fib_notify_alias_delete(net, key, &l->leaf, fa_to_delete, extack);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001714 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001715 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001716
David S. Miller21d8c492011-04-14 14:49:37 -07001717 if (!plen)
1718 tb->tb_num_default--;
1719
Alexander Duyckd5d64872015-03-04 15:02:18 -08001720 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001721
Alexander Duyckd5d64872015-03-04 15:02:18 -08001722 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001723 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001724
Alexander Duyckd5d64872015-03-04 15:02:18 -08001725 fib_release_info(fa_to_delete->fa_info);
1726 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001727 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001728}
1729
Alexander Duyck8be33e92015-03-04 14:59:19 -08001730/* Scan for the next leaf starting at the provided key value */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001731static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001732{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001733 struct key_vector *pn, *n = *tn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001734 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001735
Alexander Duyck8be33e92015-03-04 14:59:19 -08001736 /* this loop is meant to try and find the key in the trie */
Alexander Duyck88bae712015-03-06 09:54:52 -08001737 do {
1738 /* record parent and next child index */
1739 pn = n;
Alexander Duyckc2229fe2015-10-27 15:06:45 -07001740 cindex = (key > pn->key) ? get_index(key, pn) : 0;
Alexander Duyck88bae712015-03-06 09:54:52 -08001741
1742 if (cindex >> pn->bits)
1743 break;
1744
1745 /* descend into the next child */
1746 n = get_child_rcu(pn, cindex++);
1747 if (!n)
1748 break;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001749
Alexander Duyck8be33e92015-03-04 14:59:19 -08001750 /* guarantee forward progress on the keys */
1751 if (IS_LEAF(n) && (n->key >= key))
1752 goto found;
Alexander Duyck88bae712015-03-06 09:54:52 -08001753 } while (IS_TNODE(n));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001754
1755 /* this loop will search for the next leaf with a greater key */
Alexander Duyck88bae712015-03-06 09:54:52 -08001756 while (!IS_TRIE(pn)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001757 /* if we exhausted the parent node we will need to climb */
1758 if (cindex >= (1ul << pn->bits)) {
1759 t_key pkey = pn->key;
1760
1761 pn = node_parent_rcu(pn);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001762 cindex = get_index(pkey, pn) + 1;
1763 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001764 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001765
Alexander Duyck8be33e92015-03-04 14:59:19 -08001766 /* grab the next available node */
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001767 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001768 if (!n)
1769 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001770
Alexander Duyck8be33e92015-03-04 14:59:19 -08001771 /* no need to compare keys since we bumped the index */
1772 if (IS_LEAF(n))
1773 goto found;
1774
1775 /* Rescan start scanning in new node */
1776 pn = n;
1777 cindex = 0;
1778 }
1779
1780 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001781 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001782found:
1783 /* if we are at the limit for keys just return NULL for the tnode */
Alexander Duyck88bae712015-03-06 09:54:52 -08001784 *tn = pn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001785 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001786}
1787
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001788static void fib_trie_free(struct fib_table *tb)
1789{
1790 struct trie *t = (struct trie *)tb->tb_data;
1791 struct key_vector *pn = t->kv;
1792 unsigned long cindex = 1;
1793 struct hlist_node *tmp;
1794 struct fib_alias *fa;
1795
1796 /* walk trie in reverse order and free everything */
1797 for (;;) {
1798 struct key_vector *n;
1799
1800 if (!(cindex--)) {
1801 t_key pkey = pn->key;
1802
1803 if (IS_TRIE(pn))
1804 break;
1805
1806 n = pn;
1807 pn = node_parent(pn);
1808
1809 /* drop emptied tnode */
1810 put_child_root(pn, n->key, NULL);
1811 node_free(n);
1812
1813 cindex = get_index(pkey, pn);
1814
1815 continue;
1816 }
1817
1818 /* grab the next available node */
1819 n = get_child(pn, cindex);
1820 if (!n)
1821 continue;
1822
1823 if (IS_TNODE(n)) {
1824 /* record pn and cindex for leaf walking */
1825 pn = n;
1826 cindex = 1ul << n->bits;
1827
1828 continue;
1829 }
1830
1831 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1832 hlist_del_rcu(&fa->fa_list);
1833 alias_free_mem_rcu(fa);
1834 }
1835
1836 put_child_root(pn, n->key, NULL);
1837 node_free(n);
1838 }
1839
1840#ifdef CONFIG_IP_FIB_TRIE_STATS
1841 free_percpu(t->stats);
1842#endif
1843 kfree(tb);
1844}
1845
1846struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
1847{
1848 struct trie *ot = (struct trie *)oldtb->tb_data;
1849 struct key_vector *l, *tp = ot->kv;
1850 struct fib_table *local_tb;
1851 struct fib_alias *fa;
1852 struct trie *lt;
1853 t_key key = 0;
1854
1855 if (oldtb->tb_data == oldtb->__data)
1856 return oldtb;
1857
1858 local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
1859 if (!local_tb)
1860 return NULL;
1861
1862 lt = (struct trie *)local_tb->tb_data;
1863
1864 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1865 struct key_vector *local_l = NULL, *local_tp;
1866
Ido Schimmel83f35222020-07-29 11:37:13 +03001867 hlist_for_each_entry(fa, &l->leaf, fa_list) {
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001868 struct fib_alias *new_fa;
1869
1870 if (local_tb->tb_id != fa->tb_id)
1871 continue;
1872
1873 /* clone fa for new local table */
1874 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1875 if (!new_fa)
1876 goto out;
1877
1878 memcpy(new_fa, fa, sizeof(*fa));
1879
1880 /* insert clone into table */
1881 if (!local_l)
1882 local_l = fib_find_node(lt, &local_tp, l->key);
1883
1884 if (fib_insert_alias(lt, local_tp, local_l, new_fa,
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001885 NULL, l->key)) {
1886 kmem_cache_free(fn_alias_kmem, new_fa);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001887 goto out;
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001888 }
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001889 }
1890
1891 /* stop loop if key wrapped back to 0 */
1892 key = l->key + 1;
1893 if (key < l->key)
1894 break;
1895 }
1896
1897 return local_tb;
1898out:
1899 fib_trie_free(local_tb);
1900
1901 return NULL;
1902}
1903
Alexander Duyck3b709332016-11-15 05:46:06 -05001904/* Caller must hold RTNL */
1905void fib_table_flush_external(struct fib_table *tb)
1906{
1907 struct trie *t = (struct trie *)tb->tb_data;
1908 struct key_vector *pn = t->kv;
1909 unsigned long cindex = 1;
1910 struct hlist_node *tmp;
1911 struct fib_alias *fa;
1912
1913 /* walk trie in reverse order */
1914 for (;;) {
1915 unsigned char slen = 0;
1916 struct key_vector *n;
1917
1918 if (!(cindex--)) {
1919 t_key pkey = pn->key;
1920
1921 /* cannot resize the trie vector */
1922 if (IS_TRIE(pn))
1923 break;
1924
Alexander Duycka52ca622016-12-01 07:27:57 -05001925 /* update the suffix to address pulled leaves */
1926 if (pn->slen > pn->pos)
1927 update_suffix(pn);
1928
Alexander Duyck3b709332016-11-15 05:46:06 -05001929 /* resize completed node */
1930 pn = resize(t, pn);
1931 cindex = get_index(pkey, pn);
1932
1933 continue;
1934 }
1935
1936 /* grab the next available node */
1937 n = get_child(pn, cindex);
1938 if (!n)
1939 continue;
1940
1941 if (IS_TNODE(n)) {
1942 /* record pn and cindex for leaf walking */
1943 pn = n;
1944 cindex = 1ul << n->bits;
1945
1946 continue;
1947 }
1948
1949 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1950 /* if alias was cloned to local then we just
1951 * need to remove the local copy from main
1952 */
1953 if (tb->tb_id != fa->tb_id) {
1954 hlist_del_rcu(&fa->fa_list);
1955 alias_free_mem_rcu(fa);
1956 continue;
1957 }
1958
1959 /* record local slen */
1960 slen = fa->fa_slen;
1961 }
1962
1963 /* update leaf slen */
1964 n->slen = slen;
1965
1966 if (hlist_empty(&n->leaf)) {
1967 put_child_root(pn, n->key, NULL);
1968 node_free(n);
1969 }
1970 }
1971}
1972
Alexander Duyck8be33e92015-03-04 14:59:19 -08001973/* Caller must hold RTNL. */
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001974int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
Robert Olsson19baf832005-06-21 12:43:18 -07001975{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001976 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08001977 struct key_vector *pn = t->kv;
1978 unsigned long cindex = 1;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001979 struct hlist_node *tmp;
1980 struct fib_alias *fa;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001981 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001982
Alexander Duyck88bae712015-03-06 09:54:52 -08001983 /* walk trie in reverse order */
1984 for (;;) {
1985 unsigned char slen = 0;
1986 struct key_vector *n;
Robert Olsson19baf832005-06-21 12:43:18 -07001987
Alexander Duyck88bae712015-03-06 09:54:52 -08001988 if (!(cindex--)) {
1989 t_key pkey = pn->key;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001990
Alexander Duyck88bae712015-03-06 09:54:52 -08001991 /* cannot resize the trie vector */
1992 if (IS_TRIE(pn))
1993 break;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001994
Alexander Duycka52ca622016-12-01 07:27:57 -05001995 /* update the suffix to address pulled leaves */
1996 if (pn->slen > pn->pos)
1997 update_suffix(pn);
1998
Alexander Duyck88bae712015-03-06 09:54:52 -08001999 /* resize completed node */
2000 pn = resize(t, pn);
2001 cindex = get_index(pkey, pn);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002002
Alexander Duyck88bae712015-03-06 09:54:52 -08002003 continue;
2004 }
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002005
Alexander Duyck88bae712015-03-06 09:54:52 -08002006 /* grab the next available node */
2007 n = get_child(pn, cindex);
2008 if (!n)
2009 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002010
Alexander Duyck88bae712015-03-06 09:54:52 -08002011 if (IS_TNODE(n)) {
2012 /* record pn and cindex for leaf walking */
2013 pn = n;
2014 cindex = 1ul << n->bits;
2015
2016 continue;
2017 }
2018
2019 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
2020 struct fib_info *fi = fa->fa_info;
2021
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00002022 if (!fi || tb->tb_id != fa->tb_id ||
2023 (!(fi->fib_flags & RTNH_F_DEAD) &&
2024 !fib_props[fa->fa_type].error)) {
2025 slen = fa->fa_slen;
2026 continue;
2027 }
2028
2029 /* Do not flush error routes if network namespace is
2030 * not being dismantled
2031 */
2032 if (!flush_all && fib_props[fa->fa_type].error) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002033 slen = fa->fa_slen;
2034 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002035 }
2036
Ido Schimmel525bc342019-12-14 17:53:12 +02002037 fib_notify_alias_delete(net, n->key, &n->leaf, fa,
2038 NULL);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08002039 hlist_del_rcu(&fa->fa_list);
2040 fib_release_info(fa->fa_info);
2041 alias_free_mem_rcu(fa);
2042 found++;
Alexander Duyck64c62722015-01-22 15:51:45 -08002043 }
2044
Alexander Duyck88bae712015-03-06 09:54:52 -08002045 /* update leaf slen */
2046 n->slen = slen;
2047
2048 if (hlist_empty(&n->leaf)) {
2049 put_child_root(pn, n->key, NULL);
2050 node_free(n);
Alexander Duyck88bae712015-03-06 09:54:52 -08002051 }
Robert Olsson19baf832005-06-21 12:43:18 -07002052 }
2053
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07002054 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07002055 return found;
2056}
2057
David Ahern1bff1a02019-05-22 12:04:42 -07002058/* derived from fib_trie_free */
2059static void __fib_info_notify_update(struct net *net, struct fib_table *tb,
2060 struct nl_info *info)
2061{
2062 struct trie *t = (struct trie *)tb->tb_data;
2063 struct key_vector *pn = t->kv;
2064 unsigned long cindex = 1;
2065 struct fib_alias *fa;
2066
2067 for (;;) {
2068 struct key_vector *n;
2069
2070 if (!(cindex--)) {
2071 t_key pkey = pn->key;
2072
2073 if (IS_TRIE(pn))
2074 break;
2075
David Ahern1bff1a02019-05-22 12:04:42 -07002076 pn = node_parent(pn);
2077 cindex = get_index(pkey, pn);
2078 continue;
2079 }
2080
2081 /* grab the next available node */
2082 n = get_child(pn, cindex);
2083 if (!n)
2084 continue;
2085
2086 if (IS_TNODE(n)) {
2087 /* record pn and cindex for leaf walking */
2088 pn = n;
2089 cindex = 1ul << n->bits;
2090
2091 continue;
2092 }
2093
2094 hlist_for_each_entry(fa, &n->leaf, fa_list) {
2095 struct fib_info *fi = fa->fa_info;
2096
2097 if (!fi || !fi->nh_updated || fa->tb_id != tb->tb_id)
2098 continue;
2099
2100 rtmsg_fib(RTM_NEWROUTE, htonl(n->key), fa,
2101 KEYLENGTH - fa->fa_slen, tb->tb_id,
2102 info, NLM_F_REPLACE);
2103
2104 /* call_fib_entry_notifiers will be removed when
2105 * in-kernel notifier is implemented and supported
2106 * for nexthop objects
2107 */
2108 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE,
2109 n->key,
2110 KEYLENGTH - fa->fa_slen, fa,
2111 NULL);
2112 }
2113 }
2114}
2115
2116void fib_info_notify_update(struct net *net, struct nl_info *info)
2117{
2118 unsigned int h;
2119
2120 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2121 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2122 struct fib_table *tb;
2123
Ido Schimmel7f6f32b2020-08-26 19:48:10 +03002124 hlist_for_each_entry_rcu(tb, head, tb_hlist,
2125 lockdep_rtnl_is_held())
David Ahern1bff1a02019-05-22 12:04:42 -07002126 __fib_info_notify_update(net, tb, info);
2127 }
2128}
2129
Jiri Pirko55c894f2019-10-03 11:49:28 +02002130static int fib_leaf_notify(struct key_vector *l, struct fib_table *tb,
Jiri Pirkob7a59552019-10-03 11:49:30 +02002131 struct notifier_block *nb,
2132 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002133{
2134 struct fib_alias *fa;
Ido Schimmel20d15652019-12-14 17:53:13 +02002135 int last_slen = -1;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002136 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002137
2138 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2139 struct fib_info *fi = fa->fa_info;
2140
2141 if (!fi)
2142 continue;
2143
2144 /* local and main table can share the same trie,
2145 * so don't notify twice for the same entry.
2146 */
2147 if (tb->tb_id != fa->tb_id)
2148 continue;
2149
Ido Schimmel20d15652019-12-14 17:53:13 +02002150 if (fa->fa_slen == last_slen)
2151 continue;
2152
2153 last_slen = fa->fa_slen;
Ido Schimmel446f7392019-12-14 17:53:15 +02002154 err = call_fib_entry_notifier(nb, FIB_EVENT_ENTRY_REPLACE,
Ido Schimmel20d15652019-12-14 17:53:13 +02002155 l->key, KEYLENGTH - fa->fa_slen,
2156 fa, extack);
2157 if (err)
2158 return err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002159 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002160 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002161}
2162
Jiri Pirkob7a59552019-10-03 11:49:30 +02002163static int fib_table_notify(struct fib_table *tb, struct notifier_block *nb,
2164 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002165{
2166 struct trie *t = (struct trie *)tb->tb_data;
2167 struct key_vector *l, *tp = t->kv;
2168 t_key key = 0;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002169 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002170
2171 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Jiri Pirkob7a59552019-10-03 11:49:30 +02002172 err = fib_leaf_notify(l, tb, nb, extack);
Jiri Pirko55c894f2019-10-03 11:49:28 +02002173 if (err)
2174 return err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002175
2176 key = l->key + 1;
2177 /* stop in case of wrap around */
2178 if (key < l->key)
2179 break;
2180 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002181 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002182}
2183
Jiri Pirkob7a59552019-10-03 11:49:30 +02002184int fib_notify(struct net *net, struct notifier_block *nb,
2185 struct netlink_ext_ack *extack)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002186{
2187 unsigned int h;
Jiri Pirko55c894f2019-10-03 11:49:28 +02002188 int err;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002189
2190 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2191 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2192 struct fib_table *tb;
2193
Jiri Pirko55c894f2019-10-03 11:49:28 +02002194 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Jiri Pirkob7a59552019-10-03 11:49:30 +02002195 err = fib_table_notify(tb, nb, extack);
Jiri Pirko55c894f2019-10-03 11:49:28 +02002196 if (err)
2197 return err;
2198 }
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002199 }
Jiri Pirko55c894f2019-10-03 11:49:28 +02002200 return 0;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002201}
2202
Alexander Duycka7e53532015-03-04 15:02:44 -08002203static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002204{
Alexander Duycka7e53532015-03-04 15:02:44 -08002205 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08002206#ifdef CONFIG_IP_FIB_TRIE_STATS
2207 struct trie *t = (struct trie *)tb->tb_data;
2208
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002209 if (tb->tb_data == tb->__data)
2210 free_percpu(t->stats);
Alexander Duyck8274a972014-12-31 10:55:29 -08002211#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002212 kfree(tb);
2213}
2214
Alexander Duycka7e53532015-03-04 15:02:44 -08002215void fib_free_table(struct fib_table *tb)
2216{
2217 call_rcu(&tb->rcu, __trie_free_rcu);
2218}
2219
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002220static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
David Ahern18a80212018-10-15 18:56:43 -07002221 struct sk_buff *skb, struct netlink_callback *cb,
2222 struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002223{
David Ahern18a80212018-10-15 18:56:43 -07002224 unsigned int flags = NLM_F_MULTI;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002225 __be32 xkey = htonl(l->key);
Stefano Brivioee289062019-06-21 17:45:23 +02002226 int i, s_i, i_fa, s_fa, err;
Robert Olsson19baf832005-06-21 12:43:18 -07002227 struct fib_alias *fa;
Robert Olsson19baf832005-06-21 12:43:18 -07002228
Stefano Brivioee289062019-06-21 17:45:23 +02002229 if (filter->filter_set ||
2230 !filter->dump_exceptions || !filter->dump_routes)
David Ahern18a80212018-10-15 18:56:43 -07002231 flags |= NLM_F_DUMP_FILTERED;
2232
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002233 s_i = cb->args[4];
Stefano Brivioee289062019-06-21 17:45:23 +02002234 s_fa = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07002235 i = 0;
2236
Robert Olsson2373ce12005-08-25 13:01:29 -07002237 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002238 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
Stefano Brivioee289062019-06-21 17:45:23 +02002239 struct fib_info *fi = fa->fa_info;
David Ahernf6c57752017-05-15 23:19:17 -07002240
David Ahern18a80212018-10-15 18:56:43 -07002241 if (i < s_i)
2242 goto next;
Robert Olsson19baf832005-06-21 12:43:18 -07002243
Stefano Brivioee289062019-06-21 17:45:23 +02002244 i_fa = 0;
2245
David Ahern18a80212018-10-15 18:56:43 -07002246 if (tb->tb_id != fa->tb_id)
2247 goto next;
2248
2249 if (filter->filter_set) {
2250 if (filter->rt_type && fa->fa_type != filter->rt_type)
2251 goto next;
2252
2253 if ((filter->protocol &&
Stefano Brivioee289062019-06-21 17:45:23 +02002254 fi->fib_protocol != filter->protocol))
David Ahern18a80212018-10-15 18:56:43 -07002255 goto next;
2256
2257 if (filter->dev &&
Stefano Brivioee289062019-06-21 17:45:23 +02002258 !fib_info_nh_uses_dev(fi, filter->dev))
David Ahern18a80212018-10-15 18:56:43 -07002259 goto next;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002260 }
2261
Stefano Brivio885b8b42019-06-29 19:55:08 +02002262 if (filter->dump_routes) {
2263 if (!s_fa) {
Ido Schimmel1e301fd2020-01-14 13:23:10 +02002264 struct fib_rt_info fri;
2265
2266 fri.fi = fi;
2267 fri.tb_id = tb->tb_id;
2268 fri.dst = xkey;
2269 fri.dst_len = KEYLENGTH - fa->fa_slen;
2270 fri.tos = fa->fa_tos;
2271 fri.type = fa->fa_type;
Ido Schimmel90b93f12020-01-14 13:23:11 +02002272 fri.offload = fa->offload;
2273 fri.trap = fa->trap;
Stefano Brivio885b8b42019-06-29 19:55:08 +02002274 err = fib_dump_info(skb,
2275 NETLINK_CB(cb->skb).portid,
2276 cb->nlh->nlmsg_seq,
Ido Schimmel1e301fd2020-01-14 13:23:10 +02002277 RTM_NEWROUTE, &fri, flags);
Stefano Brivio885b8b42019-06-29 19:55:08 +02002278 if (err < 0)
2279 goto stop;
2280 }
2281
Stefano Brivioee289062019-06-21 17:45:23 +02002282 i_fa++;
Robert Olsson19baf832005-06-21 12:43:18 -07002283 }
Stefano Brivioee289062019-06-21 17:45:23 +02002284
2285 if (filter->dump_exceptions) {
2286 err = fib_dump_info_fnhe(skb, cb, tb->tb_id, fi,
John Fastabende93fb3e2019-08-23 17:11:38 -07002287 &i_fa, s_fa, flags);
Stefano Brivioee289062019-06-21 17:45:23 +02002288 if (err < 0)
2289 goto stop;
2290 }
2291
David Ahern18a80212018-10-15 18:56:43 -07002292next:
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002293 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07002294 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002295
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002296 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07002297 return skb->len;
Stefano Brivioee289062019-06-21 17:45:23 +02002298
2299stop:
2300 cb->args[4] = i;
2301 cb->args[5] = i_fa;
2302 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002303}
2304
Alexander Duycka7e53532015-03-04 15:02:44 -08002305/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00002306int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
David Ahern18a80212018-10-15 18:56:43 -07002307 struct netlink_callback *cb, struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002308{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002309 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002310 struct key_vector *l, *tp = t->kv;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002311 /* Dump starting at last key.
2312 * Note: 0.0.0.0/0 (ie default) is first key.
2313 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002314 int count = cb->args[2];
2315 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002316
David Ahern9827c062020-01-10 09:03:58 -08002317 /* First time here, count and key are both always 0. Count > 0
2318 * and key == 0 means the dump has wrapped around and we are done.
2319 */
2320 if (count && !key)
2321 return skb->len;
2322
Alexander Duyck8be33e92015-03-04 14:59:19 -08002323 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
David Ahernf6c57752017-05-15 23:19:17 -07002324 int err;
2325
David Ahern18a80212018-10-15 18:56:43 -07002326 err = fn_trie_dump_leaf(l, tb, skb, cb, filter);
David Ahernf6c57752017-05-15 23:19:17 -07002327 if (err < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002328 cb->args[3] = key;
2329 cb->args[2] = count;
David Ahernf6c57752017-05-15 23:19:17 -07002330 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002331 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002332
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002333 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002334 key = l->key + 1;
2335
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002336 memset(&cb->args[4], 0,
2337 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08002338
2339 /* stop loop if key wrapped back to 0 */
2340 if (key < l->key)
2341 break;
Robert Olsson19baf832005-06-21 12:43:18 -07002342 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08002343
Alexander Duyck8be33e92015-03-04 14:59:19 -08002344 cb->args[3] = key;
2345 cb->args[2] = count;
2346
Robert Olsson19baf832005-06-21 12:43:18 -07002347 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07002348}
2349
David S. Miller5348ba82011-02-01 15:30:56 -08002350void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002351{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002352 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
2353 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002354 0, SLAB_PANIC, NULL);
2355
2356 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002357 LEAF_SIZE,
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002358 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002359}
Robert Olsson19baf832005-06-21 12:43:18 -07002360
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002361struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
Robert Olsson19baf832005-06-21 12:43:18 -07002362{
2363 struct fib_table *tb;
2364 struct trie *t;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002365 size_t sz = sizeof(*tb);
Robert Olsson19baf832005-06-21 12:43:18 -07002366
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002367 if (!alias)
2368 sz += sizeof(struct trie);
2369
2370 tb = kzalloc(sz, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002371 if (!tb)
Robert Olsson19baf832005-06-21 12:43:18 -07002372 return NULL;
2373
2374 tb->tb_id = id;
David S. Miller21d8c492011-04-14 14:49:37 -07002375 tb->tb_num_default = 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002376 tb->tb_data = (alias ? alias->__data : tb->__data);
2377
2378 if (alias)
2379 return tb;
Robert Olsson19baf832005-06-21 12:43:18 -07002380
2381 t = (struct trie *) tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002382 t->kv[0].pos = KEYLENGTH;
2383 t->kv[0].slen = KEYLENGTH;
Alexander Duyck8274a972014-12-31 10:55:29 -08002384#ifdef CONFIG_IP_FIB_TRIE_STATS
2385 t->stats = alloc_percpu(struct trie_use_stats);
2386 if (!t->stats) {
2387 kfree(tb);
2388 tb = NULL;
2389 }
2390#endif
Robert Olsson19baf832005-06-21 12:43:18 -07002391
Robert Olsson19baf832005-06-21 12:43:18 -07002392 return tb;
2393}
2394
Robert Olsson19baf832005-06-21 12:43:18 -07002395#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002396/* Depth first Trie walk iterator */
2397struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002398 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002399 struct fib_table *tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002400 struct key_vector *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002401 unsigned int index;
2402 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002403};
Robert Olsson19baf832005-06-21 12:43:18 -07002404
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002405static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07002406{
Alexander Duyck98293e82014-12-31 10:56:18 -08002407 unsigned long cindex = iter->index;
Alexander Duyck88bae712015-03-06 09:54:52 -08002408 struct key_vector *pn = iter->tnode;
2409 t_key pkey;
Eric W. Biederman6640e692007-01-24 14:42:04 -08002410
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002411 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
2412 iter->tnode, iter->index, iter->depth);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002413
Alexander Duyck88bae712015-03-06 09:54:52 -08002414 while (!IS_TRIE(pn)) {
2415 while (cindex < child_length(pn)) {
2416 struct key_vector *n = get_child_rcu(pn, cindex++);
2417
2418 if (!n)
2419 continue;
2420
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002421 if (IS_LEAF(n)) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002422 iter->tnode = pn;
2423 iter->index = cindex;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002424 } else {
2425 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08002426 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002427 iter->index = 0;
2428 ++iter->depth;
2429 }
Alexander Duyck88bae712015-03-06 09:54:52 -08002430
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002431 return n;
2432 }
2433
Alexander Duyck88bae712015-03-06 09:54:52 -08002434 /* Current node exhausted, pop back up */
2435 pkey = pn->key;
2436 pn = node_parent_rcu(pn);
2437 cindex = get_index(pkey, pn) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002438 --iter->depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002439 }
2440
Alexander Duyck88bae712015-03-06 09:54:52 -08002441 /* record root node so further searches know we are done */
2442 iter->tnode = pn;
2443 iter->index = 0;
2444
Robert Olsson19baf832005-06-21 12:43:18 -07002445 return NULL;
2446}
2447
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002448static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
2449 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07002450{
Firo Yangf38b24c2015-06-08 11:54:51 +08002451 struct key_vector *n, *pn;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002452
Stephen Hemminger132adf52007-03-08 20:44:43 -08002453 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002454 return NULL;
2455
Firo Yangf38b24c2015-06-08 11:54:51 +08002456 pn = t->kv;
Alexander Duyck88bae712015-03-06 09:54:52 -08002457 n = rcu_dereference(pn->tnode[0]);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002458 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002459 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002460
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002461 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002462 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002463 iter->index = 0;
2464 iter->depth = 1;
2465 } else {
Alexander Duyck88bae712015-03-06 09:54:52 -08002466 iter->tnode = pn;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002467 iter->index = 0;
2468 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002469 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002470
2471 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002472}
2473
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002474static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07002475{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002476 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002477 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07002478
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002479 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07002480
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002481 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002482 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002483 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002484 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08002485
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002486 s->leaves++;
2487 s->totdepth += iter.depth;
2488 if (iter.depth > s->maxdepth)
2489 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08002490
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002491 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08002492 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002493 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002494 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002495 if (n->bits < MAX_STAT_DEPTH)
2496 s->nodesizes[n->bits]++;
Alexander Duyck6e22d172015-03-06 09:54:39 -08002497 s->nullpointers += tn_info(n)->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002498 }
2499 }
2500 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002501}
2502
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002503/*
Robert Olsson19baf832005-06-21 12:43:18 -07002504 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002505 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002506static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002507{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002508 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002509
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002510 if (stat->leaves)
2511 avdepth = stat->totdepth*100 / stat->leaves;
2512 else
2513 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002514
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002515 seq_printf(seq, "\tAver depth: %u.%02d\n",
2516 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002517 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002518
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002519 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002520 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002521
2522 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002523 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08002524
Stephen Hemminger187b5182008-01-12 20:55:55 -08002525 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002526 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002527
Robert Olsson06ef9212006-03-20 21:35:01 -08002528 max = MAX_STAT_DEPTH;
2529 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002530 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002531
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002532 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002533 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002534 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002535 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002536 pointers += (1<<i) * stat->nodesizes[i];
2537 }
2538 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002539 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002540
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002541 bytes += sizeof(struct key_vector *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002542 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2543 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002544}
Robert Olsson19baf832005-06-21 12:43:18 -07002545
2546#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002547static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002548 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002549{
Alexander Duyck8274a972014-12-31 10:55:29 -08002550 struct trie_use_stats s = { 0 };
2551 int cpu;
2552
2553 /* loop through all of the CPUs and gather up the stats */
2554 for_each_possible_cpu(cpu) {
2555 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2556
2557 s.gets += pcpu->gets;
2558 s.backtrack += pcpu->backtrack;
2559 s.semantic_match_passed += pcpu->semantic_match_passed;
2560 s.semantic_match_miss += pcpu->semantic_match_miss;
2561 s.null_node_hit += pcpu->null_node_hit;
2562 s.resize_node_skipped += pcpu->resize_node_skipped;
2563 }
2564
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002565 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002566 seq_printf(seq, "gets = %u\n", s.gets);
2567 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002568 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002569 s.semantic_match_passed);
2570 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2571 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2572 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002573}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002574#endif /* CONFIG_IP_FIB_TRIE_STATS */
2575
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002576static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002577{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002578 if (tb->tb_id == RT_TABLE_LOCAL)
2579 seq_puts(seq, "Local:\n");
2580 else if (tb->tb_id == RT_TABLE_MAIN)
2581 seq_puts(seq, "Main:\n");
2582 else
2583 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002584}
Robert Olsson19baf832005-06-21 12:43:18 -07002585
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002586
Robert Olsson19baf832005-06-21 12:43:18 -07002587static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2588{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002589 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002590 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002591
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002592 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002593 "Basic info: size of leaf:"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002594 " %zd bytes, size of tnode: %zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002595 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07002596
Qian Caifbe4e0c2020-03-25 18:01:00 -04002597 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002598 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2599 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002600 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002601
Sasha Levinb67bfe02013-02-27 17:06:00 -08002602 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002603 struct trie *t = (struct trie *) tb->tb_data;
2604 struct trie_stat stat;
2605
2606 if (!t)
2607 continue;
2608
2609 fib_table_print(seq, tb);
2610
2611 trie_collect_stats(t, &stat);
2612 trie_show_stats(seq, &stat);
2613#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002614 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002615#endif
2616 }
Qian Caifbe4e0c2020-03-25 18:01:00 -04002617 cond_resched_rcu();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002618 }
Qian Caifbe4e0c2020-03-25 18:01:00 -04002619 rcu_read_unlock();
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002620
Robert Olsson19baf832005-06-21 12:43:18 -07002621 return 0;
2622}
2623
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002624static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002625{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002626 struct fib_trie_iter *iter = seq->private;
2627 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002628 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002629 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002630
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002631 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2632 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002633 struct fib_table *tb;
2634
Sasha Levinb67bfe02013-02-27 17:06:00 -08002635 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002636 struct key_vector *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002637
2638 for (n = fib_trie_get_first(iter,
2639 (struct trie *) tb->tb_data);
2640 n; n = fib_trie_get_next(iter))
2641 if (pos == idx++) {
2642 iter->tb = tb;
2643 return n;
2644 }
2645 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002646 }
Robert Olsson19baf832005-06-21 12:43:18 -07002647
Robert Olsson19baf832005-06-21 12:43:18 -07002648 return NULL;
2649}
2650
2651static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002652 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002653{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002654 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002655 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002656}
2657
2658static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2659{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002660 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002661 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002662 struct fib_table *tb = iter->tb;
2663 struct hlist_node *tb_node;
2664 unsigned int h;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002665 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002666
Robert Olsson19baf832005-06-21 12:43:18 -07002667 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002668 /* next node in same table */
2669 n = fib_trie_get_next(iter);
2670 if (n)
2671 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002672
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002673 /* walk rest of this hash chain */
2674 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002675 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002676 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2677 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2678 if (n)
2679 goto found;
2680 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002681
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002682 /* new hash chain */
2683 while (++h < FIB_TABLE_HASHSZ) {
2684 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002685 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002686 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2687 if (n)
2688 goto found;
2689 }
2690 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002691 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002692
2693found:
2694 iter->tb = tb;
2695 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002696}
2697
2698static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002699 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002700{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002701 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002702}
2703
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002704static void seq_indent(struct seq_file *seq, int n)
2705{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002706 while (n-- > 0)
2707 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002708}
Robert Olsson19baf832005-06-21 12:43:18 -07002709
Eric Dumazet28d36e32008-01-14 23:09:56 -08002710static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002711{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002712 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002713 case RT_SCOPE_UNIVERSE: return "universe";
2714 case RT_SCOPE_SITE: return "site";
2715 case RT_SCOPE_LINK: return "link";
2716 case RT_SCOPE_HOST: return "host";
2717 case RT_SCOPE_NOWHERE: return "nowhere";
2718 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002719 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002720 return buf;
2721 }
2722}
2723
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002724static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002725 [RTN_UNSPEC] = "UNSPEC",
2726 [RTN_UNICAST] = "UNICAST",
2727 [RTN_LOCAL] = "LOCAL",
2728 [RTN_BROADCAST] = "BROADCAST",
2729 [RTN_ANYCAST] = "ANYCAST",
2730 [RTN_MULTICAST] = "MULTICAST",
2731 [RTN_BLACKHOLE] = "BLACKHOLE",
2732 [RTN_UNREACHABLE] = "UNREACHABLE",
2733 [RTN_PROHIBIT] = "PROHIBIT",
2734 [RTN_THROW] = "THROW",
2735 [RTN_NAT] = "NAT",
2736 [RTN_XRESOLVE] = "XRESOLVE",
2737};
2738
Eric Dumazeta034ee32010-09-09 23:32:28 +00002739static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002740{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002741 if (t < __RTN_MAX && rtn_type_names[t])
2742 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002743 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002744 return buf;
2745}
2746
2747/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002748static int fib_trie_seq_show(struct seq_file *seq, void *v)
2749{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002750 const struct fib_trie_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002751 struct key_vector *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002752
Alexander Duyck88bae712015-03-06 09:54:52 -08002753 if (IS_TRIE(node_parent_rcu(n)))
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002754 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002755
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002756 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002757 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002758
Alexander Duycke9b44012014-12-31 10:56:12 -08002759 seq_indent(seq, iter->depth-1);
2760 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2761 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
Alexander Duyck6e22d172015-03-06 09:54:39 -08002762 tn_info(n)->full_children,
2763 tn_info(n)->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002764 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002765 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002766 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002767
2768 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002769 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002770
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002771 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2772 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002773
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002774 seq_indent(seq, iter->depth + 1);
2775 seq_printf(seq, " /%zu %s %s",
2776 KEYLENGTH - fa->fa_slen,
2777 rtn_scope(buf1, sizeof(buf1),
2778 fa->fa_info->fib_scope),
2779 rtn_type(buf2, sizeof(buf2),
2780 fa->fa_type));
2781 if (fa->fa_tos)
2782 seq_printf(seq, " tos=%d", fa->fa_tos);
2783 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002784 }
Robert Olsson19baf832005-06-21 12:43:18 -07002785 }
2786
2787 return 0;
2788}
2789
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002790static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002791 .start = fib_trie_seq_start,
2792 .next = fib_trie_seq_next,
2793 .stop = fib_trie_seq_stop,
2794 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002795};
2796
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002797struct fib_route_iter {
2798 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002799 struct fib_table *main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002800 struct key_vector *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002801 loff_t pos;
2802 t_key key;
2803};
2804
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002805static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2806 loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002807{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002808 struct key_vector *l, **tp = &iter->tnode;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002809 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002810
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002811 /* use cached location of previously found key */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002812 if (iter->pos > 0 && pos >= iter->pos) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002813 key = iter->key;
2814 } else {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002815 iter->pos = 1;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002816 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002817 }
2818
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002819 pos -= iter->pos;
2820
2821 while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002822 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002823 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002824 l = NULL;
2825
2826 /* handle unlikely case of a key wrap */
2827 if (!key)
2828 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002829 }
2830
2831 if (l)
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002832 iter->key = l->key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002833 else
2834 iter->pos = 0; /* forget it */
2835
2836 return l;
2837}
2838
2839static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2840 __acquires(RCU)
2841{
2842 struct fib_route_iter *iter = seq->private;
2843 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002844 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002845
2846 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002847
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002848 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002849 if (!tb)
2850 return NULL;
2851
Alexander Duyck8be33e92015-03-04 14:59:19 -08002852 iter->main_tb = tb;
David Forster94d9f1c2016-08-03 15:13:01 +01002853 t = (struct trie *)tb->tb_data;
2854 iter->tnode = t->kv;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002855
2856 if (*pos != 0)
2857 return fib_route_get_idx(iter, *pos);
2858
Alexander Duyck8be33e92015-03-04 14:59:19 -08002859 iter->pos = 0;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002860 iter->key = KEY_MAX;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002861
2862 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002863}
2864
2865static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2866{
2867 struct fib_route_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002868 struct key_vector *l = NULL;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002869 t_key key = iter->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002870
2871 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002872
2873 /* only allow key of 0 for start of sequence */
2874 if ((v == SEQ_START_TOKEN) || key)
2875 l = leaf_walk_rcu(&iter->tnode, key);
2876
2877 if (l) {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002878 iter->key = l->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002879 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002880 } else {
2881 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002882 }
2883
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002884 return l;
2885}
2886
2887static void fib_route_seq_stop(struct seq_file *seq, void *v)
2888 __releases(RCU)
2889{
2890 rcu_read_unlock();
2891}
2892
David Ahern5481d732019-06-03 20:19:49 -07002893static unsigned int fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002894{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002895 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002896
Eric Dumazeta034ee32010-09-09 23:32:28 +00002897 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2898 flags = RTF_REJECT;
David Ahern5481d732019-06-03 20:19:49 -07002899 if (fi) {
David Aherndcb1ecb2019-06-03 20:19:50 -07002900 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
David Ahern5481d732019-06-03 20:19:49 -07002901
David Aherndcb1ecb2019-06-03 20:19:50 -07002902 if (nhc->nhc_gw.ipv4)
David Ahern5481d732019-06-03 20:19:49 -07002903 flags |= RTF_GATEWAY;
2904 }
Al Viro32ab5f82006-09-26 22:21:45 -07002905 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002906 flags |= RTF_HOST;
2907 flags |= RTF_UP;
2908 return flags;
2909}
2910
2911/*
2912 * This outputs /proc/net/route.
2913 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002914 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002915 * legacy utilities
2916 */
2917static int fib_route_seq_show(struct seq_file *seq, void *v)
2918{
Alexander Duyck654eff42015-03-11 16:36:08 -07002919 struct fib_route_iter *iter = seq->private;
2920 struct fib_table *tb = iter->main_tb;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002921 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002922 struct key_vector *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002923 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002924
2925 if (v == SEQ_START_TOKEN) {
2926 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2927 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2928 "\tWindow\tIRTT");
2929 return 0;
2930 }
2931
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002932 prefix = htonl(l->key);
2933
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002934 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
David Ahern5481d732019-06-03 20:19:49 -07002935 struct fib_info *fi = fa->fa_info;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002936 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2937 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002938
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002939 if ((fa->fa_type == RTN_BROADCAST) ||
2940 (fa->fa_type == RTN_MULTICAST))
2941 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002942
Alexander Duyck654eff42015-03-11 16:36:08 -07002943 if (fa->tb_id != tb->tb_id)
2944 continue;
2945
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002946 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002947
David Ahern5481d732019-06-03 20:19:49 -07002948 if (fi) {
David Aherndcb1ecb2019-06-03 20:19:50 -07002949 struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
2950 __be32 gw = 0;
2951
2952 if (nhc->nhc_gw_family == AF_INET)
2953 gw = nhc->nhc_gw.ipv4;
David Ahern5481d732019-06-03 20:19:49 -07002954
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002955 seq_printf(seq,
2956 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2957 "%d\t%08X\t%d\t%u\t%u",
David Aherndcb1ecb2019-06-03 20:19:50 -07002958 nhc->nhc_dev ? nhc->nhc_dev->name : "*",
2959 prefix, gw, flags, 0, 0,
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002960 fi->fib_priority,
2961 mask,
2962 (fi->fib_advmss ?
2963 fi->fib_advmss + 40 : 0),
2964 fi->fib_window,
2965 fi->fib_rtt >> 3);
David Ahern5481d732019-06-03 20:19:49 -07002966 } else {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002967 seq_printf(seq,
2968 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2969 "%d\t%08X\t%d\t%u\t%u",
2970 prefix, 0, flags, 0, 0, 0,
2971 mask, 0, 0, 0);
David Ahern5481d732019-06-03 20:19:49 -07002972 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002973 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002974 }
2975
2976 return 0;
2977}
2978
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002979static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002980 .start = fib_route_seq_start,
2981 .next = fib_route_seq_next,
2982 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002983 .show = fib_route_seq_show,
2984};
2985
Denis V. Lunev61a02652008-01-10 03:21:09 -08002986int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002987{
Christoph Hellwigc3506372018-04-10 19:42:55 +02002988 if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
2989 sizeof(struct fib_trie_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002990 goto out1;
2991
Christoph Hellwig3617d942018-04-13 20:38:35 +02002992 if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
2993 fib_triestat_seq_show, NULL))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002994 goto out2;
2995
Christoph Hellwigc3506372018-04-10 19:42:55 +02002996 if (!proc_create_net("route", 0444, net->proc_net, &fib_route_seq_ops,
2997 sizeof(struct fib_route_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002998 goto out3;
2999
Robert Olsson19baf832005-06-21 12:43:18 -07003000 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003001
3002out3:
Gao fengece31ff2013-02-18 01:34:56 +00003003 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003004out2:
Gao fengece31ff2013-02-18 01:34:56 +00003005 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07003006out1:
3007 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07003008}
3009
Denis V. Lunev61a02652008-01-10 03:21:09 -08003010void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07003011{
Gao fengece31ff2013-02-18 01:34:56 +00003012 remove_proc_entry("fib_trie", net->proc_net);
3013 remove_proc_entry("fib_triestat", net->proc_net);
3014 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07003015}
3016
3017#endif /* CONFIG_PROC_FS */