ip6_fib.c 36 KB

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