ip6_fib.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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, int allow_create,
  363. int replace_required)
  364. {
  365. struct fib6_node *fn, *in, *ln;
  366. struct fib6_node *pn = NULL;
  367. struct rt6key *key;
  368. int bit;
  369. __be32 dir = 0;
  370. __u32 sernum = fib6_new_sernum();
  371. RT6_TRACE("fib6_add_1\n");
  372. /* insert node in tree */
  373. fn = root;
  374. do {
  375. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  376. /*
  377. * Prefix match
  378. */
  379. if (plen < fn->fn_bit ||
  380. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
  381. if (!allow_create)
  382. printk(KERN_WARNING
  383. "IPv6: NLM_F_CREATE should be set when creating new route\n");
  384. goto insert_above;
  385. }
  386. /*
  387. * Exact match ?
  388. */
  389. if (plen == fn->fn_bit) {
  390. /* clean up an intermediate node */
  391. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  392. rt6_release(fn->leaf);
  393. fn->leaf = NULL;
  394. }
  395. fn->fn_sernum = sernum;
  396. return fn;
  397. }
  398. /*
  399. * We have more bits to go
  400. */
  401. /* Try to walk down on tree. */
  402. fn->fn_sernum = sernum;
  403. dir = addr_bit_set(addr, fn->fn_bit);
  404. pn = fn;
  405. fn = dir ? fn->right: fn->left;
  406. } while (fn);
  407. if (replace_required && !allow_create) {
  408. /* We should not create new node because
  409. * NLM_F_REPLACE was specified without NLM_F_CREATE
  410. * I assume it is safe to require NLM_F_CREATE when
  411. * REPLACE flag is used! Later we may want to remove the
  412. * check for replace_required, because according
  413. * to netlink specification, NLM_F_CREATE
  414. * MUST be specified if new route is created.
  415. * That would keep IPv6 consistent with IPv4
  416. */
  417. printk(KERN_WARNING
  418. "IPv6: NLM_F_CREATE should be set when creating new route - ignoring request\n");
  419. return ERR_PTR(-ENOENT);
  420. }
  421. /*
  422. * We walked to the bottom of tree.
  423. * Create new leaf node without children.
  424. */
  425. if (!allow_create)
  426. printk(KERN_WARNING "IPv6: NLM_F_CREATE should be set when creating new route\n");
  427. ln = node_alloc();
  428. if (ln == NULL)
  429. return NULL;
  430. ln->fn_bit = plen;
  431. ln->parent = pn;
  432. ln->fn_sernum = sernum;
  433. if (dir)
  434. pn->right = ln;
  435. else
  436. pn->left = ln;
  437. return ln;
  438. insert_above:
  439. /*
  440. * split since we don't have a common prefix anymore or
  441. * we have a less significant route.
  442. * we've to insert an intermediate node on the list
  443. * this new node will point to the one we need to create
  444. * and the current
  445. */
  446. pn = fn->parent;
  447. /* find 1st bit in difference between the 2 addrs.
  448. See comment in __ipv6_addr_diff: bit may be an invalid value,
  449. but if it is >= plen, the value is ignored in any case.
  450. */
  451. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  452. /*
  453. * (intermediate)[in]
  454. * / \
  455. * (new leaf node)[ln] (old node)[fn]
  456. */
  457. if (plen > bit) {
  458. in = node_alloc();
  459. ln = node_alloc();
  460. if (in == NULL || ln == NULL) {
  461. if (in)
  462. node_free(in);
  463. if (ln)
  464. node_free(ln);
  465. return NULL;
  466. }
  467. /*
  468. * new intermediate node.
  469. * RTN_RTINFO will
  470. * be off since that an address that chooses one of
  471. * the branches would not match less specific routes
  472. * in the other branch
  473. */
  474. in->fn_bit = bit;
  475. in->parent = pn;
  476. in->leaf = fn->leaf;
  477. atomic_inc(&in->leaf->rt6i_ref);
  478. in->fn_sernum = sernum;
  479. /* update parent pointer */
  480. if (dir)
  481. pn->right = in;
  482. else
  483. pn->left = in;
  484. ln->fn_bit = plen;
  485. ln->parent = in;
  486. fn->parent = in;
  487. ln->fn_sernum = sernum;
  488. if (addr_bit_set(addr, bit)) {
  489. in->right = ln;
  490. in->left = fn;
  491. } else {
  492. in->left = ln;
  493. in->right = fn;
  494. }
  495. } else { /* plen <= bit */
  496. /*
  497. * (new leaf node)[ln]
  498. * / \
  499. * (old node)[fn] NULL
  500. */
  501. ln = node_alloc();
  502. if (ln == NULL)
  503. return NULL;
  504. ln->fn_bit = plen;
  505. ln->parent = pn;
  506. ln->fn_sernum = sernum;
  507. if (dir)
  508. pn->right = ln;
  509. else
  510. pn->left = ln;
  511. if (addr_bit_set(&key->addr, plen))
  512. ln->right = fn;
  513. else
  514. ln->left = fn;
  515. fn->parent = ln;
  516. }
  517. return ln;
  518. }
  519. /*
  520. * Insert routing information in a node.
  521. */
  522. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  523. struct nl_info *info)
  524. {
  525. struct rt6_info *iter = NULL;
  526. struct rt6_info **ins;
  527. int replace = (NULL != info &&
  528. NULL != info->nlh &&
  529. (info->nlh->nlmsg_flags&NLM_F_REPLACE));
  530. int add = ((NULL == info || NULL == info->nlh) ||
  531. (info->nlh->nlmsg_flags&NLM_F_CREATE));
  532. int found = 0;
  533. ins = &fn->leaf;
  534. for (iter = fn->leaf; iter; iter=iter->dst.rt6_next) {
  535. /*
  536. * Search for duplicates
  537. */
  538. if (iter->rt6i_metric == rt->rt6i_metric) {
  539. /*
  540. * Same priority level
  541. */
  542. if (NULL != info->nlh &&
  543. (info->nlh->nlmsg_flags&NLM_F_EXCL))
  544. return -EEXIST;
  545. if (replace) {
  546. found++;
  547. break;
  548. }
  549. if (iter->rt6i_dev == rt->rt6i_dev &&
  550. iter->rt6i_idev == rt->rt6i_idev &&
  551. ipv6_addr_equal(&iter->rt6i_gateway,
  552. &rt->rt6i_gateway)) {
  553. if (!(iter->rt6i_flags&RTF_EXPIRES))
  554. return -EEXIST;
  555. iter->rt6i_expires = rt->rt6i_expires;
  556. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  557. iter->rt6i_flags &= ~RTF_EXPIRES;
  558. iter->rt6i_expires = 0;
  559. }
  560. return -EEXIST;
  561. }
  562. }
  563. if (iter->rt6i_metric > rt->rt6i_metric)
  564. break;
  565. ins = &iter->dst.rt6_next;
  566. }
  567. /* Reset round-robin state, if necessary */
  568. if (ins == &fn->leaf)
  569. fn->rr_ptr = NULL;
  570. /*
  571. * insert node
  572. */
  573. if (!replace) {
  574. if (!add)
  575. printk(KERN_WARNING "IPv6: NLM_F_CREATE should be set when creating new route\n");
  576. add:
  577. rt->dst.rt6_next = iter;
  578. *ins = rt;
  579. rt->rt6i_node = fn;
  580. atomic_inc(&rt->rt6i_ref);
  581. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  582. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  583. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  584. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  585. fn->fn_flags |= RTN_RTINFO;
  586. }
  587. } else {
  588. if (!found) {
  589. if (add)
  590. goto add;
  591. printk(KERN_WARNING "IPv6: NLM_F_REPLACE set, but no existing node found!\n");
  592. return -ENOENT;
  593. }
  594. *ins = rt;
  595. rt->rt6i_node = fn;
  596. rt->dst.rt6_next = iter->dst.rt6_next;
  597. atomic_inc(&rt->rt6i_ref);
  598. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  599. rt6_release(iter);
  600. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  601. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  602. fn->fn_flags |= RTN_RTINFO;
  603. }
  604. }
  605. return 0;
  606. }
  607. static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
  608. {
  609. if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
  610. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  611. mod_timer(&net->ipv6.ip6_fib_timer,
  612. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  613. }
  614. void fib6_force_start_gc(struct net *net)
  615. {
  616. if (!timer_pending(&net->ipv6.ip6_fib_timer))
  617. mod_timer(&net->ipv6.ip6_fib_timer,
  618. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  619. }
  620. /*
  621. * Add routing information to the routing tree.
  622. * <destination addr>/<source addr>
  623. * with source addr info in sub-trees
  624. */
  625. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  626. {
  627. struct fib6_node *fn, *pn = NULL;
  628. int err = -ENOMEM;
  629. int allow_create = 1;
  630. int replace_required = 0;
  631. if (NULL != info && NULL != info->nlh) {
  632. if (!(info->nlh->nlmsg_flags&NLM_F_CREATE))
  633. allow_create = 0;
  634. if ((info->nlh->nlmsg_flags&NLM_F_REPLACE))
  635. replace_required = 1;
  636. }
  637. if (!allow_create && !replace_required)
  638. printk(KERN_WARNING "IPv6: RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
  639. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  640. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst),
  641. allow_create, replace_required);
  642. if (IS_ERR(fn)) {
  643. err = PTR_ERR(fn);
  644. fn = NULL;
  645. }
  646. if (fn == NULL)
  647. goto out;
  648. pn = fn;
  649. #ifdef CONFIG_IPV6_SUBTREES
  650. if (rt->rt6i_src.plen) {
  651. struct fib6_node *sn;
  652. if (fn->subtree == NULL) {
  653. struct fib6_node *sfn;
  654. /*
  655. * Create subtree.
  656. *
  657. * fn[main tree]
  658. * |
  659. * sfn[subtree root]
  660. * \
  661. * sn[new leaf node]
  662. */
  663. /* Create subtree root node */
  664. sfn = node_alloc();
  665. if (sfn == NULL)
  666. goto st_failure;
  667. sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
  668. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  669. sfn->fn_flags = RTN_ROOT;
  670. sfn->fn_sernum = fib6_new_sernum();
  671. /* Now add the first leaf node to new subtree */
  672. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  673. sizeof(struct in6_addr), rt->rt6i_src.plen,
  674. offsetof(struct rt6_info, rt6i_src),
  675. allow_create, replace_required);
  676. if (sn == NULL) {
  677. /* If it is failed, discard just allocated
  678. root, and then (in st_failure) stale node
  679. in main tree.
  680. */
  681. node_free(sfn);
  682. goto st_failure;
  683. }
  684. /* Now link new subtree to main tree */
  685. sfn->parent = fn;
  686. fn->subtree = sfn;
  687. } else {
  688. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  689. sizeof(struct in6_addr), rt->rt6i_src.plen,
  690. offsetof(struct rt6_info, rt6i_src),
  691. allow_create, replace_required);
  692. if (IS_ERR(sn)) {
  693. err = PTR_ERR(sn);
  694. sn = NULL;
  695. }
  696. if (sn == NULL)
  697. goto st_failure;
  698. }
  699. if (fn->leaf == NULL) {
  700. fn->leaf = rt;
  701. atomic_inc(&rt->rt6i_ref);
  702. }
  703. fn = sn;
  704. }
  705. #endif
  706. err = fib6_add_rt2node(fn, rt, info);
  707. if (err == 0) {
  708. fib6_start_gc(info->nl_net, rt);
  709. if (!(rt->rt6i_flags&RTF_CACHE))
  710. fib6_prune_clones(info->nl_net, pn, rt);
  711. }
  712. out:
  713. if (err) {
  714. #ifdef CONFIG_IPV6_SUBTREES
  715. /*
  716. * If fib6_add_1 has cleared the old leaf pointer in the
  717. * super-tree leaf node we have to find a new one for it.
  718. */
  719. if (pn != fn && pn->leaf == rt) {
  720. pn->leaf = NULL;
  721. atomic_dec(&rt->rt6i_ref);
  722. }
  723. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  724. pn->leaf = fib6_find_prefix(info->nl_net, pn);
  725. #if RT6_DEBUG >= 2
  726. if (!pn->leaf) {
  727. WARN_ON(pn->leaf == NULL);
  728. pn->leaf = info->nl_net->ipv6.ip6_null_entry;
  729. }
  730. #endif
  731. atomic_inc(&pn->leaf->rt6i_ref);
  732. }
  733. #endif
  734. dst_free(&rt->dst);
  735. }
  736. return err;
  737. #ifdef CONFIG_IPV6_SUBTREES
  738. /* Subtree creation failed, probably main tree node
  739. is orphan. If it is, shoot it.
  740. */
  741. st_failure:
  742. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  743. fib6_repair_tree(info->nl_net, fn);
  744. dst_free(&rt->dst);
  745. return err;
  746. #endif
  747. }
  748. /*
  749. * Routing tree lookup
  750. *
  751. */
  752. struct lookup_args {
  753. int offset; /* key offset on rt6_info */
  754. const struct in6_addr *addr; /* search key */
  755. };
  756. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  757. struct lookup_args *args)
  758. {
  759. struct fib6_node *fn;
  760. __be32 dir;
  761. if (unlikely(args->offset == 0))
  762. return NULL;
  763. /*
  764. * Descend on a tree
  765. */
  766. fn = root;
  767. for (;;) {
  768. struct fib6_node *next;
  769. dir = addr_bit_set(args->addr, fn->fn_bit);
  770. next = dir ? fn->right : fn->left;
  771. if (next) {
  772. fn = next;
  773. continue;
  774. }
  775. break;
  776. }
  777. while(fn) {
  778. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  779. struct rt6key *key;
  780. key = (struct rt6key *) ((u8 *) fn->leaf +
  781. args->offset);
  782. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  783. #ifdef CONFIG_IPV6_SUBTREES
  784. if (fn->subtree)
  785. fn = fib6_lookup_1(fn->subtree, args + 1);
  786. #endif
  787. if (!fn || fn->fn_flags & RTN_RTINFO)
  788. return fn;
  789. }
  790. }
  791. if (fn->fn_flags & RTN_ROOT)
  792. break;
  793. fn = fn->parent;
  794. }
  795. return NULL;
  796. }
  797. struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
  798. const struct in6_addr *saddr)
  799. {
  800. struct fib6_node *fn;
  801. struct lookup_args args[] = {
  802. {
  803. .offset = offsetof(struct rt6_info, rt6i_dst),
  804. .addr = daddr,
  805. },
  806. #ifdef CONFIG_IPV6_SUBTREES
  807. {
  808. .offset = offsetof(struct rt6_info, rt6i_src),
  809. .addr = saddr,
  810. },
  811. #endif
  812. {
  813. .offset = 0, /* sentinel */
  814. }
  815. };
  816. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  817. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  818. fn = root;
  819. return fn;
  820. }
  821. /*
  822. * Get node with specified destination prefix (and source prefix,
  823. * if subtrees are used)
  824. */
  825. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  826. const struct in6_addr *addr,
  827. int plen, int offset)
  828. {
  829. struct fib6_node *fn;
  830. for (fn = root; fn ; ) {
  831. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  832. /*
  833. * Prefix match
  834. */
  835. if (plen < fn->fn_bit ||
  836. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  837. return NULL;
  838. if (plen == fn->fn_bit)
  839. return fn;
  840. /*
  841. * We have more bits to go
  842. */
  843. if (addr_bit_set(addr, fn->fn_bit))
  844. fn = fn->right;
  845. else
  846. fn = fn->left;
  847. }
  848. return NULL;
  849. }
  850. struct fib6_node * fib6_locate(struct fib6_node *root,
  851. const struct in6_addr *daddr, int dst_len,
  852. const struct in6_addr *saddr, int src_len)
  853. {
  854. struct fib6_node *fn;
  855. fn = fib6_locate_1(root, daddr, dst_len,
  856. offsetof(struct rt6_info, rt6i_dst));
  857. #ifdef CONFIG_IPV6_SUBTREES
  858. if (src_len) {
  859. WARN_ON(saddr == NULL);
  860. if (fn && fn->subtree)
  861. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  862. offsetof(struct rt6_info, rt6i_src));
  863. }
  864. #endif
  865. if (fn && fn->fn_flags&RTN_RTINFO)
  866. return fn;
  867. return NULL;
  868. }
  869. /*
  870. * Deletion
  871. *
  872. */
  873. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
  874. {
  875. if (fn->fn_flags&RTN_ROOT)
  876. return net->ipv6.ip6_null_entry;
  877. while(fn) {
  878. if(fn->left)
  879. return fn->left->leaf;
  880. if(fn->right)
  881. return fn->right->leaf;
  882. fn = FIB6_SUBTREE(fn);
  883. }
  884. return NULL;
  885. }
  886. /*
  887. * Called to trim the tree of intermediate nodes when possible. "fn"
  888. * is the node we want to try and remove.
  889. */
  890. static struct fib6_node *fib6_repair_tree(struct net *net,
  891. struct fib6_node *fn)
  892. {
  893. int children;
  894. int nstate;
  895. struct fib6_node *child, *pn;
  896. struct fib6_walker_t *w;
  897. int iter = 0;
  898. for (;;) {
  899. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  900. iter++;
  901. WARN_ON(fn->fn_flags & RTN_RTINFO);
  902. WARN_ON(fn->fn_flags & RTN_TL_ROOT);
  903. WARN_ON(fn->leaf != NULL);
  904. children = 0;
  905. child = NULL;
  906. if (fn->right) child = fn->right, children |= 1;
  907. if (fn->left) child = fn->left, children |= 2;
  908. if (children == 3 || FIB6_SUBTREE(fn)
  909. #ifdef CONFIG_IPV6_SUBTREES
  910. /* Subtree root (i.e. fn) may have one child */
  911. || (children && fn->fn_flags&RTN_ROOT)
  912. #endif
  913. ) {
  914. fn->leaf = fib6_find_prefix(net, fn);
  915. #if RT6_DEBUG >= 2
  916. if (fn->leaf==NULL) {
  917. WARN_ON(!fn->leaf);
  918. fn->leaf = net->ipv6.ip6_null_entry;
  919. }
  920. #endif
  921. atomic_inc(&fn->leaf->rt6i_ref);
  922. return fn->parent;
  923. }
  924. pn = fn->parent;
  925. #ifdef CONFIG_IPV6_SUBTREES
  926. if (FIB6_SUBTREE(pn) == fn) {
  927. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  928. FIB6_SUBTREE(pn) = NULL;
  929. nstate = FWS_L;
  930. } else {
  931. WARN_ON(fn->fn_flags & RTN_ROOT);
  932. #endif
  933. if (pn->right == fn) pn->right = child;
  934. else if (pn->left == fn) pn->left = child;
  935. #if RT6_DEBUG >= 2
  936. else
  937. WARN_ON(1);
  938. #endif
  939. if (child)
  940. child->parent = pn;
  941. nstate = FWS_R;
  942. #ifdef CONFIG_IPV6_SUBTREES
  943. }
  944. #endif
  945. read_lock(&fib6_walker_lock);
  946. FOR_WALKERS(w) {
  947. if (child == NULL) {
  948. if (w->root == fn) {
  949. w->root = w->node = NULL;
  950. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  951. } else if (w->node == fn) {
  952. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  953. w->node = pn;
  954. w->state = nstate;
  955. }
  956. } else {
  957. if (w->root == fn) {
  958. w->root = child;
  959. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  960. }
  961. if (w->node == fn) {
  962. w->node = child;
  963. if (children&2) {
  964. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  965. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  966. } else {
  967. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  968. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  969. }
  970. }
  971. }
  972. }
  973. read_unlock(&fib6_walker_lock);
  974. node_free(fn);
  975. if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
  976. return pn;
  977. rt6_release(pn->leaf);
  978. pn->leaf = NULL;
  979. fn = pn;
  980. }
  981. }
  982. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  983. struct nl_info *info)
  984. {
  985. struct fib6_walker_t *w;
  986. struct rt6_info *rt = *rtp;
  987. struct net *net = info->nl_net;
  988. RT6_TRACE("fib6_del_route\n");
  989. /* Unlink it */
  990. *rtp = rt->dst.rt6_next;
  991. rt->rt6i_node = NULL;
  992. net->ipv6.rt6_stats->fib_rt_entries--;
  993. net->ipv6.rt6_stats->fib_discarded_routes++;
  994. /* Reset round-robin state, if necessary */
  995. if (fn->rr_ptr == rt)
  996. fn->rr_ptr = NULL;
  997. /* Adjust walkers */
  998. read_lock(&fib6_walker_lock);
  999. FOR_WALKERS(w) {
  1000. if (w->state == FWS_C && w->leaf == rt) {
  1001. RT6_TRACE("walker %p adjusted by delroute\n", w);
  1002. w->leaf = rt->dst.rt6_next;
  1003. if (w->leaf == NULL)
  1004. w->state = FWS_U;
  1005. }
  1006. }
  1007. read_unlock(&fib6_walker_lock);
  1008. rt->dst.rt6_next = NULL;
  1009. /* If it was last route, expunge its radix tree node */
  1010. if (fn->leaf == NULL) {
  1011. fn->fn_flags &= ~RTN_RTINFO;
  1012. net->ipv6.rt6_stats->fib_route_nodes--;
  1013. fn = fib6_repair_tree(net, fn);
  1014. }
  1015. if (atomic_read(&rt->rt6i_ref) != 1) {
  1016. /* This route is used as dummy address holder in some split
  1017. * nodes. It is not leaked, but it still holds other resources,
  1018. * which must be released in time. So, scan ascendant nodes
  1019. * and replace dummy references to this route with references
  1020. * to still alive ones.
  1021. */
  1022. while (fn) {
  1023. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  1024. fn->leaf = fib6_find_prefix(net, fn);
  1025. atomic_inc(&fn->leaf->rt6i_ref);
  1026. rt6_release(rt);
  1027. }
  1028. fn = fn->parent;
  1029. }
  1030. /* No more references are possible at this point. */
  1031. BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
  1032. }
  1033. inet6_rt_notify(RTM_DELROUTE, rt, info);
  1034. rt6_release(rt);
  1035. }
  1036. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  1037. {
  1038. struct net *net = info->nl_net;
  1039. struct fib6_node *fn = rt->rt6i_node;
  1040. struct rt6_info **rtp;
  1041. #if RT6_DEBUG >= 2
  1042. if (rt->dst.obsolete>0) {
  1043. WARN_ON(fn != NULL);
  1044. return -ENOENT;
  1045. }
  1046. #endif
  1047. if (fn == NULL || rt == net->ipv6.ip6_null_entry)
  1048. return -ENOENT;
  1049. WARN_ON(!(fn->fn_flags & RTN_RTINFO));
  1050. if (!(rt->rt6i_flags&RTF_CACHE)) {
  1051. struct fib6_node *pn = fn;
  1052. #ifdef CONFIG_IPV6_SUBTREES
  1053. /* clones of this route might be in another subtree */
  1054. if (rt->rt6i_src.plen) {
  1055. while (!(pn->fn_flags&RTN_ROOT))
  1056. pn = pn->parent;
  1057. pn = pn->parent;
  1058. }
  1059. #endif
  1060. fib6_prune_clones(info->nl_net, pn, rt);
  1061. }
  1062. /*
  1063. * Walk the leaf entries looking for ourself
  1064. */
  1065. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
  1066. if (*rtp == rt) {
  1067. fib6_del_route(fn, rtp, info);
  1068. return 0;
  1069. }
  1070. }
  1071. return -ENOENT;
  1072. }
  1073. /*
  1074. * Tree traversal function.
  1075. *
  1076. * Certainly, it is not interrupt safe.
  1077. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  1078. * It means, that we can modify tree during walking
  1079. * and use this function for garbage collection, clone pruning,
  1080. * cleaning tree when a device goes down etc. etc.
  1081. *
  1082. * It guarantees that every node will be traversed,
  1083. * and that it will be traversed only once.
  1084. *
  1085. * Callback function w->func may return:
  1086. * 0 -> continue walking.
  1087. * positive value -> walking is suspended (used by tree dumps,
  1088. * and probably by gc, if it will be split to several slices)
  1089. * negative value -> terminate walking.
  1090. *
  1091. * The function itself returns:
  1092. * 0 -> walk is complete.
  1093. * >0 -> walk is incomplete (i.e. suspended)
  1094. * <0 -> walk is terminated by an error.
  1095. */
  1096. static int fib6_walk_continue(struct fib6_walker_t *w)
  1097. {
  1098. struct fib6_node *fn, *pn;
  1099. for (;;) {
  1100. fn = w->node;
  1101. if (fn == NULL)
  1102. return 0;
  1103. if (w->prune && fn != w->root &&
  1104. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  1105. w->state = FWS_C;
  1106. w->leaf = fn->leaf;
  1107. }
  1108. switch (w->state) {
  1109. #ifdef CONFIG_IPV6_SUBTREES
  1110. case FWS_S:
  1111. if (FIB6_SUBTREE(fn)) {
  1112. w->node = FIB6_SUBTREE(fn);
  1113. continue;
  1114. }
  1115. w->state = FWS_L;
  1116. #endif
  1117. case FWS_L:
  1118. if (fn->left) {
  1119. w->node = fn->left;
  1120. w->state = FWS_INIT;
  1121. continue;
  1122. }
  1123. w->state = FWS_R;
  1124. case FWS_R:
  1125. if (fn->right) {
  1126. w->node = fn->right;
  1127. w->state = FWS_INIT;
  1128. continue;
  1129. }
  1130. w->state = FWS_C;
  1131. w->leaf = fn->leaf;
  1132. case FWS_C:
  1133. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  1134. int err;
  1135. if (w->count < w->skip) {
  1136. w->count++;
  1137. continue;
  1138. }
  1139. err = w->func(w);
  1140. if (err)
  1141. return err;
  1142. w->count++;
  1143. continue;
  1144. }
  1145. w->state = FWS_U;
  1146. case FWS_U:
  1147. if (fn == w->root)
  1148. return 0;
  1149. pn = fn->parent;
  1150. w->node = pn;
  1151. #ifdef CONFIG_IPV6_SUBTREES
  1152. if (FIB6_SUBTREE(pn) == fn) {
  1153. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1154. w->state = FWS_L;
  1155. continue;
  1156. }
  1157. #endif
  1158. if (pn->left == fn) {
  1159. w->state = FWS_R;
  1160. continue;
  1161. }
  1162. if (pn->right == fn) {
  1163. w->state = FWS_C;
  1164. w->leaf = w->node->leaf;
  1165. continue;
  1166. }
  1167. #if RT6_DEBUG >= 2
  1168. WARN_ON(1);
  1169. #endif
  1170. }
  1171. }
  1172. }
  1173. static int fib6_walk(struct fib6_walker_t *w)
  1174. {
  1175. int res;
  1176. w->state = FWS_INIT;
  1177. w->node = w->root;
  1178. fib6_walker_link(w);
  1179. res = fib6_walk_continue(w);
  1180. if (res <= 0)
  1181. fib6_walker_unlink(w);
  1182. return res;
  1183. }
  1184. static int fib6_clean_node(struct fib6_walker_t *w)
  1185. {
  1186. int res;
  1187. struct rt6_info *rt;
  1188. struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
  1189. struct nl_info info = {
  1190. .nl_net = c->net,
  1191. };
  1192. for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
  1193. res = c->func(rt, c->arg);
  1194. if (res < 0) {
  1195. w->leaf = rt;
  1196. res = fib6_del(rt, &info);
  1197. if (res) {
  1198. #if RT6_DEBUG >= 2
  1199. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  1200. #endif
  1201. continue;
  1202. }
  1203. return 0;
  1204. }
  1205. WARN_ON(res != 0);
  1206. }
  1207. w->leaf = rt;
  1208. return 0;
  1209. }
  1210. /*
  1211. * Convenient frontend to tree walker.
  1212. *
  1213. * func is called on each route.
  1214. * It may return -1 -> delete this route.
  1215. * 0 -> continue walking
  1216. *
  1217. * prune==1 -> only immediate children of node (certainly,
  1218. * ignoring pure split nodes) will be scanned.
  1219. */
  1220. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1221. int (*func)(struct rt6_info *, void *arg),
  1222. int prune, void *arg)
  1223. {
  1224. struct fib6_cleaner_t c;
  1225. c.w.root = root;
  1226. c.w.func = fib6_clean_node;
  1227. c.w.prune = prune;
  1228. c.w.count = 0;
  1229. c.w.skip = 0;
  1230. c.func = func;
  1231. c.arg = arg;
  1232. c.net = net;
  1233. fib6_walk(&c.w);
  1234. }
  1235. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  1236. int prune, void *arg)
  1237. {
  1238. struct fib6_table *table;
  1239. struct hlist_node *node;
  1240. struct hlist_head *head;
  1241. unsigned int h;
  1242. rcu_read_lock();
  1243. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1244. head = &net->ipv6.fib_table_hash[h];
  1245. hlist_for_each_entry_rcu(table, node, head, tb6_hlist) {
  1246. write_lock_bh(&table->tb6_lock);
  1247. fib6_clean_tree(net, &table->tb6_root,
  1248. func, prune, arg);
  1249. write_unlock_bh(&table->tb6_lock);
  1250. }
  1251. }
  1252. rcu_read_unlock();
  1253. }
  1254. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1255. {
  1256. if (rt->rt6i_flags & RTF_CACHE) {
  1257. RT6_TRACE("pruning clone %p\n", rt);
  1258. return -1;
  1259. }
  1260. return 0;
  1261. }
  1262. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  1263. struct rt6_info *rt)
  1264. {
  1265. fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
  1266. }
  1267. /*
  1268. * Garbage collection
  1269. */
  1270. static struct fib6_gc_args
  1271. {
  1272. int timeout;
  1273. int more;
  1274. } gc_args;
  1275. static int fib6_age(struct rt6_info *rt, void *arg)
  1276. {
  1277. unsigned long now = jiffies;
  1278. /*
  1279. * check addrconf expiration here.
  1280. * Routes are expired even if they are in use.
  1281. *
  1282. * Also age clones. Note, that clones are aged out
  1283. * only if they are not in use now.
  1284. */
  1285. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  1286. if (time_after(now, rt->rt6i_expires)) {
  1287. RT6_TRACE("expiring %p\n", rt);
  1288. return -1;
  1289. }
  1290. gc_args.more++;
  1291. } else if (rt->rt6i_flags & RTF_CACHE) {
  1292. if (atomic_read(&rt->dst.__refcnt) == 0 &&
  1293. time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
  1294. RT6_TRACE("aging clone %p\n", rt);
  1295. return -1;
  1296. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  1297. (!(dst_get_neighbour_raw(&rt->dst)->flags & NTF_ROUTER))) {
  1298. RT6_TRACE("purging route %p via non-router but gateway\n",
  1299. rt);
  1300. return -1;
  1301. }
  1302. gc_args.more++;
  1303. }
  1304. return 0;
  1305. }
  1306. static DEFINE_SPINLOCK(fib6_gc_lock);
  1307. void fib6_run_gc(unsigned long expires, struct net *net)
  1308. {
  1309. if (expires != ~0UL) {
  1310. spin_lock_bh(&fib6_gc_lock);
  1311. gc_args.timeout = expires ? (int)expires :
  1312. net->ipv6.sysctl.ip6_rt_gc_interval;
  1313. } else {
  1314. if (!spin_trylock_bh(&fib6_gc_lock)) {
  1315. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1316. return;
  1317. }
  1318. gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
  1319. }
  1320. gc_args.more = icmp6_dst_gc();
  1321. fib6_clean_all(net, fib6_age, 0, NULL);
  1322. if (gc_args.more)
  1323. mod_timer(&net->ipv6.ip6_fib_timer,
  1324. round_jiffies(jiffies
  1325. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1326. else
  1327. del_timer(&net->ipv6.ip6_fib_timer);
  1328. spin_unlock_bh(&fib6_gc_lock);
  1329. }
  1330. static void fib6_gc_timer_cb(unsigned long arg)
  1331. {
  1332. fib6_run_gc(0, (struct net *)arg);
  1333. }
  1334. static int __net_init fib6_net_init(struct net *net)
  1335. {
  1336. size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
  1337. setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
  1338. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1339. if (!net->ipv6.rt6_stats)
  1340. goto out_timer;
  1341. /* Avoid false sharing : Use at least a full cache line */
  1342. size = max_t(size_t, size, L1_CACHE_BYTES);
  1343. net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1344. if (!net->ipv6.fib_table_hash)
  1345. goto out_rt6_stats;
  1346. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1347. GFP_KERNEL);
  1348. if (!net->ipv6.fib6_main_tbl)
  1349. goto out_fib_table_hash;
  1350. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1351. net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1352. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1353. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1354. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1355. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1356. GFP_KERNEL);
  1357. if (!net->ipv6.fib6_local_tbl)
  1358. goto out_fib6_main_tbl;
  1359. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1360. net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1361. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1362. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1363. #endif
  1364. fib6_tables_init(net);
  1365. return 0;
  1366. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1367. out_fib6_main_tbl:
  1368. kfree(net->ipv6.fib6_main_tbl);
  1369. #endif
  1370. out_fib_table_hash:
  1371. kfree(net->ipv6.fib_table_hash);
  1372. out_rt6_stats:
  1373. kfree(net->ipv6.rt6_stats);
  1374. out_timer:
  1375. return -ENOMEM;
  1376. }
  1377. static void fib6_net_exit(struct net *net)
  1378. {
  1379. rt6_ifdown(net, NULL);
  1380. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1381. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1382. kfree(net->ipv6.fib6_local_tbl);
  1383. #endif
  1384. kfree(net->ipv6.fib6_main_tbl);
  1385. kfree(net->ipv6.fib_table_hash);
  1386. kfree(net->ipv6.rt6_stats);
  1387. }
  1388. static struct pernet_operations fib6_net_ops = {
  1389. .init = fib6_net_init,
  1390. .exit = fib6_net_exit,
  1391. };
  1392. int __init fib6_init(void)
  1393. {
  1394. int ret = -ENOMEM;
  1395. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1396. sizeof(struct fib6_node),
  1397. 0, SLAB_HWCACHE_ALIGN,
  1398. NULL);
  1399. if (!fib6_node_kmem)
  1400. goto out;
  1401. ret = register_pernet_subsys(&fib6_net_ops);
  1402. if (ret)
  1403. goto out_kmem_cache_create;
  1404. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
  1405. NULL);
  1406. if (ret)
  1407. goto out_unregister_subsys;
  1408. out:
  1409. return ret;
  1410. out_unregister_subsys:
  1411. unregister_pernet_subsys(&fib6_net_ops);
  1412. out_kmem_cache_create:
  1413. kmem_cache_destroy(fib6_node_kmem);
  1414. goto out;
  1415. }
  1416. void fib6_gc_cleanup(void)
  1417. {
  1418. unregister_pernet_subsys(&fib6_net_ops);
  1419. kmem_cache_destroy(fib6_node_kmem);
  1420. }