ip6_fib.c 42 KB

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