ip6_fib.c 43 KB

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