ip6_fib.c 32 KB

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