ip6_fib.c 31 KB

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