ip6_fib.c 46 KB

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