ip6_fib.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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,
  361. struct in6_addr *addr, 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, sizeof(*addr));
  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. static inline bool rt6_qualify_for_ecmp(struct rt6_info *rt)
  524. {
  525. return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
  526. RTF_GATEWAY;
  527. }
  528. /*
  529. * Insert routing information in a node.
  530. */
  531. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  532. struct nl_info *info)
  533. {
  534. struct rt6_info *iter = NULL;
  535. struct rt6_info **ins;
  536. int replace = (info->nlh &&
  537. (info->nlh->nlmsg_flags & NLM_F_REPLACE));
  538. int add = (!info->nlh ||
  539. (info->nlh->nlmsg_flags & NLM_F_CREATE));
  540. int found = 0;
  541. bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
  542. ins = &fn->leaf;
  543. for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
  544. /*
  545. * Search for duplicates
  546. */
  547. if (iter->rt6i_metric == rt->rt6i_metric) {
  548. /*
  549. * Same priority level
  550. */
  551. if (info->nlh &&
  552. (info->nlh->nlmsg_flags & NLM_F_EXCL))
  553. return -EEXIST;
  554. if (replace) {
  555. found++;
  556. break;
  557. }
  558. if (iter->dst.dev == rt->dst.dev &&
  559. iter->rt6i_idev == rt->rt6i_idev &&
  560. ipv6_addr_equal(&iter->rt6i_gateway,
  561. &rt->rt6i_gateway)) {
  562. if (rt->rt6i_nsiblings)
  563. rt->rt6i_nsiblings = 0;
  564. if (!(iter->rt6i_flags & RTF_EXPIRES))
  565. return -EEXIST;
  566. if (!(rt->rt6i_flags & RTF_EXPIRES))
  567. rt6_clean_expires(iter);
  568. else
  569. rt6_set_expires(iter, rt->dst.expires);
  570. return -EEXIST;
  571. }
  572. /* If we have the same destination and the same metric,
  573. * but not the same gateway, then the route we try to
  574. * add is sibling to this route, increment our counter
  575. * of siblings, and later we will add our route to the
  576. * list.
  577. * Only static routes (which don't have flag
  578. * RTF_EXPIRES) are used for ECMPv6.
  579. *
  580. * To avoid long list, we only had siblings if the
  581. * route have a gateway.
  582. */
  583. if (rt_can_ecmp &&
  584. rt6_qualify_for_ecmp(iter))
  585. rt->rt6i_nsiblings++;
  586. }
  587. if (iter->rt6i_metric > rt->rt6i_metric)
  588. break;
  589. ins = &iter->dst.rt6_next;
  590. }
  591. /* Reset round-robin state, if necessary */
  592. if (ins == &fn->leaf)
  593. fn->rr_ptr = NULL;
  594. /* Link this route to others same route. */
  595. if (rt->rt6i_nsiblings) {
  596. unsigned int rt6i_nsiblings;
  597. struct rt6_info *sibling, *temp_sibling;
  598. /* Find the first route that have the same metric */
  599. sibling = fn->leaf;
  600. while (sibling) {
  601. if (sibling->rt6i_metric == rt->rt6i_metric &&
  602. rt6_qualify_for_ecmp(sibling)) {
  603. list_add_tail(&rt->rt6i_siblings,
  604. &sibling->rt6i_siblings);
  605. break;
  606. }
  607. sibling = sibling->dst.rt6_next;
  608. }
  609. /* For each sibling in the list, increment the counter of
  610. * siblings. BUG() if counters does not match, list of siblings
  611. * is broken!
  612. */
  613. rt6i_nsiblings = 0;
  614. list_for_each_entry_safe(sibling, temp_sibling,
  615. &rt->rt6i_siblings, rt6i_siblings) {
  616. sibling->rt6i_nsiblings++;
  617. BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
  618. rt6i_nsiblings++;
  619. }
  620. BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
  621. }
  622. /*
  623. * insert node
  624. */
  625. if (!replace) {
  626. if (!add)
  627. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  628. add:
  629. rt->dst.rt6_next = iter;
  630. *ins = rt;
  631. rt->rt6i_node = fn;
  632. atomic_inc(&rt->rt6i_ref);
  633. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  634. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  635. if (!(fn->fn_flags & RTN_RTINFO)) {
  636. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  637. fn->fn_flags |= RTN_RTINFO;
  638. }
  639. } else {
  640. if (!found) {
  641. if (add)
  642. goto add;
  643. pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
  644. return -ENOENT;
  645. }
  646. *ins = rt;
  647. rt->rt6i_node = fn;
  648. rt->dst.rt6_next = iter->dst.rt6_next;
  649. atomic_inc(&rt->rt6i_ref);
  650. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  651. rt6_release(iter);
  652. if (!(fn->fn_flags & RTN_RTINFO)) {
  653. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  654. fn->fn_flags |= RTN_RTINFO;
  655. }
  656. }
  657. return 0;
  658. }
  659. static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
  660. {
  661. if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
  662. (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
  663. mod_timer(&net->ipv6.ip6_fib_timer,
  664. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  665. }
  666. void fib6_force_start_gc(struct net *net)
  667. {
  668. if (!timer_pending(&net->ipv6.ip6_fib_timer))
  669. mod_timer(&net->ipv6.ip6_fib_timer,
  670. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  671. }
  672. /*
  673. * Add routing information to the routing tree.
  674. * <destination addr>/<source addr>
  675. * with source addr info in sub-trees
  676. */
  677. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  678. {
  679. struct fib6_node *fn, *pn = NULL;
  680. int err = -ENOMEM;
  681. int allow_create = 1;
  682. int replace_required = 0;
  683. if (info->nlh) {
  684. if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
  685. allow_create = 0;
  686. if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
  687. replace_required = 1;
  688. }
  689. if (!allow_create && !replace_required)
  690. pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
  691. fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  692. offsetof(struct rt6_info, rt6i_dst), allow_create,
  693. replace_required);
  694. if (IS_ERR(fn)) {
  695. err = PTR_ERR(fn);
  696. fn = NULL;
  697. goto out;
  698. }
  699. pn = fn;
  700. #ifdef CONFIG_IPV6_SUBTREES
  701. if (rt->rt6i_src.plen) {
  702. struct fib6_node *sn;
  703. if (!fn->subtree) {
  704. struct fib6_node *sfn;
  705. /*
  706. * Create subtree.
  707. *
  708. * fn[main tree]
  709. * |
  710. * sfn[subtree root]
  711. * \
  712. * sn[new leaf node]
  713. */
  714. /* Create subtree root node */
  715. sfn = node_alloc();
  716. if (!sfn)
  717. goto st_failure;
  718. sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
  719. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  720. sfn->fn_flags = RTN_ROOT;
  721. sfn->fn_sernum = fib6_new_sernum();
  722. /* Now add the first leaf node to new subtree */
  723. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  724. rt->rt6i_src.plen,
  725. offsetof(struct rt6_info, rt6i_src),
  726. allow_create, replace_required);
  727. if (IS_ERR(sn)) {
  728. /* If it is failed, discard just allocated
  729. root, and then (in st_failure) stale node
  730. in main tree.
  731. */
  732. node_free(sfn);
  733. err = PTR_ERR(sn);
  734. goto st_failure;
  735. }
  736. /* Now link new subtree to main tree */
  737. sfn->parent = fn;
  738. fn->subtree = sfn;
  739. } else {
  740. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  741. rt->rt6i_src.plen,
  742. offsetof(struct rt6_info, rt6i_src),
  743. allow_create, replace_required);
  744. if (IS_ERR(sn)) {
  745. err = PTR_ERR(sn);
  746. goto st_failure;
  747. }
  748. }
  749. if (!fn->leaf) {
  750. fn->leaf = rt;
  751. atomic_inc(&rt->rt6i_ref);
  752. }
  753. fn = sn;
  754. }
  755. #endif
  756. err = fib6_add_rt2node(fn, rt, info);
  757. if (!err) {
  758. fib6_start_gc(info->nl_net, rt);
  759. if (!(rt->rt6i_flags & RTF_CACHE))
  760. fib6_prune_clones(info->nl_net, pn, rt);
  761. }
  762. out:
  763. if (err) {
  764. #ifdef CONFIG_IPV6_SUBTREES
  765. /*
  766. * If fib6_add_1 has cleared the old leaf pointer in the
  767. * super-tree leaf node we have to find a new one for it.
  768. */
  769. if (pn != fn && pn->leaf == rt) {
  770. pn->leaf = NULL;
  771. atomic_dec(&rt->rt6i_ref);
  772. }
  773. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  774. pn->leaf = fib6_find_prefix(info->nl_net, pn);
  775. #if RT6_DEBUG >= 2
  776. if (!pn->leaf) {
  777. WARN_ON(pn->leaf == NULL);
  778. pn->leaf = info->nl_net->ipv6.ip6_null_entry;
  779. }
  780. #endif
  781. atomic_inc(&pn->leaf->rt6i_ref);
  782. }
  783. #endif
  784. dst_free(&rt->dst);
  785. }
  786. return err;
  787. #ifdef CONFIG_IPV6_SUBTREES
  788. /* Subtree creation failed, probably main tree node
  789. is orphan. If it is, shoot it.
  790. */
  791. st_failure:
  792. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  793. fib6_repair_tree(info->nl_net, fn);
  794. dst_free(&rt->dst);
  795. return err;
  796. #endif
  797. }
  798. /*
  799. * Routing tree lookup
  800. *
  801. */
  802. struct lookup_args {
  803. int offset; /* key offset on rt6_info */
  804. const struct in6_addr *addr; /* search key */
  805. };
  806. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  807. struct lookup_args *args)
  808. {
  809. struct fib6_node *fn;
  810. __be32 dir;
  811. if (unlikely(args->offset == 0))
  812. return NULL;
  813. /*
  814. * Descend on a tree
  815. */
  816. fn = root;
  817. for (;;) {
  818. struct fib6_node *next;
  819. dir = addr_bit_set(args->addr, fn->fn_bit);
  820. next = dir ? fn->right : fn->left;
  821. if (next) {
  822. fn = next;
  823. continue;
  824. }
  825. break;
  826. }
  827. while (fn) {
  828. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  829. struct rt6key *key;
  830. key = (struct rt6key *) ((u8 *) fn->leaf +
  831. args->offset);
  832. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  833. #ifdef CONFIG_IPV6_SUBTREES
  834. if (fn->subtree) {
  835. struct fib6_node *sfn;
  836. sfn = fib6_lookup_1(fn->subtree,
  837. args + 1);
  838. if (!sfn)
  839. goto backtrack;
  840. fn = sfn;
  841. }
  842. #endif
  843. if (fn->fn_flags & RTN_RTINFO)
  844. return fn;
  845. }
  846. }
  847. #ifdef CONFIG_IPV6_SUBTREES
  848. backtrack:
  849. #endif
  850. if (fn->fn_flags & RTN_ROOT)
  851. break;
  852. fn = fn->parent;
  853. }
  854. return NULL;
  855. }
  856. struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
  857. const struct in6_addr *saddr)
  858. {
  859. struct fib6_node *fn;
  860. struct lookup_args args[] = {
  861. {
  862. .offset = offsetof(struct rt6_info, rt6i_dst),
  863. .addr = daddr,
  864. },
  865. #ifdef CONFIG_IPV6_SUBTREES
  866. {
  867. .offset = offsetof(struct rt6_info, rt6i_src),
  868. .addr = saddr,
  869. },
  870. #endif
  871. {
  872. .offset = 0, /* sentinel */
  873. }
  874. };
  875. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  876. if (!fn || fn->fn_flags & RTN_TL_ROOT)
  877. fn = root;
  878. return fn;
  879. }
  880. /*
  881. * Get node with specified destination prefix (and source prefix,
  882. * if subtrees are used)
  883. */
  884. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  885. const struct in6_addr *addr,
  886. int plen, int offset)
  887. {
  888. struct fib6_node *fn;
  889. for (fn = root; fn ; ) {
  890. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  891. /*
  892. * Prefix match
  893. */
  894. if (plen < fn->fn_bit ||
  895. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  896. return NULL;
  897. if (plen == fn->fn_bit)
  898. return fn;
  899. /*
  900. * We have more bits to go
  901. */
  902. if (addr_bit_set(addr, fn->fn_bit))
  903. fn = fn->right;
  904. else
  905. fn = fn->left;
  906. }
  907. return NULL;
  908. }
  909. struct fib6_node * fib6_locate(struct fib6_node *root,
  910. const struct in6_addr *daddr, int dst_len,
  911. const struct in6_addr *saddr, int src_len)
  912. {
  913. struct fib6_node *fn;
  914. fn = fib6_locate_1(root, daddr, dst_len,
  915. offsetof(struct rt6_info, rt6i_dst));
  916. #ifdef CONFIG_IPV6_SUBTREES
  917. if (src_len) {
  918. WARN_ON(saddr == NULL);
  919. if (fn && fn->subtree)
  920. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  921. offsetof(struct rt6_info, rt6i_src));
  922. }
  923. #endif
  924. if (fn && fn->fn_flags & RTN_RTINFO)
  925. return fn;
  926. return NULL;
  927. }
  928. /*
  929. * Deletion
  930. *
  931. */
  932. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
  933. {
  934. if (fn->fn_flags & RTN_ROOT)
  935. return net->ipv6.ip6_null_entry;
  936. while (fn) {
  937. if (fn->left)
  938. return fn->left->leaf;
  939. if (fn->right)
  940. return fn->right->leaf;
  941. fn = FIB6_SUBTREE(fn);
  942. }
  943. return NULL;
  944. }
  945. /*
  946. * Called to trim the tree of intermediate nodes when possible. "fn"
  947. * is the node we want to try and remove.
  948. */
  949. static struct fib6_node *fib6_repair_tree(struct net *net,
  950. struct fib6_node *fn)
  951. {
  952. int children;
  953. int nstate;
  954. struct fib6_node *child, *pn;
  955. struct fib6_walker_t *w;
  956. int iter = 0;
  957. for (;;) {
  958. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  959. iter++;
  960. WARN_ON(fn->fn_flags & RTN_RTINFO);
  961. WARN_ON(fn->fn_flags & RTN_TL_ROOT);
  962. WARN_ON(fn->leaf != NULL);
  963. children = 0;
  964. child = NULL;
  965. if (fn->right) child = fn->right, children |= 1;
  966. if (fn->left) child = fn->left, children |= 2;
  967. if (children == 3 || FIB6_SUBTREE(fn)
  968. #ifdef CONFIG_IPV6_SUBTREES
  969. /* Subtree root (i.e. fn) may have one child */
  970. || (children && fn->fn_flags & RTN_ROOT)
  971. #endif
  972. ) {
  973. fn->leaf = fib6_find_prefix(net, fn);
  974. #if RT6_DEBUG >= 2
  975. if (!fn->leaf) {
  976. WARN_ON(!fn->leaf);
  977. fn->leaf = net->ipv6.ip6_null_entry;
  978. }
  979. #endif
  980. atomic_inc(&fn->leaf->rt6i_ref);
  981. return fn->parent;
  982. }
  983. pn = fn->parent;
  984. #ifdef CONFIG_IPV6_SUBTREES
  985. if (FIB6_SUBTREE(pn) == fn) {
  986. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  987. FIB6_SUBTREE(pn) = NULL;
  988. nstate = FWS_L;
  989. } else {
  990. WARN_ON(fn->fn_flags & RTN_ROOT);
  991. #endif
  992. if (pn->right == fn) pn->right = child;
  993. else if (pn->left == fn) pn->left = child;
  994. #if RT6_DEBUG >= 2
  995. else
  996. WARN_ON(1);
  997. #endif
  998. if (child)
  999. child->parent = pn;
  1000. nstate = FWS_R;
  1001. #ifdef CONFIG_IPV6_SUBTREES
  1002. }
  1003. #endif
  1004. read_lock(&fib6_walker_lock);
  1005. FOR_WALKERS(w) {
  1006. if (!child) {
  1007. if (w->root == fn) {
  1008. w->root = w->node = NULL;
  1009. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  1010. } else if (w->node == fn) {
  1011. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  1012. w->node = pn;
  1013. w->state = nstate;
  1014. }
  1015. } else {
  1016. if (w->root == fn) {
  1017. w->root = child;
  1018. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  1019. }
  1020. if (w->node == fn) {
  1021. w->node = child;
  1022. if (children&2) {
  1023. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1024. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  1025. } else {
  1026. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1027. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. read_unlock(&fib6_walker_lock);
  1033. node_free(fn);
  1034. if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
  1035. return pn;
  1036. rt6_release(pn->leaf);
  1037. pn->leaf = NULL;
  1038. fn = pn;
  1039. }
  1040. }
  1041. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  1042. struct nl_info *info)
  1043. {
  1044. struct fib6_walker_t *w;
  1045. struct rt6_info *rt = *rtp;
  1046. struct net *net = info->nl_net;
  1047. RT6_TRACE("fib6_del_route\n");
  1048. /* Unlink it */
  1049. *rtp = rt->dst.rt6_next;
  1050. rt->rt6i_node = NULL;
  1051. net->ipv6.rt6_stats->fib_rt_entries--;
  1052. net->ipv6.rt6_stats->fib_discarded_routes++;
  1053. /* Reset round-robin state, if necessary */
  1054. if (fn->rr_ptr == rt)
  1055. fn->rr_ptr = NULL;
  1056. /* Remove this entry from other siblings */
  1057. if (rt->rt6i_nsiblings) {
  1058. struct rt6_info *sibling, *next_sibling;
  1059. list_for_each_entry_safe(sibling, next_sibling,
  1060. &rt->rt6i_siblings, rt6i_siblings)
  1061. sibling->rt6i_nsiblings--;
  1062. rt->rt6i_nsiblings = 0;
  1063. list_del_init(&rt->rt6i_siblings);
  1064. }
  1065. /* Adjust walkers */
  1066. read_lock(&fib6_walker_lock);
  1067. FOR_WALKERS(w) {
  1068. if (w->state == FWS_C && w->leaf == rt) {
  1069. RT6_TRACE("walker %p adjusted by delroute\n", w);
  1070. w->leaf = rt->dst.rt6_next;
  1071. if (!w->leaf)
  1072. w->state = FWS_U;
  1073. }
  1074. }
  1075. read_unlock(&fib6_walker_lock);
  1076. rt->dst.rt6_next = NULL;
  1077. /* If it was last route, expunge its radix tree node */
  1078. if (!fn->leaf) {
  1079. fn->fn_flags &= ~RTN_RTINFO;
  1080. net->ipv6.rt6_stats->fib_route_nodes--;
  1081. fn = fib6_repair_tree(net, fn);
  1082. }
  1083. if (atomic_read(&rt->rt6i_ref) != 1) {
  1084. /* This route is used as dummy address holder in some split
  1085. * nodes. It is not leaked, but it still holds other resources,
  1086. * which must be released in time. So, scan ascendant nodes
  1087. * and replace dummy references to this route with references
  1088. * to still alive ones.
  1089. */
  1090. while (fn) {
  1091. if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
  1092. fn->leaf = fib6_find_prefix(net, fn);
  1093. atomic_inc(&fn->leaf->rt6i_ref);
  1094. rt6_release(rt);
  1095. }
  1096. fn = fn->parent;
  1097. }
  1098. /* No more references are possible at this point. */
  1099. BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
  1100. }
  1101. inet6_rt_notify(RTM_DELROUTE, rt, info);
  1102. rt6_release(rt);
  1103. }
  1104. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  1105. {
  1106. struct net *net = info->nl_net;
  1107. struct fib6_node *fn = rt->rt6i_node;
  1108. struct rt6_info **rtp;
  1109. #if RT6_DEBUG >= 2
  1110. if (rt->dst.obsolete>0) {
  1111. WARN_ON(fn != NULL);
  1112. return -ENOENT;
  1113. }
  1114. #endif
  1115. if (!fn || rt == net->ipv6.ip6_null_entry)
  1116. return -ENOENT;
  1117. WARN_ON(!(fn->fn_flags & RTN_RTINFO));
  1118. if (!(rt->rt6i_flags & RTF_CACHE)) {
  1119. struct fib6_node *pn = fn;
  1120. #ifdef CONFIG_IPV6_SUBTREES
  1121. /* clones of this route might be in another subtree */
  1122. if (rt->rt6i_src.plen) {
  1123. while (!(pn->fn_flags & RTN_ROOT))
  1124. pn = pn->parent;
  1125. pn = pn->parent;
  1126. }
  1127. #endif
  1128. fib6_prune_clones(info->nl_net, pn, rt);
  1129. }
  1130. /*
  1131. * Walk the leaf entries looking for ourself
  1132. */
  1133. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
  1134. if (*rtp == rt) {
  1135. fib6_del_route(fn, rtp, info);
  1136. return 0;
  1137. }
  1138. }
  1139. return -ENOENT;
  1140. }
  1141. /*
  1142. * Tree traversal function.
  1143. *
  1144. * Certainly, it is not interrupt safe.
  1145. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  1146. * It means, that we can modify tree during walking
  1147. * and use this function for garbage collection, clone pruning,
  1148. * cleaning tree when a device goes down etc. etc.
  1149. *
  1150. * It guarantees that every node will be traversed,
  1151. * and that it will be traversed only once.
  1152. *
  1153. * Callback function w->func may return:
  1154. * 0 -> continue walking.
  1155. * positive value -> walking is suspended (used by tree dumps,
  1156. * and probably by gc, if it will be split to several slices)
  1157. * negative value -> terminate walking.
  1158. *
  1159. * The function itself returns:
  1160. * 0 -> walk is complete.
  1161. * >0 -> walk is incomplete (i.e. suspended)
  1162. * <0 -> walk is terminated by an error.
  1163. */
  1164. static int fib6_walk_continue(struct fib6_walker_t *w)
  1165. {
  1166. struct fib6_node *fn, *pn;
  1167. for (;;) {
  1168. fn = w->node;
  1169. if (!fn)
  1170. return 0;
  1171. if (w->prune && fn != w->root &&
  1172. fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
  1173. w->state = FWS_C;
  1174. w->leaf = fn->leaf;
  1175. }
  1176. switch (w->state) {
  1177. #ifdef CONFIG_IPV6_SUBTREES
  1178. case FWS_S:
  1179. if (FIB6_SUBTREE(fn)) {
  1180. w->node = FIB6_SUBTREE(fn);
  1181. continue;
  1182. }
  1183. w->state = FWS_L;
  1184. #endif
  1185. case FWS_L:
  1186. if (fn->left) {
  1187. w->node = fn->left;
  1188. w->state = FWS_INIT;
  1189. continue;
  1190. }
  1191. w->state = FWS_R;
  1192. case FWS_R:
  1193. if (fn->right) {
  1194. w->node = fn->right;
  1195. w->state = FWS_INIT;
  1196. continue;
  1197. }
  1198. w->state = FWS_C;
  1199. w->leaf = fn->leaf;
  1200. case FWS_C:
  1201. if (w->leaf && fn->fn_flags & RTN_RTINFO) {
  1202. int err;
  1203. if (w->skip) {
  1204. w->skip--;
  1205. continue;
  1206. }
  1207. err = w->func(w);
  1208. if (err)
  1209. return err;
  1210. w->count++;
  1211. continue;
  1212. }
  1213. w->state = FWS_U;
  1214. case FWS_U:
  1215. if (fn == w->root)
  1216. return 0;
  1217. pn = fn->parent;
  1218. w->node = pn;
  1219. #ifdef CONFIG_IPV6_SUBTREES
  1220. if (FIB6_SUBTREE(pn) == fn) {
  1221. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1222. w->state = FWS_L;
  1223. continue;
  1224. }
  1225. #endif
  1226. if (pn->left == fn) {
  1227. w->state = FWS_R;
  1228. continue;
  1229. }
  1230. if (pn->right == fn) {
  1231. w->state = FWS_C;
  1232. w->leaf = w->node->leaf;
  1233. continue;
  1234. }
  1235. #if RT6_DEBUG >= 2
  1236. WARN_ON(1);
  1237. #endif
  1238. }
  1239. }
  1240. }
  1241. static int fib6_walk(struct fib6_walker_t *w)
  1242. {
  1243. int res;
  1244. w->state = FWS_INIT;
  1245. w->node = w->root;
  1246. fib6_walker_link(w);
  1247. res = fib6_walk_continue(w);
  1248. if (res <= 0)
  1249. fib6_walker_unlink(w);
  1250. return res;
  1251. }
  1252. static int fib6_clean_node(struct fib6_walker_t *w)
  1253. {
  1254. int res;
  1255. struct rt6_info *rt;
  1256. struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
  1257. struct nl_info info = {
  1258. .nl_net = c->net,
  1259. };
  1260. for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
  1261. res = c->func(rt, c->arg);
  1262. if (res < 0) {
  1263. w->leaf = rt;
  1264. res = fib6_del(rt, &info);
  1265. if (res) {
  1266. #if RT6_DEBUG >= 2
  1267. pr_debug("%s: del failed: rt=%p@%p err=%d\n",
  1268. __func__, rt, rt->rt6i_node, res);
  1269. #endif
  1270. continue;
  1271. }
  1272. return 0;
  1273. }
  1274. WARN_ON(res != 0);
  1275. }
  1276. w->leaf = rt;
  1277. return 0;
  1278. }
  1279. /*
  1280. * Convenient frontend to tree walker.
  1281. *
  1282. * func is called on each route.
  1283. * It may return -1 -> delete this route.
  1284. * 0 -> continue walking
  1285. *
  1286. * prune==1 -> only immediate children of node (certainly,
  1287. * ignoring pure split nodes) will be scanned.
  1288. */
  1289. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1290. int (*func)(struct rt6_info *, void *arg),
  1291. int prune, void *arg)
  1292. {
  1293. struct fib6_cleaner_t c;
  1294. c.w.root = root;
  1295. c.w.func = fib6_clean_node;
  1296. c.w.prune = prune;
  1297. c.w.count = 0;
  1298. c.w.skip = 0;
  1299. c.func = func;
  1300. c.arg = arg;
  1301. c.net = net;
  1302. fib6_walk(&c.w);
  1303. }
  1304. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  1305. int prune, void *arg)
  1306. {
  1307. struct fib6_table *table;
  1308. struct hlist_head *head;
  1309. unsigned int h;
  1310. rcu_read_lock();
  1311. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1312. head = &net->ipv6.fib_table_hash[h];
  1313. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  1314. write_lock_bh(&table->tb6_lock);
  1315. fib6_clean_tree(net, &table->tb6_root,
  1316. func, prune, arg);
  1317. write_unlock_bh(&table->tb6_lock);
  1318. }
  1319. }
  1320. rcu_read_unlock();
  1321. }
  1322. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1323. {
  1324. if (rt->rt6i_flags & RTF_CACHE) {
  1325. RT6_TRACE("pruning clone %p\n", rt);
  1326. return -1;
  1327. }
  1328. return 0;
  1329. }
  1330. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  1331. struct rt6_info *rt)
  1332. {
  1333. fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
  1334. }
  1335. /*
  1336. * Garbage collection
  1337. */
  1338. static struct fib6_gc_args
  1339. {
  1340. int timeout;
  1341. int more;
  1342. } gc_args;
  1343. static int fib6_age(struct rt6_info *rt, void *arg)
  1344. {
  1345. unsigned long now = jiffies;
  1346. /*
  1347. * check addrconf expiration here.
  1348. * Routes are expired even if they are in use.
  1349. *
  1350. * Also age clones. Note, that clones are aged out
  1351. * only if they are not in use now.
  1352. */
  1353. if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
  1354. if (time_after(now, rt->dst.expires)) {
  1355. RT6_TRACE("expiring %p\n", rt);
  1356. return -1;
  1357. }
  1358. gc_args.more++;
  1359. } else if (rt->rt6i_flags & RTF_CACHE) {
  1360. if (atomic_read(&rt->dst.__refcnt) == 0 &&
  1361. time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
  1362. RT6_TRACE("aging clone %p\n", rt);
  1363. return -1;
  1364. } else if (rt->rt6i_flags & RTF_GATEWAY) {
  1365. struct neighbour *neigh;
  1366. __u8 neigh_flags = 0;
  1367. neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
  1368. if (neigh) {
  1369. neigh_flags = neigh->flags;
  1370. neigh_release(neigh);
  1371. }
  1372. if (!(neigh_flags & NTF_ROUTER)) {
  1373. RT6_TRACE("purging route %p via non-router but gateway\n",
  1374. rt);
  1375. return -1;
  1376. }
  1377. }
  1378. gc_args.more++;
  1379. }
  1380. return 0;
  1381. }
  1382. static DEFINE_SPINLOCK(fib6_gc_lock);
  1383. void fib6_run_gc(unsigned long expires, struct net *net, bool force)
  1384. {
  1385. unsigned long now;
  1386. if (force) {
  1387. spin_lock_bh(&fib6_gc_lock);
  1388. } else if (!spin_trylock_bh(&fib6_gc_lock)) {
  1389. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1390. return;
  1391. }
  1392. gc_args.timeout = expires ? (int)expires :
  1393. net->ipv6.sysctl.ip6_rt_gc_interval;
  1394. gc_args.more = icmp6_dst_gc();
  1395. fib6_clean_all(net, fib6_age, 0, NULL);
  1396. now = jiffies;
  1397. net->ipv6.ip6_rt_last_gc = now;
  1398. if (gc_args.more)
  1399. mod_timer(&net->ipv6.ip6_fib_timer,
  1400. round_jiffies(now
  1401. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1402. else
  1403. del_timer(&net->ipv6.ip6_fib_timer);
  1404. spin_unlock_bh(&fib6_gc_lock);
  1405. }
  1406. static void fib6_gc_timer_cb(unsigned long arg)
  1407. {
  1408. fib6_run_gc(0, (struct net *)arg, true);
  1409. }
  1410. static int __net_init fib6_net_init(struct net *net)
  1411. {
  1412. size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
  1413. setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
  1414. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1415. if (!net->ipv6.rt6_stats)
  1416. goto out_timer;
  1417. /* Avoid false sharing : Use at least a full cache line */
  1418. size = max_t(size_t, size, L1_CACHE_BYTES);
  1419. net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1420. if (!net->ipv6.fib_table_hash)
  1421. goto out_rt6_stats;
  1422. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1423. GFP_KERNEL);
  1424. if (!net->ipv6.fib6_main_tbl)
  1425. goto out_fib_table_hash;
  1426. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1427. net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1428. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1429. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1430. inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
  1431. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1432. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1433. GFP_KERNEL);
  1434. if (!net->ipv6.fib6_local_tbl)
  1435. goto out_fib6_main_tbl;
  1436. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1437. net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1438. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1439. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1440. inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
  1441. #endif
  1442. fib6_tables_init(net);
  1443. return 0;
  1444. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1445. out_fib6_main_tbl:
  1446. kfree(net->ipv6.fib6_main_tbl);
  1447. #endif
  1448. out_fib_table_hash:
  1449. kfree(net->ipv6.fib_table_hash);
  1450. out_rt6_stats:
  1451. kfree(net->ipv6.rt6_stats);
  1452. out_timer:
  1453. return -ENOMEM;
  1454. }
  1455. static void fib6_net_exit(struct net *net)
  1456. {
  1457. rt6_ifdown(net, NULL);
  1458. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1459. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1460. inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
  1461. kfree(net->ipv6.fib6_local_tbl);
  1462. #endif
  1463. inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
  1464. kfree(net->ipv6.fib6_main_tbl);
  1465. kfree(net->ipv6.fib_table_hash);
  1466. kfree(net->ipv6.rt6_stats);
  1467. }
  1468. static struct pernet_operations fib6_net_ops = {
  1469. .init = fib6_net_init,
  1470. .exit = fib6_net_exit,
  1471. };
  1472. int __init fib6_init(void)
  1473. {
  1474. int ret = -ENOMEM;
  1475. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1476. sizeof(struct fib6_node),
  1477. 0, SLAB_HWCACHE_ALIGN,
  1478. NULL);
  1479. if (!fib6_node_kmem)
  1480. goto out;
  1481. ret = register_pernet_subsys(&fib6_net_ops);
  1482. if (ret)
  1483. goto out_kmem_cache_create;
  1484. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
  1485. NULL);
  1486. if (ret)
  1487. goto out_unregister_subsys;
  1488. out:
  1489. return ret;
  1490. out_unregister_subsys:
  1491. unregister_pernet_subsys(&fib6_net_ops);
  1492. out_kmem_cache_create:
  1493. kmem_cache_destroy(fib6_node_kmem);
  1494. goto out;
  1495. }
  1496. void fib6_gc_cleanup(void)
  1497. {
  1498. unregister_pernet_subsys(&fib6_net_ops);
  1499. kmem_cache_destroy(fib6_node_kmem);
  1500. }
  1501. #ifdef CONFIG_PROC_FS
  1502. struct ipv6_route_iter {
  1503. struct seq_net_private p;
  1504. struct fib6_walker_t w;
  1505. loff_t skip;
  1506. struct fib6_table *tbl;
  1507. __u32 sernum;
  1508. };
  1509. static int ipv6_route_seq_show(struct seq_file *seq, void *v)
  1510. {
  1511. struct rt6_info *rt = v;
  1512. struct ipv6_route_iter *iter = seq->private;
  1513. seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
  1514. #ifdef CONFIG_IPV6_SUBTREES
  1515. seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
  1516. #else
  1517. seq_puts(seq, "00000000000000000000000000000000 00 ");
  1518. #endif
  1519. if (rt->rt6i_flags & RTF_GATEWAY)
  1520. seq_printf(seq, "%pi6", &rt->rt6i_gateway);
  1521. else
  1522. seq_puts(seq, "00000000000000000000000000000000");
  1523. seq_printf(seq, " %08x %08x %08x %08x %8s\n",
  1524. rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
  1525. rt->dst.__use, rt->rt6i_flags,
  1526. rt->dst.dev ? rt->dst.dev->name : "");
  1527. iter->w.leaf = NULL;
  1528. return 0;
  1529. }
  1530. static int ipv6_route_yield(struct fib6_walker_t *w)
  1531. {
  1532. struct ipv6_route_iter *iter = w->args;
  1533. if (!iter->skip)
  1534. return 1;
  1535. do {
  1536. iter->w.leaf = iter->w.leaf->dst.rt6_next;
  1537. iter->skip--;
  1538. if (!iter->skip && iter->w.leaf)
  1539. return 1;
  1540. } while (iter->w.leaf);
  1541. return 0;
  1542. }
  1543. static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
  1544. {
  1545. memset(&iter->w, 0, sizeof(iter->w));
  1546. iter->w.func = ipv6_route_yield;
  1547. iter->w.root = &iter->tbl->tb6_root;
  1548. iter->w.state = FWS_INIT;
  1549. iter->w.node = iter->w.root;
  1550. iter->w.args = iter;
  1551. iter->sernum = iter->w.root->fn_sernum;
  1552. INIT_LIST_HEAD(&iter->w.lh);
  1553. fib6_walker_link(&iter->w);
  1554. }
  1555. static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
  1556. struct net *net)
  1557. {
  1558. unsigned int h;
  1559. struct hlist_node *node;
  1560. if (tbl) {
  1561. h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
  1562. node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
  1563. } else {
  1564. h = 0;
  1565. node = NULL;
  1566. }
  1567. while (!node && h < FIB6_TABLE_HASHSZ) {
  1568. node = rcu_dereference_bh(
  1569. hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
  1570. }
  1571. return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
  1572. }
  1573. static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
  1574. {
  1575. if (iter->sernum != iter->w.root->fn_sernum) {
  1576. iter->sernum = iter->w.root->fn_sernum;
  1577. iter->w.state = FWS_INIT;
  1578. iter->w.node = iter->w.root;
  1579. WARN_ON(iter->w.skip);
  1580. iter->w.skip = iter->w.count;
  1581. }
  1582. }
  1583. static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1584. {
  1585. int r;
  1586. struct rt6_info *n;
  1587. struct net *net = seq_file_net(seq);
  1588. struct ipv6_route_iter *iter = seq->private;
  1589. if (!v)
  1590. goto iter_table;
  1591. n = ((struct rt6_info *)v)->dst.rt6_next;
  1592. if (n) {
  1593. ++*pos;
  1594. return n;
  1595. }
  1596. iter_table:
  1597. ipv6_route_check_sernum(iter);
  1598. read_lock(&iter->tbl->tb6_lock);
  1599. r = fib6_walk_continue(&iter->w);
  1600. read_unlock(&iter->tbl->tb6_lock);
  1601. if (r > 0) {
  1602. if (v)
  1603. ++*pos;
  1604. return iter->w.leaf;
  1605. } else if (r < 0) {
  1606. fib6_walker_unlink(&iter->w);
  1607. return NULL;
  1608. }
  1609. fib6_walker_unlink(&iter->w);
  1610. iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
  1611. if (!iter->tbl)
  1612. return NULL;
  1613. ipv6_route_seq_setup_walk(iter);
  1614. goto iter_table;
  1615. }
  1616. static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
  1617. __acquires(RCU_BH)
  1618. {
  1619. struct net *net = seq_file_net(seq);
  1620. struct ipv6_route_iter *iter = seq->private;
  1621. rcu_read_lock_bh();
  1622. iter->tbl = ipv6_route_seq_next_table(NULL, net);
  1623. iter->skip = *pos;
  1624. if (iter->tbl) {
  1625. ipv6_route_seq_setup_walk(iter);
  1626. return ipv6_route_seq_next(seq, NULL, pos);
  1627. } else {
  1628. return NULL;
  1629. }
  1630. }
  1631. static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
  1632. {
  1633. struct fib6_walker_t *w = &iter->w;
  1634. return w->node && !(w->state == FWS_U && w->node == w->root);
  1635. }
  1636. static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
  1637. __releases(RCU_BH)
  1638. {
  1639. struct ipv6_route_iter *iter = seq->private;
  1640. if (ipv6_route_iter_active(iter))
  1641. fib6_walker_unlink(&iter->w);
  1642. rcu_read_unlock_bh();
  1643. }
  1644. static const struct seq_operations ipv6_route_seq_ops = {
  1645. .start = ipv6_route_seq_start,
  1646. .next = ipv6_route_seq_next,
  1647. .stop = ipv6_route_seq_stop,
  1648. .show = ipv6_route_seq_show
  1649. };
  1650. int ipv6_route_open(struct inode *inode, struct file *file)
  1651. {
  1652. return seq_open_net(inode, file, &ipv6_route_seq_ops,
  1653. sizeof(struct ipv6_route_iter));
  1654. }
  1655. #endif /* CONFIG_PROC_FS */