ip6_fib.c 42 KB

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