blob: 1e3b492690f9996d4811f040bae2cd31e92354c5 [file] [log] [blame]
Robert Olsson19baf832005-06-21 12:43:18 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
8 * & Swedish University of Agricultural Sciences.
9 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -070011 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090012 *
Robert Olsson19baf832005-06-21 12:43:18 -070013 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
14 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090016 *
Robert Olsson19baf832005-06-21 12:43:18 -070017 * An experimental study of compression methods for dynamic tries
18 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020019 * http://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070020 *
21 *
22 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
23 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
24 *
Robert Olsson19baf832005-06-21 12:43:18 -070025 *
26 * Code from fib_hash has been reused which includes the following header:
27 *
28 *
29 * INET An implementation of the TCP/IP protocol suite for the LINUX
30 * operating system. INET is implemented using the BSD Socket
31 * interface as the means of communication with the user level.
32 *
33 * IPv4 FIB: lookup engine and maintenance routines.
34 *
35 *
36 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
37 *
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
Robert Olssonfd966252005-12-22 11:25:10 -080042 *
43 * Substantial contributions to this work comes from:
44 *
45 * David S. Miller, <davem@davemloft.net>
46 * Stephen Hemminger <shemminger@osdl.org>
47 * Paul E. McKenney <paulmck@us.ibm.com>
48 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070049 */
50
Jens Låås80b71b82009-08-28 23:57:15 -070051#define VERSION "0.409"
Robert Olsson19baf832005-06-21 12:43:18 -070052
Alexey Dobriyan08009a72018-02-24 21:20:33 +030053#include <linux/cache.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080054#include <linux/uaccess.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070055#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070056#include <linux/types.h>
57#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070058#include <linux/mm.h>
59#include <linux/string.h>
60#include <linux/socket.h>
61#include <linux/sockios.h>
62#include <linux/errno.h>
63#include <linux/in.h>
64#include <linux/inet.h>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080065#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070066#include <linux/netdevice.h>
67#include <linux/if_arp.h>
68#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070069#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070070#include <linux/skbuff.h>
71#include <linux/netlink.h>
72#include <linux/init.h>
73#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040075#include <linux/export.h>
David S. Millerffa915d2015-05-27 00:19:03 -040076#include <linux/vmalloc.h>
Jiri Pirkob90eb752016-09-26 12:52:29 +020077#include <linux/notifier.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020078#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070079#include <net/ip.h>
80#include <net/protocol.h>
81#include <net/route.h>
82#include <net/tcp.h>
83#include <net/sock.h>
84#include <net/ip_fib.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020085#include <net/fib_notifier.h>
David Ahernf6d3c192015-08-28 08:42:09 -070086#include <trace/events/fib.h>
Robert Olsson19baf832005-06-21 12:43:18 -070087#include "fib_lookup.h"
88
Ido Schimmelc3852ef2016-12-03 16:45:07 +010089static int call_fib_entry_notifier(struct notifier_block *nb, struct net *net,
90 enum fib_event_type event_type, u32 dst,
David Ahern6eba87c2017-10-18 11:39:13 -070091 int dst_len, struct fib_alias *fa)
Ido Schimmelc3852ef2016-12-03 16:45:07 +010092{
93 struct fib_entry_notifier_info info = {
94 .dst = dst,
95 .dst_len = dst_len,
David Ahern6eba87c2017-10-18 11:39:13 -070096 .fi = fa->fa_info,
97 .tos = fa->fa_tos,
98 .type = fa->fa_type,
99 .tb_id = fa->tb_id,
Ido Schimmelc3852ef2016-12-03 16:45:07 +0100100 };
Ido Schimmel04b1d4e2017-08-03 13:28:11 +0200101 return call_fib4_notifier(nb, net, event_type, &info.info);
Ido Schimmelc3852ef2016-12-03 16:45:07 +0100102}
103
Jiri Pirkob90eb752016-09-26 12:52:29 +0200104static int call_fib_entry_notifiers(struct net *net,
105 enum fib_event_type event_type, u32 dst,
David Ahern6c31e5a2017-10-27 17:37:13 -0700106 int dst_len, struct fib_alias *fa,
107 struct netlink_ext_ack *extack)
Jiri Pirkob90eb752016-09-26 12:52:29 +0200108{
109 struct fib_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700110 .info.extack = extack,
Jiri Pirkob90eb752016-09-26 12:52:29 +0200111 .dst = dst,
112 .dst_len = dst_len,
David Ahern6eba87c2017-10-18 11:39:13 -0700113 .fi = fa->fa_info,
114 .tos = fa->fa_tos,
115 .type = fa->fa_type,
116 .tb_id = fa->tb_id,
Jiri Pirkob90eb752016-09-26 12:52:29 +0200117 };
Ido Schimmel04b1d4e2017-08-03 13:28:11 +0200118 return call_fib4_notifiers(net, event_type, &info.info);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200119}
120
Robert Olsson06ef9212006-03-20 21:35:01 -0800121#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -0700122
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800123#define KEYLENGTH (8*sizeof(t_key))
124#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -0700125
Robert Olsson19baf832005-06-21 12:43:18 -0700126typedef unsigned int t_key;
127
Alexander Duyck88bae712015-03-06 09:54:52 -0800128#define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
129#define IS_TNODE(n) ((n)->bits)
130#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700131
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800132struct key_vector {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800133 t_key key;
134 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
135 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
136 unsigned char slen;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800137 union {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800138 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800139 struct hlist_head leaf;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800140 /* This array is valid if (pos | bits) > 0 (TNODE) */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800141 struct key_vector __rcu *tnode[0];
Alexander Duyckadaf9812014-12-31 10:55:47 -0800142 };
Robert Olsson19baf832005-06-21 12:43:18 -0700143};
144
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800145struct tnode {
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800146 struct rcu_head rcu;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800147 t_key empty_children; /* KEYLENGTH bits needed */
148 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800149 struct key_vector __rcu *parent;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800150 struct key_vector kv[1];
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800151#define tn_bits kv[0].bits
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800152};
153
154#define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
Alexander Duyck41b489f2015-03-04 15:02:33 -0800155#define LEAF_SIZE TNODE_SIZE(1)
156
Robert Olsson19baf832005-06-21 12:43:18 -0700157#ifdef CONFIG_IP_FIB_TRIE_STATS
158struct trie_use_stats {
159 unsigned int gets;
160 unsigned int backtrack;
161 unsigned int semantic_match_passed;
162 unsigned int semantic_match_miss;
163 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700164 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700165};
166#endif
167
168struct trie_stat {
169 unsigned int totdepth;
170 unsigned int maxdepth;
171 unsigned int tnodes;
172 unsigned int leaves;
173 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800174 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800175 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700176};
Robert Olsson19baf832005-06-21 12:43:18 -0700177
178struct trie {
Alexander Duyck88bae712015-03-06 09:54:52 -0800179 struct key_vector kv[1];
Robert Olsson19baf832005-06-21 12:43:18 -0700180#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800181 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700182#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700183};
184
Alexander Duyck88bae712015-03-06 09:54:52 -0800185static struct key_vector *resize(struct trie *t, struct key_vector *tn);
David Ahern9ab948a2019-03-20 09:18:59 -0700186static unsigned int tnode_free_size;
Jarek Poplawskic3059472009-07-14 08:33:08 +0000187
188/*
David Ahern9ab948a2019-03-20 09:18:59 -0700189 * synchronize_rcu after call_rcu for outstanding dirty memory; it should be
190 * especially useful before resizing the root node with PREEMPT_NONE configs;
191 * the value was obtained experimentally, aiming to avoid visible slowdown.
Jarek Poplawskic3059472009-07-14 08:33:08 +0000192 */
David Ahern9ab948a2019-03-20 09:18:59 -0700193unsigned int sysctl_fib_sync_mem = 512 * 1024;
194unsigned int sysctl_fib_sync_mem_min = 64 * 1024;
195unsigned int sysctl_fib_sync_mem_max = 64 * 1024 * 1024;
Robert Olsson19baf832005-06-21 12:43:18 -0700196
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300197static struct kmem_cache *fn_alias_kmem __ro_after_init;
198static struct kmem_cache *trie_leaf_kmem __ro_after_init;
Robert Olsson19baf832005-06-21 12:43:18 -0700199
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800200static inline struct tnode *tn_info(struct key_vector *kv)
201{
202 return container_of(kv, struct tnode, kv[0]);
203}
204
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800205/* caller must hold RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800206#define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800207#define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700208
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800209/* caller must hold RCU read lock or RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800210#define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800211#define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700212
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800213/* wrapper for rcu_assign_pointer */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800214static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700215{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800216 if (n)
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800217 rcu_assign_pointer(tn_info(n)->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800218}
219
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800220#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800221
222/* This provides us with the number of children in this node, in the case of a
223 * leaf this will return 0 meaning none of the children are accessible.
224 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800225static inline unsigned long child_length(const struct key_vector *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800226{
227 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700228}
Robert Olsson2373ce12005-08-25 13:01:29 -0700229
Alexander Duyck88bae712015-03-06 09:54:52 -0800230#define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
231
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800232static inline unsigned long get_index(t_key key, struct key_vector *kv)
233{
234 unsigned long index = key ^ kv->key;
235
Alexander Duyck88bae712015-03-06 09:54:52 -0800236 if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
237 return 0;
238
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800239 return index >> kv->pos;
240}
241
Alexander Duycke9b44012014-12-31 10:56:12 -0800242/* To understand this stuff, an understanding of keys and all their bits is
243 * necessary. Every node in the trie has a key associated with it, but not
244 * all of the bits in that key are significant.
245 *
246 * Consider a node 'n' and its parent 'tp'.
247 *
248 * If n is a leaf, every bit in its key is significant. Its presence is
249 * necessitated by path compression, since during a tree traversal (when
250 * searching for a leaf - unless we are doing an insertion) we will completely
251 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
252 * a potentially successful search, that we have indeed been walking the
253 * correct key path.
254 *
255 * Note that we can never "miss" the correct key in the tree if present by
256 * following the wrong path. Path compression ensures that segments of the key
257 * that are the same for all keys with a given prefix are skipped, but the
258 * skipped part *is* identical for each node in the subtrie below the skipped
259 * bit! trie_insert() in this implementation takes care of that.
260 *
261 * if n is an internal node - a 'tnode' here, the various parts of its key
262 * have many different meanings.
263 *
264 * Example:
265 * _________________________________________________________________
266 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
267 * -----------------------------------------------------------------
268 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
269 *
270 * _________________________________________________________________
271 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
272 * -----------------------------------------------------------------
273 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
274 *
275 * tp->pos = 22
276 * tp->bits = 3
277 * n->pos = 13
278 * n->bits = 4
279 *
280 * First, let's just ignore the bits that come before the parent tp, that is
281 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
282 * point we do not use them for anything.
283 *
284 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
285 * index into the parent's child array. That is, they will be used to find
286 * 'n' among tp's children.
287 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800288 * The bits from (n->pos + n->bits) to (tp->pos - 1) - "S" - are skipped bits
Alexander Duycke9b44012014-12-31 10:56:12 -0800289 * for the node n.
290 *
291 * All the bits we have seen so far are significant to the node n. The rest
292 * of the bits are really not needed or indeed known in n->key.
293 *
294 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
295 * n's child array, and will of course be different for each child.
296 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800297 * The rest of the bits, from 0 to (n->pos -1) - "u" - are completely unknown
Alexander Duycke9b44012014-12-31 10:56:12 -0800298 * at this point.
299 */
Robert Olsson19baf832005-06-21 12:43:18 -0700300
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800301static const int halve_threshold = 25;
302static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700303static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700304static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700305
306static void __alias_free_mem(struct rcu_head *head)
307{
308 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
309 kmem_cache_free(fn_alias_kmem, fa);
310}
311
312static inline void alias_free_mem_rcu(struct fib_alias *fa)
313{
314 call_rcu(&fa->rcu, __alias_free_mem);
315}
316
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800317#define TNODE_KMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800318 ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck1de3d872015-03-04 15:04:46 -0800319#define TNODE_VMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800320 ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800321
322static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700323{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800324 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800325
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800326 if (!n->tn_bits)
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800327 kmem_cache_free(trie_leaf_kmem, n);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800328 else
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800329 kvfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700330}
331
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800332#define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700333
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800334static struct tnode *tnode_alloc(int bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700335{
Alexander Duyck1de3d872015-03-04 15:04:46 -0800336 size_t size;
337
338 /* verify bits is within bounds */
339 if (bits > TNODE_VMALLOC_MAX)
340 return NULL;
341
342 /* determine size and verify it is non-zero and didn't overflow */
343 size = TNODE_SIZE(1ul << bits);
344
Robert Olsson2373ce12005-08-25 13:01:29 -0700345 if (size <= PAGE_SIZE)
Eric Dumazet8d9654442008-01-13 22:31:44 -0800346 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700347 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000348 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700349}
Robert Olsson2373ce12005-08-25 13:01:29 -0700350
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800351static inline void empty_child_inc(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800352{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800353 ++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800354}
355
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800356static inline void empty_child_dec(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800357{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800358 tn_info(n)->empty_children-- ? : tn_info(n)->full_children--;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800359}
360
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800361static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
Robert Olsson19baf832005-06-21 12:43:18 -0700362{
Firo Yangf38b24c2015-06-08 11:54:51 +0800363 struct key_vector *l;
364 struct tnode *kv;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800365
Firo Yangf38b24c2015-06-08 11:54:51 +0800366 kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800367 if (!kv)
368 return NULL;
369
370 /* initialize key vector */
Firo Yangf38b24c2015-06-08 11:54:51 +0800371 l = kv->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800372 l->key = key;
373 l->pos = 0;
374 l->bits = 0;
375 l->slen = fa->fa_slen;
376
377 /* link leaf to fib alias */
378 INIT_HLIST_HEAD(&l->leaf);
379 hlist_add_head(&fa->fa_list, &l->leaf);
380
Robert Olsson19baf832005-06-21 12:43:18 -0700381 return l;
382}
383
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800384static struct key_vector *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700385{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800386 unsigned int shift = pos + bits;
Firo Yangf38b24c2015-06-08 11:54:51 +0800387 struct key_vector *tn;
388 struct tnode *tnode;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800389
390 /* verify bits and pos their msb bits clear and values are valid */
391 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700392
Firo Yangf38b24c2015-06-08 11:54:51 +0800393 tnode = tnode_alloc(bits);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800394 if (!tnode)
395 return NULL;
396
Firo Yangf38b24c2015-06-08 11:54:51 +0800397 pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
398 sizeof(struct key_vector *) << bits);
399
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800400 if (bits == KEYLENGTH)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800401 tnode->full_children = 1;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800402 else
Alexander Duyck6e22d172015-03-06 09:54:39 -0800403 tnode->empty_children = 1ul << bits;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800404
Firo Yangf38b24c2015-06-08 11:54:51 +0800405 tn = tnode->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800406 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
407 tn->pos = pos;
408 tn->bits = bits;
409 tn->slen = pos;
410
Robert Olsson19baf832005-06-21 12:43:18 -0700411 return tn;
412}
413
Alexander Duycke9b44012014-12-31 10:56:12 -0800414/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700415 * and no bits are skipped. See discussion in dyntree paper p. 6
416 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800417static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700418{
Alexander Duycke9b44012014-12-31 10:56:12 -0800419 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700420}
421
Alexander Duyckff181ed2014-12-31 10:56:43 -0800422/* Add a child at position i overwriting the old value.
423 * Update the value of full_children and empty_children.
424 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800425static void put_child(struct key_vector *tn, unsigned long i,
426 struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700427{
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800428 struct key_vector *chi = get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800429 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700430
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800431 BUG_ON(i >= child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700432
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800433 /* update emptyChildren, overflow into fullChildren */
Ian Morris00db4122015-04-03 09:17:27 +0100434 if (!n && chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800435 empty_child_inc(tn);
Ian Morris00db4122015-04-03 09:17:27 +0100436 if (n && !chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800437 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700438
Robert Olsson19baf832005-06-21 12:43:18 -0700439 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800440 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700441 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800442
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700443 if (wasfull && !isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800444 tn_info(tn)->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700445 else if (!wasfull && isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800446 tn_info(tn)->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700447
Alexander Duyck5405afd2014-12-31 10:57:08 -0800448 if (n && (tn->slen < n->slen))
449 tn->slen = n->slen;
450
Alexander Duyck41b489f2015-03-04 15:02:33 -0800451 rcu_assign_pointer(tn->tnode[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700452}
453
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800454static void update_children(struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800455{
456 unsigned long i;
457
458 /* update all of the child parent pointers */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800459 for (i = child_length(tn); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800460 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800461
462 if (!inode)
463 continue;
464
465 /* Either update the children of a tnode that
466 * already belongs to us or update the child
467 * to point to ourselves.
468 */
469 if (node_parent(inode) == tn)
470 update_children(inode);
471 else
472 node_set_parent(inode, tn);
473 }
474}
475
Alexander Duyck88bae712015-03-06 09:54:52 -0800476static inline void put_child_root(struct key_vector *tp, t_key key,
477 struct key_vector *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800478{
Alexander Duyck88bae712015-03-06 09:54:52 -0800479 if (IS_TRIE(tp))
480 rcu_assign_pointer(tp->tnode[0], n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800481 else
Alexander Duyck88bae712015-03-06 09:54:52 -0800482 put_child(tp, get_index(key, tp), n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800483}
484
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800485static inline void tnode_free_init(struct key_vector *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700486{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800487 tn_info(tn)->rcu.next = NULL;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800488}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700489
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800490static inline void tnode_free_append(struct key_vector *tn,
491 struct key_vector *n)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800492{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800493 tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
494 tn_info(tn)->rcu.next = &tn_info(n)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800495}
496
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800497static void tnode_free(struct key_vector *tn)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800498{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800499 struct callback_head *head = &tn_info(tn)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800500
501 while (head) {
502 head = head->next;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800503 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800504 node_free(tn);
505
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800506 tn = container_of(head, struct tnode, rcu)->kv;
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700507 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800508
David Ahern9ab948a2019-03-20 09:18:59 -0700509 if (tnode_free_size >= sysctl_fib_sync_mem) {
Alexander Duyckfc86a932014-12-31 10:56:49 -0800510 tnode_free_size = 0;
511 synchronize_rcu();
512 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700513}
514
Alexander Duyck88bae712015-03-06 09:54:52 -0800515static struct key_vector *replace(struct trie *t,
516 struct key_vector *oldtnode,
517 struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800518{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800519 struct key_vector *tp = node_parent(oldtnode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800520 unsigned long i;
521
522 /* setup the parent pointer out of and back into this node */
523 NODE_INIT_PARENT(tn, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -0800524 put_child_root(tp, tn->key, tn);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800525
526 /* update all of the child parent pointers */
527 update_children(tn);
528
529 /* all pointers should be clean so we are done */
530 tnode_free(oldtnode);
531
532 /* resize children now that oldtnode is freed */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800533 for (i = child_length(tn); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800534 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800535
536 /* resize child node */
537 if (tnode_full(tn, inode))
Alexander Duyck88bae712015-03-06 09:54:52 -0800538 tn = resize(t, inode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800539 }
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800540
Alexander Duyck88bae712015-03-06 09:54:52 -0800541 return tp;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800542}
543
Alexander Duyck88bae712015-03-06 09:54:52 -0800544static struct key_vector *inflate(struct trie *t,
545 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700546{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800547 struct key_vector *tn;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800548 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800549 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700550
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700551 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700552
Alexander Duycke9b44012014-12-31 10:56:12 -0800553 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700554 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800555 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700556
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800557 /* prepare oldtnode to be freed */
558 tnode_free_init(oldtnode);
559
Alexander Duyck12c081a2014-12-31 10:56:55 -0800560 /* Assemble all of the pointers in our cluster, in this case that
561 * represents all of the pointers out of our allocated nodes that
562 * point to existing tnodes and the links between our allocated
563 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700564 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800565 for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800566 struct key_vector *inode = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800567 struct key_vector *node0, *node1;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800568 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700569
Robert Olsson19baf832005-06-21 12:43:18 -0700570 /* An empty child */
Ian Morris51456b22015-04-03 09:17:26 +0100571 if (!inode)
Robert Olsson19baf832005-06-21 12:43:18 -0700572 continue;
573
574 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800575 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800576 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700577 continue;
578 }
579
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800580 /* drop the node in the old tnode free list */
581 tnode_free_append(oldtnode, inode);
582
Robert Olsson19baf832005-06-21 12:43:18 -0700583 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700584 if (inode->bits == 1) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800585 put_child(tn, 2 * i + 1, get_child(inode, 1));
586 put_child(tn, 2 * i, get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700587 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700588 }
589
Olof Johansson91b9a272005-08-09 20:24:39 -0700590 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800591 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700592 * original children. The two new nodes will have
593 * a position one bit further down the key and this
594 * means that the "significant" part of their keys
595 * (see the discussion near the top of this file)
596 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800597 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700598 * moving the key position by one step, the bit that
599 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800600 * (tn->pos) - is the one that will differ between
601 * node0 and node1. So... we synthesize that bit in the
602 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700603 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800604 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
605 if (!node1)
606 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800607 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700608
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800609 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800610 if (!node0)
611 goto nomem;
612 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700613
Alexander Duyck12c081a2014-12-31 10:56:55 -0800614 /* populate child pointers in new nodes */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800615 for (k = child_length(inode), j = k / 2; j;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800616 put_child(node1, --j, get_child(inode, --k));
617 put_child(node0, j, get_child(inode, j));
618 put_child(node1, --j, get_child(inode, --k));
619 put_child(node0, j, get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700620 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800621
Alexander Duyck12c081a2014-12-31 10:56:55 -0800622 /* link new nodes to parent */
623 NODE_INIT_PARENT(node1, tn);
624 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700625
Alexander Duyck12c081a2014-12-31 10:56:55 -0800626 /* link parent to nodes */
627 put_child(tn, 2 * i + 1, node1);
628 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700629 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800630
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800631 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800632 return replace(t, oldtnode, tn);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700633nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800634 /* all pointers should be clean so we are done */
635 tnode_free(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800636notnode:
637 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700638}
639
Alexander Duyck88bae712015-03-06 09:54:52 -0800640static struct key_vector *halve(struct trie *t,
641 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700642{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800643 struct key_vector *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800644 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700645
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700646 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700647
Alexander Duycke9b44012014-12-31 10:56:12 -0800648 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700649 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800650 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700651
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800652 /* prepare oldtnode to be freed */
653 tnode_free_init(oldtnode);
654
Alexander Duyck12c081a2014-12-31 10:56:55 -0800655 /* Assemble all of the pointers in our cluster, in this case that
656 * represents all of the pointers out of our allocated nodes that
657 * point to existing tnodes and the links between our allocated
658 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700659 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800660 for (i = child_length(oldtnode); i;) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800661 struct key_vector *node1 = get_child(oldtnode, --i);
662 struct key_vector *node0 = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800663 struct key_vector *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700664
Alexander Duyck12c081a2014-12-31 10:56:55 -0800665 /* At least one of the children is empty */
666 if (!node1 || !node0) {
667 put_child(tn, i / 2, node1 ? : node0);
668 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700669 }
Robert Olsson2f368952005-07-05 15:02:40 -0700670
Alexander Duyck12c081a2014-12-31 10:56:55 -0800671 /* Two nonempty children */
672 inode = tnode_new(node0->key, oldtnode->pos, 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800673 if (!inode)
674 goto nomem;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800675 tnode_free_append(tn, inode);
676
677 /* initialize pointers out of node */
678 put_child(inode, 1, node1);
679 put_child(inode, 0, node0);
680 NODE_INIT_PARENT(inode, tn);
681
682 /* link parent to node */
683 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700684 }
Robert Olsson19baf832005-06-21 12:43:18 -0700685
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800686 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800687 return replace(t, oldtnode, tn);
688nomem:
689 /* all pointers should be clean so we are done */
690 tnode_free(tn);
691notnode:
692 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700693}
694
Alexander Duyck88bae712015-03-06 09:54:52 -0800695static struct key_vector *collapse(struct trie *t,
696 struct key_vector *oldtnode)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800697{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800698 struct key_vector *n, *tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800699 unsigned long i;
700
701 /* scan the tnode looking for that one child that might still exist */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800702 for (n = NULL, i = child_length(oldtnode); !n && i;)
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800703 n = get_child(oldtnode, --i);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800704
705 /* compress one level */
706 tp = node_parent(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800707 put_child_root(tp, oldtnode->key, n);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800708 node_set_parent(n, tp);
709
710 /* drop dead node */
711 node_free(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800712
713 return tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800714}
715
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800716static unsigned char update_suffix(struct key_vector *tn)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800717{
718 unsigned char slen = tn->pos;
719 unsigned long stride, i;
Alexander Duycka52ca622016-12-01 07:27:57 -0500720 unsigned char slen_max;
721
722 /* only vector 0 can have a suffix length greater than or equal to
723 * tn->pos + tn->bits, the second highest node will have a suffix
724 * length at most of tn->pos + tn->bits - 1
725 */
726 slen_max = min_t(unsigned char, tn->pos + tn->bits - 1, tn->slen);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800727
728 /* search though the list of children looking for nodes that might
729 * have a suffix greater than the one we currently have. This is
730 * why we start with a stride of 2 since a stride of 1 would
731 * represent the nodes with suffix length equal to tn->pos
732 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800733 for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
Alexander Duyck754baf8d2015-03-06 09:54:14 -0800734 struct key_vector *n = get_child(tn, i);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800735
736 if (!n || (n->slen <= slen))
737 continue;
738
739 /* update stride and slen based on new value */
740 stride <<= (n->slen - slen);
741 slen = n->slen;
742 i &= ~(stride - 1);
743
Alexander Duycka52ca622016-12-01 07:27:57 -0500744 /* stop searching if we have hit the maximum possible value */
745 if (slen >= slen_max)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800746 break;
747 }
748
749 tn->slen = slen;
750
751 return slen;
752}
753
Alexander Duyckf05a4812014-12-31 10:56:37 -0800754/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
755 * the Helsinki University of Technology and Matti Tikkanen of Nokia
756 * Telecommunications, page 6:
757 * "A node is doubled if the ratio of non-empty children to all
758 * children in the *doubled* node is at least 'high'."
759 *
760 * 'high' in this instance is the variable 'inflate_threshold'. It
761 * is expressed as a percentage, so we multiply it with
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800762 * child_length() and instead of multiplying by 2 (since the
Alexander Duyckf05a4812014-12-31 10:56:37 -0800763 * child array will be doubled by inflate()) and multiplying
764 * the left-hand side by 100 (to handle the percentage thing) we
765 * multiply the left-hand side by 50.
766 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800767 * The left-hand side may look a bit weird: child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800768 * - tn->empty_children is of course the number of non-null children
769 * in the current node. tn->full_children is the number of "full"
770 * children, that is non-null tnodes with a skip value of 0.
771 * All of those will be doubled in the resulting inflated tnode, so
772 * we just count them one extra time here.
773 *
774 * A clearer way to write this would be:
775 *
776 * to_be_doubled = tn->full_children;
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800777 * not_to_be_doubled = child_length(tn) - tn->empty_children -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800778 * tn->full_children;
779 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800780 * new_child_length = child_length(tn) * 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800781 *
782 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
783 * new_child_length;
784 * if (new_fill_factor >= inflate_threshold)
785 *
786 * ...and so on, tho it would mess up the while () loop.
787 *
788 * anyway,
789 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
790 * inflate_threshold
791 *
792 * avoid a division:
793 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
794 * inflate_threshold * new_child_length
795 *
796 * expand not_to_be_doubled and to_be_doubled, and shorten:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800797 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800798 * tn->full_children) >= inflate_threshold * new_child_length
799 *
800 * expand new_child_length:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800801 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800802 * tn->full_children) >=
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800803 * inflate_threshold * child_length(tn) * 2
Alexander Duyckf05a4812014-12-31 10:56:37 -0800804 *
805 * shorten again:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800806 * 50 * (tn->full_children + child_length(tn) -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800807 * tn->empty_children) >= inflate_threshold *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800808 * child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800809 *
810 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800811static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800812{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800813 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800814 unsigned long threshold = used;
815
816 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800817 threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800818 used -= tn_info(tn)->empty_children;
819 used += tn_info(tn)->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800820
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800821 /* if bits == KEYLENGTH then pos = 0, and will fail below */
822
823 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800824}
825
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800826static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800827{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800828 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800829 unsigned long threshold = used;
830
831 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800832 threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800833 used -= tn_info(tn)->empty_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800834
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800835 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
836
837 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
838}
839
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800840static inline bool should_collapse(struct key_vector *tn)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800841{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800842 unsigned long used = child_length(tn);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800843
Alexander Duyck6e22d172015-03-06 09:54:39 -0800844 used -= tn_info(tn)->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800845
846 /* account for bits == KEYLENGTH case */
Alexander Duyck6e22d172015-03-06 09:54:39 -0800847 if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800848 used -= KEY_MAX;
849
850 /* One child or none, time to drop us from the trie */
851 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800852}
853
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800854#define MAX_WORK 10
Alexander Duyck88bae712015-03-06 09:54:52 -0800855static struct key_vector *resize(struct trie *t, struct key_vector *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800856{
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800857#ifdef CONFIG_IP_FIB_TRIE_STATS
858 struct trie_use_stats __percpu *stats = t->stats;
859#endif
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800860 struct key_vector *tp = node_parent(tn);
Alexander Duyck88bae712015-03-06 09:54:52 -0800861 unsigned long cindex = get_index(tn->key, tp);
Alexander Duycka80e89d2015-01-22 15:51:20 -0800862 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800863
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800864 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
865 tn, inflate_threshold, halve_threshold);
866
Alexander Duyckff181ed2014-12-31 10:56:43 -0800867 /* track the tnode via the pointer from the parent instead of
868 * doing it ourselves. This way we can let RCU fully do its
869 * thing without us interfering
870 */
Alexander Duyck88bae712015-03-06 09:54:52 -0800871 BUG_ON(tn != get_child(tp, cindex));
Alexander Duyckff181ed2014-12-31 10:56:43 -0800872
Alexander Duyckf05a4812014-12-31 10:56:37 -0800873 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800874 * nonempty nodes that are above the threshold.
875 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700876 while (should_inflate(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800877 tp = inflate(t, tn);
878 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800879#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800880 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800881#endif
882 break;
883 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800884
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700885 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -0800886 tn = get_child(tp, cindex);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800887 }
888
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700889 /* update parent in case inflate failed */
890 tp = node_parent(tn);
891
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800892 /* Return if at least one inflate is run */
893 if (max_work != MAX_WORK)
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700894 return tp;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800895
Alexander Duyckf05a4812014-12-31 10:56:37 -0800896 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800897 * node is above threshold.
898 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700899 while (should_halve(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800900 tp = halve(t, tn);
901 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800902#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800903 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800904#endif
905 break;
906 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800907
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700908 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -0800909 tn = get_child(tp, cindex);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800910 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800911
912 /* Only one child remains */
Alexander Duyck88bae712015-03-06 09:54:52 -0800913 if (should_collapse(tn))
914 return collapse(t, tn);
915
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700916 /* update parent in case halve failed */
Alexander Duycka52ca622016-12-01 07:27:57 -0500917 return node_parent(tn);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800918}
919
Alexander Duyck1a239172016-12-01 07:27:52 -0500920static void node_pull_suffix(struct key_vector *tn, unsigned char slen)
Robert Olsson19baf832005-06-21 12:43:18 -0700921{
Alexander Duyck1a239172016-12-01 07:27:52 -0500922 unsigned char node_slen = tn->slen;
923
924 while ((node_slen > tn->pos) && (node_slen > slen)) {
925 slen = update_suffix(tn);
926 if (node_slen == slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800927 break;
Alexander Duyck1a239172016-12-01 07:27:52 -0500928
929 tn = node_parent(tn);
930 node_slen = tn->slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800931 }
932}
933
Alexander Duyck1a239172016-12-01 07:27:52 -0500934static void node_push_suffix(struct key_vector *tn, unsigned char slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800935{
Alexander Duyck1a239172016-12-01 07:27:52 -0500936 while (tn->slen < slen) {
937 tn->slen = slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800938 tn = node_parent(tn);
939 }
940}
941
Robert Olsson2373ce12005-08-25 13:01:29 -0700942/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800943static struct key_vector *fib_find_node(struct trie *t,
944 struct key_vector **tp, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700945{
Alexander Duyck88bae712015-03-06 09:54:52 -0800946 struct key_vector *pn, *n = t->kv;
947 unsigned long index = 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700948
Alexander Duyck88bae712015-03-06 09:54:52 -0800949 do {
950 pn = n;
951 n = get_child_rcu(n, index);
952
953 if (!n)
954 break;
955
956 index = get_cindex(key, n);
Alexander Duyck939afb02014-12-31 10:56:00 -0800957
958 /* This bit of code is a bit tricky but it combines multiple
959 * checks into a single check. The prefix consists of the
960 * prefix plus zeros for the bits in the cindex. The index
961 * is the difference between the key and this value. From
962 * this we can actually derive several pieces of data.
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800963 * if (index >= (1ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800964 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800965 * else
966 * we know the value is cindex
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800967 *
968 * This check is safe even if bits == KEYLENGTH due to the
969 * fact that we can only allocate a node with 32 bits if a
970 * long is greater than 32 bits.
Alexander Duyck939afb02014-12-31 10:56:00 -0800971 */
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800972 if (index >= (1ul << n->bits)) {
973 n = NULL;
974 break;
975 }
Alexander Duyck939afb02014-12-31 10:56:00 -0800976
Alexander Duyck88bae712015-03-06 09:54:52 -0800977 /* keep searching until we find a perfect match leaf or NULL */
978 } while (IS_TNODE(n));
Robert Olsson19baf832005-06-21 12:43:18 -0700979
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800980 *tp = pn;
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800981
Alexander Duyck939afb02014-12-31 10:56:00 -0800982 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700983}
984
Alexander Duyck02525362015-01-22 15:51:39 -0800985/* Return the first fib alias matching TOS with
986 * priority less than or equal to PRIO.
987 */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800988static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
Alexander Duyck0b65bd92015-03-12 14:46:29 -0700989 u8 tos, u32 prio, u32 tb_id)
Alexander Duyck02525362015-01-22 15:51:39 -0800990{
991 struct fib_alias *fa;
992
993 if (!fah)
994 return NULL;
995
Alexander Duyck56315f92015-02-25 15:31:31 -0800996 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800997 if (fa->fa_slen < slen)
998 continue;
999 if (fa->fa_slen != slen)
1000 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001001 if (fa->tb_id > tb_id)
1002 continue;
1003 if (fa->tb_id != tb_id)
1004 break;
Alexander Duyck02525362015-01-22 15:51:39 -08001005 if (fa->fa_tos > tos)
1006 continue;
1007 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
1008 return fa;
1009 }
1010
1011 return NULL;
1012}
1013
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001014static void trie_rebalance(struct trie *t, struct key_vector *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001015{
Alexander Duyck88bae712015-03-06 09:54:52 -08001016 while (!IS_TRIE(tn))
1017 tn = resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001018}
1019
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001020static int fib_insert_node(struct trie *t, struct key_vector *tp,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001021 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001022{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001023 struct key_vector *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -08001024
Alexander Duyckd5d64872015-03-04 15:02:18 -08001025 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001026 if (!l)
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001027 goto noleaf;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001028
1029 /* retrieve child from parent node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001030 n = get_child(tp, get_index(key, tp));
Alexander Duyck836a0122014-12-31 10:56:06 -08001031
1032 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1033 *
1034 * Add a new tnode here
1035 * first tnode need some special handling
1036 * leaves us in position for handling as case 3
1037 */
1038 if (n) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001039 struct key_vector *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001040
Alexander Duycke9b44012014-12-31 10:56:12 -08001041 tn = tnode_new(key, __fls(key ^ n->key), 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001042 if (!tn)
1043 goto notnode;
Olof Johansson91b9a272005-08-09 20:24:39 -07001044
Alexander Duyck836a0122014-12-31 10:56:06 -08001045 /* initialize routes out of node */
1046 NODE_INIT_PARENT(tn, tp);
1047 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001048
Alexander Duyck836a0122014-12-31 10:56:06 -08001049 /* start adding routes into the node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001050 put_child_root(tp, key, tn);
Alexander Duyck836a0122014-12-31 10:56:06 -08001051 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001052
Alexander Duyck836a0122014-12-31 10:56:06 -08001053 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001054 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001055 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001056
Alexander Duyck836a0122014-12-31 10:56:06 -08001057 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duycka52ca622016-12-01 07:27:57 -05001058 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001059 NODE_INIT_PARENT(l, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -08001060 put_child_root(tp, key, l);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001061 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001062
Alexander Duyckd5d64872015-03-04 15:02:18 -08001063 return 0;
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001064notnode:
1065 node_free(l);
1066noleaf:
1067 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07001068}
1069
David Ahern6635f312018-03-27 18:21:57 -07001070/* fib notifier for ADD is sent before calling fib_insert_alias with
1071 * the expectation that the only possible failure ENOMEM
1072 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001073static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1074 struct key_vector *l, struct fib_alias *new,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001075 struct fib_alias *fa, t_key key)
1076{
1077 if (!l)
1078 return fib_insert_node(t, tp, new, key);
1079
1080 if (fa) {
1081 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1082 } else {
1083 struct fib_alias *last;
1084
1085 hlist_for_each_entry(last, &l->leaf, fa_list) {
1086 if (new->fa_slen < last->fa_slen)
1087 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001088 if ((new->fa_slen == last->fa_slen) &&
1089 (new->tb_id > last->tb_id))
1090 break;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001091 fa = last;
1092 }
1093
1094 if (fa)
1095 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1096 else
1097 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1098 }
1099
1100 /* if we added to the tail node then we need to update slen */
1101 if (l->slen < new->fa_slen) {
1102 l->slen = new->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001103 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001104 }
1105
1106 return 0;
1107}
1108
David Ahern78055992017-05-27 16:19:26 -06001109static bool fib_valid_key_len(u32 key, u8 plen, struct netlink_ext_ack *extack)
David Ahernba277e82017-05-27 16:19:25 -06001110{
David Ahern78055992017-05-27 16:19:26 -06001111 if (plen > KEYLENGTH) {
1112 NL_SET_ERR_MSG(extack, "Invalid prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001113 return false;
David Ahern78055992017-05-27 16:19:26 -06001114 }
David Ahernba277e82017-05-27 16:19:25 -06001115
David Ahern78055992017-05-27 16:19:26 -06001116 if ((plen < KEYLENGTH) && (key << plen)) {
1117 NL_SET_ERR_MSG(extack,
1118 "Invalid prefix for given prefix length");
David Ahernba277e82017-05-27 16:19:25 -06001119 return false;
David Ahern78055992017-05-27 16:19:26 -06001120 }
David Ahernba277e82017-05-27 16:19:25 -06001121
1122 return true;
1123}
1124
Alexander Duyckd5d64872015-03-04 15:02:18 -08001125/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001126int fib_table_insert(struct net *net, struct fib_table *tb,
David Ahern6d8422a12017-05-21 10:12:02 -06001127 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001128{
Ido Schimmel2f3a5272017-02-09 10:28:41 +01001129 enum fib_event_type event = FIB_EVENT_ENTRY_ADD;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001130 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001131 struct fib_alias *fa, *new_fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001132 struct key_vector *l, *tp;
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001133 u16 nlflags = NLM_F_EXCL;
Robert Olsson19baf832005-06-21 12:43:18 -07001134 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001135 u8 plen = cfg->fc_dst_len;
1136 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001137 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001138 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001139 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001140
Thomas Graf4e902c52006-08-17 18:14:52 -07001141 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001142
David Ahern78055992017-05-27 16:19:26 -06001143 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001144 return -EINVAL;
1145
David Ahernba277e82017-05-27 16:19:25 -06001146 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
1147
David Ahern6d8422a12017-05-21 10:12:02 -06001148 fi = fib_create_info(cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001149 if (IS_ERR(fi)) {
1150 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001151 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001152 }
Robert Olsson19baf832005-06-21 12:43:18 -07001153
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001154 l = fib_find_node(t, &tp, key);
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001155 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
1156 tb->tb_id) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001157
1158 /* Now fa, if non-NULL, points to the first fib alias
1159 * with the same keys [prefix,tos,priority], if such key already
1160 * exists or to the node before which we will insert new one.
1161 *
1162 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001163 * insert to the tail of the section matching the suffix length
1164 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001165 */
1166
Julian Anastasov936f6f82008-01-28 21:18:06 -08001167 if (fa && fa->fa_tos == tos &&
1168 fa->fa_info->fib_priority == fi->fib_priority) {
1169 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001170
1171 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001172 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001173 goto out;
1174
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001175 nlflags &= ~NLM_F_EXCL;
1176
Julian Anastasov936f6f82008-01-28 21:18:06 -08001177 /* We have 2 goals:
1178 * 1. Find exact match for type, scope, fib_info to avoid
1179 * duplicate routes
1180 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1181 */
1182 fa_match = NULL;
1183 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001184 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001185 if ((fa->fa_slen != slen) ||
1186 (fa->tb_id != tb->tb_id) ||
1187 (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001188 break;
1189 if (fa->fa_info->fib_priority != fi->fib_priority)
1190 break;
1191 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001192 fa->fa_info == fi) {
1193 fa_match = fa;
1194 break;
1195 }
1196 }
1197
Thomas Graf4e902c52006-08-17 18:14:52 -07001198 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001199 struct fib_info *fi_drop;
1200 u8 state;
1201
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001202 nlflags |= NLM_F_REPLACE;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001203 fa = fa_first;
1204 if (fa_match) {
1205 if (fa == fa_match)
1206 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001207 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001208 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001209 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001210 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001211 if (!new_fa)
Robert Olsson2373ce12005-08-25 13:01:29 -07001212 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001213
1214 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001215 new_fa->fa_tos = fa->fa_tos;
1216 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001217 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001218 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001219 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001220 new_fa->fa_slen = fa->fa_slen;
Michal Kubečekd4e64c22015-05-22 13:40:09 +02001221 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001222 new_fa->fa_default = -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001223
David Ahernc1d7ee62018-03-27 18:21:58 -07001224 err = call_fib_entry_notifiers(net,
1225 FIB_EVENT_ENTRY_REPLACE,
1226 key, plen, new_fa,
1227 extack);
1228 if (err)
1229 goto out_free_new_fa;
1230
Ido Schimmel5b7d6162017-02-09 10:28:40 +01001231 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1232 tb->tb_id, &cfg->fc_nlinfo, nlflags);
1233
Alexander Duyck56315f92015-02-25 15:31:31 -08001234 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001235
Robert Olsson2373ce12005-08-25 13:01:29 -07001236 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001237
1238 fib_release_info(fi_drop);
1239 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001240 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Jiri Pirkob90eb752016-09-26 12:52:29 +02001241
Olof Johansson91b9a272005-08-09 20:24:39 -07001242 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001243 }
1244 /* Error if we find a perfect match which
1245 * uses the same scope, type, and nexthop
1246 * information.
1247 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001248 if (fa_match)
1249 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001250
Ido Schimmel2f3a5272017-02-09 10:28:41 +01001251 if (cfg->fc_nlflags & NLM_F_APPEND) {
1252 event = FIB_EVENT_ENTRY_APPEND;
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001253 nlflags |= NLM_F_APPEND;
Ido Schimmel2f3a5272017-02-09 10:28:41 +01001254 } else {
Julian Anastasov936f6f82008-01-28 21:18:06 -08001255 fa = fa_first;
Ido Schimmel2f3a5272017-02-09 10:28:41 +01001256 }
Robert Olsson19baf832005-06-21 12:43:18 -07001257 }
1258 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001259 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001260 goto out;
1261
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001262 nlflags |= NLM_F_CREATE;
Robert Olsson19baf832005-06-21 12:43:18 -07001263 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001264 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001265 if (!new_fa)
Robert Olsson19baf832005-06-21 12:43:18 -07001266 goto out;
1267
1268 new_fa->fa_info = fi;
1269 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001270 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001271 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001272 new_fa->fa_slen = slen;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001273 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001274 new_fa->fa_default = -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001275
David Ahern6635f312018-03-27 18:21:57 -07001276 err = call_fib_entry_notifiers(net, event, key, plen, new_fa, extack);
1277 if (err)
1278 goto out_free_new_fa;
1279
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001280 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001281 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1282 if (err)
David Ahern6635f312018-03-27 18:21:57 -07001283 goto out_fib_notif;
Robert Olsson19baf832005-06-21 12:43:18 -07001284
David S. Miller21d8c492011-04-14 14:49:37 -07001285 if (!plen)
1286 tb->tb_num_default++;
1287
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001288 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001289 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001290 &cfg->fc_nlinfo, nlflags);
Robert Olsson19baf832005-06-21 12:43:18 -07001291succeeded:
1292 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001293
David Ahern6635f312018-03-27 18:21:57 -07001294out_fib_notif:
1295 /* notifier was sent that entry would be added to trie, but
1296 * the add failed and need to recover. Only failure for
1297 * fib_insert_alias is ENOMEM.
1298 */
1299 NL_SET_ERR_MSG(extack, "Failed to insert route into trie");
1300 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, key,
1301 plen, new_fa, NULL);
Robert Olssonf835e472005-06-28 15:00:39 -07001302out_free_new_fa:
1303 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001304out:
1305 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001306err:
Robert Olsson19baf832005-06-21 12:43:18 -07001307 return err;
1308}
1309
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001310static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001311{
1312 t_key prefix = n->key;
1313
1314 return (key ^ prefix) & (prefix | -prefix);
1315}
1316
Alexander Duyck345e9b52014-12-31 10:56:24 -08001317/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001318int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001319 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001320{
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001321 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001322#ifdef CONFIG_IP_FIB_TRIE_STATS
1323 struct trie_use_stats __percpu *stats = t->stats;
1324#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001325 const t_key key = ntohl(flp->daddr);
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001326 struct key_vector *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001327 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001328 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001329 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001330
Alexander Duyck88bae712015-03-06 09:54:52 -08001331 pn = t->kv;
1332 cindex = 0;
1333
1334 n = get_child_rcu(pn, cindex);
David Ahern9f323972018-05-23 17:08:47 -07001335 if (!n) {
1336 trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001337 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001338 }
Robert Olsson19baf832005-06-21 12:43:18 -07001339
1340#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001341 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001342#endif
1343
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001344 /* Step 1: Travel to the longest prefix match in the trie */
1345 for (;;) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001346 index = get_cindex(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001347
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001348 /* This bit of code is a bit tricky but it combines multiple
1349 * checks into a single check. The prefix consists of the
1350 * prefix plus zeros for the "bits" in the prefix. The index
1351 * is the difference between the key and this value. From
1352 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001353 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001354 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001355 * else
1356 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001357 *
1358 * This check is safe even if bits == KEYLENGTH due to the
1359 * fact that we can only allocate a node with 32 bits if a
1360 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001361 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001362 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001363 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001364
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001365 /* we have found a leaf. Prefixes have already been compared */
1366 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001367 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001368
1369 /* only record pn and cindex if we are going to be chopping
1370 * bits later. Otherwise we are just wasting cycles.
1371 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001372 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001373 pn = n;
1374 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001375 }
1376
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001377 n = get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001378 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001379 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001380 }
1381
1382 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1383 for (;;) {
1384 /* record the pointer where our next node pointer is stored */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001385 struct key_vector __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001386
1387 /* This test verifies that none of the bits that differ
1388 * between the key and the prefix exist in the region of
1389 * the lsb and higher in the prefix.
1390 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001391 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001392 goto backtrace;
1393
1394 /* exit out and process leaf */
1395 if (unlikely(IS_LEAF(n)))
1396 break;
1397
1398 /* Don't bother recording parent info. Since we are in
1399 * prefix match mode we will have to come back to wherever
1400 * we started this traversal anyway
1401 */
1402
1403 while ((n = rcu_dereference(*cptr)) == NULL) {
1404backtrace:
1405#ifdef CONFIG_IP_FIB_TRIE_STATS
1406 if (!n)
1407 this_cpu_inc(stats->null_node_hit);
1408#endif
1409 /* If we are at cindex 0 there are no more bits for
1410 * us to strip at this level so we must ascend back
1411 * up one level to see if there are any more bits to
1412 * be stripped there.
1413 */
1414 while (!cindex) {
1415 t_key pkey = pn->key;
1416
Alexander Duyck88bae712015-03-06 09:54:52 -08001417 /* If we don't have a parent then there is
1418 * nothing for us to do as we do not have any
1419 * further nodes to parse.
1420 */
David Ahern9f323972018-05-23 17:08:47 -07001421 if (IS_TRIE(pn)) {
1422 trace_fib_table_lookup(tb->tb_id, flp,
1423 NULL, -EAGAIN);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001424 return -EAGAIN;
David Ahern9f323972018-05-23 17:08:47 -07001425 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001426#ifdef CONFIG_IP_FIB_TRIE_STATS
1427 this_cpu_inc(stats->backtrack);
1428#endif
1429 /* Get Child's index */
Alexander Duyck88bae712015-03-06 09:54:52 -08001430 pn = node_parent_rcu(pn);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001431 cindex = get_index(pkey, pn);
1432 }
1433
1434 /* strip the least significant bit from the cindex */
1435 cindex &= cindex - 1;
1436
1437 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001438 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001439 }
Robert Olsson19baf832005-06-21 12:43:18 -07001440 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001441
Robert Olsson19baf832005-06-21 12:43:18 -07001442found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001443 /* this line carries forward the xor from earlier in the function */
1444 index = key ^ n->key;
1445
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001446 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001447 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1448 struct fib_info *fi = fa->fa_info;
1449 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001450
Alexander Duycka5829f52016-01-28 13:42:24 -08001451 if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
1452 if (index >= (1ul << fa->fa_slen))
1453 continue;
1454 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001455 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1456 continue;
1457 if (fi->fib_dead)
1458 continue;
1459 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1460 continue;
1461 fib_alias_accessed(fa);
1462 err = fib_props[fa->fa_type].error;
1463 if (unlikely(err < 0)) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001464#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001465 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001466#endif
David Ahern9f323972018-05-23 17:08:47 -07001467 trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001468 return err;
1469 }
1470 if (fi->fib_flags & RTNH_F_DEAD)
1471 continue;
1472 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1473 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1474
David Ahernb75ed8b2019-03-27 20:53:55 -07001475 if (nh->fib_nh_flags & RTNH_F_DEAD)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001476 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001477 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
1478 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001479 !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
1480 continue;
David Ahern58189ca2015-09-15 15:10:50 -07001481 if (!(flp->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) {
David Ahern613d09b2015-08-13 14:59:02 -06001482 if (flp->flowi4_oif &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001483 flp->flowi4_oif != nh->fib_nh_oif)
David Ahern613d09b2015-08-13 14:59:02 -06001484 continue;
1485 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001486
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001487 if (!(fib_flags & FIB_LOOKUP_NOREF))
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001488 refcount_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001489
David Ahern6ffd9032017-05-25 10:42:37 -07001490 res->prefix = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001491 res->prefixlen = KEYLENGTH - fa->fa_slen;
1492 res->nh_sel = nhsel;
1493 res->type = fa->fa_type;
1494 res->scope = fi->fib_scope;
1495 res->fi = fi;
1496 res->table = tb;
1497 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001498#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001499 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001500#endif
David Ahern9f323972018-05-23 17:08:47 -07001501 trace_fib_table_lookup(tb->tb_id, flp, nh, err);
David Ahernf6d3c192015-08-28 08:42:09 -07001502
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001503 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001504 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001505 }
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001506#ifdef CONFIG_IP_FIB_TRIE_STATS
1507 this_cpu_inc(stats->semantic_match_miss);
1508#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001509 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001510}
Florian Westphal6fc01432011-08-25 13:46:12 +02001511EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001512
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001513static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1514 struct key_vector *l, struct fib_alias *old)
Alexander Duyckd5d64872015-03-04 15:02:18 -08001515{
1516 /* record the location of the previous list_info entry */
1517 struct hlist_node **pprev = old->fa_list.pprev;
1518 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1519
1520 /* remove the fib_alias from the list */
1521 hlist_del_rcu(&old->fa_list);
1522
1523 /* if we emptied the list this leaf will be freed and we can sort
1524 * out parent suffix lengths as a part of trie_rebalance
1525 */
1526 if (hlist_empty(&l->leaf)) {
Alexander Duycka52ca622016-12-01 07:27:57 -05001527 if (tp->slen == l->slen)
1528 node_pull_suffix(tp, tp->pos);
Alexander Duyck88bae712015-03-06 09:54:52 -08001529 put_child_root(tp, l->key, NULL);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001530 node_free(l);
1531 trie_rebalance(t, tp);
1532 return;
1533 }
1534
1535 /* only access fa if it is pointing at the last valid hlist_node */
1536 if (*pprev)
1537 return;
1538
1539 /* update the trie with the latest suffix length */
1540 l->slen = fa->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001541 node_pull_suffix(tp, fa->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001542}
1543
1544/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001545int fib_table_delete(struct net *net, struct fib_table *tb,
David Ahern78055992017-05-27 16:19:26 -06001546 struct fib_config *cfg, struct netlink_ext_ack *extack)
Robert Olsson19baf832005-06-21 12:43:18 -07001547{
1548 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001549 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001550 struct key_vector *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001551 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001552 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001553 u8 tos = cfg->fc_tos;
1554 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001555
Thomas Graf4e902c52006-08-17 18:14:52 -07001556 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001557
David Ahern78055992017-05-27 16:19:26 -06001558 if (!fib_valid_key_len(key, plen, extack))
Robert Olsson19baf832005-06-21 12:43:18 -07001559 return -EINVAL;
1560
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001561 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001562 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001563 return -ESRCH;
1564
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001565 fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id);
Robert Olsson19baf832005-06-21 12:43:18 -07001566 if (!fa)
1567 return -ESRCH;
1568
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001569 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001570
1571 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001572 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001573 struct fib_info *fi = fa->fa_info;
1574
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001575 if ((fa->fa_slen != slen) ||
1576 (fa->tb_id != tb->tb_id) ||
1577 (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001578 break;
1579
Thomas Graf4e902c52006-08-17 18:14:52 -07001580 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1581 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001582 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001583 (!cfg->fc_prefsrc ||
1584 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001585 (!cfg->fc_protocol ||
1586 fi->fib_protocol == cfg->fc_protocol) &&
Xin Long5f9ae3d2017-08-23 10:07:26 +08001587 fib_nh_match(cfg, fi, extack) == 0 &&
1588 fib_metrics_match(cfg, fi)) {
Robert Olsson19baf832005-06-21 12:43:18 -07001589 fa_to_delete = fa;
1590 break;
1591 }
1592 }
1593
Olof Johansson91b9a272005-08-09 20:24:39 -07001594 if (!fa_to_delete)
1595 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001596
Jiri Pirkob90eb752016-09-26 12:52:29 +02001597 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, key, plen,
David Ahern6c31e5a2017-10-27 17:37:13 -07001598 fa_to_delete, extack);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001599 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001600 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001601
David S. Miller21d8c492011-04-14 14:49:37 -07001602 if (!plen)
1603 tb->tb_num_default--;
1604
Alexander Duyckd5d64872015-03-04 15:02:18 -08001605 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001606
Alexander Duyckd5d64872015-03-04 15:02:18 -08001607 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001608 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001609
Alexander Duyckd5d64872015-03-04 15:02:18 -08001610 fib_release_info(fa_to_delete->fa_info);
1611 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001612 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001613}
1614
Alexander Duyck8be33e92015-03-04 14:59:19 -08001615/* Scan for the next leaf starting at the provided key value */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001616static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001617{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001618 struct key_vector *pn, *n = *tn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001619 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001620
Alexander Duyck8be33e92015-03-04 14:59:19 -08001621 /* this loop is meant to try and find the key in the trie */
Alexander Duyck88bae712015-03-06 09:54:52 -08001622 do {
1623 /* record parent and next child index */
1624 pn = n;
Alexander Duyckc2229fe2015-10-27 15:06:45 -07001625 cindex = (key > pn->key) ? get_index(key, pn) : 0;
Alexander Duyck88bae712015-03-06 09:54:52 -08001626
1627 if (cindex >> pn->bits)
1628 break;
1629
1630 /* descend into the next child */
1631 n = get_child_rcu(pn, cindex++);
1632 if (!n)
1633 break;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001634
Alexander Duyck8be33e92015-03-04 14:59:19 -08001635 /* guarantee forward progress on the keys */
1636 if (IS_LEAF(n) && (n->key >= key))
1637 goto found;
Alexander Duyck88bae712015-03-06 09:54:52 -08001638 } while (IS_TNODE(n));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001639
1640 /* this loop will search for the next leaf with a greater key */
Alexander Duyck88bae712015-03-06 09:54:52 -08001641 while (!IS_TRIE(pn)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001642 /* if we exhausted the parent node we will need to climb */
1643 if (cindex >= (1ul << pn->bits)) {
1644 t_key pkey = pn->key;
1645
1646 pn = node_parent_rcu(pn);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001647 cindex = get_index(pkey, pn) + 1;
1648 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001649 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001650
Alexander Duyck8be33e92015-03-04 14:59:19 -08001651 /* grab the next available node */
Alexander Duyck754baf8d2015-03-06 09:54:14 -08001652 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001653 if (!n)
1654 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001655
Alexander Duyck8be33e92015-03-04 14:59:19 -08001656 /* no need to compare keys since we bumped the index */
1657 if (IS_LEAF(n))
1658 goto found;
1659
1660 /* Rescan start scanning in new node */
1661 pn = n;
1662 cindex = 0;
1663 }
1664
1665 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001666 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001667found:
1668 /* if we are at the limit for keys just return NULL for the tnode */
Alexander Duyck88bae712015-03-06 09:54:52 -08001669 *tn = pn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001670 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001671}
1672
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001673static void fib_trie_free(struct fib_table *tb)
1674{
1675 struct trie *t = (struct trie *)tb->tb_data;
1676 struct key_vector *pn = t->kv;
1677 unsigned long cindex = 1;
1678 struct hlist_node *tmp;
1679 struct fib_alias *fa;
1680
1681 /* walk trie in reverse order and free everything */
1682 for (;;) {
1683 struct key_vector *n;
1684
1685 if (!(cindex--)) {
1686 t_key pkey = pn->key;
1687
1688 if (IS_TRIE(pn))
1689 break;
1690
1691 n = pn;
1692 pn = node_parent(pn);
1693
1694 /* drop emptied tnode */
1695 put_child_root(pn, n->key, NULL);
1696 node_free(n);
1697
1698 cindex = get_index(pkey, pn);
1699
1700 continue;
1701 }
1702
1703 /* grab the next available node */
1704 n = get_child(pn, cindex);
1705 if (!n)
1706 continue;
1707
1708 if (IS_TNODE(n)) {
1709 /* record pn and cindex for leaf walking */
1710 pn = n;
1711 cindex = 1ul << n->bits;
1712
1713 continue;
1714 }
1715
1716 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1717 hlist_del_rcu(&fa->fa_list);
1718 alias_free_mem_rcu(fa);
1719 }
1720
1721 put_child_root(pn, n->key, NULL);
1722 node_free(n);
1723 }
1724
1725#ifdef CONFIG_IP_FIB_TRIE_STATS
1726 free_percpu(t->stats);
1727#endif
1728 kfree(tb);
1729}
1730
1731struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
1732{
1733 struct trie *ot = (struct trie *)oldtb->tb_data;
1734 struct key_vector *l, *tp = ot->kv;
1735 struct fib_table *local_tb;
1736 struct fib_alias *fa;
1737 struct trie *lt;
1738 t_key key = 0;
1739
1740 if (oldtb->tb_data == oldtb->__data)
1741 return oldtb;
1742
1743 local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
1744 if (!local_tb)
1745 return NULL;
1746
1747 lt = (struct trie *)local_tb->tb_data;
1748
1749 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1750 struct key_vector *local_l = NULL, *local_tp;
1751
1752 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1753 struct fib_alias *new_fa;
1754
1755 if (local_tb->tb_id != fa->tb_id)
1756 continue;
1757
1758 /* clone fa for new local table */
1759 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1760 if (!new_fa)
1761 goto out;
1762
1763 memcpy(new_fa, fa, sizeof(*fa));
1764
1765 /* insert clone into table */
1766 if (!local_l)
1767 local_l = fib_find_node(lt, &local_tp, l->key);
1768
1769 if (fib_insert_alias(lt, local_tp, local_l, new_fa,
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001770 NULL, l->key)) {
1771 kmem_cache_free(fn_alias_kmem, new_fa);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001772 goto out;
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001773 }
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001774 }
1775
1776 /* stop loop if key wrapped back to 0 */
1777 key = l->key + 1;
1778 if (key < l->key)
1779 break;
1780 }
1781
1782 return local_tb;
1783out:
1784 fib_trie_free(local_tb);
1785
1786 return NULL;
1787}
1788
Alexander Duyck3b709332016-11-15 05:46:06 -05001789/* Caller must hold RTNL */
1790void fib_table_flush_external(struct fib_table *tb)
1791{
1792 struct trie *t = (struct trie *)tb->tb_data;
1793 struct key_vector *pn = t->kv;
1794 unsigned long cindex = 1;
1795 struct hlist_node *tmp;
1796 struct fib_alias *fa;
1797
1798 /* walk trie in reverse order */
1799 for (;;) {
1800 unsigned char slen = 0;
1801 struct key_vector *n;
1802
1803 if (!(cindex--)) {
1804 t_key pkey = pn->key;
1805
1806 /* cannot resize the trie vector */
1807 if (IS_TRIE(pn))
1808 break;
1809
Alexander Duycka52ca622016-12-01 07:27:57 -05001810 /* update the suffix to address pulled leaves */
1811 if (pn->slen > pn->pos)
1812 update_suffix(pn);
1813
Alexander Duyck3b709332016-11-15 05:46:06 -05001814 /* resize completed node */
1815 pn = resize(t, pn);
1816 cindex = get_index(pkey, pn);
1817
1818 continue;
1819 }
1820
1821 /* grab the next available node */
1822 n = get_child(pn, cindex);
1823 if (!n)
1824 continue;
1825
1826 if (IS_TNODE(n)) {
1827 /* record pn and cindex for leaf walking */
1828 pn = n;
1829 cindex = 1ul << n->bits;
1830
1831 continue;
1832 }
1833
1834 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1835 /* if alias was cloned to local then we just
1836 * need to remove the local copy from main
1837 */
1838 if (tb->tb_id != fa->tb_id) {
1839 hlist_del_rcu(&fa->fa_list);
1840 alias_free_mem_rcu(fa);
1841 continue;
1842 }
1843
1844 /* record local slen */
1845 slen = fa->fa_slen;
1846 }
1847
1848 /* update leaf slen */
1849 n->slen = slen;
1850
1851 if (hlist_empty(&n->leaf)) {
1852 put_child_root(pn, n->key, NULL);
1853 node_free(n);
1854 }
1855 }
1856}
1857
Alexander Duyck8be33e92015-03-04 14:59:19 -08001858/* Caller must hold RTNL. */
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001859int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
Robert Olsson19baf832005-06-21 12:43:18 -07001860{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001861 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08001862 struct key_vector *pn = t->kv;
1863 unsigned long cindex = 1;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001864 struct hlist_node *tmp;
1865 struct fib_alias *fa;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001866 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001867
Alexander Duyck88bae712015-03-06 09:54:52 -08001868 /* walk trie in reverse order */
1869 for (;;) {
1870 unsigned char slen = 0;
1871 struct key_vector *n;
Robert Olsson19baf832005-06-21 12:43:18 -07001872
Alexander Duyck88bae712015-03-06 09:54:52 -08001873 if (!(cindex--)) {
1874 t_key pkey = pn->key;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001875
Alexander Duyck88bae712015-03-06 09:54:52 -08001876 /* cannot resize the trie vector */
1877 if (IS_TRIE(pn))
1878 break;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001879
Alexander Duycka52ca622016-12-01 07:27:57 -05001880 /* update the suffix to address pulled leaves */
1881 if (pn->slen > pn->pos)
1882 update_suffix(pn);
1883
Alexander Duyck88bae712015-03-06 09:54:52 -08001884 /* resize completed node */
1885 pn = resize(t, pn);
1886 cindex = get_index(pkey, pn);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001887
Alexander Duyck88bae712015-03-06 09:54:52 -08001888 continue;
1889 }
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001890
Alexander Duyck88bae712015-03-06 09:54:52 -08001891 /* grab the next available node */
1892 n = get_child(pn, cindex);
1893 if (!n)
1894 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001895
Alexander Duyck88bae712015-03-06 09:54:52 -08001896 if (IS_TNODE(n)) {
1897 /* record pn and cindex for leaf walking */
1898 pn = n;
1899 cindex = 1ul << n->bits;
1900
1901 continue;
1902 }
1903
1904 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1905 struct fib_info *fi = fa->fa_info;
1906
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001907 if (!fi || tb->tb_id != fa->tb_id ||
1908 (!(fi->fib_flags & RTNH_F_DEAD) &&
1909 !fib_props[fa->fa_type].error)) {
1910 slen = fa->fa_slen;
1911 continue;
1912 }
1913
1914 /* Do not flush error routes if network namespace is
1915 * not being dismantled
1916 */
1917 if (!flush_all && fib_props[fa->fa_type].error) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001918 slen = fa->fa_slen;
1919 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001920 }
1921
Jiri Pirkob90eb752016-09-26 12:52:29 +02001922 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
1923 n->key,
David Ahern6c31e5a2017-10-27 17:37:13 -07001924 KEYLENGTH - fa->fa_slen, fa,
1925 NULL);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001926 hlist_del_rcu(&fa->fa_list);
1927 fib_release_info(fa->fa_info);
1928 alias_free_mem_rcu(fa);
1929 found++;
Alexander Duyck64c62722015-01-22 15:51:45 -08001930 }
1931
Alexander Duyck88bae712015-03-06 09:54:52 -08001932 /* update leaf slen */
1933 n->slen = slen;
1934
1935 if (hlist_empty(&n->leaf)) {
1936 put_child_root(pn, n->key, NULL);
1937 node_free(n);
Alexander Duyck88bae712015-03-06 09:54:52 -08001938 }
Robert Olsson19baf832005-06-21 12:43:18 -07001939 }
1940
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001941 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001942 return found;
1943}
1944
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001945static void fib_leaf_notify(struct net *net, struct key_vector *l,
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001946 struct fib_table *tb, struct notifier_block *nb)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001947{
1948 struct fib_alias *fa;
1949
1950 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1951 struct fib_info *fi = fa->fa_info;
1952
1953 if (!fi)
1954 continue;
1955
1956 /* local and main table can share the same trie,
1957 * so don't notify twice for the same entry.
1958 */
1959 if (tb->tb_id != fa->tb_id)
1960 continue;
1961
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001962 call_fib_entry_notifier(nb, net, FIB_EVENT_ENTRY_ADD, l->key,
David Ahern6eba87c2017-10-18 11:39:13 -07001963 KEYLENGTH - fa->fa_slen, fa);
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001964 }
1965}
1966
1967static void fib_table_notify(struct net *net, struct fib_table *tb,
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001968 struct notifier_block *nb)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001969{
1970 struct trie *t = (struct trie *)tb->tb_data;
1971 struct key_vector *l, *tp = t->kv;
1972 t_key key = 0;
1973
1974 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001975 fib_leaf_notify(net, l, tb, nb);
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001976
1977 key = l->key + 1;
1978 /* stop in case of wrap around */
1979 if (key < l->key)
1980 break;
1981 }
1982}
1983
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001984void fib_notify(struct net *net, struct notifier_block *nb)
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001985{
1986 unsigned int h;
1987
1988 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1989 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
1990 struct fib_table *tb;
1991
1992 hlist_for_each_entry_rcu(tb, head, tb_hlist)
Ido Schimmeld05f7a72017-03-10 08:56:19 +01001993 fib_table_notify(net, tb, nb);
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001994 }
1995}
1996
Alexander Duycka7e53532015-03-04 15:02:44 -08001997static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001998{
Alexander Duycka7e53532015-03-04 15:02:44 -08001999 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08002000#ifdef CONFIG_IP_FIB_TRIE_STATS
2001 struct trie *t = (struct trie *)tb->tb_data;
2002
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002003 if (tb->tb_data == tb->__data)
2004 free_percpu(t->stats);
Alexander Duyck8274a972014-12-31 10:55:29 -08002005#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002006 kfree(tb);
2007}
2008
Alexander Duycka7e53532015-03-04 15:02:44 -08002009void fib_free_table(struct fib_table *tb)
2010{
2011 call_rcu(&tb->rcu, __trie_free_rcu);
2012}
2013
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002014static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
David Ahern18a80212018-10-15 18:56:43 -07002015 struct sk_buff *skb, struct netlink_callback *cb,
2016 struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002017{
David Ahern18a80212018-10-15 18:56:43 -07002018 unsigned int flags = NLM_F_MULTI;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002019 __be32 xkey = htonl(l->key);
Robert Olsson19baf832005-06-21 12:43:18 -07002020 struct fib_alias *fa;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002021 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07002022
David Ahern18a80212018-10-15 18:56:43 -07002023 if (filter->filter_set)
2024 flags |= NLM_F_DUMP_FILTERED;
2025
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002026 s_i = cb->args[4];
Robert Olsson19baf832005-06-21 12:43:18 -07002027 i = 0;
2028
Robert Olsson2373ce12005-08-25 13:01:29 -07002029 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002030 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
David Ahernf6c57752017-05-15 23:19:17 -07002031 int err;
2032
David Ahern18a80212018-10-15 18:56:43 -07002033 if (i < s_i)
2034 goto next;
Robert Olsson19baf832005-06-21 12:43:18 -07002035
David Ahern18a80212018-10-15 18:56:43 -07002036 if (tb->tb_id != fa->tb_id)
2037 goto next;
2038
2039 if (filter->filter_set) {
2040 if (filter->rt_type && fa->fa_type != filter->rt_type)
2041 goto next;
2042
2043 if ((filter->protocol &&
2044 fa->fa_info->fib_protocol != filter->protocol))
2045 goto next;
2046
2047 if (filter->dev &&
2048 !fib_info_nh_uses_dev(fa->fa_info, filter->dev))
2049 goto next;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002050 }
2051
David Ahernf6c57752017-05-15 23:19:17 -07002052 err = fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
2053 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2054 tb->tb_id, fa->fa_type,
2055 xkey, KEYLENGTH - fa->fa_slen,
David Ahern18a80212018-10-15 18:56:43 -07002056 fa->fa_tos, fa->fa_info, flags);
David Ahernf6c57752017-05-15 23:19:17 -07002057 if (err < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002058 cb->args[4] = i;
David Ahernf6c57752017-05-15 23:19:17 -07002059 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002060 }
David Ahern18a80212018-10-15 18:56:43 -07002061next:
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002062 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07002063 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002064
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002065 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07002066 return skb->len;
2067}
2068
Alexander Duycka7e53532015-03-04 15:02:44 -08002069/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00002070int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
David Ahern18a80212018-10-15 18:56:43 -07002071 struct netlink_callback *cb, struct fib_dump_filter *filter)
Robert Olsson19baf832005-06-21 12:43:18 -07002072{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002073 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002074 struct key_vector *l, *tp = t->kv;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002075 /* Dump starting at last key.
2076 * Note: 0.0.0.0/0 (ie default) is first key.
2077 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002078 int count = cb->args[2];
2079 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002080
Alexander Duyck8be33e92015-03-04 14:59:19 -08002081 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
David Ahernf6c57752017-05-15 23:19:17 -07002082 int err;
2083
David Ahern18a80212018-10-15 18:56:43 -07002084 err = fn_trie_dump_leaf(l, tb, skb, cb, filter);
David Ahernf6c57752017-05-15 23:19:17 -07002085 if (err < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002086 cb->args[3] = key;
2087 cb->args[2] = count;
David Ahernf6c57752017-05-15 23:19:17 -07002088 return err;
Robert Olsson19baf832005-06-21 12:43:18 -07002089 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002090
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002091 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002092 key = l->key + 1;
2093
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002094 memset(&cb->args[4], 0,
2095 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08002096
2097 /* stop loop if key wrapped back to 0 */
2098 if (key < l->key)
2099 break;
Robert Olsson19baf832005-06-21 12:43:18 -07002100 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08002101
Alexander Duyck8be33e92015-03-04 14:59:19 -08002102 cb->args[3] = key;
2103 cb->args[2] = count;
2104
Robert Olsson19baf832005-06-21 12:43:18 -07002105 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07002106}
2107
David S. Miller5348ba82011-02-01 15:30:56 -08002108void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002109{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002110 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
2111 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002112 0, SLAB_PANIC, NULL);
2113
2114 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002115 LEAF_SIZE,
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002116 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002117}
Robert Olsson19baf832005-06-21 12:43:18 -07002118
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002119struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
Robert Olsson19baf832005-06-21 12:43:18 -07002120{
2121 struct fib_table *tb;
2122 struct trie *t;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002123 size_t sz = sizeof(*tb);
Robert Olsson19baf832005-06-21 12:43:18 -07002124
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002125 if (!alias)
2126 sz += sizeof(struct trie);
2127
2128 tb = kzalloc(sz, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002129 if (!tb)
Robert Olsson19baf832005-06-21 12:43:18 -07002130 return NULL;
2131
2132 tb->tb_id = id;
David S. Miller21d8c492011-04-14 14:49:37 -07002133 tb->tb_num_default = 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002134 tb->tb_data = (alias ? alias->__data : tb->__data);
2135
2136 if (alias)
2137 return tb;
Robert Olsson19baf832005-06-21 12:43:18 -07002138
2139 t = (struct trie *) tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002140 t->kv[0].pos = KEYLENGTH;
2141 t->kv[0].slen = KEYLENGTH;
Alexander Duyck8274a972014-12-31 10:55:29 -08002142#ifdef CONFIG_IP_FIB_TRIE_STATS
2143 t->stats = alloc_percpu(struct trie_use_stats);
2144 if (!t->stats) {
2145 kfree(tb);
2146 tb = NULL;
2147 }
2148#endif
Robert Olsson19baf832005-06-21 12:43:18 -07002149
Robert Olsson19baf832005-06-21 12:43:18 -07002150 return tb;
2151}
2152
Robert Olsson19baf832005-06-21 12:43:18 -07002153#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002154/* Depth first Trie walk iterator */
2155struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002156 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002157 struct fib_table *tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002158 struct key_vector *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002159 unsigned int index;
2160 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002161};
Robert Olsson19baf832005-06-21 12:43:18 -07002162
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002163static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07002164{
Alexander Duyck98293e82014-12-31 10:56:18 -08002165 unsigned long cindex = iter->index;
Alexander Duyck88bae712015-03-06 09:54:52 -08002166 struct key_vector *pn = iter->tnode;
2167 t_key pkey;
Eric W. Biederman6640e692007-01-24 14:42:04 -08002168
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002169 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
2170 iter->tnode, iter->index, iter->depth);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002171
Alexander Duyck88bae712015-03-06 09:54:52 -08002172 while (!IS_TRIE(pn)) {
2173 while (cindex < child_length(pn)) {
2174 struct key_vector *n = get_child_rcu(pn, cindex++);
2175
2176 if (!n)
2177 continue;
2178
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002179 if (IS_LEAF(n)) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002180 iter->tnode = pn;
2181 iter->index = cindex;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002182 } else {
2183 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08002184 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002185 iter->index = 0;
2186 ++iter->depth;
2187 }
Alexander Duyck88bae712015-03-06 09:54:52 -08002188
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002189 return n;
2190 }
2191
Alexander Duyck88bae712015-03-06 09:54:52 -08002192 /* Current node exhausted, pop back up */
2193 pkey = pn->key;
2194 pn = node_parent_rcu(pn);
2195 cindex = get_index(pkey, pn) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002196 --iter->depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002197 }
2198
Alexander Duyck88bae712015-03-06 09:54:52 -08002199 /* record root node so further searches know we are done */
2200 iter->tnode = pn;
2201 iter->index = 0;
2202
Robert Olsson19baf832005-06-21 12:43:18 -07002203 return NULL;
2204}
2205
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002206static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
2207 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07002208{
Firo Yangf38b24c2015-06-08 11:54:51 +08002209 struct key_vector *n, *pn;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002210
Stephen Hemminger132adf52007-03-08 20:44:43 -08002211 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002212 return NULL;
2213
Firo Yangf38b24c2015-06-08 11:54:51 +08002214 pn = t->kv;
Alexander Duyck88bae712015-03-06 09:54:52 -08002215 n = rcu_dereference(pn->tnode[0]);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002216 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002217 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002218
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002219 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002220 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002221 iter->index = 0;
2222 iter->depth = 1;
2223 } else {
Alexander Duyck88bae712015-03-06 09:54:52 -08002224 iter->tnode = pn;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002225 iter->index = 0;
2226 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002227 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002228
2229 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002230}
2231
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002232static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07002233{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002234 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002235 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07002236
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002237 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07002238
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002239 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002240 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002241 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002242 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08002243
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002244 s->leaves++;
2245 s->totdepth += iter.depth;
2246 if (iter.depth > s->maxdepth)
2247 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08002248
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002249 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08002250 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002251 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002252 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002253 if (n->bits < MAX_STAT_DEPTH)
2254 s->nodesizes[n->bits]++;
Alexander Duyck6e22d172015-03-06 09:54:39 -08002255 s->nullpointers += tn_info(n)->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002256 }
2257 }
2258 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002259}
2260
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002261/*
Robert Olsson19baf832005-06-21 12:43:18 -07002262 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002263 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002264static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002265{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002266 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002267
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002268 if (stat->leaves)
2269 avdepth = stat->totdepth*100 / stat->leaves;
2270 else
2271 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002272
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002273 seq_printf(seq, "\tAver depth: %u.%02d\n",
2274 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002275 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002276
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002277 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002278 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002279
2280 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002281 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08002282
Stephen Hemminger187b5182008-01-12 20:55:55 -08002283 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002284 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002285
Robert Olsson06ef9212006-03-20 21:35:01 -08002286 max = MAX_STAT_DEPTH;
2287 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002288 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002289
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002290 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002291 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002292 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002293 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002294 pointers += (1<<i) * stat->nodesizes[i];
2295 }
2296 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002297 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002298
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002299 bytes += sizeof(struct key_vector *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002300 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2301 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002302}
Robert Olsson19baf832005-06-21 12:43:18 -07002303
2304#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002305static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002306 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002307{
Alexander Duyck8274a972014-12-31 10:55:29 -08002308 struct trie_use_stats s = { 0 };
2309 int cpu;
2310
2311 /* loop through all of the CPUs and gather up the stats */
2312 for_each_possible_cpu(cpu) {
2313 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2314
2315 s.gets += pcpu->gets;
2316 s.backtrack += pcpu->backtrack;
2317 s.semantic_match_passed += pcpu->semantic_match_passed;
2318 s.semantic_match_miss += pcpu->semantic_match_miss;
2319 s.null_node_hit += pcpu->null_node_hit;
2320 s.resize_node_skipped += pcpu->resize_node_skipped;
2321 }
2322
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002323 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002324 seq_printf(seq, "gets = %u\n", s.gets);
2325 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002326 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002327 s.semantic_match_passed);
2328 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2329 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2330 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002331}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002332#endif /* CONFIG_IP_FIB_TRIE_STATS */
2333
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002334static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002335{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002336 if (tb->tb_id == RT_TABLE_LOCAL)
2337 seq_puts(seq, "Local:\n");
2338 else if (tb->tb_id == RT_TABLE_MAIN)
2339 seq_puts(seq, "Main:\n");
2340 else
2341 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002342}
Robert Olsson19baf832005-06-21 12:43:18 -07002343
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002344
Robert Olsson19baf832005-06-21 12:43:18 -07002345static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2346{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002347 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002348 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002349
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002350 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002351 "Basic info: size of leaf:"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002352 " %zd bytes, size of tnode: %zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002353 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07002354
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002355 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2356 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002357 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002358
Sasha Levinb67bfe02013-02-27 17:06:00 -08002359 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002360 struct trie *t = (struct trie *) tb->tb_data;
2361 struct trie_stat stat;
2362
2363 if (!t)
2364 continue;
2365
2366 fib_table_print(seq, tb);
2367
2368 trie_collect_stats(t, &stat);
2369 trie_show_stats(seq, &stat);
2370#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002371 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002372#endif
2373 }
2374 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002375
Robert Olsson19baf832005-06-21 12:43:18 -07002376 return 0;
2377}
2378
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002379static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002380{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002381 struct fib_trie_iter *iter = seq->private;
2382 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002383 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002384 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002385
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002386 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2387 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002388 struct fib_table *tb;
2389
Sasha Levinb67bfe02013-02-27 17:06:00 -08002390 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002391 struct key_vector *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002392
2393 for (n = fib_trie_get_first(iter,
2394 (struct trie *) tb->tb_data);
2395 n; n = fib_trie_get_next(iter))
2396 if (pos == idx++) {
2397 iter->tb = tb;
2398 return n;
2399 }
2400 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002401 }
Robert Olsson19baf832005-06-21 12:43:18 -07002402
Robert Olsson19baf832005-06-21 12:43:18 -07002403 return NULL;
2404}
2405
2406static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002407 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002408{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002409 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002410 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002411}
2412
2413static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2414{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002415 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002416 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002417 struct fib_table *tb = iter->tb;
2418 struct hlist_node *tb_node;
2419 unsigned int h;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002420 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002421
Robert Olsson19baf832005-06-21 12:43:18 -07002422 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002423 /* next node in same table */
2424 n = fib_trie_get_next(iter);
2425 if (n)
2426 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002427
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002428 /* walk rest of this hash chain */
2429 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002430 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002431 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2432 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2433 if (n)
2434 goto found;
2435 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002436
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002437 /* new hash chain */
2438 while (++h < FIB_TABLE_HASHSZ) {
2439 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002440 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002441 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2442 if (n)
2443 goto found;
2444 }
2445 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002446 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002447
2448found:
2449 iter->tb = tb;
2450 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002451}
2452
2453static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002454 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002455{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002456 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002457}
2458
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002459static void seq_indent(struct seq_file *seq, int n)
2460{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002461 while (n-- > 0)
2462 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002463}
Robert Olsson19baf832005-06-21 12:43:18 -07002464
Eric Dumazet28d36e32008-01-14 23:09:56 -08002465static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002466{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002467 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002468 case RT_SCOPE_UNIVERSE: return "universe";
2469 case RT_SCOPE_SITE: return "site";
2470 case RT_SCOPE_LINK: return "link";
2471 case RT_SCOPE_HOST: return "host";
2472 case RT_SCOPE_NOWHERE: return "nowhere";
2473 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002474 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002475 return buf;
2476 }
2477}
2478
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002479static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002480 [RTN_UNSPEC] = "UNSPEC",
2481 [RTN_UNICAST] = "UNICAST",
2482 [RTN_LOCAL] = "LOCAL",
2483 [RTN_BROADCAST] = "BROADCAST",
2484 [RTN_ANYCAST] = "ANYCAST",
2485 [RTN_MULTICAST] = "MULTICAST",
2486 [RTN_BLACKHOLE] = "BLACKHOLE",
2487 [RTN_UNREACHABLE] = "UNREACHABLE",
2488 [RTN_PROHIBIT] = "PROHIBIT",
2489 [RTN_THROW] = "THROW",
2490 [RTN_NAT] = "NAT",
2491 [RTN_XRESOLVE] = "XRESOLVE",
2492};
2493
Eric Dumazeta034ee32010-09-09 23:32:28 +00002494static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002495{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002496 if (t < __RTN_MAX && rtn_type_names[t])
2497 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002498 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002499 return buf;
2500}
2501
2502/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002503static int fib_trie_seq_show(struct seq_file *seq, void *v)
2504{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002505 const struct fib_trie_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002506 struct key_vector *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002507
Alexander Duyck88bae712015-03-06 09:54:52 -08002508 if (IS_TRIE(node_parent_rcu(n)))
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002509 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002510
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002511 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002512 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002513
Alexander Duycke9b44012014-12-31 10:56:12 -08002514 seq_indent(seq, iter->depth-1);
2515 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2516 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
Alexander Duyck6e22d172015-03-06 09:54:39 -08002517 tn_info(n)->full_children,
2518 tn_info(n)->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002519 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002520 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002521 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002522
2523 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002524 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002525
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002526 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2527 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002528
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002529 seq_indent(seq, iter->depth + 1);
2530 seq_printf(seq, " /%zu %s %s",
2531 KEYLENGTH - fa->fa_slen,
2532 rtn_scope(buf1, sizeof(buf1),
2533 fa->fa_info->fib_scope),
2534 rtn_type(buf2, sizeof(buf2),
2535 fa->fa_type));
2536 if (fa->fa_tos)
2537 seq_printf(seq, " tos=%d", fa->fa_tos);
2538 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002539 }
Robert Olsson19baf832005-06-21 12:43:18 -07002540 }
2541
2542 return 0;
2543}
2544
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002545static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002546 .start = fib_trie_seq_start,
2547 .next = fib_trie_seq_next,
2548 .stop = fib_trie_seq_stop,
2549 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002550};
2551
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002552struct fib_route_iter {
2553 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002554 struct fib_table *main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002555 struct key_vector *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002556 loff_t pos;
2557 t_key key;
2558};
2559
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002560static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2561 loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002562{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002563 struct key_vector *l, **tp = &iter->tnode;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002564 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002565
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002566 /* use cached location of previously found key */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002567 if (iter->pos > 0 && pos >= iter->pos) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002568 key = iter->key;
2569 } else {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002570 iter->pos = 1;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002571 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002572 }
2573
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002574 pos -= iter->pos;
2575
2576 while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002577 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002578 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002579 l = NULL;
2580
2581 /* handle unlikely case of a key wrap */
2582 if (!key)
2583 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002584 }
2585
2586 if (l)
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002587 iter->key = l->key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002588 else
2589 iter->pos = 0; /* forget it */
2590
2591 return l;
2592}
2593
2594static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2595 __acquires(RCU)
2596{
2597 struct fib_route_iter *iter = seq->private;
2598 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002599 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002600
2601 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002602
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002603 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002604 if (!tb)
2605 return NULL;
2606
Alexander Duyck8be33e92015-03-04 14:59:19 -08002607 iter->main_tb = tb;
David Forster94d9f1c2016-08-03 15:13:01 +01002608 t = (struct trie *)tb->tb_data;
2609 iter->tnode = t->kv;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002610
2611 if (*pos != 0)
2612 return fib_route_get_idx(iter, *pos);
2613
Alexander Duyck8be33e92015-03-04 14:59:19 -08002614 iter->pos = 0;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002615 iter->key = KEY_MAX;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002616
2617 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002618}
2619
2620static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2621{
2622 struct fib_route_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002623 struct key_vector *l = NULL;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002624 t_key key = iter->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002625
2626 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002627
2628 /* only allow key of 0 for start of sequence */
2629 if ((v == SEQ_START_TOKEN) || key)
2630 l = leaf_walk_rcu(&iter->tnode, key);
2631
2632 if (l) {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002633 iter->key = l->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002634 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002635 } else {
2636 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002637 }
2638
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002639 return l;
2640}
2641
2642static void fib_route_seq_stop(struct seq_file *seq, void *v)
2643 __releases(RCU)
2644{
2645 rcu_read_unlock();
2646}
2647
Eric Dumazeta034ee32010-09-09 23:32:28 +00002648static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002649{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002650 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002651
Eric Dumazeta034ee32010-09-09 23:32:28 +00002652 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2653 flags = RTF_REJECT;
David Ahernb75ed8b2019-03-27 20:53:55 -07002654 if (fi && fi->fib_nh->fib_nh_gw4)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002655 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002656 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002657 flags |= RTF_HOST;
2658 flags |= RTF_UP;
2659 return flags;
2660}
2661
2662/*
2663 * This outputs /proc/net/route.
2664 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002665 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002666 * legacy utilities
2667 */
2668static int fib_route_seq_show(struct seq_file *seq, void *v)
2669{
Alexander Duyck654eff42015-03-11 16:36:08 -07002670 struct fib_route_iter *iter = seq->private;
2671 struct fib_table *tb = iter->main_tb;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002672 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002673 struct key_vector *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002674 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002675
2676 if (v == SEQ_START_TOKEN) {
2677 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2678 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2679 "\tWindow\tIRTT");
2680 return 0;
2681 }
2682
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002683 prefix = htonl(l->key);
2684
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002685 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2686 const struct fib_info *fi = fa->fa_info;
2687 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2688 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002689
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002690 if ((fa->fa_type == RTN_BROADCAST) ||
2691 (fa->fa_type == RTN_MULTICAST))
2692 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002693
Alexander Duyck654eff42015-03-11 16:36:08 -07002694 if (fa->tb_id != tb->tb_id)
2695 continue;
2696
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002697 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002698
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002699 if (fi)
2700 seq_printf(seq,
2701 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2702 "%d\t%08X\t%d\t%u\t%u",
2703 fi->fib_dev ? fi->fib_dev->name : "*",
2704 prefix,
David Ahernb75ed8b2019-03-27 20:53:55 -07002705 fi->fib_nh->fib_nh_gw4, flags, 0, 0,
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002706 fi->fib_priority,
2707 mask,
2708 (fi->fib_advmss ?
2709 fi->fib_advmss + 40 : 0),
2710 fi->fib_window,
2711 fi->fib_rtt >> 3);
2712 else
2713 seq_printf(seq,
2714 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2715 "%d\t%08X\t%d\t%u\t%u",
2716 prefix, 0, flags, 0, 0, 0,
2717 mask, 0, 0, 0);
Tetsuo Handa652586d2013-11-14 14:31:57 -08002718
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002719 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002720 }
2721
2722 return 0;
2723}
2724
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002725static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002726 .start = fib_route_seq_start,
2727 .next = fib_route_seq_next,
2728 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002729 .show = fib_route_seq_show,
2730};
2731
Denis V. Lunev61a02652008-01-10 03:21:09 -08002732int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002733{
Christoph Hellwigc3506372018-04-10 19:42:55 +02002734 if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
2735 sizeof(struct fib_trie_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002736 goto out1;
2737
Christoph Hellwig3617d942018-04-13 20:38:35 +02002738 if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
2739 fib_triestat_seq_show, NULL))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002740 goto out2;
2741
Christoph Hellwigc3506372018-04-10 19:42:55 +02002742 if (!proc_create_net("route", 0444, net->proc_net, &fib_route_seq_ops,
2743 sizeof(struct fib_route_iter)))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002744 goto out3;
2745
Robert Olsson19baf832005-06-21 12:43:18 -07002746 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002747
2748out3:
Gao fengece31ff2013-02-18 01:34:56 +00002749 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002750out2:
Gao fengece31ff2013-02-18 01:34:56 +00002751 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002752out1:
2753 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002754}
2755
Denis V. Lunev61a02652008-01-10 03:21:09 -08002756void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002757{
Gao fengece31ff2013-02-18 01:34:56 +00002758 remove_proc_entry("fib_trie", net->proc_net);
2759 remove_proc_entry("fib_triestat", net->proc_net);
2760 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002761}
2762
2763#endif /* CONFIG_PROC_FS */