ip6_fib.c 56 KB

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