ip6_fib.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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. */
  21. #include <linux/config.h>
  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. #ifdef CONFIG_PROC_FS
  30. #include <linux/proc_fs.h>
  31. #endif
  32. #include <net/ipv6.h>
  33. #include <net/ndisc.h>
  34. #include <net/addrconf.h>
  35. #include <net/ip6_fib.h>
  36. #include <net/ip6_route.h>
  37. #define RT6_DEBUG 2
  38. #if RT6_DEBUG >= 3
  39. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  40. #else
  41. #define RT6_TRACE(x...) do { ; } while (0)
  42. #endif
  43. struct rt6_statistics rt6_stats;
  44. static kmem_cache_t * fib6_node_kmem;
  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. int (*func)(struct rt6_info *, void *arg);
  59. void *arg;
  60. };
  61. DEFINE_RWLOCK(fib6_walker_lock);
  62. #ifdef CONFIG_IPV6_SUBTREES
  63. #define FWS_INIT FWS_S
  64. #define SUBTREE(fn) ((fn)->subtree)
  65. #else
  66. #define FWS_INIT FWS_L
  67. #define SUBTREE(fn) NULL
  68. #endif
  69. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
  70. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
  71. /*
  72. * A routing update causes an increase of the serial number on the
  73. * affected subtree. This allows for cached routes to be asynchronously
  74. * tested when modifications are made to the destination cache as a
  75. * result of redirects, path MTU changes, etc.
  76. */
  77. static __u32 rt_sernum;
  78. static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
  79. struct fib6_walker_t fib6_walker_list = {
  80. .prev = &fib6_walker_list,
  81. .next = &fib6_walker_list,
  82. };
  83. #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
  84. static __inline__ u32 fib6_new_sernum(void)
  85. {
  86. u32 n = ++rt_sernum;
  87. if ((__s32)n <= 0)
  88. rt_sernum = n = 1;
  89. return n;
  90. }
  91. /*
  92. * Auxiliary address test functions for the radix tree.
  93. *
  94. * These assume a 32bit processor (although it will work on
  95. * 64bit processors)
  96. */
  97. /*
  98. * test bit
  99. */
  100. static __inline__ int addr_bit_set(void *token, int fn_bit)
  101. {
  102. __u32 *addr = token;
  103. return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
  104. }
  105. /*
  106. * find the first different bit between two addresses
  107. * length of address must be a multiple of 32bits
  108. */
  109. static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
  110. {
  111. __u32 *a1 = token1;
  112. __u32 *a2 = token2;
  113. int i;
  114. addrlen >>= 2;
  115. for (i = 0; i < addrlen; i++) {
  116. __u32 xb;
  117. xb = a1[i] ^ a2[i];
  118. if (xb) {
  119. int j = 31;
  120. xb = ntohl(xb);
  121. while ((xb & (1 << j)) == 0)
  122. j--;
  123. return (i * 32 + 31 - j);
  124. }
  125. }
  126. /*
  127. * we should *never* get to this point since that
  128. * would mean the addrs are equal
  129. *
  130. * However, we do get to it 8) And exacly, when
  131. * addresses are equal 8)
  132. *
  133. * ip route add 1111::/128 via ...
  134. * ip route add 1111::/64 via ...
  135. * and we are here.
  136. *
  137. * Ideally, this function should stop comparison
  138. * at prefix length. It does not, but it is still OK,
  139. * if returned value is greater than prefix length.
  140. * --ANK (980803)
  141. */
  142. return addrlen<<5;
  143. }
  144. static __inline__ struct fib6_node * node_alloc(void)
  145. {
  146. struct fib6_node *fn;
  147. if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
  148. memset(fn, 0, sizeof(struct fib6_node));
  149. return fn;
  150. }
  151. static __inline__ void node_free(struct fib6_node * fn)
  152. {
  153. kmem_cache_free(fib6_node_kmem, fn);
  154. }
  155. static __inline__ void rt6_release(struct rt6_info *rt)
  156. {
  157. if (atomic_dec_and_test(&rt->rt6i_ref))
  158. dst_free(&rt->u.dst);
  159. }
  160. /*
  161. * Routing Table
  162. *
  163. * return the appropriate node for a routing tree "add" operation
  164. * by either creating and inserting or by returning an existing
  165. * node.
  166. */
  167. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  168. int addrlen, int plen,
  169. int offset)
  170. {
  171. struct fib6_node *fn, *in, *ln;
  172. struct fib6_node *pn = NULL;
  173. struct rt6key *key;
  174. int bit;
  175. int dir = 0;
  176. __u32 sernum = fib6_new_sernum();
  177. RT6_TRACE("fib6_add_1\n");
  178. /* insert node in tree */
  179. fn = root;
  180. do {
  181. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  182. /*
  183. * Prefix match
  184. */
  185. if (plen < fn->fn_bit ||
  186. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  187. goto insert_above;
  188. /*
  189. * Exact match ?
  190. */
  191. if (plen == fn->fn_bit) {
  192. /* clean up an intermediate node */
  193. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  194. rt6_release(fn->leaf);
  195. fn->leaf = NULL;
  196. }
  197. fn->fn_sernum = sernum;
  198. return fn;
  199. }
  200. /*
  201. * We have more bits to go
  202. */
  203. /* Try to walk down on tree. */
  204. fn->fn_sernum = sernum;
  205. dir = addr_bit_set(addr, fn->fn_bit);
  206. pn = fn;
  207. fn = dir ? fn->right: fn->left;
  208. } while (fn);
  209. /*
  210. * We walked to the bottom of tree.
  211. * Create new leaf node without children.
  212. */
  213. ln = node_alloc();
  214. if (ln == NULL)
  215. return NULL;
  216. ln->fn_bit = plen;
  217. ln->parent = pn;
  218. ln->fn_sernum = sernum;
  219. if (dir)
  220. pn->right = ln;
  221. else
  222. pn->left = ln;
  223. return ln;
  224. insert_above:
  225. /*
  226. * split since we don't have a common prefix anymore or
  227. * we have a less significant route.
  228. * we've to insert an intermediate node on the list
  229. * this new node will point to the one we need to create
  230. * and the current
  231. */
  232. pn = fn->parent;
  233. /* find 1st bit in difference between the 2 addrs.
  234. See comment in addr_diff: bit may be an invalid value,
  235. but if it is >= plen, the value is ignored in any case.
  236. */
  237. bit = addr_diff(addr, &key->addr, addrlen);
  238. /*
  239. * (intermediate)[in]
  240. * / \
  241. * (new leaf node)[ln] (old node)[fn]
  242. */
  243. if (plen > bit) {
  244. in = node_alloc();
  245. ln = node_alloc();
  246. if (in == NULL || ln == NULL) {
  247. if (in)
  248. node_free(in);
  249. if (ln)
  250. node_free(ln);
  251. return NULL;
  252. }
  253. /*
  254. * new intermediate node.
  255. * RTN_RTINFO will
  256. * be off since that an address that chooses one of
  257. * the branches would not match less specific routes
  258. * in the other branch
  259. */
  260. in->fn_bit = bit;
  261. in->parent = pn;
  262. in->leaf = fn->leaf;
  263. atomic_inc(&in->leaf->rt6i_ref);
  264. in->fn_sernum = sernum;
  265. /* update parent pointer */
  266. if (dir)
  267. pn->right = in;
  268. else
  269. pn->left = in;
  270. ln->fn_bit = plen;
  271. ln->parent = in;
  272. fn->parent = in;
  273. ln->fn_sernum = sernum;
  274. if (addr_bit_set(addr, bit)) {
  275. in->right = ln;
  276. in->left = fn;
  277. } else {
  278. in->left = ln;
  279. in->right = fn;
  280. }
  281. } else { /* plen <= bit */
  282. /*
  283. * (new leaf node)[ln]
  284. * / \
  285. * (old node)[fn] NULL
  286. */
  287. ln = node_alloc();
  288. if (ln == NULL)
  289. return NULL;
  290. ln->fn_bit = plen;
  291. ln->parent = pn;
  292. ln->fn_sernum = sernum;
  293. if (dir)
  294. pn->right = ln;
  295. else
  296. pn->left = ln;
  297. if (addr_bit_set(&key->addr, plen))
  298. ln->right = fn;
  299. else
  300. ln->left = fn;
  301. fn->parent = ln;
  302. }
  303. return ln;
  304. }
  305. /*
  306. * Insert routing information in a node.
  307. */
  308. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  309. struct nlmsghdr *nlh, struct netlink_skb_parms *req)
  310. {
  311. struct rt6_info *iter = NULL;
  312. struct rt6_info **ins;
  313. ins = &fn->leaf;
  314. if (fn->fn_flags&RTN_TL_ROOT &&
  315. fn->leaf == &ip6_null_entry &&
  316. !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
  317. fn->leaf = rt;
  318. rt->u.next = NULL;
  319. goto out;
  320. }
  321. for (iter = fn->leaf; iter; iter=iter->u.next) {
  322. /*
  323. * Search for duplicates
  324. */
  325. if (iter->rt6i_metric == rt->rt6i_metric) {
  326. /*
  327. * Same priority level
  328. */
  329. if (iter->rt6i_dev == rt->rt6i_dev &&
  330. iter->rt6i_idev == rt->rt6i_idev &&
  331. ipv6_addr_equal(&iter->rt6i_gateway,
  332. &rt->rt6i_gateway)) {
  333. if (!(iter->rt6i_flags&RTF_EXPIRES))
  334. return -EEXIST;
  335. iter->rt6i_expires = rt->rt6i_expires;
  336. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  337. iter->rt6i_flags &= ~RTF_EXPIRES;
  338. iter->rt6i_expires = 0;
  339. }
  340. return -EEXIST;
  341. }
  342. }
  343. if (iter->rt6i_metric > rt->rt6i_metric)
  344. break;
  345. ins = &iter->u.next;
  346. }
  347. /*
  348. * insert node
  349. */
  350. out:
  351. rt->u.next = iter;
  352. *ins = rt;
  353. rt->rt6i_node = fn;
  354. atomic_inc(&rt->rt6i_ref);
  355. inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
  356. rt6_stats.fib_rt_entries++;
  357. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  358. rt6_stats.fib_route_nodes++;
  359. fn->fn_flags |= RTN_RTINFO;
  360. }
  361. return 0;
  362. }
  363. static __inline__ void fib6_start_gc(struct rt6_info *rt)
  364. {
  365. if (ip6_fib_timer.expires == 0 &&
  366. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  367. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  368. }
  369. void fib6_force_start_gc(void)
  370. {
  371. if (ip6_fib_timer.expires == 0)
  372. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  373. }
  374. /*
  375. * Add routing information to the routing tree.
  376. * <destination addr>/<source addr>
  377. * with source addr info in sub-trees
  378. */
  379. int fib6_add(struct fib6_node *root, struct rt6_info *rt,
  380. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  381. {
  382. struct fib6_node *fn;
  383. int err = -ENOMEM;
  384. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  385. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  386. if (fn == NULL)
  387. goto out;
  388. #ifdef CONFIG_IPV6_SUBTREES
  389. if (rt->rt6i_src.plen) {
  390. struct fib6_node *sn;
  391. if (fn->subtree == NULL) {
  392. struct fib6_node *sfn;
  393. /*
  394. * Create subtree.
  395. *
  396. * fn[main tree]
  397. * |
  398. * sfn[subtree root]
  399. * \
  400. * sn[new leaf node]
  401. */
  402. /* Create subtree root node */
  403. sfn = node_alloc();
  404. if (sfn == NULL)
  405. goto st_failure;
  406. sfn->leaf = &ip6_null_entry;
  407. atomic_inc(&ip6_null_entry.rt6i_ref);
  408. sfn->fn_flags = RTN_ROOT;
  409. sfn->fn_sernum = fib6_new_sernum();
  410. /* Now add the first leaf node to new subtree */
  411. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  412. sizeof(struct in6_addr), rt->rt6i_src.plen,
  413. offsetof(struct rt6_info, rt6i_src));
  414. if (sn == NULL) {
  415. /* If it is failed, discard just allocated
  416. root, and then (in st_failure) stale node
  417. in main tree.
  418. */
  419. node_free(sfn);
  420. goto st_failure;
  421. }
  422. /* Now link new subtree to main tree */
  423. sfn->parent = fn;
  424. fn->subtree = sfn;
  425. if (fn->leaf == NULL) {
  426. fn->leaf = rt;
  427. atomic_inc(&rt->rt6i_ref);
  428. }
  429. } else {
  430. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  431. sizeof(struct in6_addr), rt->rt6i_src.plen,
  432. offsetof(struct rt6_info, rt6i_src));
  433. if (sn == NULL)
  434. goto st_failure;
  435. }
  436. fn = sn;
  437. }
  438. #endif
  439. err = fib6_add_rt2node(fn, rt, nlh, req);
  440. if (err == 0) {
  441. fib6_start_gc(rt);
  442. if (!(rt->rt6i_flags&RTF_CACHE))
  443. fib6_prune_clones(fn, rt);
  444. }
  445. out:
  446. if (err)
  447. dst_free(&rt->u.dst);
  448. return err;
  449. #ifdef CONFIG_IPV6_SUBTREES
  450. /* Subtree creation failed, probably main tree node
  451. is orphan. If it is, shoot it.
  452. */
  453. st_failure:
  454. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  455. fib6_repair_tree(fn);
  456. dst_free(&rt->u.dst);
  457. return err;
  458. #endif
  459. }
  460. /*
  461. * Routing tree lookup
  462. *
  463. */
  464. struct lookup_args {
  465. int offset; /* key offset on rt6_info */
  466. struct in6_addr *addr; /* search key */
  467. };
  468. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  469. struct lookup_args *args)
  470. {
  471. struct fib6_node *fn;
  472. int dir;
  473. /*
  474. * Descend on a tree
  475. */
  476. fn = root;
  477. for (;;) {
  478. struct fib6_node *next;
  479. dir = addr_bit_set(args->addr, fn->fn_bit);
  480. next = dir ? fn->right : fn->left;
  481. if (next) {
  482. fn = next;
  483. continue;
  484. }
  485. break;
  486. }
  487. while ((fn->fn_flags & RTN_ROOT) == 0) {
  488. #ifdef CONFIG_IPV6_SUBTREES
  489. if (fn->subtree) {
  490. struct fib6_node *st;
  491. struct lookup_args *narg;
  492. narg = args + 1;
  493. if (narg->addr) {
  494. st = fib6_lookup_1(fn->subtree, narg);
  495. if (st && !(st->fn_flags & RTN_ROOT))
  496. return st;
  497. }
  498. }
  499. #endif
  500. if (fn->fn_flags & RTN_RTINFO) {
  501. struct rt6key *key;
  502. key = (struct rt6key *) ((u8 *) fn->leaf +
  503. args->offset);
  504. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
  505. return fn;
  506. }
  507. fn = fn->parent;
  508. }
  509. return NULL;
  510. }
  511. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  512. struct in6_addr *saddr)
  513. {
  514. struct lookup_args args[2];
  515. struct fib6_node *fn;
  516. args[0].offset = offsetof(struct rt6_info, rt6i_dst);
  517. args[0].addr = daddr;
  518. #ifdef CONFIG_IPV6_SUBTREES
  519. args[1].offset = offsetof(struct rt6_info, rt6i_src);
  520. args[1].addr = saddr;
  521. #endif
  522. fn = fib6_lookup_1(root, args);
  523. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  524. fn = root;
  525. return fn;
  526. }
  527. /*
  528. * Get node with specified destination prefix (and source prefix,
  529. * if subtrees are used)
  530. */
  531. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  532. struct in6_addr *addr,
  533. int plen, int offset)
  534. {
  535. struct fib6_node *fn;
  536. for (fn = root; fn ; ) {
  537. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  538. /*
  539. * Prefix match
  540. */
  541. if (plen < fn->fn_bit ||
  542. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  543. return NULL;
  544. if (plen == fn->fn_bit)
  545. return fn;
  546. /*
  547. * We have more bits to go
  548. */
  549. if (addr_bit_set(addr, fn->fn_bit))
  550. fn = fn->right;
  551. else
  552. fn = fn->left;
  553. }
  554. return NULL;
  555. }
  556. struct fib6_node * fib6_locate(struct fib6_node *root,
  557. struct in6_addr *daddr, int dst_len,
  558. struct in6_addr *saddr, int src_len)
  559. {
  560. struct fib6_node *fn;
  561. fn = fib6_locate_1(root, daddr, dst_len,
  562. offsetof(struct rt6_info, rt6i_dst));
  563. #ifdef CONFIG_IPV6_SUBTREES
  564. if (src_len) {
  565. BUG_TRAP(saddr!=NULL);
  566. if (fn == NULL)
  567. fn = fn->subtree;
  568. if (fn)
  569. fn = fib6_locate_1(fn, saddr, src_len,
  570. offsetof(struct rt6_info, rt6i_src));
  571. }
  572. #endif
  573. if (fn && fn->fn_flags&RTN_RTINFO)
  574. return fn;
  575. return NULL;
  576. }
  577. /*
  578. * Deletion
  579. *
  580. */
  581. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  582. {
  583. if (fn->fn_flags&RTN_ROOT)
  584. return &ip6_null_entry;
  585. while(fn) {
  586. if(fn->left)
  587. return fn->left->leaf;
  588. if(fn->right)
  589. return fn->right->leaf;
  590. fn = SUBTREE(fn);
  591. }
  592. return NULL;
  593. }
  594. /*
  595. * Called to trim the tree of intermediate nodes when possible. "fn"
  596. * is the node we want to try and remove.
  597. */
  598. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
  599. {
  600. int children;
  601. int nstate;
  602. struct fib6_node *child, *pn;
  603. struct fib6_walker_t *w;
  604. int iter = 0;
  605. for (;;) {
  606. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  607. iter++;
  608. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  609. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  610. BUG_TRAP(fn->leaf==NULL);
  611. children = 0;
  612. child = NULL;
  613. if (fn->right) child = fn->right, children |= 1;
  614. if (fn->left) child = fn->left, children |= 2;
  615. if (children == 3 || SUBTREE(fn)
  616. #ifdef CONFIG_IPV6_SUBTREES
  617. /* Subtree root (i.e. fn) may have one child */
  618. || (children && fn->fn_flags&RTN_ROOT)
  619. #endif
  620. ) {
  621. fn->leaf = fib6_find_prefix(fn);
  622. #if RT6_DEBUG >= 2
  623. if (fn->leaf==NULL) {
  624. BUG_TRAP(fn->leaf);
  625. fn->leaf = &ip6_null_entry;
  626. }
  627. #endif
  628. atomic_inc(&fn->leaf->rt6i_ref);
  629. return fn->parent;
  630. }
  631. pn = fn->parent;
  632. #ifdef CONFIG_IPV6_SUBTREES
  633. if (SUBTREE(pn) == fn) {
  634. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  635. SUBTREE(pn) = NULL;
  636. nstate = FWS_L;
  637. } else {
  638. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  639. #endif
  640. if (pn->right == fn) pn->right = child;
  641. else if (pn->left == fn) pn->left = child;
  642. #if RT6_DEBUG >= 2
  643. else BUG_TRAP(0);
  644. #endif
  645. if (child)
  646. child->parent = pn;
  647. nstate = FWS_R;
  648. #ifdef CONFIG_IPV6_SUBTREES
  649. }
  650. #endif
  651. read_lock(&fib6_walker_lock);
  652. FOR_WALKERS(w) {
  653. if (child == NULL) {
  654. if (w->root == fn) {
  655. w->root = w->node = NULL;
  656. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  657. } else if (w->node == fn) {
  658. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  659. w->node = pn;
  660. w->state = nstate;
  661. }
  662. } else {
  663. if (w->root == fn) {
  664. w->root = child;
  665. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  666. }
  667. if (w->node == fn) {
  668. w->node = child;
  669. if (children&2) {
  670. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  671. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  672. } else {
  673. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  674. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  675. }
  676. }
  677. }
  678. }
  679. read_unlock(&fib6_walker_lock);
  680. node_free(fn);
  681. if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
  682. return pn;
  683. rt6_release(pn->leaf);
  684. pn->leaf = NULL;
  685. fn = pn;
  686. }
  687. }
  688. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  689. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  690. {
  691. struct fib6_walker_t *w;
  692. struct rt6_info *rt = *rtp;
  693. RT6_TRACE("fib6_del_route\n");
  694. /* Unlink it */
  695. *rtp = rt->u.next;
  696. rt->rt6i_node = NULL;
  697. rt6_stats.fib_rt_entries--;
  698. rt6_stats.fib_discarded_routes++;
  699. /* Adjust walkers */
  700. read_lock(&fib6_walker_lock);
  701. FOR_WALKERS(w) {
  702. if (w->state == FWS_C && w->leaf == rt) {
  703. RT6_TRACE("walker %p adjusted by delroute\n", w);
  704. w->leaf = rt->u.next;
  705. if (w->leaf == NULL)
  706. w->state = FWS_U;
  707. }
  708. }
  709. read_unlock(&fib6_walker_lock);
  710. rt->u.next = NULL;
  711. if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
  712. fn->leaf = &ip6_null_entry;
  713. /* If it was last route, expunge its radix tree node */
  714. if (fn->leaf == NULL) {
  715. fn->fn_flags &= ~RTN_RTINFO;
  716. rt6_stats.fib_route_nodes--;
  717. fn = fib6_repair_tree(fn);
  718. }
  719. if (atomic_read(&rt->rt6i_ref) != 1) {
  720. /* This route is used as dummy address holder in some split
  721. * nodes. It is not leaked, but it still holds other resources,
  722. * which must be released in time. So, scan ascendant nodes
  723. * and replace dummy references to this route with references
  724. * to still alive ones.
  725. */
  726. while (fn) {
  727. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  728. fn->leaf = fib6_find_prefix(fn);
  729. atomic_inc(&fn->leaf->rt6i_ref);
  730. rt6_release(rt);
  731. }
  732. fn = fn->parent;
  733. }
  734. /* No more references are possible at this point. */
  735. if (atomic_read(&rt->rt6i_ref) != 1) BUG();
  736. }
  737. inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
  738. rt6_release(rt);
  739. }
  740. int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  741. {
  742. struct fib6_node *fn = rt->rt6i_node;
  743. struct rt6_info **rtp;
  744. #if RT6_DEBUG >= 2
  745. if (rt->u.dst.obsolete>0) {
  746. BUG_TRAP(fn==NULL);
  747. return -ENOENT;
  748. }
  749. #endif
  750. if (fn == NULL || rt == &ip6_null_entry)
  751. return -ENOENT;
  752. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  753. if (!(rt->rt6i_flags&RTF_CACHE))
  754. fib6_prune_clones(fn, rt);
  755. /*
  756. * Walk the leaf entries looking for ourself
  757. */
  758. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
  759. if (*rtp == rt) {
  760. fib6_del_route(fn, rtp, nlh, _rtattr, req);
  761. return 0;
  762. }
  763. }
  764. return -ENOENT;
  765. }
  766. /*
  767. * Tree traversal function.
  768. *
  769. * Certainly, it is not interrupt safe.
  770. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  771. * It means, that we can modify tree during walking
  772. * and use this function for garbage collection, clone pruning,
  773. * cleaning tree when a device goes down etc. etc.
  774. *
  775. * It guarantees that every node will be traversed,
  776. * and that it will be traversed only once.
  777. *
  778. * Callback function w->func may return:
  779. * 0 -> continue walking.
  780. * positive value -> walking is suspended (used by tree dumps,
  781. * and probably by gc, if it will be split to several slices)
  782. * negative value -> terminate walking.
  783. *
  784. * The function itself returns:
  785. * 0 -> walk is complete.
  786. * >0 -> walk is incomplete (i.e. suspended)
  787. * <0 -> walk is terminated by an error.
  788. */
  789. int fib6_walk_continue(struct fib6_walker_t *w)
  790. {
  791. struct fib6_node *fn, *pn;
  792. for (;;) {
  793. fn = w->node;
  794. if (fn == NULL)
  795. return 0;
  796. if (w->prune && fn != w->root &&
  797. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  798. w->state = FWS_C;
  799. w->leaf = fn->leaf;
  800. }
  801. switch (w->state) {
  802. #ifdef CONFIG_IPV6_SUBTREES
  803. case FWS_S:
  804. if (SUBTREE(fn)) {
  805. w->node = SUBTREE(fn);
  806. continue;
  807. }
  808. w->state = FWS_L;
  809. #endif
  810. case FWS_L:
  811. if (fn->left) {
  812. w->node = fn->left;
  813. w->state = FWS_INIT;
  814. continue;
  815. }
  816. w->state = FWS_R;
  817. case FWS_R:
  818. if (fn->right) {
  819. w->node = fn->right;
  820. w->state = FWS_INIT;
  821. continue;
  822. }
  823. w->state = FWS_C;
  824. w->leaf = fn->leaf;
  825. case FWS_C:
  826. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  827. int err = w->func(w);
  828. if (err)
  829. return err;
  830. continue;
  831. }
  832. w->state = FWS_U;
  833. case FWS_U:
  834. if (fn == w->root)
  835. return 0;
  836. pn = fn->parent;
  837. w->node = pn;
  838. #ifdef CONFIG_IPV6_SUBTREES
  839. if (SUBTREE(pn) == fn) {
  840. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  841. w->state = FWS_L;
  842. continue;
  843. }
  844. #endif
  845. if (pn->left == fn) {
  846. w->state = FWS_R;
  847. continue;
  848. }
  849. if (pn->right == fn) {
  850. w->state = FWS_C;
  851. w->leaf = w->node->leaf;
  852. continue;
  853. }
  854. #if RT6_DEBUG >= 2
  855. BUG_TRAP(0);
  856. #endif
  857. }
  858. }
  859. }
  860. int fib6_walk(struct fib6_walker_t *w)
  861. {
  862. int res;
  863. w->state = FWS_INIT;
  864. w->node = w->root;
  865. fib6_walker_link(w);
  866. res = fib6_walk_continue(w);
  867. if (res <= 0)
  868. fib6_walker_unlink(w);
  869. return res;
  870. }
  871. static int fib6_clean_node(struct fib6_walker_t *w)
  872. {
  873. int res;
  874. struct rt6_info *rt;
  875. struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
  876. for (rt = w->leaf; rt; rt = rt->u.next) {
  877. res = c->func(rt, c->arg);
  878. if (res < 0) {
  879. w->leaf = rt;
  880. res = fib6_del(rt, NULL, NULL, NULL);
  881. if (res) {
  882. #if RT6_DEBUG >= 2
  883. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  884. #endif
  885. continue;
  886. }
  887. return 0;
  888. }
  889. BUG_TRAP(res==0);
  890. }
  891. w->leaf = rt;
  892. return 0;
  893. }
  894. /*
  895. * Convenient frontend to tree walker.
  896. *
  897. * func is called on each route.
  898. * It may return -1 -> delete this route.
  899. * 0 -> continue walking
  900. *
  901. * prune==1 -> only immediate children of node (certainly,
  902. * ignoring pure split nodes) will be scanned.
  903. */
  904. void fib6_clean_tree(struct fib6_node *root,
  905. int (*func)(struct rt6_info *, void *arg),
  906. int prune, void *arg)
  907. {
  908. struct fib6_cleaner_t c;
  909. c.w.root = root;
  910. c.w.func = fib6_clean_node;
  911. c.w.prune = prune;
  912. c.func = func;
  913. c.arg = arg;
  914. fib6_walk(&c.w);
  915. }
  916. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  917. {
  918. if (rt->rt6i_flags & RTF_CACHE) {
  919. RT6_TRACE("pruning clone %p\n", rt);
  920. return -1;
  921. }
  922. return 0;
  923. }
  924. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
  925. {
  926. fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
  927. }
  928. /*
  929. * Garbage collection
  930. */
  931. static struct fib6_gc_args
  932. {
  933. int timeout;
  934. int more;
  935. } gc_args;
  936. static int fib6_age(struct rt6_info *rt, void *arg)
  937. {
  938. unsigned long now = jiffies;
  939. /*
  940. * check addrconf expiration here.
  941. * Routes are expired even if they are in use.
  942. *
  943. * Also age clones. Note, that clones are aged out
  944. * only if they are not in use now.
  945. */
  946. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  947. if (time_after(now, rt->rt6i_expires)) {
  948. RT6_TRACE("expiring %p\n", rt);
  949. rt6_reset_dflt_pointer(rt);
  950. return -1;
  951. }
  952. gc_args.more++;
  953. } else if (rt->rt6i_flags & RTF_CACHE) {
  954. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  955. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  956. RT6_TRACE("aging clone %p\n", rt);
  957. return -1;
  958. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  959. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  960. RT6_TRACE("purging route %p via non-router but gateway\n",
  961. rt);
  962. return -1;
  963. }
  964. gc_args.more++;
  965. }
  966. return 0;
  967. }
  968. static DEFINE_SPINLOCK(fib6_gc_lock);
  969. void fib6_run_gc(unsigned long dummy)
  970. {
  971. if (dummy != ~0UL) {
  972. spin_lock_bh(&fib6_gc_lock);
  973. gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
  974. } else {
  975. local_bh_disable();
  976. if (!spin_trylock(&fib6_gc_lock)) {
  977. mod_timer(&ip6_fib_timer, jiffies + HZ);
  978. local_bh_enable();
  979. return;
  980. }
  981. gc_args.timeout = ip6_rt_gc_interval;
  982. }
  983. gc_args.more = 0;
  984. write_lock_bh(&rt6_lock);
  985. ndisc_dst_gc(&gc_args.more);
  986. fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
  987. write_unlock_bh(&rt6_lock);
  988. if (gc_args.more)
  989. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  990. else {
  991. del_timer(&ip6_fib_timer);
  992. ip6_fib_timer.expires = 0;
  993. }
  994. spin_unlock_bh(&fib6_gc_lock);
  995. }
  996. void __init fib6_init(void)
  997. {
  998. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  999. sizeof(struct fib6_node),
  1000. 0, SLAB_HWCACHE_ALIGN,
  1001. NULL, NULL);
  1002. if (!fib6_node_kmem)
  1003. panic("cannot create fib6_nodes cache");
  1004. }
  1005. void fib6_gc_cleanup(void)
  1006. {
  1007. del_timer(&ip6_fib_timer);
  1008. kmem_cache_destroy(fib6_node_kmem);
  1009. }