ip6_fib.c 33 KB

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