ip6_fib.c 55 KB

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