ip6_fib.c 43 KB

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