ip6_fib.c 44 KB

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