ip6_fib.c 36 KB

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