ip6_fib.c 32 KB

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