ip6_fib.c 45 KB

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