ip6_fib.c 46 KB

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