ip6_fib.c 36 KB

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