ip6_fib.c 45 KB

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