ip6_fib.c 44 KB

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