ip6_fib.c 44 KB

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