fib_trie.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
  5. * & Swedish University of Agricultural Sciences.
  6. *
  7. * Jens Laas <jens.laas@data.slu.se> Swedish University of
  8. * Agricultural Sciences.
  9. *
  10. * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
  11. *
  12. * This work is based on the LPC-trie which is originally described in:
  13. *
  14. * An experimental study of compression methods for dynamic tries
  15. * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
  16. * https://www.csc.kth.se/~snilsson/software/dyntrie2/
  17. *
  18. * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
  19. * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
  20. *
  21. * Code from fib_hash has been reused which includes the following header:
  22. *
  23. * INET An implementation of the TCP/IP protocol suite for the LINUX
  24. * operating system. INET is implemented using the BSD Socket
  25. * interface as the means of communication with the user level.
  26. *
  27. * IPv4 FIB: lookup engine and maintenance routines.
  28. *
  29. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  30. *
  31. * Substantial contributions to this work comes from:
  32. *
  33. * David S. Miller, <davem@davemloft.net>
  34. * Stephen Hemminger <shemminger@osdl.org>
  35. * Paul E. McKenney <paulmck@us.ibm.com>
  36. * Patrick McHardy <kaber@trash.net>
  37. */
  38. #include <linux/cache.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/bitops.h>
  41. #include <linux/types.h>
  42. #include <linux/kernel.h>
  43. #include <linux/mm.h>
  44. #include <linux/string.h>
  45. #include <linux/socket.h>
  46. #include <linux/sockios.h>
  47. #include <linux/errno.h>
  48. #include <linux/in.h>
  49. #include <linux/inet.h>
  50. #include <linux/inetdevice.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/rcupdate.h>
  55. #include <linux/skbuff.h>
  56. #include <linux/netlink.h>
  57. #include <linux/init.h>
  58. #include <linux/list.h>
  59. #include <linux/slab.h>
  60. #include <linux/export.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/notifier.h>
  63. #include <net/net_namespace.h>
  64. #include <net/ip.h>
  65. #include <net/protocol.h>
  66. #include <net/route.h>
  67. #include <net/tcp.h>
  68. #include <net/sock.h>
  69. #include <net/ip_fib.h>
  70. #include <net/fib_notifier.h>
  71. #include <trace/events/fib.h>
  72. #include "fib_lookup.h"
  73. static int call_fib_entry_notifier(struct notifier_block *nb,
  74. enum fib_event_type event_type, u32 dst,
  75. int dst_len, struct fib_alias *fa,
  76. struct netlink_ext_ack *extack)
  77. {
  78. struct fib_entry_notifier_info info = {
  79. .info.extack = extack,
  80. .dst = dst,
  81. .dst_len = dst_len,
  82. .fi = fa->fa_info,
  83. .tos = fa->fa_tos,
  84. .type = fa->fa_type,
  85. .tb_id = fa->tb_id,
  86. };
  87. return call_fib4_notifier(nb, event_type, &info.info);
  88. }
  89. static int call_fib_entry_notifiers(struct net *net,
  90. enum fib_event_type event_type, u32 dst,
  91. int dst_len, struct fib_alias *fa,
  92. struct netlink_ext_ack *extack)
  93. {
  94. struct fib_entry_notifier_info info = {
  95. .info.extack = extack,
  96. .dst = dst,
  97. .dst_len = dst_len,
  98. .fi = fa->fa_info,
  99. .tos = fa->fa_tos,
  100. .type = fa->fa_type,
  101. .tb_id = fa->tb_id,
  102. };
  103. return call_fib4_notifiers(net, event_type, &info.info);
  104. }
  105. #define MAX_STAT_DEPTH 32
  106. #define KEYLENGTH (8*sizeof(t_key))
  107. #define KEY_MAX ((t_key)~0)
  108. typedef unsigned int t_key;
  109. #define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
  110. #define IS_TNODE(n) ((n)->bits)
  111. #define IS_LEAF(n) (!(n)->bits)
  112. struct key_vector {
  113. t_key key;
  114. unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  115. unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  116. unsigned char slen;
  117. union {
  118. /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
  119. struct hlist_head leaf;
  120. /* This array is valid if (pos | bits) > 0 (TNODE) */
  121. struct key_vector __rcu *tnode[0];
  122. };
  123. };
  124. struct tnode {
  125. struct rcu_head rcu;
  126. t_key empty_children; /* KEYLENGTH bits needed */
  127. t_key full_children; /* KEYLENGTH bits needed */
  128. struct key_vector __rcu *parent;
  129. struct key_vector kv[1];
  130. #define tn_bits kv[0].bits
  131. };
  132. #define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
  133. #define LEAF_SIZE TNODE_SIZE(1)
  134. #ifdef CONFIG_IP_FIB_TRIE_STATS
  135. struct trie_use_stats {
  136. unsigned int gets;
  137. unsigned int backtrack;
  138. unsigned int semantic_match_passed;
  139. unsigned int semantic_match_miss;
  140. unsigned int null_node_hit;
  141. unsigned int resize_node_skipped;
  142. };
  143. #endif
  144. struct trie_stat {
  145. unsigned int totdepth;
  146. unsigned int maxdepth;
  147. unsigned int tnodes;
  148. unsigned int leaves;
  149. unsigned int nullpointers;
  150. unsigned int prefixes;
  151. unsigned int nodesizes[MAX_STAT_DEPTH];
  152. };
  153. struct trie {
  154. struct key_vector kv[1];
  155. #ifdef CONFIG_IP_FIB_TRIE_STATS
  156. struct trie_use_stats __percpu *stats;
  157. #endif
  158. };
  159. static struct key_vector *resize(struct trie *t, struct key_vector *tn);
  160. static unsigned int tnode_free_size;
  161. /*
  162. * synchronize_rcu after call_rcu for outstanding dirty memory; it should be
  163. * especially useful before resizing the root node with PREEMPT_NONE configs;
  164. * the value was obtained experimentally, aiming to avoid visible slowdown.
  165. */
  166. unsigned int sysctl_fib_sync_mem = 512 * 1024;
  167. unsigned int sysctl_fib_sync_mem_min = 64 * 1024;
  168. unsigned int sysctl_fib_sync_mem_max = 64 * 1024 * 1024;
  169. static struct kmem_cache *fn_alias_kmem __ro_after_init;
  170. static struct kmem_cache *trie_leaf_kmem __ro_after_init;
  171. static inline struct tnode *tn_info(struct key_vector *kv)
  172. {
  173. return container_of(kv, struct tnode, kv[0]);
  174. }
  175. /* caller must hold RTNL */
  176. #define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
  177. #define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
  178. /* caller must hold RCU read lock or RTNL */
  179. #define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
  180. #define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
  181. /* wrapper for rcu_assign_pointer */
  182. static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
  183. {
  184. if (n)
  185. rcu_assign_pointer(tn_info(n)->parent, tp);
  186. }
  187. #define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
  188. /* This provides us with the number of children in this node, in the case of a
  189. * leaf this will return 0 meaning none of the children are accessible.
  190. */
  191. static inline unsigned long child_length(const struct key_vector *tn)
  192. {
  193. return (1ul << tn->bits) & ~(1ul);
  194. }
  195. #define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
  196. static inline unsigned long get_index(t_key key, struct key_vector *kv)
  197. {
  198. unsigned long index = key ^ kv->key;
  199. if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
  200. return 0;
  201. return index >> kv->pos;
  202. }
  203. /* To understand this stuff, an understanding of keys and all their bits is
  204. * necessary. Every node in the trie has a key associated with it, but not
  205. * all of the bits in that key are significant.
  206. *
  207. * Consider a node 'n' and its parent 'tp'.
  208. *
  209. * If n is a leaf, every bit in its key is significant. Its presence is
  210. * necessitated by path compression, since during a tree traversal (when
  211. * searching for a leaf - unless we are doing an insertion) we will completely
  212. * ignore all skipped bits we encounter. Thus we need to verify, at the end of
  213. * a potentially successful search, that we have indeed been walking the
  214. * correct key path.
  215. *
  216. * Note that we can never "miss" the correct key in the tree if present by
  217. * following the wrong path. Path compression ensures that segments of the key
  218. * that are the same for all keys with a given prefix are skipped, but the
  219. * skipped part *is* identical for each node in the subtrie below the skipped
  220. * bit! trie_insert() in this implementation takes care of that.
  221. *
  222. * if n is an internal node - a 'tnode' here, the various parts of its key
  223. * have many different meanings.
  224. *
  225. * Example:
  226. * _________________________________________________________________
  227. * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
  228. * -----------------------------------------------------------------
  229. * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
  230. *
  231. * _________________________________________________________________
  232. * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
  233. * -----------------------------------------------------------------
  234. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  235. *
  236. * tp->pos = 22
  237. * tp->bits = 3
  238. * n->pos = 13
  239. * n->bits = 4
  240. *
  241. * First, let's just ignore the bits that come before the parent tp, that is
  242. * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
  243. * point we do not use them for anything.
  244. *
  245. * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
  246. * index into the parent's child array. That is, they will be used to find
  247. * 'n' among tp's children.
  248. *
  249. * The bits from (n->pos + n->bits) to (tp->pos - 1) - "S" - are skipped bits
  250. * for the node n.
  251. *
  252. * All the bits we have seen so far are significant to the node n. The rest
  253. * of the bits are really not needed or indeed known in n->key.
  254. *
  255. * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
  256. * n's child array, and will of course be different for each child.
  257. *
  258. * The rest of the bits, from 0 to (n->pos -1) - "u" - are completely unknown
  259. * at this point.
  260. */
  261. static const int halve_threshold = 25;
  262. static const int inflate_threshold = 50;
  263. static const int halve_threshold_root = 15;
  264. static const int inflate_threshold_root = 30;
  265. static void __alias_free_mem(struct rcu_head *head)
  266. {
  267. struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
  268. kmem_cache_free(fn_alias_kmem, fa);
  269. }
  270. static inline void alias_free_mem_rcu(struct fib_alias *fa)
  271. {
  272. call_rcu(&fa->rcu, __alias_free_mem);
  273. }
  274. #define TNODE_VMALLOC_MAX \
  275. ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
  276. static void __node_free_rcu(struct rcu_head *head)
  277. {
  278. struct tnode *n = container_of(head, struct tnode, rcu);
  279. if (!n->tn_bits)
  280. kmem_cache_free(trie_leaf_kmem, n);
  281. else
  282. kvfree(n);
  283. }
  284. #define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
  285. static struct tnode *tnode_alloc(int bits)
  286. {
  287. size_t size;
  288. /* verify bits is within bounds */
  289. if (bits > TNODE_VMALLOC_MAX)
  290. return NULL;
  291. /* determine size and verify it is non-zero and didn't overflow */
  292. size = TNODE_SIZE(1ul << bits);
  293. if (size <= PAGE_SIZE)
  294. return kzalloc(size, GFP_KERNEL);
  295. else
  296. return vzalloc(size);
  297. }
  298. static inline void empty_child_inc(struct key_vector *n)
  299. {
  300. tn_info(n)->empty_children++;
  301. if (!tn_info(n)->empty_children)
  302. tn_info(n)->full_children++;
  303. }
  304. static inline void empty_child_dec(struct key_vector *n)
  305. {
  306. if (!tn_info(n)->empty_children)
  307. tn_info(n)->full_children--;
  308. tn_info(n)->empty_children--;
  309. }
  310. static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
  311. {
  312. struct key_vector *l;
  313. struct tnode *kv;
  314. kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
  315. if (!kv)
  316. return NULL;
  317. /* initialize key vector */
  318. l = kv->kv;
  319. l->key = key;
  320. l->pos = 0;
  321. l->bits = 0;
  322. l->slen = fa->fa_slen;
  323. /* link leaf to fib alias */
  324. INIT_HLIST_HEAD(&l->leaf);
  325. hlist_add_head(&fa->fa_list, &l->leaf);
  326. return l;
  327. }
  328. static struct key_vector *tnode_new(t_key key, int pos, int bits)
  329. {
  330. unsigned int shift = pos + bits;
  331. struct key_vector *tn;
  332. struct tnode *tnode;
  333. /* verify bits and pos their msb bits clear and values are valid */
  334. BUG_ON(!bits || (shift > KEYLENGTH));
  335. tnode = tnode_alloc(bits);
  336. if (!tnode)
  337. return NULL;
  338. pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
  339. sizeof(struct key_vector *) << bits);
  340. if (bits == KEYLENGTH)
  341. tnode->full_children = 1;
  342. else
  343. tnode->empty_children = 1ul << bits;
  344. tn = tnode->kv;
  345. tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
  346. tn->pos = pos;
  347. tn->bits = bits;
  348. tn->slen = pos;
  349. return tn;
  350. }
  351. /* Check whether a tnode 'n' is "full", i.e. it is an internal node
  352. * and no bits are skipped. See discussion in dyntree paper p. 6
  353. */
  354. static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
  355. {
  356. return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
  357. }
  358. /* Add a child at position i overwriting the old value.
  359. * Update the value of full_children and empty_children.
  360. */
  361. static void put_child(struct key_vector *tn, unsigned long i,
  362. struct key_vector *n)
  363. {
  364. struct key_vector *chi = get_child(tn, i);
  365. int isfull, wasfull;
  366. BUG_ON(i >= child_length(tn));
  367. /* update emptyChildren, overflow into fullChildren */
  368. if (!n && chi)
  369. empty_child_inc(tn);
  370. if (n && !chi)
  371. empty_child_dec(tn);
  372. /* update fullChildren */
  373. wasfull = tnode_full(tn, chi);
  374. isfull = tnode_full(tn, n);
  375. if (wasfull && !isfull)
  376. tn_info(tn)->full_children--;
  377. else if (!wasfull && isfull)
  378. tn_info(tn)->full_children++;
  379. if (n && (tn->slen < n->slen))
  380. tn->slen = n->slen;
  381. rcu_assign_pointer(tn->tnode[i], n);
  382. }
  383. static void update_children(struct key_vector *tn)
  384. {
  385. unsigned long i;
  386. /* update all of the child parent pointers */
  387. for (i = child_length(tn); i;) {
  388. struct key_vector *inode = get_child(tn, --i);
  389. if (!inode)
  390. continue;
  391. /* Either update the children of a tnode that
  392. * already belongs to us or update the child
  393. * to point to ourselves.
  394. */
  395. if (node_parent(inode) == tn)
  396. update_children(inode);
  397. else
  398. node_set_parent(inode, tn);
  399. }
  400. }
  401. static inline void put_child_root(struct key_vector *tp, t_key key,
  402. struct key_vector *n)
  403. {
  404. if (IS_TRIE(tp))
  405. rcu_assign_pointer(tp->tnode[0], n);
  406. else
  407. put_child(tp, get_index(key, tp), n);
  408. }
  409. static inline void tnode_free_init(struct key_vector *tn)
  410. {
  411. tn_info(tn)->rcu.next = NULL;
  412. }
  413. static inline void tnode_free_append(struct key_vector *tn,
  414. struct key_vector *n)
  415. {
  416. tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
  417. tn_info(tn)->rcu.next = &tn_info(n)->rcu;
  418. }
  419. static void tnode_free(struct key_vector *tn)
  420. {
  421. struct callback_head *head = &tn_info(tn)->rcu;
  422. while (head) {
  423. head = head->next;
  424. tnode_free_size += TNODE_SIZE(1ul << tn->bits);
  425. node_free(tn);
  426. tn = container_of(head, struct tnode, rcu)->kv;
  427. }
  428. if (tnode_free_size >= sysctl_fib_sync_mem) {
  429. tnode_free_size = 0;
  430. synchronize_rcu();
  431. }
  432. }
  433. static struct key_vector *replace(struct trie *t,
  434. struct key_vector *oldtnode,
  435. struct key_vector *tn)
  436. {
  437. struct key_vector *tp = node_parent(oldtnode);
  438. unsigned long i;
  439. /* setup the parent pointer out of and back into this node */
  440. NODE_INIT_PARENT(tn, tp);
  441. put_child_root(tp, tn->key, tn);
  442. /* update all of the child parent pointers */
  443. update_children(tn);
  444. /* all pointers should be clean so we are done */
  445. tnode_free(oldtnode);
  446. /* resize children now that oldtnode is freed */
  447. for (i = child_length(tn); i;) {
  448. struct key_vector *inode = get_child(tn, --i);
  449. /* resize child node */
  450. if (tnode_full(tn, inode))
  451. tn = resize(t, inode);
  452. }
  453. return tp;
  454. }
  455. static struct key_vector *inflate(struct trie *t,
  456. struct key_vector *oldtnode)
  457. {
  458. struct key_vector *tn;
  459. unsigned long i;
  460. t_key m;
  461. pr_debug("In inflate\n");
  462. tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
  463. if (!tn)
  464. goto notnode;
  465. /* prepare oldtnode to be freed */
  466. tnode_free_init(oldtnode);
  467. /* Assemble all of the pointers in our cluster, in this case that
  468. * represents all of the pointers out of our allocated nodes that
  469. * point to existing tnodes and the links between our allocated
  470. * nodes.
  471. */
  472. for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
  473. struct key_vector *inode = get_child(oldtnode, --i);
  474. struct key_vector *node0, *node1;
  475. unsigned long j, k;
  476. /* An empty child */
  477. if (!inode)
  478. continue;
  479. /* A leaf or an internal node with skipped bits */
  480. if (!tnode_full(oldtnode, inode)) {
  481. put_child(tn, get_index(inode->key, tn), inode);
  482. continue;
  483. }
  484. /* drop the node in the old tnode free list */
  485. tnode_free_append(oldtnode, inode);
  486. /* An internal node with two children */
  487. if (inode->bits == 1) {
  488. put_child(tn, 2 * i + 1, get_child(inode, 1));
  489. put_child(tn, 2 * i, get_child(inode, 0));
  490. continue;
  491. }
  492. /* We will replace this node 'inode' with two new
  493. * ones, 'node0' and 'node1', each with half of the
  494. * original children. The two new nodes will have
  495. * a position one bit further down the key and this
  496. * means that the "significant" part of their keys
  497. * (see the discussion near the top of this file)
  498. * will differ by one bit, which will be "0" in
  499. * node0's key and "1" in node1's key. Since we are
  500. * moving the key position by one step, the bit that
  501. * we are moving away from - the bit at position
  502. * (tn->pos) - is the one that will differ between
  503. * node0 and node1. So... we synthesize that bit in the
  504. * two new keys.
  505. */
  506. node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
  507. if (!node1)
  508. goto nomem;
  509. node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
  510. tnode_free_append(tn, node1);
  511. if (!node0)
  512. goto nomem;
  513. tnode_free_append(tn, node0);
  514. /* populate child pointers in new nodes */
  515. for (k = child_length(inode), j = k / 2; j;) {
  516. put_child(node1, --j, get_child(inode, --k));
  517. put_child(node0, j, get_child(inode, j));
  518. put_child(node1, --j, get_child(inode, --k));
  519. put_child(node0, j, get_child(inode, j));
  520. }
  521. /* link new nodes to parent */
  522. NODE_INIT_PARENT(node1, tn);
  523. NODE_INIT_PARENT(node0, tn);
  524. /* link parent to nodes */
  525. put_child(tn, 2 * i + 1, node1);
  526. put_child(tn, 2 * i, node0);
  527. }
  528. /* setup the parent pointers into and out of this node */
  529. return replace(t, oldtnode, tn);
  530. nomem:
  531. /* all pointers should be clean so we are done */
  532. tnode_free(tn);
  533. notnode:
  534. return NULL;
  535. }
  536. static struct key_vector *halve(struct trie *t,
  537. struct key_vector *oldtnode)
  538. {
  539. struct key_vector *tn;
  540. unsigned long i;
  541. pr_debug("In halve\n");
  542. tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
  543. if (!tn)
  544. goto notnode;
  545. /* prepare oldtnode to be freed */
  546. tnode_free_init(oldtnode);
  547. /* Assemble all of the pointers in our cluster, in this case that
  548. * represents all of the pointers out of our allocated nodes that
  549. * point to existing tnodes and the links between our allocated
  550. * nodes.
  551. */
  552. for (i = child_length(oldtnode); i;) {
  553. struct key_vector *node1 = get_child(oldtnode, --i);
  554. struct key_vector *node0 = get_child(oldtnode, --i);
  555. struct key_vector *inode;
  556. /* At least one of the children is empty */
  557. if (!node1 || !node0) {
  558. put_child(tn, i / 2, node1 ? : node0);
  559. continue;
  560. }
  561. /* Two nonempty children */
  562. inode = tnode_new(node0->key, oldtnode->pos, 1);
  563. if (!inode)
  564. goto nomem;
  565. tnode_free_append(tn, inode);
  566. /* initialize pointers out of node */
  567. put_child(inode, 1, node1);
  568. put_child(inode, 0, node0);
  569. NODE_INIT_PARENT(inode, tn);
  570. /* link parent to node */
  571. put_child(tn, i / 2, inode);
  572. }
  573. /* setup the parent pointers into and out of this node */
  574. return replace(t, oldtnode, tn);
  575. nomem:
  576. /* all pointers should be clean so we are done */
  577. tnode_free(tn);
  578. notnode:
  579. return NULL;
  580. }
  581. static struct key_vector *collapse(struct trie *t,
  582. struct key_vector *oldtnode)
  583. {
  584. struct key_vector *n, *tp;
  585. unsigned long i;
  586. /* scan the tnode looking for that one child that might still exist */
  587. for (n = NULL, i = child_length(oldtnode); !n && i;)
  588. n = get_child(oldtnode, --i);
  589. /* compress one level */
  590. tp = node_parent(oldtnode);
  591. put_child_root(tp, oldtnode->key, n);
  592. node_set_parent(n, tp);
  593. /* drop dead node */
  594. node_free(oldtnode);
  595. return tp;
  596. }
  597. static unsigned char update_suffix(struct key_vector *tn)
  598. {
  599. unsigned char slen = tn->pos;
  600. unsigned long stride, i;
  601. unsigned char slen_max;
  602. /* only vector 0 can have a suffix length greater than or equal to
  603. * tn->pos + tn->bits, the second highest node will have a suffix
  604. * length at most of tn->pos + tn->bits - 1
  605. */
  606. slen_max = min_t(unsigned char, tn->pos + tn->bits - 1, tn->slen);
  607. /* search though the list of children looking for nodes that might
  608. * have a suffix greater than the one we currently have. This is
  609. * why we start with a stride of 2 since a stride of 1 would
  610. * represent the nodes with suffix length equal to tn->pos
  611. */
  612. for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
  613. struct key_vector *n = get_child(tn, i);
  614. if (!n || (n->slen <= slen))
  615. continue;
  616. /* update stride and slen based on new value */
  617. stride <<= (n->slen - slen);
  618. slen = n->slen;
  619. i &= ~(stride - 1);
  620. /* stop searching if we have hit the maximum possible value */
  621. if (slen >= slen_max)
  622. break;
  623. }
  624. tn->slen = slen;
  625. return slen;
  626. }
  627. /* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  628. * the Helsinki University of Technology and Matti Tikkanen of Nokia
  629. * Telecommunications, page 6:
  630. * "A node is doubled if the ratio of non-empty children to all
  631. * children in the *doubled* node is at least 'high'."
  632. *
  633. * 'high' in this instance is the variable 'inflate_threshold'. It
  634. * is expressed as a percentage, so we multiply it with
  635. * child_length() and instead of multiplying by 2 (since the
  636. * child array will be doubled by inflate()) and multiplying
  637. * the left-hand side by 100 (to handle the percentage thing) we
  638. * multiply the left-hand side by 50.
  639. *
  640. * The left-hand side may look a bit weird: child_length(tn)
  641. * - tn->empty_children is of course the number of non-null children
  642. * in the current node. tn->full_children is the number of "full"
  643. * children, that is non-null tnodes with a skip value of 0.
  644. * All of those will be doubled in the resulting inflated tnode, so
  645. * we just count them one extra time here.
  646. *
  647. * A clearer way to write this would be:
  648. *
  649. * to_be_doubled = tn->full_children;
  650. * not_to_be_doubled = child_length(tn) - tn->empty_children -
  651. * tn->full_children;
  652. *
  653. * new_child_length = child_length(tn) * 2;
  654. *
  655. * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
  656. * new_child_length;
  657. * if (new_fill_factor >= inflate_threshold)
  658. *
  659. * ...and so on, tho it would mess up the while () loop.
  660. *
  661. * anyway,
  662. * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
  663. * inflate_threshold
  664. *
  665. * avoid a division:
  666. * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
  667. * inflate_threshold * new_child_length
  668. *
  669. * expand not_to_be_doubled and to_be_doubled, and shorten:
  670. * 100 * (child_length(tn) - tn->empty_children +
  671. * tn->full_children) >= inflate_threshold * new_child_length
  672. *
  673. * expand new_child_length:
  674. * 100 * (child_length(tn) - tn->empty_children +
  675. * tn->full_children) >=
  676. * inflate_threshold * child_length(tn) * 2
  677. *
  678. * shorten again:
  679. * 50 * (tn->full_children + child_length(tn) -
  680. * tn->empty_children) >= inflate_threshold *
  681. * child_length(tn)
  682. *
  683. */
  684. static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
  685. {
  686. unsigned long used = child_length(tn);
  687. unsigned long threshold = used;
  688. /* Keep root node larger */
  689. threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
  690. used -= tn_info(tn)->empty_children;
  691. used += tn_info(tn)->full_children;
  692. /* if bits == KEYLENGTH then pos = 0, and will fail below */
  693. return (used > 1) && tn->pos && ((50 * used) >= threshold);
  694. }
  695. static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
  696. {
  697. unsigned long used = child_length(tn);
  698. unsigned long threshold = used;
  699. /* Keep root node larger */
  700. threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
  701. used -= tn_info(tn)->empty_children;
  702. /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
  703. return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
  704. }
  705. static inline bool should_collapse(struct key_vector *tn)
  706. {
  707. unsigned long used = child_length(tn);
  708. used -= tn_info(tn)->empty_children;
  709. /* account for bits == KEYLENGTH case */
  710. if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
  711. used -= KEY_MAX;
  712. /* One child or none, time to drop us from the trie */
  713. return used < 2;
  714. }
  715. #define MAX_WORK 10
  716. static struct key_vector *resize(struct trie *t, struct key_vector *tn)
  717. {
  718. #ifdef CONFIG_IP_FIB_TRIE_STATS
  719. struct trie_use_stats __percpu *stats = t->stats;
  720. #endif
  721. struct key_vector *tp = node_parent(tn);
  722. unsigned long cindex = get_index(tn->key, tp);
  723. int max_work = MAX_WORK;
  724. pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  725. tn, inflate_threshold, halve_threshold);
  726. /* track the tnode via the pointer from the parent instead of
  727. * doing it ourselves. This way we can let RCU fully do its
  728. * thing without us interfering
  729. */
  730. BUG_ON(tn != get_child(tp, cindex));
  731. /* Double as long as the resulting node has a number of
  732. * nonempty nodes that are above the threshold.
  733. */
  734. while (should_inflate(tp, tn) && max_work) {
  735. tp = inflate(t, tn);
  736. if (!tp) {
  737. #ifdef CONFIG_IP_FIB_TRIE_STATS
  738. this_cpu_inc(stats->resize_node_skipped);
  739. #endif
  740. break;
  741. }
  742. max_work--;
  743. tn = get_child(tp, cindex);
  744. }
  745. /* update parent in case inflate failed */
  746. tp = node_parent(tn);
  747. /* Return if at least one inflate is run */
  748. if (max_work != MAX_WORK)
  749. return tp;
  750. /* Halve as long as the number of empty children in this
  751. * node is above threshold.
  752. */
  753. while (should_halve(tp, tn) && max_work) {
  754. tp = halve(t, tn);
  755. if (!tp) {
  756. #ifdef CONFIG_IP_FIB_TRIE_STATS
  757. this_cpu_inc(stats->resize_node_skipped);
  758. #endif
  759. break;
  760. }
  761. max_work--;
  762. tn = get_child(tp, cindex);
  763. }
  764. /* Only one child remains */
  765. if (should_collapse(tn))
  766. return collapse(t, tn);
  767. /* update parent in case halve failed */
  768. return node_parent(tn);
  769. }
  770. static void node_pull_suffix(struct key_vector *tn, unsigned char slen)
  771. {
  772. unsigned char node_slen = tn->slen;
  773. while ((node_slen > tn->pos) && (node_slen > slen)) {
  774. slen = update_suffix(tn);
  775. if (node_slen == slen)
  776. break;
  777. tn = node_parent(tn);
  778. node_slen = tn->slen;
  779. }
  780. }
  781. static void node_push_suffix(struct key_vector *tn, unsigned char slen)
  782. {
  783. while (tn->slen < slen) {
  784. tn->slen = slen;
  785. tn = node_parent(tn);
  786. }
  787. }
  788. /* rcu_read_lock needs to be hold by caller from readside */
  789. static struct key_vector *fib_find_node(struct trie *t,
  790. struct key_vector **tp, u32 key)
  791. {
  792. struct key_vector *pn, *n = t->kv;
  793. unsigned long index = 0;
  794. do {
  795. pn = n;
  796. n = get_child_rcu(n, index);
  797. if (!n)
  798. break;
  799. index = get_cindex(key, n);
  800. /* This bit of code is a bit tricky but it combines multiple
  801. * checks into a single check. The prefix consists of the
  802. * prefix plus zeros for the bits in the cindex. The index
  803. * is the difference between the key and this value. From
  804. * this we can actually derive several pieces of data.
  805. * if (index >= (1ul << bits))
  806. * we have a mismatch in skip bits and failed
  807. * else
  808. * we know the value is cindex
  809. *
  810. * This check is safe even if bits == KEYLENGTH due to the
  811. * fact that we can only allocate a node with 32 bits if a
  812. * long is greater than 32 bits.
  813. */
  814. if (index >= (1ul << n->bits)) {
  815. n = NULL;
  816. break;
  817. }
  818. /* keep searching until we find a perfect match leaf or NULL */
  819. } while (IS_TNODE(n));
  820. *tp = pn;
  821. return n;
  822. }
  823. /* Return the first fib alias matching TOS with
  824. * priority less than or equal to PRIO.
  825. * If 'find_first' is set, return the first matching
  826. * fib alias, regardless of TOS and priority.
  827. */
  828. static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
  829. u8 tos, u32 prio, u32 tb_id,
  830. bool find_first)
  831. {
  832. struct fib_alias *fa;
  833. if (!fah)
  834. return NULL;
  835. hlist_for_each_entry(fa, fah, fa_list) {
  836. if (fa->fa_slen < slen)
  837. continue;
  838. if (fa->fa_slen != slen)
  839. break;
  840. if (fa->tb_id > tb_id)
  841. continue;
  842. if (fa->tb_id != tb_id)
  843. break;
  844. if (find_first)
  845. return fa;
  846. if (fa->fa_tos > tos)
  847. continue;
  848. if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
  849. return fa;
  850. }
  851. return NULL;
  852. }
  853. static struct fib_alias *
  854. fib_find_matching_alias(struct net *net, const struct fib_rt_info *fri)
  855. {
  856. u8 slen = KEYLENGTH - fri->dst_len;
  857. struct key_vector *l, *tp;
  858. struct fib_table *tb;
  859. struct fib_alias *fa;
  860. struct trie *t;
  861. tb = fib_get_table(net, fri->tb_id);
  862. if (!tb)
  863. return NULL;
  864. t = (struct trie *)tb->tb_data;
  865. l = fib_find_node(t, &tp, be32_to_cpu(fri->dst));
  866. if (!l)
  867. return NULL;
  868. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  869. if (fa->fa_slen == slen && fa->tb_id == fri->tb_id &&
  870. fa->fa_tos == fri->tos && fa->fa_info == fri->fi &&
  871. fa->fa_type == fri->type)
  872. return fa;
  873. }
  874. return NULL;
  875. }
  876. void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri)
  877. {
  878. struct fib_alias *fa_match;
  879. rcu_read_lock();
  880. fa_match = fib_find_matching_alias(net, fri);
  881. if (!fa_match)
  882. goto out;
  883. fa_match->offload = fri->offload;
  884. fa_match->trap = fri->trap;
  885. out:
  886. rcu_read_unlock();
  887. }
  888. EXPORT_SYMBOL_GPL(fib_alias_hw_flags_set);
  889. static void trie_rebalance(struct trie *t, struct key_vector *tn)
  890. {
  891. while (!IS_TRIE(tn))
  892. tn = resize(t, tn);
  893. }
  894. static int fib_insert_node(struct trie *t, struct key_vector *tp,
  895. struct fib_alias *new, t_key key)
  896. {
  897. struct key_vector *n, *l;
  898. l = leaf_new(key, new);
  899. if (!l)
  900. goto noleaf;
  901. /* retrieve child from parent node */
  902. n = get_child(tp, get_index(key, tp));
  903. /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
  904. *
  905. * Add a new tnode here
  906. * first tnode need some special handling
  907. * leaves us in position for handling as case 3
  908. */
  909. if (n) {
  910. struct key_vector *tn;
  911. tn = tnode_new(key, __fls(key ^ n->key), 1);
  912. if (!tn)
  913. goto notnode;
  914. /* initialize routes out of node */
  915. NODE_INIT_PARENT(tn, tp);
  916. put_child(tn, get_index(key, tn) ^ 1, n);
  917. /* start adding routes into the node */
  918. put_child_root(tp, key, tn);
  919. node_set_parent(n, tn);
  920. /* parent now has a NULL spot where the leaf can go */
  921. tp = tn;
  922. }
  923. /* Case 3: n is NULL, and will just insert a new leaf */
  924. node_push_suffix(tp, new->fa_slen);
  925. NODE_INIT_PARENT(l, tp);
  926. put_child_root(tp, key, l);
  927. trie_rebalance(t, tp);
  928. return 0;
  929. notnode:
  930. node_free(l);
  931. noleaf:
  932. return -ENOMEM;
  933. }
  934. static int fib_insert_alias(struct trie *t, struct key_vector *tp,
  935. struct key_vector *l, struct fib_alias *new,
  936. struct fib_alias *fa, t_key key)
  937. {
  938. if (!l)
  939. return fib_insert_node(t, tp, new, key);
  940. if (fa) {
  941. hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
  942. } else {
  943. struct fib_alias *last;
  944. hlist_for_each_entry(last, &l->leaf, fa_list) {
  945. if (new->fa_slen < last->fa_slen)
  946. break;
  947. if ((new->fa_slen == last->fa_slen) &&
  948. (new->tb_id > last->tb_id))
  949. break;
  950. fa = last;
  951. }
  952. if (fa)
  953. hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
  954. else
  955. hlist_add_head_rcu(&new->fa_list, &l->leaf);
  956. }
  957. /* if we added to the tail node then we need to update slen */
  958. if (l->slen < new->fa_slen) {
  959. l->slen = new->fa_slen;
  960. node_push_suffix(tp, new->fa_slen);
  961. }
  962. return 0;
  963. }
  964. static bool fib_valid_key_len(u32 key, u8 plen, struct netlink_ext_ack *extack)
  965. {
  966. if (plen > KEYLENGTH) {
  967. NL_SET_ERR_MSG(extack, "Invalid prefix length");
  968. return false;
  969. }
  970. if ((plen < KEYLENGTH) && (key << plen)) {
  971. NL_SET_ERR_MSG(extack,
  972. "Invalid prefix for given prefix length");
  973. return false;
  974. }
  975. return true;
  976. }
  977. static void fib_remove_alias(struct trie *t, struct key_vector *tp,
  978. struct key_vector *l, struct fib_alias *old);
  979. /* Caller must hold RTNL. */
  980. int fib_table_insert(struct net *net, struct fib_table *tb,
  981. struct fib_config *cfg, struct netlink_ext_ack *extack)
  982. {
  983. struct trie *t = (struct trie *)tb->tb_data;
  984. struct fib_alias *fa, *new_fa;
  985. struct key_vector *l, *tp;
  986. u16 nlflags = NLM_F_EXCL;
  987. struct fib_info *fi;
  988. u8 plen = cfg->fc_dst_len;
  989. u8 slen = KEYLENGTH - plen;
  990. u8 tos = cfg->fc_tos;
  991. u32 key;
  992. int err;
  993. key = ntohl(cfg->fc_dst);
  994. if (!fib_valid_key_len(key, plen, extack))
  995. return -EINVAL;
  996. pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
  997. fi = fib_create_info(cfg, extack);
  998. if (IS_ERR(fi)) {
  999. err = PTR_ERR(fi);
  1000. goto err;
  1001. }
  1002. l = fib_find_node(t, &tp, key);
  1003. fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
  1004. tb->tb_id, false) : NULL;
  1005. /* Now fa, if non-NULL, points to the first fib alias
  1006. * with the same keys [prefix,tos,priority], if such key already
  1007. * exists or to the node before which we will insert new one.
  1008. *
  1009. * If fa is NULL, we will need to allocate a new one and
  1010. * insert to the tail of the section matching the suffix length
  1011. * of the new alias.
  1012. */
  1013. if (fa && fa->fa_tos == tos &&
  1014. fa->fa_info->fib_priority == fi->fib_priority) {
  1015. struct fib_alias *fa_first, *fa_match;
  1016. err = -EEXIST;
  1017. if (cfg->fc_nlflags & NLM_F_EXCL)
  1018. goto out;
  1019. nlflags &= ~NLM_F_EXCL;
  1020. /* We have 2 goals:
  1021. * 1. Find exact match for type, scope, fib_info to avoid
  1022. * duplicate routes
  1023. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  1024. */
  1025. fa_match = NULL;
  1026. fa_first = fa;
  1027. hlist_for_each_entry_from(fa, fa_list) {
  1028. if ((fa->fa_slen != slen) ||
  1029. (fa->tb_id != tb->tb_id) ||
  1030. (fa->fa_tos != tos))
  1031. break;
  1032. if (fa->fa_info->fib_priority != fi->fib_priority)
  1033. break;
  1034. if (fa->fa_type == cfg->fc_type &&
  1035. fa->fa_info == fi) {
  1036. fa_match = fa;
  1037. break;
  1038. }
  1039. }
  1040. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  1041. struct fib_info *fi_drop;
  1042. u8 state;
  1043. nlflags |= NLM_F_REPLACE;
  1044. fa = fa_first;
  1045. if (fa_match) {
  1046. if (fa == fa_match)
  1047. err = 0;
  1048. goto out;
  1049. }
  1050. err = -ENOBUFS;
  1051. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1052. if (!new_fa)
  1053. goto out;
  1054. fi_drop = fa->fa_info;
  1055. new_fa->fa_tos = fa->fa_tos;
  1056. new_fa->fa_info = fi;
  1057. new_fa->fa_type = cfg->fc_type;
  1058. state = fa->fa_state;
  1059. new_fa->fa_state = state & ~FA_S_ACCESSED;
  1060. new_fa->fa_slen = fa->fa_slen;
  1061. new_fa->tb_id = tb->tb_id;
  1062. new_fa->fa_default = -1;
  1063. new_fa->offload = 0;
  1064. new_fa->trap = 0;
  1065. hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
  1066. if (fib_find_alias(&l->leaf, fa->fa_slen, 0, 0,
  1067. tb->tb_id, true) == new_fa) {
  1068. enum fib_event_type fib_event;
  1069. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1070. err = call_fib_entry_notifiers(net, fib_event,
  1071. key, plen,
  1072. new_fa, extack);
  1073. if (err) {
  1074. hlist_replace_rcu(&new_fa->fa_list,
  1075. &fa->fa_list);
  1076. goto out_free_new_fa;
  1077. }
  1078. }
  1079. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
  1080. tb->tb_id, &cfg->fc_nlinfo, nlflags);
  1081. alias_free_mem_rcu(fa);
  1082. fib_release_info(fi_drop);
  1083. if (state & FA_S_ACCESSED)
  1084. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1085. goto succeeded;
  1086. }
  1087. /* Error if we find a perfect match which
  1088. * uses the same scope, type, and nexthop
  1089. * information.
  1090. */
  1091. if (fa_match)
  1092. goto out;
  1093. if (cfg->fc_nlflags & NLM_F_APPEND)
  1094. nlflags |= NLM_F_APPEND;
  1095. else
  1096. fa = fa_first;
  1097. }
  1098. err = -ENOENT;
  1099. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  1100. goto out;
  1101. nlflags |= NLM_F_CREATE;
  1102. err = -ENOBUFS;
  1103. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1104. if (!new_fa)
  1105. goto out;
  1106. new_fa->fa_info = fi;
  1107. new_fa->fa_tos = tos;
  1108. new_fa->fa_type = cfg->fc_type;
  1109. new_fa->fa_state = 0;
  1110. new_fa->fa_slen = slen;
  1111. new_fa->tb_id = tb->tb_id;
  1112. new_fa->fa_default = -1;
  1113. new_fa->offload = 0;
  1114. new_fa->trap = 0;
  1115. /* Insert new entry to the list. */
  1116. err = fib_insert_alias(t, tp, l, new_fa, fa, key);
  1117. if (err)
  1118. goto out_free_new_fa;
  1119. /* The alias was already inserted, so the node must exist. */
  1120. l = l ? l : fib_find_node(t, &tp, key);
  1121. if (WARN_ON_ONCE(!l))
  1122. goto out_free_new_fa;
  1123. if (fib_find_alias(&l->leaf, new_fa->fa_slen, 0, 0, tb->tb_id, true) ==
  1124. new_fa) {
  1125. enum fib_event_type fib_event;
  1126. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1127. err = call_fib_entry_notifiers(net, fib_event, key, plen,
  1128. new_fa, extack);
  1129. if (err)
  1130. goto out_remove_new_fa;
  1131. }
  1132. if (!plen)
  1133. tb->tb_num_default++;
  1134. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1135. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
  1136. &cfg->fc_nlinfo, nlflags);
  1137. succeeded:
  1138. return 0;
  1139. out_remove_new_fa:
  1140. fib_remove_alias(t, tp, l, new_fa);
  1141. out_free_new_fa:
  1142. kmem_cache_free(fn_alias_kmem, new_fa);
  1143. out:
  1144. fib_release_info(fi);
  1145. err:
  1146. return err;
  1147. }
  1148. static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
  1149. {
  1150. t_key prefix = n->key;
  1151. return (key ^ prefix) & (prefix | -prefix);
  1152. }
  1153. bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
  1154. const struct flowi4 *flp)
  1155. {
  1156. if (nhc->nhc_flags & RTNH_F_DEAD)
  1157. return false;
  1158. if (ip_ignore_linkdown(nhc->nhc_dev) &&
  1159. nhc->nhc_flags & RTNH_F_LINKDOWN &&
  1160. !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
  1161. return false;
  1162. if (!(flp->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) {
  1163. if (flp->flowi4_oif &&
  1164. flp->flowi4_oif != nhc->nhc_oif)
  1165. return false;
  1166. }
  1167. return true;
  1168. }
  1169. /* should be called with rcu_read_lock */
  1170. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  1171. struct fib_result *res, int fib_flags)
  1172. {
  1173. struct trie *t = (struct trie *) tb->tb_data;
  1174. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1175. struct trie_use_stats __percpu *stats = t->stats;
  1176. #endif
  1177. const t_key key = ntohl(flp->daddr);
  1178. struct key_vector *n, *pn;
  1179. struct fib_alias *fa;
  1180. unsigned long index;
  1181. t_key cindex;
  1182. pn = t->kv;
  1183. cindex = 0;
  1184. n = get_child_rcu(pn, cindex);
  1185. if (!n) {
  1186. trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
  1187. return -EAGAIN;
  1188. }
  1189. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1190. this_cpu_inc(stats->gets);
  1191. #endif
  1192. /* Step 1: Travel to the longest prefix match in the trie */
  1193. for (;;) {
  1194. index = get_cindex(key, n);
  1195. /* This bit of code is a bit tricky but it combines multiple
  1196. * checks into a single check. The prefix consists of the
  1197. * prefix plus zeros for the "bits" in the prefix. The index
  1198. * is the difference between the key and this value. From
  1199. * this we can actually derive several pieces of data.
  1200. * if (index >= (1ul << bits))
  1201. * we have a mismatch in skip bits and failed
  1202. * else
  1203. * we know the value is cindex
  1204. *
  1205. * This check is safe even if bits == KEYLENGTH due to the
  1206. * fact that we can only allocate a node with 32 bits if a
  1207. * long is greater than 32 bits.
  1208. */
  1209. if (index >= (1ul << n->bits))
  1210. break;
  1211. /* we have found a leaf. Prefixes have already been compared */
  1212. if (IS_LEAF(n))
  1213. goto found;
  1214. /* only record pn and cindex if we are going to be chopping
  1215. * bits later. Otherwise we are just wasting cycles.
  1216. */
  1217. if (n->slen > n->pos) {
  1218. pn = n;
  1219. cindex = index;
  1220. }
  1221. n = get_child_rcu(n, index);
  1222. if (unlikely(!n))
  1223. goto backtrace;
  1224. }
  1225. /* Step 2: Sort out leaves and begin backtracing for longest prefix */
  1226. for (;;) {
  1227. /* record the pointer where our next node pointer is stored */
  1228. struct key_vector __rcu **cptr = n->tnode;
  1229. /* This test verifies that none of the bits that differ
  1230. * between the key and the prefix exist in the region of
  1231. * the lsb and higher in the prefix.
  1232. */
  1233. if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
  1234. goto backtrace;
  1235. /* exit out and process leaf */
  1236. if (unlikely(IS_LEAF(n)))
  1237. break;
  1238. /* Don't bother recording parent info. Since we are in
  1239. * prefix match mode we will have to come back to wherever
  1240. * we started this traversal anyway
  1241. */
  1242. while ((n = rcu_dereference(*cptr)) == NULL) {
  1243. backtrace:
  1244. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1245. if (!n)
  1246. this_cpu_inc(stats->null_node_hit);
  1247. #endif
  1248. /* If we are at cindex 0 there are no more bits for
  1249. * us to strip at this level so we must ascend back
  1250. * up one level to see if there are any more bits to
  1251. * be stripped there.
  1252. */
  1253. while (!cindex) {
  1254. t_key pkey = pn->key;
  1255. /* If we don't have a parent then there is
  1256. * nothing for us to do as we do not have any
  1257. * further nodes to parse.
  1258. */
  1259. if (IS_TRIE(pn)) {
  1260. trace_fib_table_lookup(tb->tb_id, flp,
  1261. NULL, -EAGAIN);
  1262. return -EAGAIN;
  1263. }
  1264. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1265. this_cpu_inc(stats->backtrack);
  1266. #endif
  1267. /* Get Child's index */
  1268. pn = node_parent_rcu(pn);
  1269. cindex = get_index(pkey, pn);
  1270. }
  1271. /* strip the least significant bit from the cindex */
  1272. cindex &= cindex - 1;
  1273. /* grab pointer for next child node */
  1274. cptr = &pn->tnode[cindex];
  1275. }
  1276. }
  1277. found:
  1278. /* this line carries forward the xor from earlier in the function */
  1279. index = key ^ n->key;
  1280. /* Step 3: Process the leaf, if that fails fall back to backtracing */
  1281. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  1282. struct fib_info *fi = fa->fa_info;
  1283. struct fib_nh_common *nhc;
  1284. int nhsel, err;
  1285. if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
  1286. if (index >= (1ul << fa->fa_slen))
  1287. continue;
  1288. }
  1289. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1290. continue;
  1291. if (fi->fib_dead)
  1292. continue;
  1293. if (fa->fa_info->fib_scope < flp->flowi4_scope)
  1294. continue;
  1295. fib_alias_accessed(fa);
  1296. err = fib_props[fa->fa_type].error;
  1297. if (unlikely(err < 0)) {
  1298. out_reject:
  1299. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1300. this_cpu_inc(stats->semantic_match_passed);
  1301. #endif
  1302. trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
  1303. return err;
  1304. }
  1305. if (fi->fib_flags & RTNH_F_DEAD)
  1306. continue;
  1307. if (unlikely(fi->nh)) {
  1308. if (nexthop_is_blackhole(fi->nh)) {
  1309. err = fib_props[RTN_BLACKHOLE].error;
  1310. goto out_reject;
  1311. }
  1312. nhc = nexthop_get_nhc_lookup(fi->nh, fib_flags, flp,
  1313. &nhsel);
  1314. if (nhc)
  1315. goto set_result;
  1316. goto miss;
  1317. }
  1318. for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
  1319. nhc = fib_info_nhc(fi, nhsel);
  1320. if (!fib_lookup_good_nhc(nhc, fib_flags, flp))
  1321. continue;
  1322. set_result:
  1323. if (!(fib_flags & FIB_LOOKUP_NOREF))
  1324. refcount_inc(&fi->fib_clntref);
  1325. res->prefix = htonl(n->key);
  1326. res->prefixlen = KEYLENGTH - fa->fa_slen;
  1327. res->nh_sel = nhsel;
  1328. res->nhc = nhc;
  1329. res->type = fa->fa_type;
  1330. res->scope = fi->fib_scope;
  1331. res->fi = fi;
  1332. res->table = tb;
  1333. res->fa_head = &n->leaf;
  1334. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1335. this_cpu_inc(stats->semantic_match_passed);
  1336. #endif
  1337. trace_fib_table_lookup(tb->tb_id, flp, nhc, err);
  1338. return err;
  1339. }
  1340. }
  1341. miss:
  1342. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1343. this_cpu_inc(stats->semantic_match_miss);
  1344. #endif
  1345. goto backtrace;
  1346. }
  1347. EXPORT_SYMBOL_GPL(fib_table_lookup);
  1348. static void fib_remove_alias(struct trie *t, struct key_vector *tp,
  1349. struct key_vector *l, struct fib_alias *old)
  1350. {
  1351. /* record the location of the previous list_info entry */
  1352. struct hlist_node **pprev = old->fa_list.pprev;
  1353. struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
  1354. /* remove the fib_alias from the list */
  1355. hlist_del_rcu(&old->fa_list);
  1356. /* if we emptied the list this leaf will be freed and we can sort
  1357. * out parent suffix lengths as a part of trie_rebalance
  1358. */
  1359. if (hlist_empty(&l->leaf)) {
  1360. if (tp->slen == l->slen)
  1361. node_pull_suffix(tp, tp->pos);
  1362. put_child_root(tp, l->key, NULL);
  1363. node_free(l);
  1364. trie_rebalance(t, tp);
  1365. return;
  1366. }
  1367. /* only access fa if it is pointing at the last valid hlist_node */
  1368. if (*pprev)
  1369. return;
  1370. /* update the trie with the latest suffix length */
  1371. l->slen = fa->fa_slen;
  1372. node_pull_suffix(tp, fa->fa_slen);
  1373. }
  1374. static void fib_notify_alias_delete(struct net *net, u32 key,
  1375. struct hlist_head *fah,
  1376. struct fib_alias *fa_to_delete,
  1377. struct netlink_ext_ack *extack)
  1378. {
  1379. struct fib_alias *fa_next, *fa_to_notify;
  1380. u32 tb_id = fa_to_delete->tb_id;
  1381. u8 slen = fa_to_delete->fa_slen;
  1382. enum fib_event_type fib_event;
  1383. /* Do not notify if we do not care about the route. */
  1384. if (fib_find_alias(fah, slen, 0, 0, tb_id, true) != fa_to_delete)
  1385. return;
  1386. /* Determine if the route should be replaced by the next route in the
  1387. * list.
  1388. */
  1389. fa_next = hlist_entry_safe(fa_to_delete->fa_list.next,
  1390. struct fib_alias, fa_list);
  1391. if (fa_next && fa_next->fa_slen == slen && fa_next->tb_id == tb_id) {
  1392. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1393. fa_to_notify = fa_next;
  1394. } else {
  1395. fib_event = FIB_EVENT_ENTRY_DEL;
  1396. fa_to_notify = fa_to_delete;
  1397. }
  1398. call_fib_entry_notifiers(net, fib_event, key, KEYLENGTH - slen,
  1399. fa_to_notify, extack);
  1400. }
  1401. /* Caller must hold RTNL. */
  1402. int fib_table_delete(struct net *net, struct fib_table *tb,
  1403. struct fib_config *cfg, struct netlink_ext_ack *extack)
  1404. {
  1405. struct trie *t = (struct trie *) tb->tb_data;
  1406. struct fib_alias *fa, *fa_to_delete;
  1407. struct key_vector *l, *tp;
  1408. u8 plen = cfg->fc_dst_len;
  1409. u8 slen = KEYLENGTH - plen;
  1410. u8 tos = cfg->fc_tos;
  1411. u32 key;
  1412. key = ntohl(cfg->fc_dst);
  1413. if (!fib_valid_key_len(key, plen, extack))
  1414. return -EINVAL;
  1415. l = fib_find_node(t, &tp, key);
  1416. if (!l)
  1417. return -ESRCH;
  1418. fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id, false);
  1419. if (!fa)
  1420. return -ESRCH;
  1421. pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
  1422. fa_to_delete = NULL;
  1423. hlist_for_each_entry_from(fa, fa_list) {
  1424. struct fib_info *fi = fa->fa_info;
  1425. if ((fa->fa_slen != slen) ||
  1426. (fa->tb_id != tb->tb_id) ||
  1427. (fa->fa_tos != tos))
  1428. break;
  1429. if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
  1430. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  1431. fa->fa_info->fib_scope == cfg->fc_scope) &&
  1432. (!cfg->fc_prefsrc ||
  1433. fi->fib_prefsrc == cfg->fc_prefsrc) &&
  1434. (!cfg->fc_protocol ||
  1435. fi->fib_protocol == cfg->fc_protocol) &&
  1436. fib_nh_match(net, cfg, fi, extack) == 0 &&
  1437. fib_metrics_match(cfg, fi)) {
  1438. fa_to_delete = fa;
  1439. break;
  1440. }
  1441. }
  1442. if (!fa_to_delete)
  1443. return -ESRCH;
  1444. fib_notify_alias_delete(net, key, &l->leaf, fa_to_delete, extack);
  1445. rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
  1446. &cfg->fc_nlinfo, 0);
  1447. if (!plen)
  1448. tb->tb_num_default--;
  1449. fib_remove_alias(t, tp, l, fa_to_delete);
  1450. if (fa_to_delete->fa_state & FA_S_ACCESSED)
  1451. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1452. fib_release_info(fa_to_delete->fa_info);
  1453. alias_free_mem_rcu(fa_to_delete);
  1454. return 0;
  1455. }
  1456. /* Scan for the next leaf starting at the provided key value */
  1457. static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
  1458. {
  1459. struct key_vector *pn, *n = *tn;
  1460. unsigned long cindex;
  1461. /* this loop is meant to try and find the key in the trie */
  1462. do {
  1463. /* record parent and next child index */
  1464. pn = n;
  1465. cindex = (key > pn->key) ? get_index(key, pn) : 0;
  1466. if (cindex >> pn->bits)
  1467. break;
  1468. /* descend into the next child */
  1469. n = get_child_rcu(pn, cindex++);
  1470. if (!n)
  1471. break;
  1472. /* guarantee forward progress on the keys */
  1473. if (IS_LEAF(n) && (n->key >= key))
  1474. goto found;
  1475. } while (IS_TNODE(n));
  1476. /* this loop will search for the next leaf with a greater key */
  1477. while (!IS_TRIE(pn)) {
  1478. /* if we exhausted the parent node we will need to climb */
  1479. if (cindex >= (1ul << pn->bits)) {
  1480. t_key pkey = pn->key;
  1481. pn = node_parent_rcu(pn);
  1482. cindex = get_index(pkey, pn) + 1;
  1483. continue;
  1484. }
  1485. /* grab the next available node */
  1486. n = get_child_rcu(pn, cindex++);
  1487. if (!n)
  1488. continue;
  1489. /* no need to compare keys since we bumped the index */
  1490. if (IS_LEAF(n))
  1491. goto found;
  1492. /* Rescan start scanning in new node */
  1493. pn = n;
  1494. cindex = 0;
  1495. }
  1496. *tn = pn;
  1497. return NULL; /* Root of trie */
  1498. found:
  1499. /* if we are at the limit for keys just return NULL for the tnode */
  1500. *tn = pn;
  1501. return n;
  1502. }
  1503. static void fib_trie_free(struct fib_table *tb)
  1504. {
  1505. struct trie *t = (struct trie *)tb->tb_data;
  1506. struct key_vector *pn = t->kv;
  1507. unsigned long cindex = 1;
  1508. struct hlist_node *tmp;
  1509. struct fib_alias *fa;
  1510. /* walk trie in reverse order and free everything */
  1511. for (;;) {
  1512. struct key_vector *n;
  1513. if (!(cindex--)) {
  1514. t_key pkey = pn->key;
  1515. if (IS_TRIE(pn))
  1516. break;
  1517. n = pn;
  1518. pn = node_parent(pn);
  1519. /* drop emptied tnode */
  1520. put_child_root(pn, n->key, NULL);
  1521. node_free(n);
  1522. cindex = get_index(pkey, pn);
  1523. continue;
  1524. }
  1525. /* grab the next available node */
  1526. n = get_child(pn, cindex);
  1527. if (!n)
  1528. continue;
  1529. if (IS_TNODE(n)) {
  1530. /* record pn and cindex for leaf walking */
  1531. pn = n;
  1532. cindex = 1ul << n->bits;
  1533. continue;
  1534. }
  1535. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1536. hlist_del_rcu(&fa->fa_list);
  1537. alias_free_mem_rcu(fa);
  1538. }
  1539. put_child_root(pn, n->key, NULL);
  1540. node_free(n);
  1541. }
  1542. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1543. free_percpu(t->stats);
  1544. #endif
  1545. kfree(tb);
  1546. }
  1547. struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
  1548. {
  1549. struct trie *ot = (struct trie *)oldtb->tb_data;
  1550. struct key_vector *l, *tp = ot->kv;
  1551. struct fib_table *local_tb;
  1552. struct fib_alias *fa;
  1553. struct trie *lt;
  1554. t_key key = 0;
  1555. if (oldtb->tb_data == oldtb->__data)
  1556. return oldtb;
  1557. local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
  1558. if (!local_tb)
  1559. return NULL;
  1560. lt = (struct trie *)local_tb->tb_data;
  1561. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1562. struct key_vector *local_l = NULL, *local_tp;
  1563. hlist_for_each_entry(fa, &l->leaf, fa_list) {
  1564. struct fib_alias *new_fa;
  1565. if (local_tb->tb_id != fa->tb_id)
  1566. continue;
  1567. /* clone fa for new local table */
  1568. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1569. if (!new_fa)
  1570. goto out;
  1571. memcpy(new_fa, fa, sizeof(*fa));
  1572. /* insert clone into table */
  1573. if (!local_l)
  1574. local_l = fib_find_node(lt, &local_tp, l->key);
  1575. if (fib_insert_alias(lt, local_tp, local_l, new_fa,
  1576. NULL, l->key)) {
  1577. kmem_cache_free(fn_alias_kmem, new_fa);
  1578. goto out;
  1579. }
  1580. }
  1581. /* stop loop if key wrapped back to 0 */
  1582. key = l->key + 1;
  1583. if (key < l->key)
  1584. break;
  1585. }
  1586. return local_tb;
  1587. out:
  1588. fib_trie_free(local_tb);
  1589. return NULL;
  1590. }
  1591. /* Caller must hold RTNL */
  1592. void fib_table_flush_external(struct fib_table *tb)
  1593. {
  1594. struct trie *t = (struct trie *)tb->tb_data;
  1595. struct key_vector *pn = t->kv;
  1596. unsigned long cindex = 1;
  1597. struct hlist_node *tmp;
  1598. struct fib_alias *fa;
  1599. /* walk trie in reverse order */
  1600. for (;;) {
  1601. unsigned char slen = 0;
  1602. struct key_vector *n;
  1603. if (!(cindex--)) {
  1604. t_key pkey = pn->key;
  1605. /* cannot resize the trie vector */
  1606. if (IS_TRIE(pn))
  1607. break;
  1608. /* update the suffix to address pulled leaves */
  1609. if (pn->slen > pn->pos)
  1610. update_suffix(pn);
  1611. /* resize completed node */
  1612. pn = resize(t, pn);
  1613. cindex = get_index(pkey, pn);
  1614. continue;
  1615. }
  1616. /* grab the next available node */
  1617. n = get_child(pn, cindex);
  1618. if (!n)
  1619. continue;
  1620. if (IS_TNODE(n)) {
  1621. /* record pn and cindex for leaf walking */
  1622. pn = n;
  1623. cindex = 1ul << n->bits;
  1624. continue;
  1625. }
  1626. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1627. /* if alias was cloned to local then we just
  1628. * need to remove the local copy from main
  1629. */
  1630. if (tb->tb_id != fa->tb_id) {
  1631. hlist_del_rcu(&fa->fa_list);
  1632. alias_free_mem_rcu(fa);
  1633. continue;
  1634. }
  1635. /* record local slen */
  1636. slen = fa->fa_slen;
  1637. }
  1638. /* update leaf slen */
  1639. n->slen = slen;
  1640. if (hlist_empty(&n->leaf)) {
  1641. put_child_root(pn, n->key, NULL);
  1642. node_free(n);
  1643. }
  1644. }
  1645. }
  1646. /* Caller must hold RTNL. */
  1647. int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
  1648. {
  1649. struct trie *t = (struct trie *)tb->tb_data;
  1650. struct key_vector *pn = t->kv;
  1651. unsigned long cindex = 1;
  1652. struct hlist_node *tmp;
  1653. struct fib_alias *fa;
  1654. int found = 0;
  1655. /* walk trie in reverse order */
  1656. for (;;) {
  1657. unsigned char slen = 0;
  1658. struct key_vector *n;
  1659. if (!(cindex--)) {
  1660. t_key pkey = pn->key;
  1661. /* cannot resize the trie vector */
  1662. if (IS_TRIE(pn))
  1663. break;
  1664. /* update the suffix to address pulled leaves */
  1665. if (pn->slen > pn->pos)
  1666. update_suffix(pn);
  1667. /* resize completed node */
  1668. pn = resize(t, pn);
  1669. cindex = get_index(pkey, pn);
  1670. continue;
  1671. }
  1672. /* grab the next available node */
  1673. n = get_child(pn, cindex);
  1674. if (!n)
  1675. continue;
  1676. if (IS_TNODE(n)) {
  1677. /* record pn and cindex for leaf walking */
  1678. pn = n;
  1679. cindex = 1ul << n->bits;
  1680. continue;
  1681. }
  1682. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1683. struct fib_info *fi = fa->fa_info;
  1684. if (!fi || tb->tb_id != fa->tb_id ||
  1685. (!(fi->fib_flags & RTNH_F_DEAD) &&
  1686. !fib_props[fa->fa_type].error)) {
  1687. slen = fa->fa_slen;
  1688. continue;
  1689. }
  1690. /* Do not flush error routes if network namespace is
  1691. * not being dismantled
  1692. */
  1693. if (!flush_all && fib_props[fa->fa_type].error) {
  1694. slen = fa->fa_slen;
  1695. continue;
  1696. }
  1697. fib_notify_alias_delete(net, n->key, &n->leaf, fa,
  1698. NULL);
  1699. hlist_del_rcu(&fa->fa_list);
  1700. fib_release_info(fa->fa_info);
  1701. alias_free_mem_rcu(fa);
  1702. found++;
  1703. }
  1704. /* update leaf slen */
  1705. n->slen = slen;
  1706. if (hlist_empty(&n->leaf)) {
  1707. put_child_root(pn, n->key, NULL);
  1708. node_free(n);
  1709. }
  1710. }
  1711. pr_debug("trie_flush found=%d\n", found);
  1712. return found;
  1713. }
  1714. /* derived from fib_trie_free */
  1715. static void __fib_info_notify_update(struct net *net, struct fib_table *tb,
  1716. struct nl_info *info)
  1717. {
  1718. struct trie *t = (struct trie *)tb->tb_data;
  1719. struct key_vector *pn = t->kv;
  1720. unsigned long cindex = 1;
  1721. struct fib_alias *fa;
  1722. for (;;) {
  1723. struct key_vector *n;
  1724. if (!(cindex--)) {
  1725. t_key pkey = pn->key;
  1726. if (IS_TRIE(pn))
  1727. break;
  1728. pn = node_parent(pn);
  1729. cindex = get_index(pkey, pn);
  1730. continue;
  1731. }
  1732. /* grab the next available node */
  1733. n = get_child(pn, cindex);
  1734. if (!n)
  1735. continue;
  1736. if (IS_TNODE(n)) {
  1737. /* record pn and cindex for leaf walking */
  1738. pn = n;
  1739. cindex = 1ul << n->bits;
  1740. continue;
  1741. }
  1742. hlist_for_each_entry(fa, &n->leaf, fa_list) {
  1743. struct fib_info *fi = fa->fa_info;
  1744. if (!fi || !fi->nh_updated || fa->tb_id != tb->tb_id)
  1745. continue;
  1746. rtmsg_fib(RTM_NEWROUTE, htonl(n->key), fa,
  1747. KEYLENGTH - fa->fa_slen, tb->tb_id,
  1748. info, NLM_F_REPLACE);
  1749. /* call_fib_entry_notifiers will be removed when
  1750. * in-kernel notifier is implemented and supported
  1751. * for nexthop objects
  1752. */
  1753. call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE,
  1754. n->key,
  1755. KEYLENGTH - fa->fa_slen, fa,
  1756. NULL);
  1757. }
  1758. }
  1759. }
  1760. void fib_info_notify_update(struct net *net, struct nl_info *info)
  1761. {
  1762. unsigned int h;
  1763. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1764. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1765. struct fib_table *tb;
  1766. hlist_for_each_entry_rcu(tb, head, tb_hlist,
  1767. lockdep_rtnl_is_held())
  1768. __fib_info_notify_update(net, tb, info);
  1769. }
  1770. }
  1771. static int fib_leaf_notify(struct key_vector *l, struct fib_table *tb,
  1772. struct notifier_block *nb,
  1773. struct netlink_ext_ack *extack)
  1774. {
  1775. struct fib_alias *fa;
  1776. int last_slen = -1;
  1777. int err;
  1778. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1779. struct fib_info *fi = fa->fa_info;
  1780. if (!fi)
  1781. continue;
  1782. /* local and main table can share the same trie,
  1783. * so don't notify twice for the same entry.
  1784. */
  1785. if (tb->tb_id != fa->tb_id)
  1786. continue;
  1787. if (fa->fa_slen == last_slen)
  1788. continue;
  1789. last_slen = fa->fa_slen;
  1790. err = call_fib_entry_notifier(nb, FIB_EVENT_ENTRY_REPLACE,
  1791. l->key, KEYLENGTH - fa->fa_slen,
  1792. fa, extack);
  1793. if (err)
  1794. return err;
  1795. }
  1796. return 0;
  1797. }
  1798. static int fib_table_notify(struct fib_table *tb, struct notifier_block *nb,
  1799. struct netlink_ext_ack *extack)
  1800. {
  1801. struct trie *t = (struct trie *)tb->tb_data;
  1802. struct key_vector *l, *tp = t->kv;
  1803. t_key key = 0;
  1804. int err;
  1805. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1806. err = fib_leaf_notify(l, tb, nb, extack);
  1807. if (err)
  1808. return err;
  1809. key = l->key + 1;
  1810. /* stop in case of wrap around */
  1811. if (key < l->key)
  1812. break;
  1813. }
  1814. return 0;
  1815. }
  1816. int fib_notify(struct net *net, struct notifier_block *nb,
  1817. struct netlink_ext_ack *extack)
  1818. {
  1819. unsigned int h;
  1820. int err;
  1821. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1822. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1823. struct fib_table *tb;
  1824. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  1825. err = fib_table_notify(tb, nb, extack);
  1826. if (err)
  1827. return err;
  1828. }
  1829. }
  1830. return 0;
  1831. }
  1832. static void __trie_free_rcu(struct rcu_head *head)
  1833. {
  1834. struct fib_table *tb = container_of(head, struct fib_table, rcu);
  1835. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1836. struct trie *t = (struct trie *)tb->tb_data;
  1837. if (tb->tb_data == tb->__data)
  1838. free_percpu(t->stats);
  1839. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  1840. kfree(tb);
  1841. }
  1842. void fib_free_table(struct fib_table *tb)
  1843. {
  1844. call_rcu(&tb->rcu, __trie_free_rcu);
  1845. }
  1846. static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
  1847. struct sk_buff *skb, struct netlink_callback *cb,
  1848. struct fib_dump_filter *filter)
  1849. {
  1850. unsigned int flags = NLM_F_MULTI;
  1851. __be32 xkey = htonl(l->key);
  1852. int i, s_i, i_fa, s_fa, err;
  1853. struct fib_alias *fa;
  1854. if (filter->filter_set ||
  1855. !filter->dump_exceptions || !filter->dump_routes)
  1856. flags |= NLM_F_DUMP_FILTERED;
  1857. s_i = cb->args[4];
  1858. s_fa = cb->args[5];
  1859. i = 0;
  1860. /* rcu_read_lock is hold by caller */
  1861. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1862. struct fib_info *fi = fa->fa_info;
  1863. if (i < s_i)
  1864. goto next;
  1865. i_fa = 0;
  1866. if (tb->tb_id != fa->tb_id)
  1867. goto next;
  1868. if (filter->filter_set) {
  1869. if (filter->rt_type && fa->fa_type != filter->rt_type)
  1870. goto next;
  1871. if ((filter->protocol &&
  1872. fi->fib_protocol != filter->protocol))
  1873. goto next;
  1874. if (filter->dev &&
  1875. !fib_info_nh_uses_dev(fi, filter->dev))
  1876. goto next;
  1877. }
  1878. if (filter->dump_routes) {
  1879. if (!s_fa) {
  1880. struct fib_rt_info fri;
  1881. fri.fi = fi;
  1882. fri.tb_id = tb->tb_id;
  1883. fri.dst = xkey;
  1884. fri.dst_len = KEYLENGTH - fa->fa_slen;
  1885. fri.tos = fa->fa_tos;
  1886. fri.type = fa->fa_type;
  1887. fri.offload = fa->offload;
  1888. fri.trap = fa->trap;
  1889. err = fib_dump_info(skb,
  1890. NETLINK_CB(cb->skb).portid,
  1891. cb->nlh->nlmsg_seq,
  1892. RTM_NEWROUTE, &fri, flags);
  1893. if (err < 0)
  1894. goto stop;
  1895. }
  1896. i_fa++;
  1897. }
  1898. if (filter->dump_exceptions) {
  1899. err = fib_dump_info_fnhe(skb, cb, tb->tb_id, fi,
  1900. &i_fa, s_fa, flags);
  1901. if (err < 0)
  1902. goto stop;
  1903. }
  1904. next:
  1905. i++;
  1906. }
  1907. cb->args[4] = i;
  1908. return skb->len;
  1909. stop:
  1910. cb->args[4] = i;
  1911. cb->args[5] = i_fa;
  1912. return err;
  1913. }
  1914. /* rcu_read_lock needs to be hold by caller from readside */
  1915. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  1916. struct netlink_callback *cb, struct fib_dump_filter *filter)
  1917. {
  1918. struct trie *t = (struct trie *)tb->tb_data;
  1919. struct key_vector *l, *tp = t->kv;
  1920. /* Dump starting at last key.
  1921. * Note: 0.0.0.0/0 (ie default) is first key.
  1922. */
  1923. int count = cb->args[2];
  1924. t_key key = cb->args[3];
  1925. /* First time here, count and key are both always 0. Count > 0
  1926. * and key == 0 means the dump has wrapped around and we are done.
  1927. */
  1928. if (count && !key)
  1929. return skb->len;
  1930. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1931. int err;
  1932. err = fn_trie_dump_leaf(l, tb, skb, cb, filter);
  1933. if (err < 0) {
  1934. cb->args[3] = key;
  1935. cb->args[2] = count;
  1936. return err;
  1937. }
  1938. ++count;
  1939. key = l->key + 1;
  1940. memset(&cb->args[4], 0,
  1941. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  1942. /* stop loop if key wrapped back to 0 */
  1943. if (key < l->key)
  1944. break;
  1945. }
  1946. cb->args[3] = key;
  1947. cb->args[2] = count;
  1948. return skb->len;
  1949. }
  1950. void __init fib_trie_init(void)
  1951. {
  1952. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  1953. sizeof(struct fib_alias),
  1954. 0, SLAB_PANIC, NULL);
  1955. trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
  1956. LEAF_SIZE,
  1957. 0, SLAB_PANIC, NULL);
  1958. }
  1959. struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
  1960. {
  1961. struct fib_table *tb;
  1962. struct trie *t;
  1963. size_t sz = sizeof(*tb);
  1964. if (!alias)
  1965. sz += sizeof(struct trie);
  1966. tb = kzalloc(sz, GFP_KERNEL);
  1967. if (!tb)
  1968. return NULL;
  1969. tb->tb_id = id;
  1970. tb->tb_num_default = 0;
  1971. tb->tb_data = (alias ? alias->__data : tb->__data);
  1972. if (alias)
  1973. return tb;
  1974. t = (struct trie *) tb->tb_data;
  1975. t->kv[0].pos = KEYLENGTH;
  1976. t->kv[0].slen = KEYLENGTH;
  1977. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1978. t->stats = alloc_percpu(struct trie_use_stats);
  1979. if (!t->stats) {
  1980. kfree(tb);
  1981. tb = NULL;
  1982. }
  1983. #endif
  1984. return tb;
  1985. }
  1986. #ifdef CONFIG_PROC_FS
  1987. /* Depth first Trie walk iterator */
  1988. struct fib_trie_iter {
  1989. struct seq_net_private p;
  1990. struct fib_table *tb;
  1991. struct key_vector *tnode;
  1992. unsigned int index;
  1993. unsigned int depth;
  1994. };
  1995. static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
  1996. {
  1997. unsigned long cindex = iter->index;
  1998. struct key_vector *pn = iter->tnode;
  1999. t_key pkey;
  2000. pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
  2001. iter->tnode, iter->index, iter->depth);
  2002. while (!IS_TRIE(pn)) {
  2003. while (cindex < child_length(pn)) {
  2004. struct key_vector *n = get_child_rcu(pn, cindex++);
  2005. if (!n)
  2006. continue;
  2007. if (IS_LEAF(n)) {
  2008. iter->tnode = pn;
  2009. iter->index = cindex;
  2010. } else {
  2011. /* push down one level */
  2012. iter->tnode = n;
  2013. iter->index = 0;
  2014. ++iter->depth;
  2015. }
  2016. return n;
  2017. }
  2018. /* Current node exhausted, pop back up */
  2019. pkey = pn->key;
  2020. pn = node_parent_rcu(pn);
  2021. cindex = get_index(pkey, pn) + 1;
  2022. --iter->depth;
  2023. }
  2024. /* record root node so further searches know we are done */
  2025. iter->tnode = pn;
  2026. iter->index = 0;
  2027. return NULL;
  2028. }
  2029. static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
  2030. struct trie *t)
  2031. {
  2032. struct key_vector *n, *pn;
  2033. if (!t)
  2034. return NULL;
  2035. pn = t->kv;
  2036. n = rcu_dereference(pn->tnode[0]);
  2037. if (!n)
  2038. return NULL;
  2039. if (IS_TNODE(n)) {
  2040. iter->tnode = n;
  2041. iter->index = 0;
  2042. iter->depth = 1;
  2043. } else {
  2044. iter->tnode = pn;
  2045. iter->index = 0;
  2046. iter->depth = 0;
  2047. }
  2048. return n;
  2049. }
  2050. static void trie_collect_stats(struct trie *t, struct trie_stat *s)
  2051. {
  2052. struct key_vector *n;
  2053. struct fib_trie_iter iter;
  2054. memset(s, 0, sizeof(*s));
  2055. rcu_read_lock();
  2056. for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
  2057. if (IS_LEAF(n)) {
  2058. struct fib_alias *fa;
  2059. s->leaves++;
  2060. s->totdepth += iter.depth;
  2061. if (iter.depth > s->maxdepth)
  2062. s->maxdepth = iter.depth;
  2063. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
  2064. ++s->prefixes;
  2065. } else {
  2066. s->tnodes++;
  2067. if (n->bits < MAX_STAT_DEPTH)
  2068. s->nodesizes[n->bits]++;
  2069. s->nullpointers += tn_info(n)->empty_children;
  2070. }
  2071. }
  2072. rcu_read_unlock();
  2073. }
  2074. /*
  2075. * This outputs /proc/net/fib_triestats
  2076. */
  2077. static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
  2078. {
  2079. unsigned int i, max, pointers, bytes, avdepth;
  2080. if (stat->leaves)
  2081. avdepth = stat->totdepth*100 / stat->leaves;
  2082. else
  2083. avdepth = 0;
  2084. seq_printf(seq, "\tAver depth: %u.%02d\n",
  2085. avdepth / 100, avdepth % 100);
  2086. seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
  2087. seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
  2088. bytes = LEAF_SIZE * stat->leaves;
  2089. seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
  2090. bytes += sizeof(struct fib_alias) * stat->prefixes;
  2091. seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
  2092. bytes += TNODE_SIZE(0) * stat->tnodes;
  2093. max = MAX_STAT_DEPTH;
  2094. while (max > 0 && stat->nodesizes[max-1] == 0)
  2095. max--;
  2096. pointers = 0;
  2097. for (i = 1; i < max; i++)
  2098. if (stat->nodesizes[i] != 0) {
  2099. seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
  2100. pointers += (1<<i) * stat->nodesizes[i];
  2101. }
  2102. seq_putc(seq, '\n');
  2103. seq_printf(seq, "\tPointers: %u\n", pointers);
  2104. bytes += sizeof(struct key_vector *) * pointers;
  2105. seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
  2106. seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
  2107. }
  2108. #ifdef CONFIG_IP_FIB_TRIE_STATS
  2109. static void trie_show_usage(struct seq_file *seq,
  2110. const struct trie_use_stats __percpu *stats)
  2111. {
  2112. struct trie_use_stats s = { 0 };
  2113. int cpu;
  2114. /* loop through all of the CPUs and gather up the stats */
  2115. for_each_possible_cpu(cpu) {
  2116. const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
  2117. s.gets += pcpu->gets;
  2118. s.backtrack += pcpu->backtrack;
  2119. s.semantic_match_passed += pcpu->semantic_match_passed;
  2120. s.semantic_match_miss += pcpu->semantic_match_miss;
  2121. s.null_node_hit += pcpu->null_node_hit;
  2122. s.resize_node_skipped += pcpu->resize_node_skipped;
  2123. }
  2124. seq_printf(seq, "\nCounters:\n---------\n");
  2125. seq_printf(seq, "gets = %u\n", s.gets);
  2126. seq_printf(seq, "backtracks = %u\n", s.backtrack);
  2127. seq_printf(seq, "semantic match passed = %u\n",
  2128. s.semantic_match_passed);
  2129. seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
  2130. seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
  2131. seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
  2132. }
  2133. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  2134. static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
  2135. {
  2136. if (tb->tb_id == RT_TABLE_LOCAL)
  2137. seq_puts(seq, "Local:\n");
  2138. else if (tb->tb_id == RT_TABLE_MAIN)
  2139. seq_puts(seq, "Main:\n");
  2140. else
  2141. seq_printf(seq, "Id %d:\n", tb->tb_id);
  2142. }
  2143. static int fib_triestat_seq_show(struct seq_file *seq, void *v)
  2144. {
  2145. struct net *net = (struct net *)seq->private;
  2146. unsigned int h;
  2147. seq_printf(seq,
  2148. "Basic info: size of leaf:"
  2149. " %zd bytes, size of tnode: %zd bytes.\n",
  2150. LEAF_SIZE, TNODE_SIZE(0));
  2151. rcu_read_lock();
  2152. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  2153. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2154. struct fib_table *tb;
  2155. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2156. struct trie *t = (struct trie *) tb->tb_data;
  2157. struct trie_stat stat;
  2158. if (!t)
  2159. continue;
  2160. fib_table_print(seq, tb);
  2161. trie_collect_stats(t, &stat);
  2162. trie_show_stats(seq, &stat);
  2163. #ifdef CONFIG_IP_FIB_TRIE_STATS
  2164. trie_show_usage(seq, t->stats);
  2165. #endif
  2166. }
  2167. cond_resched_rcu();
  2168. }
  2169. rcu_read_unlock();
  2170. return 0;
  2171. }
  2172. static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
  2173. {
  2174. struct fib_trie_iter *iter = seq->private;
  2175. struct net *net = seq_file_net(seq);
  2176. loff_t idx = 0;
  2177. unsigned int h;
  2178. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  2179. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2180. struct fib_table *tb;
  2181. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2182. struct key_vector *n;
  2183. for (n = fib_trie_get_first(iter,
  2184. (struct trie *) tb->tb_data);
  2185. n; n = fib_trie_get_next(iter))
  2186. if (pos == idx++) {
  2187. iter->tb = tb;
  2188. return n;
  2189. }
  2190. }
  2191. }
  2192. return NULL;
  2193. }
  2194. static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
  2195. __acquires(RCU)
  2196. {
  2197. rcu_read_lock();
  2198. return fib_trie_get_idx(seq, *pos);
  2199. }
  2200. static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2201. {
  2202. struct fib_trie_iter *iter = seq->private;
  2203. struct net *net = seq_file_net(seq);
  2204. struct fib_table *tb = iter->tb;
  2205. struct hlist_node *tb_node;
  2206. unsigned int h;
  2207. struct key_vector *n;
  2208. ++*pos;
  2209. /* next node in same table */
  2210. n = fib_trie_get_next(iter);
  2211. if (n)
  2212. return n;
  2213. /* walk rest of this hash chain */
  2214. h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
  2215. while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
  2216. tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
  2217. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  2218. if (n)
  2219. goto found;
  2220. }
  2221. /* new hash chain */
  2222. while (++h < FIB_TABLE_HASHSZ) {
  2223. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2224. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2225. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  2226. if (n)
  2227. goto found;
  2228. }
  2229. }
  2230. return NULL;
  2231. found:
  2232. iter->tb = tb;
  2233. return n;
  2234. }
  2235. static void fib_trie_seq_stop(struct seq_file *seq, void *v)
  2236. __releases(RCU)
  2237. {
  2238. rcu_read_unlock();
  2239. }
  2240. static void seq_indent(struct seq_file *seq, int n)
  2241. {
  2242. while (n-- > 0)
  2243. seq_puts(seq, " ");
  2244. }
  2245. static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
  2246. {
  2247. switch (s) {
  2248. case RT_SCOPE_UNIVERSE: return "universe";
  2249. case RT_SCOPE_SITE: return "site";
  2250. case RT_SCOPE_LINK: return "link";
  2251. case RT_SCOPE_HOST: return "host";
  2252. case RT_SCOPE_NOWHERE: return "nowhere";
  2253. default:
  2254. snprintf(buf, len, "scope=%d", s);
  2255. return buf;
  2256. }
  2257. }
  2258. static const char *const rtn_type_names[__RTN_MAX] = {
  2259. [RTN_UNSPEC] = "UNSPEC",
  2260. [RTN_UNICAST] = "UNICAST",
  2261. [RTN_LOCAL] = "LOCAL",
  2262. [RTN_BROADCAST] = "BROADCAST",
  2263. [RTN_ANYCAST] = "ANYCAST",
  2264. [RTN_MULTICAST] = "MULTICAST",
  2265. [RTN_BLACKHOLE] = "BLACKHOLE",
  2266. [RTN_UNREACHABLE] = "UNREACHABLE",
  2267. [RTN_PROHIBIT] = "PROHIBIT",
  2268. [RTN_THROW] = "THROW",
  2269. [RTN_NAT] = "NAT",
  2270. [RTN_XRESOLVE] = "XRESOLVE",
  2271. };
  2272. static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
  2273. {
  2274. if (t < __RTN_MAX && rtn_type_names[t])
  2275. return rtn_type_names[t];
  2276. snprintf(buf, len, "type %u", t);
  2277. return buf;
  2278. }
  2279. /* Pretty print the trie */
  2280. static int fib_trie_seq_show(struct seq_file *seq, void *v)
  2281. {
  2282. const struct fib_trie_iter *iter = seq->private;
  2283. struct key_vector *n = v;
  2284. if (IS_TRIE(node_parent_rcu(n)))
  2285. fib_table_print(seq, iter->tb);
  2286. if (IS_TNODE(n)) {
  2287. __be32 prf = htonl(n->key);
  2288. seq_indent(seq, iter->depth-1);
  2289. seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
  2290. &prf, KEYLENGTH - n->pos - n->bits, n->bits,
  2291. tn_info(n)->full_children,
  2292. tn_info(n)->empty_children);
  2293. } else {
  2294. __be32 val = htonl(n->key);
  2295. struct fib_alias *fa;
  2296. seq_indent(seq, iter->depth);
  2297. seq_printf(seq, " |-- %pI4\n", &val);
  2298. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  2299. char buf1[32], buf2[32];
  2300. seq_indent(seq, iter->depth + 1);
  2301. seq_printf(seq, " /%zu %s %s",
  2302. KEYLENGTH - fa->fa_slen,
  2303. rtn_scope(buf1, sizeof(buf1),
  2304. fa->fa_info->fib_scope),
  2305. rtn_type(buf2, sizeof(buf2),
  2306. fa->fa_type));
  2307. if (fa->fa_tos)
  2308. seq_printf(seq, " tos=%d", fa->fa_tos);
  2309. seq_putc(seq, '\n');
  2310. }
  2311. }
  2312. return 0;
  2313. }
  2314. static const struct seq_operations fib_trie_seq_ops = {
  2315. .start = fib_trie_seq_start,
  2316. .next = fib_trie_seq_next,
  2317. .stop = fib_trie_seq_stop,
  2318. .show = fib_trie_seq_show,
  2319. };
  2320. struct fib_route_iter {
  2321. struct seq_net_private p;
  2322. struct fib_table *main_tb;
  2323. struct key_vector *tnode;
  2324. loff_t pos;
  2325. t_key key;
  2326. };
  2327. static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
  2328. loff_t pos)
  2329. {
  2330. struct key_vector *l, **tp = &iter->tnode;
  2331. t_key key;
  2332. /* use cached location of previously found key */
  2333. if (iter->pos > 0 && pos >= iter->pos) {
  2334. key = iter->key;
  2335. } else {
  2336. iter->pos = 1;
  2337. key = 0;
  2338. }
  2339. pos -= iter->pos;
  2340. while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
  2341. key = l->key + 1;
  2342. iter->pos++;
  2343. l = NULL;
  2344. /* handle unlikely case of a key wrap */
  2345. if (!key)
  2346. break;
  2347. }
  2348. if (l)
  2349. iter->key = l->key; /* remember it */
  2350. else
  2351. iter->pos = 0; /* forget it */
  2352. return l;
  2353. }
  2354. static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
  2355. __acquires(RCU)
  2356. {
  2357. struct fib_route_iter *iter = seq->private;
  2358. struct fib_table *tb;
  2359. struct trie *t;
  2360. rcu_read_lock();
  2361. tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  2362. if (!tb)
  2363. return NULL;
  2364. iter->main_tb = tb;
  2365. t = (struct trie *)tb->tb_data;
  2366. iter->tnode = t->kv;
  2367. if (*pos != 0)
  2368. return fib_route_get_idx(iter, *pos);
  2369. iter->pos = 0;
  2370. iter->key = KEY_MAX;
  2371. return SEQ_START_TOKEN;
  2372. }
  2373. static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2374. {
  2375. struct fib_route_iter *iter = seq->private;
  2376. struct key_vector *l = NULL;
  2377. t_key key = iter->key + 1;
  2378. ++*pos;
  2379. /* only allow key of 0 for start of sequence */
  2380. if ((v == SEQ_START_TOKEN) || key)
  2381. l = leaf_walk_rcu(&iter->tnode, key);
  2382. if (l) {
  2383. iter->key = l->key;
  2384. iter->pos++;
  2385. } else {
  2386. iter->pos = 0;
  2387. }
  2388. return l;
  2389. }
  2390. static void fib_route_seq_stop(struct seq_file *seq, void *v)
  2391. __releases(RCU)
  2392. {
  2393. rcu_read_unlock();
  2394. }
  2395. static unsigned int fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  2396. {
  2397. unsigned int flags = 0;
  2398. if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
  2399. flags = RTF_REJECT;
  2400. if (fi) {
  2401. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  2402. if (nhc->nhc_gw.ipv4)
  2403. flags |= RTF_GATEWAY;
  2404. }
  2405. if (mask == htonl(0xFFFFFFFF))
  2406. flags |= RTF_HOST;
  2407. flags |= RTF_UP;
  2408. return flags;
  2409. }
  2410. /*
  2411. * This outputs /proc/net/route.
  2412. * The format of the file is not supposed to be changed
  2413. * and needs to be same as fib_hash output to avoid breaking
  2414. * legacy utilities
  2415. */
  2416. static int fib_route_seq_show(struct seq_file *seq, void *v)
  2417. {
  2418. struct fib_route_iter *iter = seq->private;
  2419. struct fib_table *tb = iter->main_tb;
  2420. struct fib_alias *fa;
  2421. struct key_vector *l = v;
  2422. __be32 prefix;
  2423. if (v == SEQ_START_TOKEN) {
  2424. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  2425. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  2426. "\tWindow\tIRTT");
  2427. return 0;
  2428. }
  2429. prefix = htonl(l->key);
  2430. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  2431. struct fib_info *fi = fa->fa_info;
  2432. __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
  2433. unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
  2434. if ((fa->fa_type == RTN_BROADCAST) ||
  2435. (fa->fa_type == RTN_MULTICAST))
  2436. continue;
  2437. if (fa->tb_id != tb->tb_id)
  2438. continue;
  2439. seq_setwidth(seq, 127);
  2440. if (fi) {
  2441. struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  2442. __be32 gw = 0;
  2443. if (nhc->nhc_gw_family == AF_INET)
  2444. gw = nhc->nhc_gw.ipv4;
  2445. seq_printf(seq,
  2446. "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
  2447. "%d\t%08X\t%d\t%u\t%u",
  2448. nhc->nhc_dev ? nhc->nhc_dev->name : "*",
  2449. prefix, gw, flags, 0, 0,
  2450. fi->fib_priority,
  2451. mask,
  2452. (fi->fib_advmss ?
  2453. fi->fib_advmss + 40 : 0),
  2454. fi->fib_window,
  2455. fi->fib_rtt >> 3);
  2456. } else {
  2457. seq_printf(seq,
  2458. "*\t%08X\t%08X\t%04X\t%d\t%u\t"
  2459. "%d\t%08X\t%d\t%u\t%u",
  2460. prefix, 0, flags, 0, 0, 0,
  2461. mask, 0, 0, 0);
  2462. }
  2463. seq_pad(seq, '\n');
  2464. }
  2465. return 0;
  2466. }
  2467. static const struct seq_operations fib_route_seq_ops = {
  2468. .start = fib_route_seq_start,
  2469. .next = fib_route_seq_next,
  2470. .stop = fib_route_seq_stop,
  2471. .show = fib_route_seq_show,
  2472. };
  2473. int __net_init fib_proc_init(struct net *net)
  2474. {
  2475. if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  2476. sizeof(struct fib_trie_iter)))
  2477. goto out1;
  2478. if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
  2479. fib_triestat_seq_show, NULL))
  2480. goto out2;
  2481. if (!proc_create_net("route", 0444, net->proc_net, &fib_route_seq_ops,
  2482. sizeof(struct fib_route_iter)))
  2483. goto out3;
  2484. return 0;
  2485. out3:
  2486. remove_proc_entry("fib_triestat", net->proc_net);
  2487. out2:
  2488. remove_proc_entry("fib_trie", net->proc_net);
  2489. out1:
  2490. return -ENOMEM;
  2491. }
  2492. void __net_exit fib_proc_exit(struct net *net)
  2493. {
  2494. remove_proc_entry("fib_trie", net->proc_net);
  2495. remove_proc_entry("fib_triestat", net->proc_net);
  2496. remove_proc_entry("route", net->proc_net);
  2497. }
  2498. #endif /* CONFIG_PROC_FS */