ip6_fib.c 45 KB

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