ip6_fib.c 33 KB

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