ip6_fib.c 32 KB

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