ip6_fib.c 43 KB

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