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