ip6_fib.c 29 KB

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