ip6_fib.c 32 KB

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