ip6_fib.c 33 KB

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