ip6_fib.c 46 KB

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