ip6_fib.c 45 KB

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