ip6_fib.c 35 KB

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