ip6_fib.c 38 KB

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