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