ip6_fib.c 45 KB

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