ip6_fib.c 36 KB

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