ip6_fib.c 45 KB

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