ip6_fib.c 36 KB

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