ip6_fib.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. struct net *net = skb->sk->sk_net;
  303. unsigned int h, s_h;
  304. unsigned int e = 0, s_e;
  305. struct rt6_rtnl_dump_arg arg;
  306. struct fib6_walker_t *w;
  307. struct fib6_table *tb;
  308. struct hlist_node *node;
  309. int res = 0;
  310. if (net != &init_net)
  311. return 0;
  312. s_h = cb->args[0];
  313. s_e = cb->args[1];
  314. w = (void *)cb->args[2];
  315. if (w == NULL) {
  316. /* New dump:
  317. *
  318. * 1. hook callback destructor.
  319. */
  320. cb->args[3] = (long)cb->done;
  321. cb->done = fib6_dump_done;
  322. /*
  323. * 2. allocate and initialize walker.
  324. */
  325. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  326. if (w == NULL)
  327. return -ENOMEM;
  328. w->func = fib6_dump_node;
  329. cb->args[2] = (long)w;
  330. }
  331. arg.skb = skb;
  332. arg.cb = cb;
  333. w->args = &arg;
  334. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  335. e = 0;
  336. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
  337. if (e < s_e)
  338. goto next;
  339. res = fib6_dump_table(tb, skb, cb);
  340. if (res != 0)
  341. goto out;
  342. next:
  343. e++;
  344. }
  345. }
  346. out:
  347. cb->args[1] = e;
  348. cb->args[0] = h;
  349. res = res < 0 ? res : skb->len;
  350. if (res <= 0)
  351. fib6_dump_end(cb);
  352. return res;
  353. }
  354. /*
  355. * Routing Table
  356. *
  357. * return the appropriate node for a routing tree "add" operation
  358. * by either creating and inserting or by returning an existing
  359. * node.
  360. */
  361. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  362. int addrlen, int plen,
  363. int offset)
  364. {
  365. struct fib6_node *fn, *in, *ln;
  366. struct fib6_node *pn = NULL;
  367. struct rt6key *key;
  368. int bit;
  369. __be32 dir = 0;
  370. __u32 sernum = fib6_new_sernum();
  371. RT6_TRACE("fib6_add_1\n");
  372. /* insert node in tree */
  373. fn = root;
  374. do {
  375. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  376. /*
  377. * Prefix match
  378. */
  379. if (plen < fn->fn_bit ||
  380. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  381. goto insert_above;
  382. /*
  383. * Exact match ?
  384. */
  385. if (plen == fn->fn_bit) {
  386. /* clean up an intermediate node */
  387. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  388. rt6_release(fn->leaf);
  389. fn->leaf = NULL;
  390. }
  391. fn->fn_sernum = sernum;
  392. return fn;
  393. }
  394. /*
  395. * We have more bits to go
  396. */
  397. /* Try to walk down on tree. */
  398. fn->fn_sernum = sernum;
  399. dir = addr_bit_set(addr, fn->fn_bit);
  400. pn = fn;
  401. fn = dir ? fn->right: fn->left;
  402. } while (fn);
  403. /*
  404. * We walked to the bottom of tree.
  405. * Create new leaf node without children.
  406. */
  407. ln = node_alloc();
  408. if (ln == NULL)
  409. return NULL;
  410. ln->fn_bit = plen;
  411. ln->parent = pn;
  412. ln->fn_sernum = sernum;
  413. if (dir)
  414. pn->right = ln;
  415. else
  416. pn->left = ln;
  417. return ln;
  418. insert_above:
  419. /*
  420. * split since we don't have a common prefix anymore or
  421. * we have a less significant route.
  422. * we've to insert an intermediate node on the list
  423. * this new node will point to the one we need to create
  424. * and the current
  425. */
  426. pn = fn->parent;
  427. /* find 1st bit in difference between the 2 addrs.
  428. See comment in __ipv6_addr_diff: bit may be an invalid value,
  429. but if it is >= plen, the value is ignored in any case.
  430. */
  431. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  432. /*
  433. * (intermediate)[in]
  434. * / \
  435. * (new leaf node)[ln] (old node)[fn]
  436. */
  437. if (plen > bit) {
  438. in = node_alloc();
  439. ln = node_alloc();
  440. if (in == NULL || ln == NULL) {
  441. if (in)
  442. node_free(in);
  443. if (ln)
  444. node_free(ln);
  445. return NULL;
  446. }
  447. /*
  448. * new intermediate node.
  449. * RTN_RTINFO will
  450. * be off since that an address that chooses one of
  451. * the branches would not match less specific routes
  452. * in the other branch
  453. */
  454. in->fn_bit = bit;
  455. in->parent = pn;
  456. in->leaf = fn->leaf;
  457. atomic_inc(&in->leaf->rt6i_ref);
  458. in->fn_sernum = sernum;
  459. /* update parent pointer */
  460. if (dir)
  461. pn->right = in;
  462. else
  463. pn->left = in;
  464. ln->fn_bit = plen;
  465. ln->parent = in;
  466. fn->parent = in;
  467. ln->fn_sernum = sernum;
  468. if (addr_bit_set(addr, bit)) {
  469. in->right = ln;
  470. in->left = fn;
  471. } else {
  472. in->left = ln;
  473. in->right = fn;
  474. }
  475. } else { /* plen <= bit */
  476. /*
  477. * (new leaf node)[ln]
  478. * / \
  479. * (old node)[fn] NULL
  480. */
  481. ln = node_alloc();
  482. if (ln == NULL)
  483. return NULL;
  484. ln->fn_bit = plen;
  485. ln->parent = pn;
  486. ln->fn_sernum = sernum;
  487. if (dir)
  488. pn->right = ln;
  489. else
  490. pn->left = ln;
  491. if (addr_bit_set(&key->addr, plen))
  492. ln->right = fn;
  493. else
  494. ln->left = fn;
  495. fn->parent = ln;
  496. }
  497. return ln;
  498. }
  499. /*
  500. * Insert routing information in a node.
  501. */
  502. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  503. struct nl_info *info)
  504. {
  505. struct rt6_info *iter = NULL;
  506. struct rt6_info **ins;
  507. ins = &fn->leaf;
  508. for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
  509. /*
  510. * Search for duplicates
  511. */
  512. if (iter->rt6i_metric == rt->rt6i_metric) {
  513. /*
  514. * Same priority level
  515. */
  516. if (iter->rt6i_dev == rt->rt6i_dev &&
  517. iter->rt6i_idev == rt->rt6i_idev &&
  518. ipv6_addr_equal(&iter->rt6i_gateway,
  519. &rt->rt6i_gateway)) {
  520. if (!(iter->rt6i_flags&RTF_EXPIRES))
  521. return -EEXIST;
  522. iter->rt6i_expires = rt->rt6i_expires;
  523. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  524. iter->rt6i_flags &= ~RTF_EXPIRES;
  525. iter->rt6i_expires = 0;
  526. }
  527. return -EEXIST;
  528. }
  529. }
  530. if (iter->rt6i_metric > rt->rt6i_metric)
  531. break;
  532. ins = &iter->u.dst.rt6_next;
  533. }
  534. /* Reset round-robin state, if necessary */
  535. if (ins == &fn->leaf)
  536. fn->rr_ptr = NULL;
  537. /*
  538. * insert node
  539. */
  540. rt->u.dst.rt6_next = iter;
  541. *ins = rt;
  542. rt->rt6i_node = fn;
  543. atomic_inc(&rt->rt6i_ref);
  544. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  545. rt6_stats.fib_rt_entries++;
  546. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  547. rt6_stats.fib_route_nodes++;
  548. fn->fn_flags |= RTN_RTINFO;
  549. }
  550. return 0;
  551. }
  552. static __inline__ void fib6_start_gc(struct rt6_info *rt)
  553. {
  554. if (ip6_fib_timer.expires == 0 &&
  555. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  556. mod_timer(&ip6_fib_timer, jiffies +
  557. init_net.ipv6.sysctl.ip6_rt_gc_interval);
  558. }
  559. void fib6_force_start_gc(void)
  560. {
  561. if (ip6_fib_timer.expires == 0)
  562. mod_timer(&ip6_fib_timer, jiffies +
  563. init_net.ipv6.sysctl.ip6_rt_gc_interval);
  564. }
  565. /*
  566. * Add routing information to the routing tree.
  567. * <destination addr>/<source addr>
  568. * with source addr info in sub-trees
  569. */
  570. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  571. {
  572. struct fib6_node *fn, *pn = NULL;
  573. int err = -ENOMEM;
  574. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  575. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  576. if (fn == NULL)
  577. goto out;
  578. pn = fn;
  579. #ifdef CONFIG_IPV6_SUBTREES
  580. if (rt->rt6i_src.plen) {
  581. struct fib6_node *sn;
  582. if (fn->subtree == NULL) {
  583. struct fib6_node *sfn;
  584. /*
  585. * Create subtree.
  586. *
  587. * fn[main tree]
  588. * |
  589. * sfn[subtree root]
  590. * \
  591. * sn[new leaf node]
  592. */
  593. /* Create subtree root node */
  594. sfn = node_alloc();
  595. if (sfn == NULL)
  596. goto st_failure;
  597. sfn->leaf = &ip6_null_entry;
  598. atomic_inc(&ip6_null_entry.rt6i_ref);
  599. sfn->fn_flags = RTN_ROOT;
  600. sfn->fn_sernum = fib6_new_sernum();
  601. /* Now add the first leaf node to new subtree */
  602. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  603. sizeof(struct in6_addr), rt->rt6i_src.plen,
  604. offsetof(struct rt6_info, rt6i_src));
  605. if (sn == NULL) {
  606. /* If it is failed, discard just allocated
  607. root, and then (in st_failure) stale node
  608. in main tree.
  609. */
  610. node_free(sfn);
  611. goto st_failure;
  612. }
  613. /* Now link new subtree to main tree */
  614. sfn->parent = fn;
  615. fn->subtree = sfn;
  616. } else {
  617. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  618. sizeof(struct in6_addr), rt->rt6i_src.plen,
  619. offsetof(struct rt6_info, rt6i_src));
  620. if (sn == NULL)
  621. goto st_failure;
  622. }
  623. if (fn->leaf == NULL) {
  624. fn->leaf = rt;
  625. atomic_inc(&rt->rt6i_ref);
  626. }
  627. fn = sn;
  628. }
  629. #endif
  630. err = fib6_add_rt2node(fn, rt, info);
  631. if (err == 0) {
  632. fib6_start_gc(rt);
  633. if (!(rt->rt6i_flags&RTF_CACHE))
  634. fib6_prune_clones(pn, rt);
  635. }
  636. out:
  637. if (err) {
  638. #ifdef CONFIG_IPV6_SUBTREES
  639. /*
  640. * If fib6_add_1 has cleared the old leaf pointer in the
  641. * super-tree leaf node we have to find a new one for it.
  642. */
  643. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  644. pn->leaf = fib6_find_prefix(pn);
  645. #if RT6_DEBUG >= 2
  646. if (!pn->leaf) {
  647. BUG_TRAP(pn->leaf != NULL);
  648. pn->leaf = &ip6_null_entry;
  649. }
  650. #endif
  651. atomic_inc(&pn->leaf->rt6i_ref);
  652. }
  653. #endif
  654. dst_free(&rt->u.dst);
  655. }
  656. return err;
  657. #ifdef CONFIG_IPV6_SUBTREES
  658. /* Subtree creation failed, probably main tree node
  659. is orphan. If it is, shoot it.
  660. */
  661. st_failure:
  662. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  663. fib6_repair_tree(fn);
  664. dst_free(&rt->u.dst);
  665. return err;
  666. #endif
  667. }
  668. /*
  669. * Routing tree lookup
  670. *
  671. */
  672. struct lookup_args {
  673. int offset; /* key offset on rt6_info */
  674. struct in6_addr *addr; /* search key */
  675. };
  676. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  677. struct lookup_args *args)
  678. {
  679. struct fib6_node *fn;
  680. __be32 dir;
  681. if (unlikely(args->offset == 0))
  682. return NULL;
  683. /*
  684. * Descend on a tree
  685. */
  686. fn = root;
  687. for (;;) {
  688. struct fib6_node *next;
  689. dir = addr_bit_set(args->addr, fn->fn_bit);
  690. next = dir ? fn->right : fn->left;
  691. if (next) {
  692. fn = next;
  693. continue;
  694. }
  695. break;
  696. }
  697. while(fn) {
  698. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  699. struct rt6key *key;
  700. key = (struct rt6key *) ((u8 *) fn->leaf +
  701. args->offset);
  702. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  703. #ifdef CONFIG_IPV6_SUBTREES
  704. if (fn->subtree)
  705. fn = fib6_lookup_1(fn->subtree, args + 1);
  706. #endif
  707. if (!fn || fn->fn_flags & RTN_RTINFO)
  708. return fn;
  709. }
  710. }
  711. if (fn->fn_flags & RTN_ROOT)
  712. break;
  713. fn = fn->parent;
  714. }
  715. return NULL;
  716. }
  717. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  718. struct in6_addr *saddr)
  719. {
  720. struct fib6_node *fn;
  721. struct lookup_args args[] = {
  722. {
  723. .offset = offsetof(struct rt6_info, rt6i_dst),
  724. .addr = daddr,
  725. },
  726. #ifdef CONFIG_IPV6_SUBTREES
  727. {
  728. .offset = offsetof(struct rt6_info, rt6i_src),
  729. .addr = saddr,
  730. },
  731. #endif
  732. {
  733. .offset = 0, /* sentinel */
  734. }
  735. };
  736. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  737. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  738. fn = root;
  739. return fn;
  740. }
  741. /*
  742. * Get node with specified destination prefix (and source prefix,
  743. * if subtrees are used)
  744. */
  745. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  746. struct in6_addr *addr,
  747. int plen, int offset)
  748. {
  749. struct fib6_node *fn;
  750. for (fn = root; fn ; ) {
  751. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  752. /*
  753. * Prefix match
  754. */
  755. if (plen < fn->fn_bit ||
  756. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  757. return NULL;
  758. if (plen == fn->fn_bit)
  759. return fn;
  760. /*
  761. * We have more bits to go
  762. */
  763. if (addr_bit_set(addr, fn->fn_bit))
  764. fn = fn->right;
  765. else
  766. fn = fn->left;
  767. }
  768. return NULL;
  769. }
  770. struct fib6_node * fib6_locate(struct fib6_node *root,
  771. struct in6_addr *daddr, int dst_len,
  772. struct in6_addr *saddr, int src_len)
  773. {
  774. struct fib6_node *fn;
  775. fn = fib6_locate_1(root, daddr, dst_len,
  776. offsetof(struct rt6_info, rt6i_dst));
  777. #ifdef CONFIG_IPV6_SUBTREES
  778. if (src_len) {
  779. BUG_TRAP(saddr!=NULL);
  780. if (fn && fn->subtree)
  781. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  782. offsetof(struct rt6_info, rt6i_src));
  783. }
  784. #endif
  785. if (fn && fn->fn_flags&RTN_RTINFO)
  786. return fn;
  787. return NULL;
  788. }
  789. /*
  790. * Deletion
  791. *
  792. */
  793. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  794. {
  795. if (fn->fn_flags&RTN_ROOT)
  796. return &ip6_null_entry;
  797. while(fn) {
  798. if(fn->left)
  799. return fn->left->leaf;
  800. if(fn->right)
  801. return fn->right->leaf;
  802. fn = FIB6_SUBTREE(fn);
  803. }
  804. return NULL;
  805. }
  806. /*
  807. * Called to trim the tree of intermediate nodes when possible. "fn"
  808. * is the node we want to try and remove.
  809. */
  810. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
  811. {
  812. int children;
  813. int nstate;
  814. struct fib6_node *child, *pn;
  815. struct fib6_walker_t *w;
  816. int iter = 0;
  817. for (;;) {
  818. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  819. iter++;
  820. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  821. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  822. BUG_TRAP(fn->leaf==NULL);
  823. children = 0;
  824. child = NULL;
  825. if (fn->right) child = fn->right, children |= 1;
  826. if (fn->left) child = fn->left, children |= 2;
  827. if (children == 3 || FIB6_SUBTREE(fn)
  828. #ifdef CONFIG_IPV6_SUBTREES
  829. /* Subtree root (i.e. fn) may have one child */
  830. || (children && fn->fn_flags&RTN_ROOT)
  831. #endif
  832. ) {
  833. fn->leaf = fib6_find_prefix(fn);
  834. #if RT6_DEBUG >= 2
  835. if (fn->leaf==NULL) {
  836. BUG_TRAP(fn->leaf);
  837. fn->leaf = &ip6_null_entry;
  838. }
  839. #endif
  840. atomic_inc(&fn->leaf->rt6i_ref);
  841. return fn->parent;
  842. }
  843. pn = fn->parent;
  844. #ifdef CONFIG_IPV6_SUBTREES
  845. if (FIB6_SUBTREE(pn) == fn) {
  846. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  847. FIB6_SUBTREE(pn) = NULL;
  848. nstate = FWS_L;
  849. } else {
  850. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  851. #endif
  852. if (pn->right == fn) pn->right = child;
  853. else if (pn->left == fn) pn->left = child;
  854. #if RT6_DEBUG >= 2
  855. else BUG_TRAP(0);
  856. #endif
  857. if (child)
  858. child->parent = pn;
  859. nstate = FWS_R;
  860. #ifdef CONFIG_IPV6_SUBTREES
  861. }
  862. #endif
  863. read_lock(&fib6_walker_lock);
  864. FOR_WALKERS(w) {
  865. if (child == NULL) {
  866. if (w->root == fn) {
  867. w->root = w->node = NULL;
  868. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  869. } else if (w->node == fn) {
  870. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  871. w->node = pn;
  872. w->state = nstate;
  873. }
  874. } else {
  875. if (w->root == fn) {
  876. w->root = child;
  877. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  878. }
  879. if (w->node == fn) {
  880. w->node = child;
  881. if (children&2) {
  882. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  883. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  884. } else {
  885. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  886. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  887. }
  888. }
  889. }
  890. }
  891. read_unlock(&fib6_walker_lock);
  892. node_free(fn);
  893. if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
  894. return pn;
  895. rt6_release(pn->leaf);
  896. pn->leaf = NULL;
  897. fn = pn;
  898. }
  899. }
  900. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  901. struct nl_info *info)
  902. {
  903. struct fib6_walker_t *w;
  904. struct rt6_info *rt = *rtp;
  905. RT6_TRACE("fib6_del_route\n");
  906. /* Unlink it */
  907. *rtp = rt->u.dst.rt6_next;
  908. rt->rt6i_node = NULL;
  909. rt6_stats.fib_rt_entries--;
  910. rt6_stats.fib_discarded_routes++;
  911. /* Reset round-robin state, if necessary */
  912. if (fn->rr_ptr == rt)
  913. fn->rr_ptr = NULL;
  914. /* Adjust walkers */
  915. read_lock(&fib6_walker_lock);
  916. FOR_WALKERS(w) {
  917. if (w->state == FWS_C && w->leaf == rt) {
  918. RT6_TRACE("walker %p adjusted by delroute\n", w);
  919. w->leaf = rt->u.dst.rt6_next;
  920. if (w->leaf == NULL)
  921. w->state = FWS_U;
  922. }
  923. }
  924. read_unlock(&fib6_walker_lock);
  925. rt->u.dst.rt6_next = NULL;
  926. /* If it was last route, expunge its radix tree node */
  927. if (fn->leaf == NULL) {
  928. fn->fn_flags &= ~RTN_RTINFO;
  929. rt6_stats.fib_route_nodes--;
  930. fn = fib6_repair_tree(fn);
  931. }
  932. if (atomic_read(&rt->rt6i_ref) != 1) {
  933. /* This route is used as dummy address holder in some split
  934. * nodes. It is not leaked, but it still holds other resources,
  935. * which must be released in time. So, scan ascendant nodes
  936. * and replace dummy references to this route with references
  937. * to still alive ones.
  938. */
  939. while (fn) {
  940. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  941. fn->leaf = fib6_find_prefix(fn);
  942. atomic_inc(&fn->leaf->rt6i_ref);
  943. rt6_release(rt);
  944. }
  945. fn = fn->parent;
  946. }
  947. /* No more references are possible at this point. */
  948. BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
  949. }
  950. inet6_rt_notify(RTM_DELROUTE, rt, info);
  951. rt6_release(rt);
  952. }
  953. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  954. {
  955. struct fib6_node *fn = rt->rt6i_node;
  956. struct rt6_info **rtp;
  957. #if RT6_DEBUG >= 2
  958. if (rt->u.dst.obsolete>0) {
  959. BUG_TRAP(fn==NULL);
  960. return -ENOENT;
  961. }
  962. #endif
  963. if (fn == NULL || rt == &ip6_null_entry)
  964. return -ENOENT;
  965. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  966. if (!(rt->rt6i_flags&RTF_CACHE)) {
  967. struct fib6_node *pn = fn;
  968. #ifdef CONFIG_IPV6_SUBTREES
  969. /* clones of this route might be in another subtree */
  970. if (rt->rt6i_src.plen) {
  971. while (!(pn->fn_flags&RTN_ROOT))
  972. pn = pn->parent;
  973. pn = pn->parent;
  974. }
  975. #endif
  976. fib6_prune_clones(pn, rt);
  977. }
  978. /*
  979. * Walk the leaf entries looking for ourself
  980. */
  981. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) {
  982. if (*rtp == rt) {
  983. fib6_del_route(fn, rtp, info);
  984. return 0;
  985. }
  986. }
  987. return -ENOENT;
  988. }
  989. /*
  990. * Tree traversal function.
  991. *
  992. * Certainly, it is not interrupt safe.
  993. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  994. * It means, that we can modify tree during walking
  995. * and use this function for garbage collection, clone pruning,
  996. * cleaning tree when a device goes down etc. etc.
  997. *
  998. * It guarantees that every node will be traversed,
  999. * and that it will be traversed only once.
  1000. *
  1001. * Callback function w->func may return:
  1002. * 0 -> continue walking.
  1003. * positive value -> walking is suspended (used by tree dumps,
  1004. * and probably by gc, if it will be split to several slices)
  1005. * negative value -> terminate walking.
  1006. *
  1007. * The function itself returns:
  1008. * 0 -> walk is complete.
  1009. * >0 -> walk is incomplete (i.e. suspended)
  1010. * <0 -> walk is terminated by an error.
  1011. */
  1012. static int fib6_walk_continue(struct fib6_walker_t *w)
  1013. {
  1014. struct fib6_node *fn, *pn;
  1015. for (;;) {
  1016. fn = w->node;
  1017. if (fn == NULL)
  1018. return 0;
  1019. if (w->prune && fn != w->root &&
  1020. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  1021. w->state = FWS_C;
  1022. w->leaf = fn->leaf;
  1023. }
  1024. switch (w->state) {
  1025. #ifdef CONFIG_IPV6_SUBTREES
  1026. case FWS_S:
  1027. if (FIB6_SUBTREE(fn)) {
  1028. w->node = FIB6_SUBTREE(fn);
  1029. continue;
  1030. }
  1031. w->state = FWS_L;
  1032. #endif
  1033. case FWS_L:
  1034. if (fn->left) {
  1035. w->node = fn->left;
  1036. w->state = FWS_INIT;
  1037. continue;
  1038. }
  1039. w->state = FWS_R;
  1040. case FWS_R:
  1041. if (fn->right) {
  1042. w->node = fn->right;
  1043. w->state = FWS_INIT;
  1044. continue;
  1045. }
  1046. w->state = FWS_C;
  1047. w->leaf = fn->leaf;
  1048. case FWS_C:
  1049. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  1050. int err = w->func(w);
  1051. if (err)
  1052. return err;
  1053. continue;
  1054. }
  1055. w->state = FWS_U;
  1056. case FWS_U:
  1057. if (fn == w->root)
  1058. return 0;
  1059. pn = fn->parent;
  1060. w->node = pn;
  1061. #ifdef CONFIG_IPV6_SUBTREES
  1062. if (FIB6_SUBTREE(pn) == fn) {
  1063. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  1064. w->state = FWS_L;
  1065. continue;
  1066. }
  1067. #endif
  1068. if (pn->left == fn) {
  1069. w->state = FWS_R;
  1070. continue;
  1071. }
  1072. if (pn->right == fn) {
  1073. w->state = FWS_C;
  1074. w->leaf = w->node->leaf;
  1075. continue;
  1076. }
  1077. #if RT6_DEBUG >= 2
  1078. BUG_TRAP(0);
  1079. #endif
  1080. }
  1081. }
  1082. }
  1083. static int fib6_walk(struct fib6_walker_t *w)
  1084. {
  1085. int res;
  1086. w->state = FWS_INIT;
  1087. w->node = w->root;
  1088. fib6_walker_link(w);
  1089. res = fib6_walk_continue(w);
  1090. if (res <= 0)
  1091. fib6_walker_unlink(w);
  1092. return res;
  1093. }
  1094. static int fib6_clean_node(struct fib6_walker_t *w)
  1095. {
  1096. struct nl_info info = {
  1097. .nl_net = &init_net,
  1098. };
  1099. int res;
  1100. struct rt6_info *rt;
  1101. struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
  1102. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  1103. res = c->func(rt, c->arg);
  1104. if (res < 0) {
  1105. w->leaf = rt;
  1106. res = fib6_del(rt, &info);
  1107. if (res) {
  1108. #if RT6_DEBUG >= 2
  1109. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  1110. #endif
  1111. continue;
  1112. }
  1113. return 0;
  1114. }
  1115. BUG_TRAP(res==0);
  1116. }
  1117. w->leaf = rt;
  1118. return 0;
  1119. }
  1120. /*
  1121. * Convenient frontend to tree walker.
  1122. *
  1123. * func is called on each route.
  1124. * It may return -1 -> delete this route.
  1125. * 0 -> continue walking
  1126. *
  1127. * prune==1 -> only immediate children of node (certainly,
  1128. * ignoring pure split nodes) will be scanned.
  1129. */
  1130. static void fib6_clean_tree(struct fib6_node *root,
  1131. int (*func)(struct rt6_info *, void *arg),
  1132. int prune, void *arg)
  1133. {
  1134. struct fib6_cleaner_t c;
  1135. c.w.root = root;
  1136. c.w.func = fib6_clean_node;
  1137. c.w.prune = prune;
  1138. c.func = func;
  1139. c.arg = arg;
  1140. fib6_walk(&c.w);
  1141. }
  1142. void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
  1143. int prune, void *arg)
  1144. {
  1145. struct fib6_table *table;
  1146. struct hlist_node *node;
  1147. unsigned int h;
  1148. rcu_read_lock();
  1149. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1150. hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
  1151. tb6_hlist) {
  1152. write_lock_bh(&table->tb6_lock);
  1153. fib6_clean_tree(&table->tb6_root, func, prune, arg);
  1154. write_unlock_bh(&table->tb6_lock);
  1155. }
  1156. }
  1157. rcu_read_unlock();
  1158. }
  1159. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1160. {
  1161. if (rt->rt6i_flags & RTF_CACHE) {
  1162. RT6_TRACE("pruning clone %p\n", rt);
  1163. return -1;
  1164. }
  1165. return 0;
  1166. }
  1167. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
  1168. {
  1169. fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
  1170. }
  1171. /*
  1172. * Garbage collection
  1173. */
  1174. static struct fib6_gc_args
  1175. {
  1176. int timeout;
  1177. int more;
  1178. } gc_args;
  1179. static int fib6_age(struct rt6_info *rt, void *arg)
  1180. {
  1181. unsigned long now = jiffies;
  1182. /*
  1183. * check addrconf expiration here.
  1184. * Routes are expired even if they are in use.
  1185. *
  1186. * Also age clones. Note, that clones are aged out
  1187. * only if they are not in use now.
  1188. */
  1189. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  1190. if (time_after(now, rt->rt6i_expires)) {
  1191. RT6_TRACE("expiring %p\n", rt);
  1192. return -1;
  1193. }
  1194. gc_args.more++;
  1195. } else if (rt->rt6i_flags & RTF_CACHE) {
  1196. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  1197. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  1198. RT6_TRACE("aging clone %p\n", rt);
  1199. return -1;
  1200. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  1201. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  1202. RT6_TRACE("purging route %p via non-router but gateway\n",
  1203. rt);
  1204. return -1;
  1205. }
  1206. gc_args.more++;
  1207. }
  1208. return 0;
  1209. }
  1210. static DEFINE_SPINLOCK(fib6_gc_lock);
  1211. void fib6_run_gc(unsigned long dummy)
  1212. {
  1213. if (dummy != ~0UL) {
  1214. spin_lock_bh(&fib6_gc_lock);
  1215. gc_args.timeout = dummy ? (int)dummy :
  1216. init_net.ipv6.sysctl.ip6_rt_gc_interval;
  1217. } else {
  1218. local_bh_disable();
  1219. if (!spin_trylock(&fib6_gc_lock)) {
  1220. mod_timer(&ip6_fib_timer, jiffies + HZ);
  1221. local_bh_enable();
  1222. return;
  1223. }
  1224. gc_args.timeout = init_net.ipv6.sysctl.ip6_rt_gc_interval;
  1225. }
  1226. gc_args.more = 0;
  1227. ndisc_dst_gc(&gc_args.more);
  1228. fib6_clean_all(fib6_age, 0, NULL);
  1229. if (gc_args.more)
  1230. mod_timer(&ip6_fib_timer, jiffies +
  1231. init_net.ipv6.sysctl.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. int __init fib6_init(void)
  1239. {
  1240. int ret;
  1241. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1242. sizeof(struct fib6_node),
  1243. 0, SLAB_HWCACHE_ALIGN,
  1244. NULL);
  1245. if (!fib6_node_kmem)
  1246. return -ENOMEM;
  1247. fib6_tables_init();
  1248. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
  1249. if (ret)
  1250. goto out_kmem_cache_create;
  1251. out:
  1252. return ret;
  1253. out_kmem_cache_create:
  1254. kmem_cache_destroy(fib6_node_kmem);
  1255. goto out;
  1256. }
  1257. void fib6_gc_cleanup(void)
  1258. {
  1259. del_timer(&ip6_fib_timer);
  1260. kmem_cache_destroy(fib6_node_kmem);
  1261. }