ip6_fib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*
  2. * Linux INET6 implementation
  3. * Forwarding Information Database
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /*
  16. * Changes:
  17. * Yuji SEKIYA @USAGI: Support default route on router node;
  18. * remove ip6_null_entry from the top of
  19. * routing table.
  20. * Ville Nuorvala: Fixed routing subtrees.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/net.h>
  25. #include <linux/route.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/in6.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #ifdef CONFIG_PROC_FS
  31. #include <linux/proc_fs.h>
  32. #endif
  33. #include <net/ipv6.h>
  34. #include <net/ndisc.h>
  35. #include <net/addrconf.h>
  36. #include <net/ip6_fib.h>
  37. #include <net/ip6_route.h>
  38. #define RT6_DEBUG 2
  39. #if RT6_DEBUG >= 3
  40. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  41. #else
  42. #define RT6_TRACE(x...) do { ; } while (0)
  43. #endif
  44. struct rt6_statistics rt6_stats;
  45. static struct kmem_cache * fib6_node_kmem __read_mostly;
  46. enum fib_walk_state_t
  47. {
  48. #ifdef CONFIG_IPV6_SUBTREES
  49. FWS_S,
  50. #endif
  51. FWS_L,
  52. FWS_R,
  53. FWS_C,
  54. FWS_U
  55. };
  56. struct fib6_cleaner_t
  57. {
  58. struct fib6_walker_t w;
  59. int (*func)(struct rt6_info *, void *arg);
  60. void *arg;
  61. };
  62. static DEFINE_RWLOCK(fib6_walker_lock);
  63. #ifdef CONFIG_IPV6_SUBTREES
  64. #define FWS_INIT FWS_S
  65. #else
  66. #define FWS_INIT FWS_L
  67. #endif
  68. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
  69. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn);
  70. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
  71. static int fib6_walk(struct fib6_walker_t *w);
  72. static int fib6_walk_continue(struct fib6_walker_t *w);
  73. /*
  74. * A routing update causes an increase of the serial number on the
  75. * affected subtree. This allows for cached routes to be asynchronously
  76. * tested when modifications are made to the destination cache as a
  77. * result of redirects, path MTU changes, etc.
  78. */
  79. static __u32 rt_sernum;
  80. static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
  81. static struct fib6_walker_t fib6_walker_list = {
  82. .prev = &fib6_walker_list,
  83. .next = &fib6_walker_list,
  84. };
  85. #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
  86. static inline void fib6_walker_link(struct fib6_walker_t *w)
  87. {
  88. write_lock_bh(&fib6_walker_lock);
  89. w->next = fib6_walker_list.next;
  90. w->prev = &fib6_walker_list;
  91. w->next->prev = w;
  92. w->prev->next = w;
  93. write_unlock_bh(&fib6_walker_lock);
  94. }
  95. static inline void fib6_walker_unlink(struct fib6_walker_t *w)
  96. {
  97. write_lock_bh(&fib6_walker_lock);
  98. w->next->prev = w->prev;
  99. w->prev->next = w->next;
  100. w->prev = w->next = w;
  101. write_unlock_bh(&fib6_walker_lock);
  102. }
  103. static __inline__ u32 fib6_new_sernum(void)
  104. {
  105. u32 n = ++rt_sernum;
  106. if ((__s32)n <= 0)
  107. rt_sernum = n = 1;
  108. return n;
  109. }
  110. /*
  111. * Auxiliary address test functions for the radix tree.
  112. *
  113. * These assume a 32bit processor (although it will work on
  114. * 64bit processors)
  115. */
  116. /*
  117. * test bit
  118. */
  119. static __inline__ __be32 addr_bit_set(void *token, int fn_bit)
  120. {
  121. __be32 *addr = token;
  122. return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
  123. }
  124. static __inline__ struct fib6_node * node_alloc(void)
  125. {
  126. struct fib6_node *fn;
  127. fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
  128. return fn;
  129. }
  130. static __inline__ void node_free(struct fib6_node * fn)
  131. {
  132. kmem_cache_free(fib6_node_kmem, fn);
  133. }
  134. static __inline__ void rt6_release(struct rt6_info *rt)
  135. {
  136. if (atomic_dec_and_test(&rt->rt6i_ref))
  137. dst_free(&rt->u.dst);
  138. }
  139. static struct fib6_table fib6_main_tbl = {
  140. .tb6_id = RT6_TABLE_MAIN,
  141. .tb6_root = {
  142. .leaf = &ip6_null_entry,
  143. .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
  144. },
  145. };
  146. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  147. #define FIB_TABLE_HASHSZ 256
  148. #else
  149. #define FIB_TABLE_HASHSZ 1
  150. #endif
  151. static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
  152. static void fib6_link_table(struct fib6_table *tb)
  153. {
  154. unsigned int h;
  155. /*
  156. * Initialize table lock at a single place to give lockdep a key,
  157. * tables aren't visible prior to being linked to the list.
  158. */
  159. rwlock_init(&tb->tb6_lock);
  160. h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
  161. /*
  162. * No protection necessary, this is the only list mutatation
  163. * operation, tables never disappear once they exist.
  164. */
  165. hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]);
  166. }
  167. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  168. static struct fib6_table fib6_local_tbl = {
  169. .tb6_id = RT6_TABLE_LOCAL,
  170. .tb6_root = {
  171. .leaf = &ip6_null_entry,
  172. .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
  173. },
  174. };
  175. static struct fib6_table *fib6_alloc_table(u32 id)
  176. {
  177. struct fib6_table *table;
  178. table = kzalloc(sizeof(*table), GFP_ATOMIC);
  179. if (table != NULL) {
  180. table->tb6_id = id;
  181. table->tb6_root.leaf = &ip6_null_entry;
  182. table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  183. }
  184. return table;
  185. }
  186. struct fib6_table *fib6_new_table(u32 id)
  187. {
  188. struct fib6_table *tb;
  189. if (id == 0)
  190. id = RT6_TABLE_MAIN;
  191. tb = fib6_get_table(id);
  192. if (tb)
  193. return tb;
  194. tb = fib6_alloc_table(id);
  195. if (tb != NULL)
  196. fib6_link_table(tb);
  197. return tb;
  198. }
  199. struct fib6_table *fib6_get_table(u32 id)
  200. {
  201. struct fib6_table *tb;
  202. struct hlist_node *node;
  203. unsigned int h;
  204. if (id == 0)
  205. id = RT6_TABLE_MAIN;
  206. h = id & (FIB_TABLE_HASHSZ - 1);
  207. rcu_read_lock();
  208. hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) {
  209. if (tb->tb6_id == id) {
  210. rcu_read_unlock();
  211. return tb;
  212. }
  213. }
  214. rcu_read_unlock();
  215. return NULL;
  216. }
  217. static void __init fib6_tables_init(void)
  218. {
  219. fib6_link_table(&fib6_main_tbl);
  220. fib6_link_table(&fib6_local_tbl);
  221. }
  222. #else
  223. struct fib6_table *fib6_new_table(u32 id)
  224. {
  225. return fib6_get_table(id);
  226. }
  227. struct fib6_table *fib6_get_table(u32 id)
  228. {
  229. return &fib6_main_tbl;
  230. }
  231. struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
  232. pol_lookup_t lookup)
  233. {
  234. return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags);
  235. }
  236. static void __init fib6_tables_init(void)
  237. {
  238. fib6_link_table(&fib6_main_tbl);
  239. }
  240. #endif
  241. static int fib6_dump_node(struct fib6_walker_t *w)
  242. {
  243. int res;
  244. struct rt6_info *rt;
  245. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  246. res = rt6_dump_route(rt, w->args);
  247. if (res < 0) {
  248. /* Frame is full, suspend walking */
  249. w->leaf = rt;
  250. return 1;
  251. }
  252. BUG_TRAP(res!=0);
  253. }
  254. w->leaf = NULL;
  255. return 0;
  256. }
  257. static void fib6_dump_end(struct netlink_callback *cb)
  258. {
  259. struct fib6_walker_t *w = (void*)cb->args[2];
  260. if (w) {
  261. cb->args[2] = 0;
  262. kfree(w);
  263. }
  264. cb->done = (void*)cb->args[3];
  265. cb->args[1] = 3;
  266. }
  267. static int fib6_dump_done(struct netlink_callback *cb)
  268. {
  269. fib6_dump_end(cb);
  270. return cb->done ? cb->done(cb) : 0;
  271. }
  272. static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
  273. struct netlink_callback *cb)
  274. {
  275. struct fib6_walker_t *w;
  276. int res;
  277. w = (void *)cb->args[2];
  278. w->root = &table->tb6_root;
  279. if (cb->args[4] == 0) {
  280. read_lock_bh(&table->tb6_lock);
  281. res = fib6_walk(w);
  282. read_unlock_bh(&table->tb6_lock);
  283. if (res > 0)
  284. cb->args[4] = 1;
  285. } else {
  286. read_lock_bh(&table->tb6_lock);
  287. res = fib6_walk_continue(w);
  288. read_unlock_bh(&table->tb6_lock);
  289. if (res != 0) {
  290. if (res < 0)
  291. fib6_walker_unlink(w);
  292. goto end;
  293. }
  294. fib6_walker_unlink(w);
  295. cb->args[4] = 0;
  296. }
  297. end:
  298. return res;
  299. }
  300. static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  301. {
  302. unsigned int h, s_h;
  303. unsigned int e = 0, s_e;
  304. struct rt6_rtnl_dump_arg arg;
  305. struct fib6_walker_t *w;
  306. struct fib6_table *tb;
  307. struct hlist_node *node;
  308. int res = 0;
  309. s_h = cb->args[0];
  310. s_e = cb->args[1];
  311. w = (void *)cb->args[2];
  312. if (w == NULL) {
  313. /* New dump:
  314. *
  315. * 1. hook callback destructor.
  316. */
  317. cb->args[3] = (long)cb->done;
  318. cb->done = fib6_dump_done;
  319. /*
  320. * 2. allocate and initialize walker.
  321. */
  322. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  323. if (w == NULL)
  324. return -ENOMEM;
  325. w->func = fib6_dump_node;
  326. cb->args[2] = (long)w;
  327. }
  328. arg.skb = skb;
  329. arg.cb = cb;
  330. w->args = &arg;
  331. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  332. e = 0;
  333. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
  334. if (e < s_e)
  335. goto next;
  336. res = fib6_dump_table(tb, skb, cb);
  337. if (res != 0)
  338. goto out;
  339. next:
  340. e++;
  341. }
  342. }
  343. out:
  344. cb->args[1] = e;
  345. cb->args[0] = h;
  346. res = res < 0 ? res : skb->len;
  347. if (res <= 0)
  348. fib6_dump_end(cb);
  349. return res;
  350. }
  351. /*
  352. * Routing Table
  353. *
  354. * return the appropriate node for a routing tree "add" operation
  355. * by either creating and inserting or by returning an existing
  356. * node.
  357. */
  358. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  359. int addrlen, int plen,
  360. int offset)
  361. {
  362. struct fib6_node *fn, *in, *ln;
  363. struct fib6_node *pn = NULL;
  364. struct rt6key *key;
  365. int bit;
  366. __be32 dir = 0;
  367. __u32 sernum = fib6_new_sernum();
  368. RT6_TRACE("fib6_add_1\n");
  369. /* insert node in tree */
  370. fn = root;
  371. do {
  372. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  373. /*
  374. * Prefix match
  375. */
  376. if (plen < fn->fn_bit ||
  377. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  378. goto insert_above;
  379. /*
  380. * Exact match ?
  381. */
  382. if (plen == fn->fn_bit) {
  383. /* clean up an intermediate node */
  384. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  385. rt6_release(fn->leaf);
  386. fn->leaf = NULL;
  387. }
  388. fn->fn_sernum = sernum;
  389. return fn;
  390. }
  391. /*
  392. * We have more bits to go
  393. */
  394. /* Try to walk down on tree. */
  395. fn->fn_sernum = sernum;
  396. dir = addr_bit_set(addr, fn->fn_bit);
  397. pn = fn;
  398. fn = dir ? fn->right: fn->left;
  399. } while (fn);
  400. /*
  401. * We walked to the bottom of tree.
  402. * Create new leaf node without children.
  403. */
  404. ln = node_alloc();
  405. if (ln == NULL)
  406. return NULL;
  407. ln->fn_bit = plen;
  408. ln->parent = pn;
  409. ln->fn_sernum = sernum;
  410. if (dir)
  411. pn->right = ln;
  412. else
  413. pn->left = ln;
  414. return ln;
  415. insert_above:
  416. /*
  417. * split since we don't have a common prefix anymore or
  418. * we have a less significant route.
  419. * we've to insert an intermediate node on the list
  420. * this new node will point to the one we need to create
  421. * and the current
  422. */
  423. pn = fn->parent;
  424. /* find 1st bit in difference between the 2 addrs.
  425. See comment in __ipv6_addr_diff: bit may be an invalid value,
  426. but if it is >= plen, the value is ignored in any case.
  427. */
  428. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  429. /*
  430. * (intermediate)[in]
  431. * / \
  432. * (new leaf node)[ln] (old node)[fn]
  433. */
  434. if (plen > bit) {
  435. in = node_alloc();
  436. ln = node_alloc();
  437. if (in == NULL || ln == NULL) {
  438. if (in)
  439. node_free(in);
  440. if (ln)
  441. node_free(ln);
  442. return NULL;
  443. }
  444. /*
  445. * new intermediate node.
  446. * RTN_RTINFO will
  447. * be off since that an address that chooses one of
  448. * the branches would not match less specific routes
  449. * in the other branch
  450. */
  451. in->fn_bit = bit;
  452. in->parent = pn;
  453. in->leaf = fn->leaf;
  454. atomic_inc(&in->leaf->rt6i_ref);
  455. in->fn_sernum = sernum;
  456. /* update parent pointer */
  457. if (dir)
  458. pn->right = in;
  459. else
  460. pn->left = in;
  461. ln->fn_bit = plen;
  462. ln->parent = in;
  463. fn->parent = in;
  464. ln->fn_sernum = sernum;
  465. if (addr_bit_set(addr, bit)) {
  466. in->right = ln;
  467. in->left = fn;
  468. } else {
  469. in->left = ln;
  470. in->right = fn;
  471. }
  472. } else { /* plen <= bit */
  473. /*
  474. * (new leaf node)[ln]
  475. * / \
  476. * (old node)[fn] NULL
  477. */
  478. ln = node_alloc();
  479. if (ln == NULL)
  480. return NULL;
  481. ln->fn_bit = plen;
  482. ln->parent = pn;
  483. ln->fn_sernum = sernum;
  484. if (dir)
  485. pn->right = ln;
  486. else
  487. pn->left = ln;
  488. if (addr_bit_set(&key->addr, plen))
  489. ln->right = fn;
  490. else
  491. ln->left = fn;
  492. fn->parent = ln;
  493. }
  494. return ln;
  495. }
  496. /*
  497. * Insert routing information in a node.
  498. */
  499. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  500. struct nl_info *info)
  501. {
  502. struct rt6_info *iter = NULL;
  503. struct rt6_info **ins;
  504. ins = &fn->leaf;
  505. if (fn->fn_flags&RTN_TL_ROOT &&
  506. fn->leaf == &ip6_null_entry &&
  507. !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
  508. fn->leaf = rt;
  509. rt->u.dst.rt6_next = NULL;
  510. goto out;
  511. }
  512. for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
  513. /*
  514. * Search for duplicates
  515. */
  516. if (iter->rt6i_metric == rt->rt6i_metric) {
  517. /*
  518. * Same priority level
  519. */
  520. if (iter->rt6i_dev == rt->rt6i_dev &&
  521. iter->rt6i_idev == rt->rt6i_idev &&
  522. ipv6_addr_equal(&iter->rt6i_gateway,
  523. &rt->rt6i_gateway)) {
  524. if (!(iter->rt6i_flags&RTF_EXPIRES))
  525. return -EEXIST;
  526. iter->rt6i_expires = rt->rt6i_expires;
  527. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  528. iter->rt6i_flags &= ~RTF_EXPIRES;
  529. iter->rt6i_expires = 0;
  530. }
  531. return -EEXIST;
  532. }
  533. }
  534. if (iter->rt6i_metric > rt->rt6i_metric)
  535. break;
  536. ins = &iter->u.dst.rt6_next;
  537. }
  538. /* Reset round-robin state, if necessary */
  539. if (ins == &fn->leaf)
  540. fn->rr_ptr = NULL;
  541. /*
  542. * insert node
  543. */
  544. out:
  545. rt->u.dst.rt6_next = iter;
  546. *ins = rt;
  547. rt->rt6i_node = fn;
  548. atomic_inc(&rt->rt6i_ref);
  549. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  550. rt6_stats.fib_rt_entries++;
  551. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  552. rt6_stats.fib_route_nodes++;
  553. fn->fn_flags |= RTN_RTINFO;
  554. }
  555. return 0;
  556. }
  557. static __inline__ void fib6_start_gc(struct rt6_info *rt)
  558. {
  559. if (ip6_fib_timer.expires == 0 &&
  560. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  561. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  562. }
  563. void fib6_force_start_gc(void)
  564. {
  565. if (ip6_fib_timer.expires == 0)
  566. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  567. }
  568. /*
  569. * Add routing information to the routing tree.
  570. * <destination addr>/<source addr>
  571. * with source addr info in sub-trees
  572. */
  573. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  574. {
  575. struct fib6_node *fn, *pn = NULL;
  576. int err = -ENOMEM;
  577. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  578. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  579. if (fn == NULL)
  580. goto out;
  581. pn = fn;
  582. #ifdef CONFIG_IPV6_SUBTREES
  583. if (rt->rt6i_src.plen) {
  584. struct fib6_node *sn;
  585. if (fn->subtree == NULL) {
  586. struct fib6_node *sfn;
  587. /*
  588. * Create subtree.
  589. *
  590. * fn[main tree]
  591. * |
  592. * sfn[subtree root]
  593. * \
  594. * sn[new leaf node]
  595. */
  596. /* Create subtree root node */
  597. sfn = node_alloc();
  598. if (sfn == NULL)
  599. goto st_failure;
  600. sfn->leaf = &ip6_null_entry;
  601. atomic_inc(&ip6_null_entry.rt6i_ref);
  602. sfn->fn_flags = RTN_ROOT;
  603. sfn->fn_sernum = fib6_new_sernum();
  604. /* Now add the first leaf node to new subtree */
  605. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  606. sizeof(struct in6_addr), rt->rt6i_src.plen,
  607. offsetof(struct rt6_info, rt6i_src));
  608. if (sn == NULL) {
  609. /* If it is failed, discard just allocated
  610. root, and then (in st_failure) stale node
  611. in main tree.
  612. */
  613. node_free(sfn);
  614. goto st_failure;
  615. }
  616. /* Now link new subtree to main tree */
  617. sfn->parent = fn;
  618. fn->subtree = sfn;
  619. } else {
  620. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  621. sizeof(struct in6_addr), rt->rt6i_src.plen,
  622. offsetof(struct rt6_info, rt6i_src));
  623. if (sn == NULL)
  624. goto st_failure;
  625. }
  626. if (fn->leaf == NULL) {
  627. fn->leaf = rt;
  628. atomic_inc(&rt->rt6i_ref);
  629. }
  630. fn = sn;
  631. }
  632. #endif
  633. err = fib6_add_rt2node(fn, rt, info);
  634. if (err == 0) {
  635. fib6_start_gc(rt);
  636. if (!(rt->rt6i_flags&RTF_CACHE))
  637. fib6_prune_clones(pn, rt);
  638. }
  639. out:
  640. if (err) {
  641. #ifdef CONFIG_IPV6_SUBTREES
  642. /*
  643. * If fib6_add_1 has cleared the old leaf pointer in the
  644. * super-tree leaf node we have to find a new one for it.
  645. */
  646. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  647. pn->leaf = fib6_find_prefix(pn);
  648. #if RT6_DEBUG >= 2
  649. if (!pn->leaf) {
  650. BUG_TRAP(pn->leaf != NULL);
  651. pn->leaf = &ip6_null_entry;
  652. }
  653. #endif
  654. atomic_inc(&pn->leaf->rt6i_ref);
  655. }
  656. #endif
  657. dst_free(&rt->u.dst);
  658. }
  659. return err;
  660. #ifdef CONFIG_IPV6_SUBTREES
  661. /* Subtree creation failed, probably main tree node
  662. is orphan. If it is, shoot it.
  663. */
  664. st_failure:
  665. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  666. fib6_repair_tree(fn);
  667. dst_free(&rt->u.dst);
  668. return err;
  669. #endif
  670. }
  671. /*
  672. * Routing tree lookup
  673. *
  674. */
  675. struct lookup_args {
  676. int offset; /* key offset on rt6_info */
  677. struct in6_addr *addr; /* search key */
  678. };
  679. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  680. struct lookup_args *args)
  681. {
  682. struct fib6_node *fn;
  683. __be32 dir;
  684. if (unlikely(args->offset == 0))
  685. return NULL;
  686. /*
  687. * Descend on a tree
  688. */
  689. fn = root;
  690. for (;;) {
  691. struct fib6_node *next;
  692. dir = addr_bit_set(args->addr, fn->fn_bit);
  693. next = dir ? fn->right : fn->left;
  694. if (next) {
  695. fn = next;
  696. continue;
  697. }
  698. break;
  699. }
  700. while(fn) {
  701. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  702. struct rt6key *key;
  703. key = (struct rt6key *) ((u8 *) fn->leaf +
  704. args->offset);
  705. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  706. #ifdef CONFIG_IPV6_SUBTREES
  707. if (fn->subtree)
  708. fn = fib6_lookup_1(fn->subtree, args + 1);
  709. #endif
  710. if (!fn || fn->fn_flags & RTN_RTINFO)
  711. return fn;
  712. }
  713. }
  714. if (fn->fn_flags & RTN_ROOT)
  715. break;
  716. fn = fn->parent;
  717. }
  718. return NULL;
  719. }
  720. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  721. struct in6_addr *saddr)
  722. {
  723. struct fib6_node *fn;
  724. struct lookup_args args[] = {
  725. {
  726. .offset = offsetof(struct rt6_info, rt6i_dst),
  727. .addr = daddr,
  728. },
  729. #ifdef CONFIG_IPV6_SUBTREES
  730. {
  731. .offset = offsetof(struct rt6_info, rt6i_src),
  732. .addr = saddr,
  733. },
  734. #endif
  735. {
  736. .offset = 0, /* sentinel */
  737. }
  738. };
  739. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  740. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  741. fn = root;
  742. return fn;
  743. }
  744. /*
  745. * Get node with specified destination prefix (and source prefix,
  746. * if subtrees are used)
  747. */
  748. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  749. struct in6_addr *addr,
  750. int plen, int offset)
  751. {
  752. struct fib6_node *fn;
  753. for (fn = root; fn ; ) {
  754. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  755. /*
  756. * Prefix match
  757. */
  758. if (plen < fn->fn_bit ||
  759. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  760. return NULL;
  761. if (plen == fn->fn_bit)
  762. return fn;
  763. /*
  764. * We have more bits to go
  765. */
  766. if (addr_bit_set(addr, fn->fn_bit))
  767. fn = fn->right;
  768. else
  769. fn = fn->left;
  770. }
  771. return NULL;
  772. }
  773. struct fib6_node * fib6_locate(struct fib6_node *root,
  774. struct in6_addr *daddr, int dst_len,
  775. struct in6_addr *saddr, int src_len)
  776. {
  777. struct fib6_node *fn;
  778. fn = fib6_locate_1(root, daddr, dst_len,
  779. offsetof(struct rt6_info, rt6i_dst));
  780. #ifdef CONFIG_IPV6_SUBTREES
  781. if (src_len) {
  782. BUG_TRAP(saddr!=NULL);
  783. if (fn && fn->subtree)
  784. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  785. offsetof(struct rt6_info, rt6i_src));
  786. }
  787. #endif
  788. if (fn && fn->fn_flags&RTN_RTINFO)
  789. return fn;
  790. return NULL;
  791. }
  792. /*
  793. * Deletion
  794. *
  795. */
  796. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  797. {
  798. if (fn->fn_flags&RTN_ROOT)
  799. return &ip6_null_entry;
  800. while(fn) {
  801. if(fn->left)
  802. return fn->left->leaf;
  803. if(fn->right)
  804. return fn->right->leaf;
  805. fn = FIB6_SUBTREE(fn);
  806. }
  807. return NULL;
  808. }
  809. /*
  810. * Called to trim the tree of intermediate nodes when possible. "fn"
  811. * is the node we want to try and remove.
  812. */
  813. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
  814. {
  815. int children;
  816. int nstate;
  817. struct fib6_node *child, *pn;
  818. struct fib6_walker_t *w;
  819. int iter = 0;
  820. for (;;) {
  821. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  822. iter++;
  823. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  824. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  825. BUG_TRAP(fn->leaf==NULL);
  826. children = 0;
  827. child = NULL;
  828. if (fn->right) child = fn->right, children |= 1;
  829. if (fn->left) child = fn->left, children |= 2;
  830. if (children == 3 || FIB6_SUBTREE(fn)
  831. #ifdef CONFIG_IPV6_SUBTREES
  832. /* Subtree root (i.e. fn) may have one child */
  833. || (children && fn->fn_flags&RTN_ROOT)
  834. #endif
  835. ) {
  836. fn->leaf = fib6_find_prefix(fn);
  837. #if RT6_DEBUG >= 2
  838. if (fn->leaf==NULL) {
  839. BUG_TRAP(fn->leaf);
  840. fn->leaf = &ip6_null_entry;
  841. }
  842. #endif
  843. atomic_inc(&fn->leaf->rt6i_ref);
  844. return fn->parent;
  845. }
  846. pn = fn->parent;
  847. #ifdef CONFIG_IPV6_SUBTREES
  848. if (FIB6_SUBTREE(pn) == fn) {
  849. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  850. FIB6_SUBTREE(pn) = NULL;
  851. nstate = FWS_L;
  852. } else {
  853. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  854. #endif
  855. if (pn->right == fn) pn->right = child;
  856. else if (pn->left == fn) pn->left = child;
  857. #if RT6_DEBUG >= 2
  858. else BUG_TRAP(0);
  859. #endif
  860. if (child)
  861. child->parent = pn;
  862. nstate = FWS_R;
  863. #ifdef CONFIG_IPV6_SUBTREES
  864. }
  865. #endif
  866. read_lock(&fib6_walker_lock);
  867. FOR_WALKERS(w) {
  868. if (child == NULL) {
  869. if (w->root == fn) {
  870. w->root = w->node = NULL;
  871. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  872. } else if (w->node == fn) {
  873. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  874. w->node = pn;
  875. w->state = nstate;
  876. }
  877. } else {
  878. if (w->root == fn) {
  879. w->root = child;
  880. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  881. }
  882. if (w->node == fn) {
  883. w->node = child;
  884. if (children&2) {
  885. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  886. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  887. } else {
  888. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  889. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  890. }
  891. }
  892. }
  893. }
  894. read_unlock(&fib6_walker_lock);
  895. node_free(fn);
  896. if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
  897. return pn;
  898. rt6_release(pn->leaf);
  899. pn->leaf = NULL;
  900. fn = pn;
  901. }
  902. }
  903. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  904. struct nl_info *info)
  905. {
  906. struct fib6_walker_t *w;
  907. struct rt6_info *rt = *rtp;
  908. RT6_TRACE("fib6_del_route\n");
  909. /* Unlink it */
  910. *rtp = rt->u.dst.rt6_next;
  911. rt->rt6i_node = NULL;
  912. rt6_stats.fib_rt_entries--;
  913. rt6_stats.fib_discarded_routes++;
  914. /* Reset round-robin state, if necessary */
  915. if (fn->rr_ptr == rt)
  916. fn->rr_ptr = NULL;
  917. /* Adjust walkers */
  918. read_lock(&fib6_walker_lock);
  919. FOR_WALKERS(w) {
  920. if (w->state == FWS_C && w->leaf == rt) {
  921. RT6_TRACE("walker %p adjusted by delroute\n", w);
  922. w->leaf = rt->u.dst.rt6_next;
  923. if (w->leaf == NULL)
  924. w->state = FWS_U;
  925. }
  926. }
  927. read_unlock(&fib6_walker_lock);
  928. rt->u.dst.rt6_next = NULL;
  929. if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
  930. fn->leaf = &ip6_null_entry;
  931. /* If it was last route, expunge its radix tree node */
  932. if (fn->leaf == NULL) {
  933. fn->fn_flags &= ~RTN_RTINFO;
  934. rt6_stats.fib_route_nodes--;
  935. fn = fib6_repair_tree(fn);
  936. }
  937. if (atomic_read(&rt->rt6i_ref) != 1) {
  938. /* This route is used as dummy address holder in some split
  939. * nodes. It is not leaked, but it still holds other resources,
  940. * which must be released in time. So, scan ascendant nodes
  941. * and replace dummy references to this route with references
  942. * to still alive ones.
  943. */
  944. while (fn) {
  945. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  946. fn->leaf = fib6_find_prefix(fn);
  947. atomic_inc(&fn->leaf->rt6i_ref);
  948. rt6_release(rt);
  949. }
  950. fn = fn->parent;
  951. }
  952. /* No more references are possible at this point. */
  953. if (atomic_read(&rt->rt6i_ref) != 1) BUG();
  954. }
  955. inet6_rt_notify(RTM_DELROUTE, rt, info);
  956. rt6_release(rt);
  957. }
  958. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  959. {
  960. struct fib6_node *fn = rt->rt6i_node;
  961. struct rt6_info **rtp;
  962. #if RT6_DEBUG >= 2
  963. if (rt->u.dst.obsolete>0) {
  964. BUG_TRAP(fn==NULL);
  965. return -ENOENT;
  966. }
  967. #endif
  968. if (fn == NULL || rt == &ip6_null_entry)
  969. return -ENOENT;
  970. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  971. if (!(rt->rt6i_flags&RTF_CACHE)) {
  972. struct fib6_node *pn = fn;
  973. #ifdef CONFIG_IPV6_SUBTREES
  974. /* clones of this route might be in another subtree */
  975. if (rt->rt6i_src.plen) {
  976. while (!(pn->fn_flags&RTN_ROOT))
  977. pn = pn->parent;
  978. pn = pn->parent;
  979. }
  980. #endif
  981. fib6_prune_clones(pn, rt);
  982. }
  983. /*
  984. * Walk the leaf entries looking for ourself
  985. */
  986. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) {
  987. if (*rtp == rt) {
  988. fib6_del_route(fn, rtp, info);
  989. return 0;
  990. }
  991. }
  992. return -ENOENT;
  993. }
  994. /*
  995. * Tree traversal function.
  996. *
  997. * Certainly, it is not interrupt safe.
  998. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  999. * It means, that we can modify tree during walking
  1000. * and use this function for garbage collection, clone pruning,
  1001. * cleaning tree when a device goes down etc. etc.
  1002. *
  1003. * It guarantees that every node will be traversed,
  1004. * and that it will be traversed only once.
  1005. *
  1006. * Callback function w->func may return:
  1007. * 0 -> continue walking.
  1008. * positive value -> walking is suspended (used by tree dumps,
  1009. * and probably by gc, if it will be split to several slices)
  1010. * negative value -> terminate walking.
  1011. *
  1012. * The function itself returns:
  1013. * 0 -> walk is complete.
  1014. * >0 -> walk is incomplete (i.e. suspended)
  1015. * <0 -> walk is terminated by an error.
  1016. */
  1017. static int fib6_walk_continue(struct fib6_walker_t *w)
  1018. {
  1019. struct fib6_node *fn, *pn;
  1020. for (;;) {
  1021. fn = w->node;
  1022. if (fn == NULL)
  1023. return 0;
  1024. if (w->prune && fn != w->root &&
  1025. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  1026. w->state = FWS_C;
  1027. w->leaf = fn->leaf;
  1028. }
  1029. switch (w->state) {
  1030. #ifdef CONFIG_IPV6_SUBTREES
  1031. case FWS_S:
  1032. if (FIB6_SUBTREE(fn)) {
  1033. w->node = FIB6_SUBTREE(fn);
  1034. continue;
  1035. }
  1036. w->state = FWS_L;
  1037. #endif
  1038. case FWS_L:
  1039. if (fn->left) {
  1040. w->node = fn->left;
  1041. w->state = FWS_INIT;
  1042. continue;
  1043. }
  1044. w->state = FWS_R;
  1045. case FWS_R:
  1046. if (fn->right) {
  1047. w->node = fn->right;
  1048. w->state = FWS_INIT;
  1049. continue;
  1050. }
  1051. w->state = FWS_C;
  1052. w->leaf = fn->leaf;
  1053. case FWS_C:
  1054. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  1055. int err = w->func(w);
  1056. if (err)
  1057. return err;
  1058. continue;
  1059. }
  1060. w->state = FWS_U;
  1061. case FWS_U:
  1062. if (fn == w->root)
  1063. return 0;
  1064. pn = fn->parent;
  1065. w->node = pn;
  1066. #ifdef CONFIG_IPV6_SUBTREES
  1067. if (FIB6_SUBTREE(pn) == fn) {
  1068. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  1069. w->state = FWS_L;
  1070. continue;
  1071. }
  1072. #endif
  1073. if (pn->left == fn) {
  1074. w->state = FWS_R;
  1075. continue;
  1076. }
  1077. if (pn->right == fn) {
  1078. w->state = FWS_C;
  1079. w->leaf = w->node->leaf;
  1080. continue;
  1081. }
  1082. #if RT6_DEBUG >= 2
  1083. BUG_TRAP(0);
  1084. #endif
  1085. }
  1086. }
  1087. }
  1088. static int fib6_walk(struct fib6_walker_t *w)
  1089. {
  1090. int res;
  1091. w->state = FWS_INIT;
  1092. w->node = w->root;
  1093. fib6_walker_link(w);
  1094. res = fib6_walk_continue(w);
  1095. if (res <= 0)
  1096. fib6_walker_unlink(w);
  1097. return res;
  1098. }
  1099. static int fib6_clean_node(struct fib6_walker_t *w)
  1100. {
  1101. int res;
  1102. struct rt6_info *rt;
  1103. struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
  1104. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  1105. res = c->func(rt, c->arg);
  1106. if (res < 0) {
  1107. w->leaf = rt;
  1108. res = fib6_del(rt, NULL);
  1109. if (res) {
  1110. #if RT6_DEBUG >= 2
  1111. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  1112. #endif
  1113. continue;
  1114. }
  1115. return 0;
  1116. }
  1117. BUG_TRAP(res==0);
  1118. }
  1119. w->leaf = rt;
  1120. return 0;
  1121. }
  1122. /*
  1123. * Convenient frontend to tree walker.
  1124. *
  1125. * func is called on each route.
  1126. * It may return -1 -> delete this route.
  1127. * 0 -> continue walking
  1128. *
  1129. * prune==1 -> only immediate children of node (certainly,
  1130. * ignoring pure split nodes) will be scanned.
  1131. */
  1132. static void fib6_clean_tree(struct fib6_node *root,
  1133. int (*func)(struct rt6_info *, void *arg),
  1134. int prune, void *arg)
  1135. {
  1136. struct fib6_cleaner_t c;
  1137. c.w.root = root;
  1138. c.w.func = fib6_clean_node;
  1139. c.w.prune = prune;
  1140. c.func = func;
  1141. c.arg = arg;
  1142. fib6_walk(&c.w);
  1143. }
  1144. void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
  1145. int prune, void *arg)
  1146. {
  1147. struct fib6_table *table;
  1148. struct hlist_node *node;
  1149. unsigned int h;
  1150. rcu_read_lock();
  1151. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1152. hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
  1153. tb6_hlist) {
  1154. write_lock_bh(&table->tb6_lock);
  1155. fib6_clean_tree(&table->tb6_root, func, prune, arg);
  1156. write_unlock_bh(&table->tb6_lock);
  1157. }
  1158. }
  1159. rcu_read_unlock();
  1160. }
  1161. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1162. {
  1163. if (rt->rt6i_flags & RTF_CACHE) {
  1164. RT6_TRACE("pruning clone %p\n", rt);
  1165. return -1;
  1166. }
  1167. return 0;
  1168. }
  1169. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
  1170. {
  1171. fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
  1172. }
  1173. /*
  1174. * Garbage collection
  1175. */
  1176. static struct fib6_gc_args
  1177. {
  1178. int timeout;
  1179. int more;
  1180. } gc_args;
  1181. static int fib6_age(struct rt6_info *rt, void *arg)
  1182. {
  1183. unsigned long now = jiffies;
  1184. /*
  1185. * check addrconf expiration here.
  1186. * Routes are expired even if they are in use.
  1187. *
  1188. * Also age clones. Note, that clones are aged out
  1189. * only if they are not in use now.
  1190. */
  1191. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  1192. if (time_after(now, rt->rt6i_expires)) {
  1193. RT6_TRACE("expiring %p\n", rt);
  1194. return -1;
  1195. }
  1196. gc_args.more++;
  1197. } else if (rt->rt6i_flags & RTF_CACHE) {
  1198. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  1199. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  1200. RT6_TRACE("aging clone %p\n", rt);
  1201. return -1;
  1202. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  1203. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  1204. RT6_TRACE("purging route %p via non-router but gateway\n",
  1205. rt);
  1206. return -1;
  1207. }
  1208. gc_args.more++;
  1209. }
  1210. return 0;
  1211. }
  1212. static DEFINE_SPINLOCK(fib6_gc_lock);
  1213. void fib6_run_gc(unsigned long dummy)
  1214. {
  1215. if (dummy != ~0UL) {
  1216. spin_lock_bh(&fib6_gc_lock);
  1217. gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
  1218. } else {
  1219. local_bh_disable();
  1220. if (!spin_trylock(&fib6_gc_lock)) {
  1221. mod_timer(&ip6_fib_timer, jiffies + HZ);
  1222. local_bh_enable();
  1223. return;
  1224. }
  1225. gc_args.timeout = ip6_rt_gc_interval;
  1226. }
  1227. gc_args.more = 0;
  1228. ndisc_dst_gc(&gc_args.more);
  1229. fib6_clean_all(fib6_age, 0, NULL);
  1230. if (gc_args.more)
  1231. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  1232. else {
  1233. del_timer(&ip6_fib_timer);
  1234. ip6_fib_timer.expires = 0;
  1235. }
  1236. spin_unlock_bh(&fib6_gc_lock);
  1237. }
  1238. void __init fib6_init(void)
  1239. {
  1240. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1241. sizeof(struct fib6_node),
  1242. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1243. NULL, NULL);
  1244. fib6_tables_init();
  1245. __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
  1246. }
  1247. void fib6_gc_cleanup(void)
  1248. {
  1249. del_timer(&ip6_fib_timer);
  1250. kmem_cache_destroy(fib6_node_kmem);
  1251. }