ip6_fib.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 nlmsghdr *nlh, struct netlink_skb_parms *req)
  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, nlh, req);
  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,
  570. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  571. {
  572. struct fib6_node *fn;
  573. int err = -ENOMEM;
  574. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  575. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  576. if (fn == NULL)
  577. goto out;
  578. #ifdef CONFIG_IPV6_SUBTREES
  579. if (rt->rt6i_src.plen) {
  580. struct fib6_node *sn;
  581. if (fn->subtree == NULL) {
  582. struct fib6_node *sfn;
  583. /*
  584. * Create subtree.
  585. *
  586. * fn[main tree]
  587. * |
  588. * sfn[subtree root]
  589. * \
  590. * sn[new leaf node]
  591. */
  592. /* Create subtree root node */
  593. sfn = node_alloc();
  594. if (sfn == NULL)
  595. goto st_failure;
  596. sfn->leaf = &ip6_null_entry;
  597. atomic_inc(&ip6_null_entry.rt6i_ref);
  598. sfn->fn_flags = RTN_ROOT;
  599. sfn->fn_sernum = fib6_new_sernum();
  600. /* Now add the first leaf node to new subtree */
  601. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  602. sizeof(struct in6_addr), rt->rt6i_src.plen,
  603. offsetof(struct rt6_info, rt6i_src));
  604. if (sn == NULL) {
  605. /* If it is failed, discard just allocated
  606. root, and then (in st_failure) stale node
  607. in main tree.
  608. */
  609. node_free(sfn);
  610. goto st_failure;
  611. }
  612. /* Now link new subtree to main tree */
  613. sfn->parent = fn;
  614. fn->subtree = sfn;
  615. if (fn->leaf == NULL) {
  616. fn->leaf = rt;
  617. atomic_inc(&rt->rt6i_ref);
  618. }
  619. } else {
  620. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  621. sizeof(struct in6_addr), rt->rt6i_src.plen,
  622. offsetof(struct rt6_info, rt6i_src));
  623. if (sn == NULL)
  624. goto st_failure;
  625. }
  626. fn = sn;
  627. }
  628. #endif
  629. err = fib6_add_rt2node(fn, rt, nlh, req);
  630. if (err == 0) {
  631. fib6_start_gc(rt);
  632. if (!(rt->rt6i_flags&RTF_CACHE))
  633. fib6_prune_clones(fn, rt);
  634. }
  635. out:
  636. if (err)
  637. dst_free(&rt->u.dst);
  638. return err;
  639. #ifdef CONFIG_IPV6_SUBTREES
  640. /* Subtree creation failed, probably main tree node
  641. is orphan. If it is, shoot it.
  642. */
  643. st_failure:
  644. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  645. fib6_repair_tree(fn);
  646. dst_free(&rt->u.dst);
  647. return err;
  648. #endif
  649. }
  650. /*
  651. * Routing tree lookup
  652. *
  653. */
  654. struct lookup_args {
  655. int offset; /* key offset on rt6_info */
  656. struct in6_addr *addr; /* search key */
  657. };
  658. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  659. struct lookup_args *args)
  660. {
  661. struct fib6_node *fn;
  662. int dir;
  663. /*
  664. * Descend on a tree
  665. */
  666. fn = root;
  667. for (;;) {
  668. struct fib6_node *next;
  669. dir = addr_bit_set(args->addr, fn->fn_bit);
  670. next = dir ? fn->right : fn->left;
  671. if (next) {
  672. fn = next;
  673. continue;
  674. }
  675. break;
  676. }
  677. while ((fn->fn_flags & RTN_ROOT) == 0) {
  678. #ifdef CONFIG_IPV6_SUBTREES
  679. if (fn->subtree) {
  680. struct fib6_node *st;
  681. struct lookup_args *narg;
  682. narg = args + 1;
  683. if (narg->addr) {
  684. st = fib6_lookup_1(fn->subtree, narg);
  685. if (st && !(st->fn_flags & RTN_ROOT))
  686. return st;
  687. }
  688. }
  689. #endif
  690. if (fn->fn_flags & RTN_RTINFO) {
  691. struct rt6key *key;
  692. key = (struct rt6key *) ((u8 *) fn->leaf +
  693. args->offset);
  694. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
  695. return fn;
  696. }
  697. fn = fn->parent;
  698. }
  699. return NULL;
  700. }
  701. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  702. struct in6_addr *saddr)
  703. {
  704. struct lookup_args args[2];
  705. struct fib6_node *fn;
  706. args[0].offset = offsetof(struct rt6_info, rt6i_dst);
  707. args[0].addr = daddr;
  708. #ifdef CONFIG_IPV6_SUBTREES
  709. args[1].offset = offsetof(struct rt6_info, rt6i_src);
  710. args[1].addr = saddr;
  711. #endif
  712. fn = fib6_lookup_1(root, args);
  713. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  714. fn = root;
  715. return fn;
  716. }
  717. /*
  718. * Get node with specified destination prefix (and source prefix,
  719. * if subtrees are used)
  720. */
  721. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  722. struct in6_addr *addr,
  723. int plen, int offset)
  724. {
  725. struct fib6_node *fn;
  726. for (fn = root; fn ; ) {
  727. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  728. /*
  729. * Prefix match
  730. */
  731. if (plen < fn->fn_bit ||
  732. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  733. return NULL;
  734. if (plen == fn->fn_bit)
  735. return fn;
  736. /*
  737. * We have more bits to go
  738. */
  739. if (addr_bit_set(addr, fn->fn_bit))
  740. fn = fn->right;
  741. else
  742. fn = fn->left;
  743. }
  744. return NULL;
  745. }
  746. struct fib6_node * fib6_locate(struct fib6_node *root,
  747. struct in6_addr *daddr, int dst_len,
  748. struct in6_addr *saddr, int src_len)
  749. {
  750. struct fib6_node *fn;
  751. fn = fib6_locate_1(root, daddr, dst_len,
  752. offsetof(struct rt6_info, rt6i_dst));
  753. #ifdef CONFIG_IPV6_SUBTREES
  754. if (src_len) {
  755. BUG_TRAP(saddr!=NULL);
  756. if (fn == NULL)
  757. fn = fn->subtree;
  758. if (fn)
  759. fn = fib6_locate_1(fn, saddr, src_len,
  760. offsetof(struct rt6_info, rt6i_src));
  761. }
  762. #endif
  763. if (fn && fn->fn_flags&RTN_RTINFO)
  764. return fn;
  765. return NULL;
  766. }
  767. /*
  768. * Deletion
  769. *
  770. */
  771. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  772. {
  773. if (fn->fn_flags&RTN_ROOT)
  774. return &ip6_null_entry;
  775. while(fn) {
  776. if(fn->left)
  777. return fn->left->leaf;
  778. if(fn->right)
  779. return fn->right->leaf;
  780. fn = SUBTREE(fn);
  781. }
  782. return NULL;
  783. }
  784. /*
  785. * Called to trim the tree of intermediate nodes when possible. "fn"
  786. * is the node we want to try and remove.
  787. */
  788. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
  789. {
  790. int children;
  791. int nstate;
  792. struct fib6_node *child, *pn;
  793. struct fib6_walker_t *w;
  794. int iter = 0;
  795. for (;;) {
  796. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  797. iter++;
  798. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  799. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  800. BUG_TRAP(fn->leaf==NULL);
  801. children = 0;
  802. child = NULL;
  803. if (fn->right) child = fn->right, children |= 1;
  804. if (fn->left) child = fn->left, children |= 2;
  805. if (children == 3 || SUBTREE(fn)
  806. #ifdef CONFIG_IPV6_SUBTREES
  807. /* Subtree root (i.e. fn) may have one child */
  808. || (children && fn->fn_flags&RTN_ROOT)
  809. #endif
  810. ) {
  811. fn->leaf = fib6_find_prefix(fn);
  812. #if RT6_DEBUG >= 2
  813. if (fn->leaf==NULL) {
  814. BUG_TRAP(fn->leaf);
  815. fn->leaf = &ip6_null_entry;
  816. }
  817. #endif
  818. atomic_inc(&fn->leaf->rt6i_ref);
  819. return fn->parent;
  820. }
  821. pn = fn->parent;
  822. #ifdef CONFIG_IPV6_SUBTREES
  823. if (SUBTREE(pn) == fn) {
  824. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  825. SUBTREE(pn) = NULL;
  826. nstate = FWS_L;
  827. } else {
  828. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  829. #endif
  830. if (pn->right == fn) pn->right = child;
  831. else if (pn->left == fn) pn->left = child;
  832. #if RT6_DEBUG >= 2
  833. else BUG_TRAP(0);
  834. #endif
  835. if (child)
  836. child->parent = pn;
  837. nstate = FWS_R;
  838. #ifdef CONFIG_IPV6_SUBTREES
  839. }
  840. #endif
  841. read_lock(&fib6_walker_lock);
  842. FOR_WALKERS(w) {
  843. if (child == NULL) {
  844. if (w->root == fn) {
  845. w->root = w->node = NULL;
  846. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  847. } else if (w->node == fn) {
  848. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  849. w->node = pn;
  850. w->state = nstate;
  851. }
  852. } else {
  853. if (w->root == fn) {
  854. w->root = child;
  855. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  856. }
  857. if (w->node == fn) {
  858. w->node = child;
  859. if (children&2) {
  860. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  861. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  862. } else {
  863. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  864. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  865. }
  866. }
  867. }
  868. }
  869. read_unlock(&fib6_walker_lock);
  870. node_free(fn);
  871. if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
  872. return pn;
  873. rt6_release(pn->leaf);
  874. pn->leaf = NULL;
  875. fn = pn;
  876. }
  877. }
  878. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  879. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  880. {
  881. struct fib6_walker_t *w;
  882. struct rt6_info *rt = *rtp;
  883. RT6_TRACE("fib6_del_route\n");
  884. /* Unlink it */
  885. *rtp = rt->u.next;
  886. rt->rt6i_node = NULL;
  887. rt6_stats.fib_rt_entries--;
  888. rt6_stats.fib_discarded_routes++;
  889. /* Adjust walkers */
  890. read_lock(&fib6_walker_lock);
  891. FOR_WALKERS(w) {
  892. if (w->state == FWS_C && w->leaf == rt) {
  893. RT6_TRACE("walker %p adjusted by delroute\n", w);
  894. w->leaf = rt->u.next;
  895. if (w->leaf == NULL)
  896. w->state = FWS_U;
  897. }
  898. }
  899. read_unlock(&fib6_walker_lock);
  900. rt->u.next = NULL;
  901. if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
  902. fn->leaf = &ip6_null_entry;
  903. /* If it was last route, expunge its radix tree node */
  904. if (fn->leaf == NULL) {
  905. fn->fn_flags &= ~RTN_RTINFO;
  906. rt6_stats.fib_route_nodes--;
  907. fn = fib6_repair_tree(fn);
  908. }
  909. if (atomic_read(&rt->rt6i_ref) != 1) {
  910. /* This route is used as dummy address holder in some split
  911. * nodes. It is not leaked, but it still holds other resources,
  912. * which must be released in time. So, scan ascendant nodes
  913. * and replace dummy references to this route with references
  914. * to still alive ones.
  915. */
  916. while (fn) {
  917. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  918. fn->leaf = fib6_find_prefix(fn);
  919. atomic_inc(&fn->leaf->rt6i_ref);
  920. rt6_release(rt);
  921. }
  922. fn = fn->parent;
  923. }
  924. /* No more references are possible at this point. */
  925. if (atomic_read(&rt->rt6i_ref) != 1) BUG();
  926. }
  927. inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
  928. rt6_release(rt);
  929. }
  930. int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  931. {
  932. struct fib6_node *fn = rt->rt6i_node;
  933. struct rt6_info **rtp;
  934. #if RT6_DEBUG >= 2
  935. if (rt->u.dst.obsolete>0) {
  936. BUG_TRAP(fn==NULL);
  937. return -ENOENT;
  938. }
  939. #endif
  940. if (fn == NULL || rt == &ip6_null_entry)
  941. return -ENOENT;
  942. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  943. if (!(rt->rt6i_flags&RTF_CACHE))
  944. fib6_prune_clones(fn, rt);
  945. /*
  946. * Walk the leaf entries looking for ourself
  947. */
  948. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
  949. if (*rtp == rt) {
  950. fib6_del_route(fn, rtp, nlh, _rtattr, req);
  951. return 0;
  952. }
  953. }
  954. return -ENOENT;
  955. }
  956. /*
  957. * Tree traversal function.
  958. *
  959. * Certainly, it is not interrupt safe.
  960. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  961. * It means, that we can modify tree during walking
  962. * and use this function for garbage collection, clone pruning,
  963. * cleaning tree when a device goes down etc. etc.
  964. *
  965. * It guarantees that every node will be traversed,
  966. * and that it will be traversed only once.
  967. *
  968. * Callback function w->func may return:
  969. * 0 -> continue walking.
  970. * positive value -> walking is suspended (used by tree dumps,
  971. * and probably by gc, if it will be split to several slices)
  972. * negative value -> terminate walking.
  973. *
  974. * The function itself returns:
  975. * 0 -> walk is complete.
  976. * >0 -> walk is incomplete (i.e. suspended)
  977. * <0 -> walk is terminated by an error.
  978. */
  979. static int fib6_walk_continue(struct fib6_walker_t *w)
  980. {
  981. struct fib6_node *fn, *pn;
  982. for (;;) {
  983. fn = w->node;
  984. if (fn == NULL)
  985. return 0;
  986. if (w->prune && fn != w->root &&
  987. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  988. w->state = FWS_C;
  989. w->leaf = fn->leaf;
  990. }
  991. switch (w->state) {
  992. #ifdef CONFIG_IPV6_SUBTREES
  993. case FWS_S:
  994. if (SUBTREE(fn)) {
  995. w->node = SUBTREE(fn);
  996. continue;
  997. }
  998. w->state = FWS_L;
  999. #endif
  1000. case FWS_L:
  1001. if (fn->left) {
  1002. w->node = fn->left;
  1003. w->state = FWS_INIT;
  1004. continue;
  1005. }
  1006. w->state = FWS_R;
  1007. case FWS_R:
  1008. if (fn->right) {
  1009. w->node = fn->right;
  1010. w->state = FWS_INIT;
  1011. continue;
  1012. }
  1013. w->state = FWS_C;
  1014. w->leaf = fn->leaf;
  1015. case FWS_C:
  1016. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  1017. int err = w->func(w);
  1018. if (err)
  1019. return err;
  1020. continue;
  1021. }
  1022. w->state = FWS_U;
  1023. case FWS_U:
  1024. if (fn == w->root)
  1025. return 0;
  1026. pn = fn->parent;
  1027. w->node = pn;
  1028. #ifdef CONFIG_IPV6_SUBTREES
  1029. if (SUBTREE(pn) == fn) {
  1030. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  1031. w->state = FWS_L;
  1032. continue;
  1033. }
  1034. #endif
  1035. if (pn->left == fn) {
  1036. w->state = FWS_R;
  1037. continue;
  1038. }
  1039. if (pn->right == fn) {
  1040. w->state = FWS_C;
  1041. w->leaf = w->node->leaf;
  1042. continue;
  1043. }
  1044. #if RT6_DEBUG >= 2
  1045. BUG_TRAP(0);
  1046. #endif
  1047. }
  1048. }
  1049. }
  1050. static int fib6_walk(struct fib6_walker_t *w)
  1051. {
  1052. int res;
  1053. w->state = FWS_INIT;
  1054. w->node = w->root;
  1055. fib6_walker_link(w);
  1056. res = fib6_walk_continue(w);
  1057. if (res <= 0)
  1058. fib6_walker_unlink(w);
  1059. return res;
  1060. }
  1061. static int fib6_clean_node(struct fib6_walker_t *w)
  1062. {
  1063. int res;
  1064. struct rt6_info *rt;
  1065. struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
  1066. for (rt = w->leaf; rt; rt = rt->u.next) {
  1067. res = c->func(rt, c->arg);
  1068. if (res < 0) {
  1069. w->leaf = rt;
  1070. res = fib6_del(rt, NULL, NULL, NULL);
  1071. if (res) {
  1072. #if RT6_DEBUG >= 2
  1073. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  1074. #endif
  1075. continue;
  1076. }
  1077. return 0;
  1078. }
  1079. BUG_TRAP(res==0);
  1080. }
  1081. w->leaf = rt;
  1082. return 0;
  1083. }
  1084. /*
  1085. * Convenient frontend to tree walker.
  1086. *
  1087. * func is called on each route.
  1088. * It may return -1 -> delete this route.
  1089. * 0 -> continue walking
  1090. *
  1091. * prune==1 -> only immediate children of node (certainly,
  1092. * ignoring pure split nodes) will be scanned.
  1093. */
  1094. static void fib6_clean_tree(struct fib6_node *root,
  1095. int (*func)(struct rt6_info *, void *arg),
  1096. int prune, void *arg)
  1097. {
  1098. struct fib6_cleaner_t c;
  1099. c.w.root = root;
  1100. c.w.func = fib6_clean_node;
  1101. c.w.prune = prune;
  1102. c.func = func;
  1103. c.arg = arg;
  1104. fib6_walk(&c.w);
  1105. }
  1106. void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
  1107. int prune, void *arg)
  1108. {
  1109. struct fib6_table *table;
  1110. struct hlist_node *node;
  1111. unsigned int h;
  1112. rcu_read_lock();
  1113. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1114. hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
  1115. tb6_hlist) {
  1116. write_lock_bh(&table->tb6_lock);
  1117. fib6_clean_tree(&table->tb6_root, func, prune, arg);
  1118. write_unlock_bh(&table->tb6_lock);
  1119. }
  1120. }
  1121. rcu_read_unlock();
  1122. }
  1123. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1124. {
  1125. if (rt->rt6i_flags & RTF_CACHE) {
  1126. RT6_TRACE("pruning clone %p\n", rt);
  1127. return -1;
  1128. }
  1129. return 0;
  1130. }
  1131. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
  1132. {
  1133. fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
  1134. }
  1135. /*
  1136. * Garbage collection
  1137. */
  1138. static struct fib6_gc_args
  1139. {
  1140. int timeout;
  1141. int more;
  1142. } gc_args;
  1143. static int fib6_age(struct rt6_info *rt, void *arg)
  1144. {
  1145. unsigned long now = jiffies;
  1146. /*
  1147. * check addrconf expiration here.
  1148. * Routes are expired even if they are in use.
  1149. *
  1150. * Also age clones. Note, that clones are aged out
  1151. * only if they are not in use now.
  1152. */
  1153. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  1154. if (time_after(now, rt->rt6i_expires)) {
  1155. RT6_TRACE("expiring %p\n", rt);
  1156. return -1;
  1157. }
  1158. gc_args.more++;
  1159. } else if (rt->rt6i_flags & RTF_CACHE) {
  1160. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  1161. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  1162. RT6_TRACE("aging clone %p\n", rt);
  1163. return -1;
  1164. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  1165. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  1166. RT6_TRACE("purging route %p via non-router but gateway\n",
  1167. rt);
  1168. return -1;
  1169. }
  1170. gc_args.more++;
  1171. }
  1172. return 0;
  1173. }
  1174. static DEFINE_SPINLOCK(fib6_gc_lock);
  1175. void fib6_run_gc(unsigned long dummy)
  1176. {
  1177. if (dummy != ~0UL) {
  1178. spin_lock_bh(&fib6_gc_lock);
  1179. gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
  1180. } else {
  1181. local_bh_disable();
  1182. if (!spin_trylock(&fib6_gc_lock)) {
  1183. mod_timer(&ip6_fib_timer, jiffies + HZ);
  1184. local_bh_enable();
  1185. return;
  1186. }
  1187. gc_args.timeout = ip6_rt_gc_interval;
  1188. }
  1189. gc_args.more = 0;
  1190. ndisc_dst_gc(&gc_args.more);
  1191. fib6_clean_all(fib6_age, 0, NULL);
  1192. if (gc_args.more)
  1193. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  1194. else {
  1195. del_timer(&ip6_fib_timer);
  1196. ip6_fib_timer.expires = 0;
  1197. }
  1198. spin_unlock_bh(&fib6_gc_lock);
  1199. }
  1200. void __init fib6_init(void)
  1201. {
  1202. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1203. sizeof(struct fib6_node),
  1204. 0, SLAB_HWCACHE_ALIGN,
  1205. NULL, NULL);
  1206. if (!fib6_node_kmem)
  1207. panic("cannot create fib6_nodes cache");
  1208. fib6_tables_init();
  1209. }
  1210. void fib6_gc_cleanup(void)
  1211. {
  1212. del_timer(&ip6_fib_timer);
  1213. kmem_cache_destroy(fib6_node_kmem);
  1214. }