ip6_fib.c 38 KB

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