ip6_fib.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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. 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, sizeof(struct in6_addr),
  692. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst),
  693. allow_create, replace_required);
  694. if (IS_ERR(fn)) {
  695. err = PTR_ERR(fn);
  696. goto out;
  697. }
  698. pn = fn;
  699. #ifdef CONFIG_IPV6_SUBTREES
  700. if (rt->rt6i_src.plen) {
  701. struct fib6_node *sn;
  702. if (!fn->subtree) {
  703. struct fib6_node *sfn;
  704. /*
  705. * Create subtree.
  706. *
  707. * fn[main tree]
  708. * |
  709. * sfn[subtree root]
  710. * \
  711. * sn[new leaf node]
  712. */
  713. /* Create subtree root node */
  714. sfn = node_alloc();
  715. if (!sfn)
  716. goto st_failure;
  717. sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
  718. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  719. sfn->fn_flags = RTN_ROOT;
  720. sfn->fn_sernum = fib6_new_sernum();
  721. /* Now add the first leaf node to new subtree */
  722. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  723. sizeof(struct in6_addr), rt->rt6i_src.plen,
  724. offsetof(struct rt6_info, rt6i_src),
  725. allow_create, replace_required);
  726. if (IS_ERR(sn)) {
  727. /* If it is failed, discard just allocated
  728. root, and then (in st_failure) stale node
  729. in main tree.
  730. */
  731. node_free(sfn);
  732. err = PTR_ERR(sn);
  733. goto st_failure;
  734. }
  735. /* Now link new subtree to main tree */
  736. sfn->parent = fn;
  737. fn->subtree = sfn;
  738. } else {
  739. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  740. sizeof(struct in6_addr), rt->rt6i_src.plen,
  741. offsetof(struct rt6_info, rt6i_src),
  742. allow_create, replace_required);
  743. if (IS_ERR(sn)) {
  744. err = PTR_ERR(sn);
  745. goto st_failure;
  746. }
  747. }
  748. if (!fn->leaf) {
  749. fn->leaf = rt;
  750. atomic_inc(&rt->rt6i_ref);
  751. }
  752. fn = sn;
  753. }
  754. #endif
  755. err = fib6_add_rt2node(fn, rt, info);
  756. if (!err) {
  757. fib6_start_gc(info->nl_net, rt);
  758. if (!(rt->rt6i_flags & RTF_CACHE))
  759. fib6_prune_clones(info->nl_net, pn, rt);
  760. }
  761. out:
  762. if (err) {
  763. #ifdef CONFIG_IPV6_SUBTREES
  764. /*
  765. * If fib6_add_1 has cleared the old leaf pointer in the
  766. * super-tree leaf node we have to find a new one for it.
  767. */
  768. if (pn != fn && pn->leaf == rt) {
  769. pn->leaf = NULL;
  770. atomic_dec(&rt->rt6i_ref);
  771. }
  772. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  773. pn->leaf = fib6_find_prefix(info->nl_net, pn);
  774. #if RT6_DEBUG >= 2
  775. if (!pn->leaf) {
  776. WARN_ON(pn->leaf == NULL);
  777. pn->leaf = info->nl_net->ipv6.ip6_null_entry;
  778. }
  779. #endif
  780. atomic_inc(&pn->leaf->rt6i_ref);
  781. }
  782. #endif
  783. dst_free(&rt->dst);
  784. }
  785. return err;
  786. #ifdef CONFIG_IPV6_SUBTREES
  787. /* Subtree creation failed, probably main tree node
  788. is orphan. If it is, shoot it.
  789. */
  790. st_failure:
  791. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  792. fib6_repair_tree(info->nl_net, fn);
  793. dst_free(&rt->dst);
  794. return err;
  795. #endif
  796. }
  797. /*
  798. * Routing tree lookup
  799. *
  800. */
  801. struct lookup_args {
  802. int offset; /* key offset on rt6_info */
  803. const struct in6_addr *addr; /* search key */
  804. };
  805. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  806. struct lookup_args *args)
  807. {
  808. struct fib6_node *fn;
  809. __be32 dir;
  810. if (unlikely(args->offset == 0))
  811. return NULL;
  812. /*
  813. * Descend on a tree
  814. */
  815. fn = root;
  816. for (;;) {
  817. struct fib6_node *next;
  818. dir = addr_bit_set(args->addr, fn->fn_bit);
  819. next = dir ? fn->right : fn->left;
  820. if (next) {
  821. fn = next;
  822. continue;
  823. }
  824. break;
  825. }
  826. while (fn) {
  827. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  828. struct rt6key *key;
  829. key = (struct rt6key *) ((u8 *) fn->leaf +
  830. args->offset);
  831. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  832. #ifdef CONFIG_IPV6_SUBTREES
  833. if (fn->subtree)
  834. fn = fib6_lookup_1(fn->subtree, args + 1);
  835. #endif
  836. if (!fn || fn->fn_flags & RTN_RTINFO)
  837. return fn;
  838. }
  839. }
  840. if (fn->fn_flags & RTN_ROOT)
  841. break;
  842. fn = fn->parent;
  843. }
  844. return NULL;
  845. }
  846. struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
  847. const struct in6_addr *saddr)
  848. {
  849. struct fib6_node *fn;
  850. struct lookup_args args[] = {
  851. {
  852. .offset = offsetof(struct rt6_info, rt6i_dst),
  853. .addr = daddr,
  854. },
  855. #ifdef CONFIG_IPV6_SUBTREES
  856. {
  857. .offset = offsetof(struct rt6_info, rt6i_src),
  858. .addr = saddr,
  859. },
  860. #endif
  861. {
  862. .offset = 0, /* sentinel */
  863. }
  864. };
  865. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  866. if (!fn || fn->fn_flags & RTN_TL_ROOT)
  867. fn = root;
  868. return fn;
  869. }
  870. /*
  871. * Get node with specified destination prefix (and source prefix,
  872. * if subtrees are used)
  873. */
  874. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  875. const struct in6_addr *addr,
  876. int plen, int offset)
  877. {
  878. struct fib6_node *fn;
  879. for (fn = root; fn ; ) {
  880. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  881. /*
  882. * Prefix match
  883. */
  884. if (plen < fn->fn_bit ||
  885. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  886. return NULL;
  887. if (plen == fn->fn_bit)
  888. return fn;
  889. /*
  890. * We have more bits to go
  891. */
  892. if (addr_bit_set(addr, fn->fn_bit))
  893. fn = fn->right;
  894. else
  895. fn = fn->left;
  896. }
  897. return NULL;
  898. }
  899. struct fib6_node * fib6_locate(struct fib6_node *root,
  900. const struct in6_addr *daddr, int dst_len,
  901. const struct in6_addr *saddr, int src_len)
  902. {
  903. struct fib6_node *fn;
  904. fn = fib6_locate_1(root, daddr, dst_len,
  905. offsetof(struct rt6_info, rt6i_dst));
  906. #ifdef CONFIG_IPV6_SUBTREES
  907. if (src_len) {
  908. WARN_ON(saddr == NULL);
  909. if (fn && fn->subtree)
  910. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  911. offsetof(struct rt6_info, rt6i_src));
  912. }
  913. #endif
  914. if (fn && fn->fn_flags & RTN_RTINFO)
  915. return fn;
  916. return NULL;
  917. }
  918. /*
  919. * Deletion
  920. *
  921. */
  922. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
  923. {
  924. if (fn->fn_flags & RTN_ROOT)
  925. return net->ipv6.ip6_null_entry;
  926. while (fn) {
  927. if (fn->left)
  928. return fn->left->leaf;
  929. if (fn->right)
  930. return fn->right->leaf;
  931. fn = FIB6_SUBTREE(fn);
  932. }
  933. return NULL;
  934. }
  935. /*
  936. * Called to trim the tree of intermediate nodes when possible. "fn"
  937. * is the node we want to try and remove.
  938. */
  939. static struct fib6_node *fib6_repair_tree(struct net *net,
  940. struct fib6_node *fn)
  941. {
  942. int children;
  943. int nstate;
  944. struct fib6_node *child, *pn;
  945. struct fib6_walker_t *w;
  946. int iter = 0;
  947. for (;;) {
  948. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  949. iter++;
  950. WARN_ON(fn->fn_flags & RTN_RTINFO);
  951. WARN_ON(fn->fn_flags & RTN_TL_ROOT);
  952. WARN_ON(fn->leaf != NULL);
  953. children = 0;
  954. child = NULL;
  955. if (fn->right) child = fn->right, children |= 1;
  956. if (fn->left) child = fn->left, children |= 2;
  957. if (children == 3 || FIB6_SUBTREE(fn)
  958. #ifdef CONFIG_IPV6_SUBTREES
  959. /* Subtree root (i.e. fn) may have one child */
  960. || (children && fn->fn_flags & RTN_ROOT)
  961. #endif
  962. ) {
  963. fn->leaf = fib6_find_prefix(net, fn);
  964. #if RT6_DEBUG >= 2
  965. if (!fn->leaf) {
  966. WARN_ON(!fn->leaf);
  967. fn->leaf = net->ipv6.ip6_null_entry;
  968. }
  969. #endif
  970. atomic_inc(&fn->leaf->rt6i_ref);
  971. return fn->parent;
  972. }
  973. pn = fn->parent;
  974. #ifdef CONFIG_IPV6_SUBTREES
  975. if (FIB6_SUBTREE(pn) == fn) {
  976. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  977. FIB6_SUBTREE(pn) = NULL;
  978. nstate = FWS_L;
  979. } else {
  980. WARN_ON(fn->fn_flags & RTN_ROOT);
  981. #endif
  982. if (pn->right == fn) pn->right = child;
  983. else if (pn->left == fn) pn->left = child;
  984. #if RT6_DEBUG >= 2
  985. else
  986. WARN_ON(1);
  987. #endif
  988. if (child)
  989. child->parent = pn;
  990. nstate = FWS_R;
  991. #ifdef CONFIG_IPV6_SUBTREES
  992. }
  993. #endif
  994. read_lock(&fib6_walker_lock);
  995. FOR_WALKERS(w) {
  996. if (!child) {
  997. if (w->root == fn) {
  998. w->root = w->node = NULL;
  999. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  1000. } else if (w->node == fn) {
  1001. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  1002. w->node = pn;
  1003. w->state = nstate;
  1004. }
  1005. } else {
  1006. if (w->root == fn) {
  1007. w->root = child;
  1008. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  1009. }
  1010. if (w->node == fn) {
  1011. w->node = child;
  1012. if (children&2) {
  1013. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1014. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  1015. } else {
  1016. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1017. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. read_unlock(&fib6_walker_lock);
  1023. node_free(fn);
  1024. if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
  1025. return pn;
  1026. rt6_release(pn->leaf);
  1027. pn->leaf = NULL;
  1028. fn = pn;
  1029. }
  1030. }
  1031. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  1032. struct nl_info *info)
  1033. {
  1034. struct fib6_walker_t *w;
  1035. struct rt6_info *rt = *rtp;
  1036. struct net *net = info->nl_net;
  1037. RT6_TRACE("fib6_del_route\n");
  1038. /* Unlink it */
  1039. *rtp = rt->dst.rt6_next;
  1040. rt->rt6i_node = NULL;
  1041. net->ipv6.rt6_stats->fib_rt_entries--;
  1042. net->ipv6.rt6_stats->fib_discarded_routes++;
  1043. /* Reset round-robin state, if necessary */
  1044. if (fn->rr_ptr == rt)
  1045. fn->rr_ptr = NULL;
  1046. /* Remove this entry from other siblings */
  1047. if (rt->rt6i_nsiblings) {
  1048. struct rt6_info *sibling, *next_sibling;
  1049. list_for_each_entry_safe(sibling, next_sibling,
  1050. &rt->rt6i_siblings, rt6i_siblings)
  1051. sibling->rt6i_nsiblings--;
  1052. rt->rt6i_nsiblings = 0;
  1053. list_del_init(&rt->rt6i_siblings);
  1054. }
  1055. /* Adjust walkers */
  1056. read_lock(&fib6_walker_lock);
  1057. FOR_WALKERS(w) {
  1058. if (w->state == FWS_C && w->leaf == rt) {
  1059. RT6_TRACE("walker %p adjusted by delroute\n", w);
  1060. w->leaf = rt->dst.rt6_next;
  1061. if (!w->leaf)
  1062. w->state = FWS_U;
  1063. }
  1064. }
  1065. read_unlock(&fib6_walker_lock);
  1066. rt->dst.rt6_next = NULL;
  1067. /* If it was last route, expunge its radix tree node */
  1068. if (!fn->leaf) {
  1069. fn->fn_flags &= ~RTN_RTINFO;
  1070. net->ipv6.rt6_stats->fib_route_nodes--;
  1071. fn = fib6_repair_tree(net, fn);
  1072. }
  1073. if (atomic_read(&rt->rt6i_ref) != 1) {
  1074. /* This route is used as dummy address holder in some split
  1075. * nodes. It is not leaked, but it still holds other resources,
  1076. * which must be released in time. So, scan ascendant nodes
  1077. * and replace dummy references to this route with references
  1078. * to still alive ones.
  1079. */
  1080. while (fn) {
  1081. if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
  1082. fn->leaf = fib6_find_prefix(net, fn);
  1083. atomic_inc(&fn->leaf->rt6i_ref);
  1084. rt6_release(rt);
  1085. }
  1086. fn = fn->parent;
  1087. }
  1088. /* No more references are possible at this point. */
  1089. BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
  1090. }
  1091. inet6_rt_notify(RTM_DELROUTE, rt, info);
  1092. rt6_release(rt);
  1093. }
  1094. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  1095. {
  1096. struct net *net = info->nl_net;
  1097. struct fib6_node *fn = rt->rt6i_node;
  1098. struct rt6_info **rtp;
  1099. #if RT6_DEBUG >= 2
  1100. if (rt->dst.obsolete>0) {
  1101. WARN_ON(fn != NULL);
  1102. return -ENOENT;
  1103. }
  1104. #endif
  1105. if (!fn || rt == net->ipv6.ip6_null_entry)
  1106. return -ENOENT;
  1107. WARN_ON(!(fn->fn_flags & RTN_RTINFO));
  1108. if (!(rt->rt6i_flags & RTF_CACHE)) {
  1109. struct fib6_node *pn = fn;
  1110. #ifdef CONFIG_IPV6_SUBTREES
  1111. /* clones of this route might be in another subtree */
  1112. if (rt->rt6i_src.plen) {
  1113. while (!(pn->fn_flags & RTN_ROOT))
  1114. pn = pn->parent;
  1115. pn = pn->parent;
  1116. }
  1117. #endif
  1118. fib6_prune_clones(info->nl_net, pn, rt);
  1119. }
  1120. /*
  1121. * Walk the leaf entries looking for ourself
  1122. */
  1123. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
  1124. if (*rtp == rt) {
  1125. fib6_del_route(fn, rtp, info);
  1126. return 0;
  1127. }
  1128. }
  1129. return -ENOENT;
  1130. }
  1131. /*
  1132. * Tree traversal function.
  1133. *
  1134. * Certainly, it is not interrupt safe.
  1135. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  1136. * It means, that we can modify tree during walking
  1137. * and use this function for garbage collection, clone pruning,
  1138. * cleaning tree when a device goes down etc. etc.
  1139. *
  1140. * It guarantees that every node will be traversed,
  1141. * and that it will be traversed only once.
  1142. *
  1143. * Callback function w->func may return:
  1144. * 0 -> continue walking.
  1145. * positive value -> walking is suspended (used by tree dumps,
  1146. * and probably by gc, if it will be split to several slices)
  1147. * negative value -> terminate walking.
  1148. *
  1149. * The function itself returns:
  1150. * 0 -> walk is complete.
  1151. * >0 -> walk is incomplete (i.e. suspended)
  1152. * <0 -> walk is terminated by an error.
  1153. */
  1154. static int fib6_walk_continue(struct fib6_walker_t *w)
  1155. {
  1156. struct fib6_node *fn, *pn;
  1157. for (;;) {
  1158. fn = w->node;
  1159. if (!fn)
  1160. return 0;
  1161. if (w->prune && fn != w->root &&
  1162. fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
  1163. w->state = FWS_C;
  1164. w->leaf = fn->leaf;
  1165. }
  1166. switch (w->state) {
  1167. #ifdef CONFIG_IPV6_SUBTREES
  1168. case FWS_S:
  1169. if (FIB6_SUBTREE(fn)) {
  1170. w->node = FIB6_SUBTREE(fn);
  1171. continue;
  1172. }
  1173. w->state = FWS_L;
  1174. #endif
  1175. case FWS_L:
  1176. if (fn->left) {
  1177. w->node = fn->left;
  1178. w->state = FWS_INIT;
  1179. continue;
  1180. }
  1181. w->state = FWS_R;
  1182. case FWS_R:
  1183. if (fn->right) {
  1184. w->node = fn->right;
  1185. w->state = FWS_INIT;
  1186. continue;
  1187. }
  1188. w->state = FWS_C;
  1189. w->leaf = fn->leaf;
  1190. case FWS_C:
  1191. if (w->leaf && fn->fn_flags & RTN_RTINFO) {
  1192. int err;
  1193. if (w->skip) {
  1194. w->skip--;
  1195. continue;
  1196. }
  1197. err = w->func(w);
  1198. if (err)
  1199. return err;
  1200. w->count++;
  1201. continue;
  1202. }
  1203. w->state = FWS_U;
  1204. case FWS_U:
  1205. if (fn == w->root)
  1206. return 0;
  1207. pn = fn->parent;
  1208. w->node = pn;
  1209. #ifdef CONFIG_IPV6_SUBTREES
  1210. if (FIB6_SUBTREE(pn) == fn) {
  1211. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1212. w->state = FWS_L;
  1213. continue;
  1214. }
  1215. #endif
  1216. if (pn->left == fn) {
  1217. w->state = FWS_R;
  1218. continue;
  1219. }
  1220. if (pn->right == fn) {
  1221. w->state = FWS_C;
  1222. w->leaf = w->node->leaf;
  1223. continue;
  1224. }
  1225. #if RT6_DEBUG >= 2
  1226. WARN_ON(1);
  1227. #endif
  1228. }
  1229. }
  1230. }
  1231. static int fib6_walk(struct fib6_walker_t *w)
  1232. {
  1233. int res;
  1234. w->state = FWS_INIT;
  1235. w->node = w->root;
  1236. fib6_walker_link(w);
  1237. res = fib6_walk_continue(w);
  1238. if (res <= 0)
  1239. fib6_walker_unlink(w);
  1240. return res;
  1241. }
  1242. static int fib6_clean_node(struct fib6_walker_t *w)
  1243. {
  1244. int res;
  1245. struct rt6_info *rt;
  1246. struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
  1247. struct nl_info info = {
  1248. .nl_net = c->net,
  1249. };
  1250. for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
  1251. res = c->func(rt, c->arg);
  1252. if (res < 0) {
  1253. w->leaf = rt;
  1254. res = fib6_del(rt, &info);
  1255. if (res) {
  1256. #if RT6_DEBUG >= 2
  1257. pr_debug("%s: del failed: rt=%p@%p err=%d\n",
  1258. __func__, rt, rt->rt6i_node, res);
  1259. #endif
  1260. continue;
  1261. }
  1262. return 0;
  1263. }
  1264. WARN_ON(res != 0);
  1265. }
  1266. w->leaf = rt;
  1267. return 0;
  1268. }
  1269. /*
  1270. * Convenient frontend to tree walker.
  1271. *
  1272. * func is called on each route.
  1273. * It may return -1 -> delete this route.
  1274. * 0 -> continue walking
  1275. *
  1276. * prune==1 -> only immediate children of node (certainly,
  1277. * ignoring pure split nodes) will be scanned.
  1278. */
  1279. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1280. int (*func)(struct rt6_info *, void *arg),
  1281. int prune, void *arg)
  1282. {
  1283. struct fib6_cleaner_t c;
  1284. c.w.root = root;
  1285. c.w.func = fib6_clean_node;
  1286. c.w.prune = prune;
  1287. c.w.count = 0;
  1288. c.w.skip = 0;
  1289. c.func = func;
  1290. c.arg = arg;
  1291. c.net = net;
  1292. fib6_walk(&c.w);
  1293. }
  1294. void fib6_clean_all_ro(struct net *net, int (*func)(struct rt6_info *, void *arg),
  1295. int prune, void *arg)
  1296. {
  1297. struct fib6_table *table;
  1298. struct hlist_head *head;
  1299. unsigned int h;
  1300. rcu_read_lock();
  1301. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1302. head = &net->ipv6.fib_table_hash[h];
  1303. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  1304. read_lock_bh(&table->tb6_lock);
  1305. fib6_clean_tree(net, &table->tb6_root,
  1306. func, prune, arg);
  1307. read_unlock_bh(&table->tb6_lock);
  1308. }
  1309. }
  1310. rcu_read_unlock();
  1311. }
  1312. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  1313. int prune, void *arg)
  1314. {
  1315. struct fib6_table *table;
  1316. struct hlist_head *head;
  1317. unsigned int h;
  1318. rcu_read_lock();
  1319. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1320. head = &net->ipv6.fib_table_hash[h];
  1321. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  1322. write_lock_bh(&table->tb6_lock);
  1323. fib6_clean_tree(net, &table->tb6_root,
  1324. func, prune, arg);
  1325. write_unlock_bh(&table->tb6_lock);
  1326. }
  1327. }
  1328. rcu_read_unlock();
  1329. }
  1330. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1331. {
  1332. if (rt->rt6i_flags & RTF_CACHE) {
  1333. RT6_TRACE("pruning clone %p\n", rt);
  1334. return -1;
  1335. }
  1336. return 0;
  1337. }
  1338. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  1339. struct rt6_info *rt)
  1340. {
  1341. fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
  1342. }
  1343. /*
  1344. * Garbage collection
  1345. */
  1346. static struct fib6_gc_args
  1347. {
  1348. int timeout;
  1349. int more;
  1350. } gc_args;
  1351. static int fib6_age(struct rt6_info *rt, void *arg)
  1352. {
  1353. unsigned long now = jiffies;
  1354. /*
  1355. * check addrconf expiration here.
  1356. * Routes are expired even if they are in use.
  1357. *
  1358. * Also age clones. Note, that clones are aged out
  1359. * only if they are not in use now.
  1360. */
  1361. if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
  1362. if (time_after(now, rt->dst.expires)) {
  1363. RT6_TRACE("expiring %p\n", rt);
  1364. return -1;
  1365. }
  1366. gc_args.more++;
  1367. } else if (rt->rt6i_flags & RTF_CACHE) {
  1368. if (atomic_read(&rt->dst.__refcnt) == 0 &&
  1369. time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
  1370. RT6_TRACE("aging clone %p\n", rt);
  1371. return -1;
  1372. } else if (rt->rt6i_flags & RTF_GATEWAY) {
  1373. struct neighbour *neigh;
  1374. __u8 neigh_flags = 0;
  1375. neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
  1376. if (neigh) {
  1377. neigh_flags = neigh->flags;
  1378. neigh_release(neigh);
  1379. }
  1380. if (!(neigh_flags & NTF_ROUTER)) {
  1381. RT6_TRACE("purging route %p via non-router but gateway\n",
  1382. rt);
  1383. return -1;
  1384. }
  1385. }
  1386. gc_args.more++;
  1387. }
  1388. return 0;
  1389. }
  1390. static DEFINE_SPINLOCK(fib6_gc_lock);
  1391. void fib6_run_gc(unsigned long expires, struct net *net)
  1392. {
  1393. if (expires != ~0UL) {
  1394. spin_lock_bh(&fib6_gc_lock);
  1395. gc_args.timeout = expires ? (int)expires :
  1396. net->ipv6.sysctl.ip6_rt_gc_interval;
  1397. } else {
  1398. if (!spin_trylock_bh(&fib6_gc_lock)) {
  1399. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1400. return;
  1401. }
  1402. gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
  1403. }
  1404. gc_args.more = icmp6_dst_gc();
  1405. fib6_clean_all(net, fib6_age, 0, NULL);
  1406. if (gc_args.more)
  1407. mod_timer(&net->ipv6.ip6_fib_timer,
  1408. round_jiffies(jiffies
  1409. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1410. else
  1411. del_timer(&net->ipv6.ip6_fib_timer);
  1412. spin_unlock_bh(&fib6_gc_lock);
  1413. }
  1414. static void fib6_gc_timer_cb(unsigned long arg)
  1415. {
  1416. fib6_run_gc(0, (struct net *)arg);
  1417. }
  1418. static int __net_init fib6_net_init(struct net *net)
  1419. {
  1420. size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
  1421. setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
  1422. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1423. if (!net->ipv6.rt6_stats)
  1424. goto out_timer;
  1425. /* Avoid false sharing : Use at least a full cache line */
  1426. size = max_t(size_t, size, L1_CACHE_BYTES);
  1427. net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1428. if (!net->ipv6.fib_table_hash)
  1429. goto out_rt6_stats;
  1430. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1431. GFP_KERNEL);
  1432. if (!net->ipv6.fib6_main_tbl)
  1433. goto out_fib_table_hash;
  1434. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1435. net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1436. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1437. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1438. inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
  1439. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1440. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1441. GFP_KERNEL);
  1442. if (!net->ipv6.fib6_local_tbl)
  1443. goto out_fib6_main_tbl;
  1444. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1445. net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1446. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1447. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1448. inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
  1449. #endif
  1450. fib6_tables_init(net);
  1451. return 0;
  1452. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1453. out_fib6_main_tbl:
  1454. kfree(net->ipv6.fib6_main_tbl);
  1455. #endif
  1456. out_fib_table_hash:
  1457. kfree(net->ipv6.fib_table_hash);
  1458. out_rt6_stats:
  1459. kfree(net->ipv6.rt6_stats);
  1460. out_timer:
  1461. return -ENOMEM;
  1462. }
  1463. static void fib6_net_exit(struct net *net)
  1464. {
  1465. rt6_ifdown(net, NULL);
  1466. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1467. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1468. inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
  1469. kfree(net->ipv6.fib6_local_tbl);
  1470. #endif
  1471. inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
  1472. kfree(net->ipv6.fib6_main_tbl);
  1473. kfree(net->ipv6.fib_table_hash);
  1474. kfree(net->ipv6.rt6_stats);
  1475. }
  1476. static struct pernet_operations fib6_net_ops = {
  1477. .init = fib6_net_init,
  1478. .exit = fib6_net_exit,
  1479. };
  1480. int __init fib6_init(void)
  1481. {
  1482. int ret = -ENOMEM;
  1483. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1484. sizeof(struct fib6_node),
  1485. 0, SLAB_HWCACHE_ALIGN,
  1486. NULL);
  1487. if (!fib6_node_kmem)
  1488. goto out;
  1489. ret = register_pernet_subsys(&fib6_net_ops);
  1490. if (ret)
  1491. goto out_kmem_cache_create;
  1492. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
  1493. NULL);
  1494. if (ret)
  1495. goto out_unregister_subsys;
  1496. out:
  1497. return ret;
  1498. out_unregister_subsys:
  1499. unregister_pernet_subsys(&fib6_net_ops);
  1500. out_kmem_cache_create:
  1501. kmem_cache_destroy(fib6_node_kmem);
  1502. goto out;
  1503. }
  1504. void fib6_gc_cleanup(void)
  1505. {
  1506. unregister_pernet_subsys(&fib6_net_ops);
  1507. kmem_cache_destroy(fib6_node_kmem);
  1508. }