ip6_fib.c 46 KB

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