ip6_fib.c 44 KB

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