ip6_fib.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. /*
  2. * Linux INET6 implementation
  3. * Forwarding Information Database
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /*
  16. * Changes:
  17. * Yuji SEKIYA @USAGI: Support default route on router node;
  18. * remove ip6_null_entry from the top of
  19. * routing table.
  20. * Ville Nuorvala: Fixed routing subtrees.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/net.h>
  25. #include <linux/route.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/in6.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #ifdef CONFIG_PROC_FS
  31. #include <linux/proc_fs.h>
  32. #endif
  33. #include <net/ipv6.h>
  34. #include <net/ndisc.h>
  35. #include <net/addrconf.h>
  36. #include <net/ip6_fib.h>
  37. #include <net/ip6_route.h>
  38. #define RT6_DEBUG 2
  39. #if RT6_DEBUG >= 3
  40. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  41. #else
  42. #define RT6_TRACE(x...) do { ; } while (0)
  43. #endif
  44. static struct kmem_cache * fib6_node_kmem __read_mostly;
  45. enum fib_walk_state_t
  46. {
  47. #ifdef CONFIG_IPV6_SUBTREES
  48. FWS_S,
  49. #endif
  50. FWS_L,
  51. FWS_R,
  52. FWS_C,
  53. FWS_U
  54. };
  55. struct fib6_cleaner_t
  56. {
  57. struct fib6_walker_t w;
  58. struct net *net;
  59. int (*func)(struct rt6_info *, void *arg);
  60. void *arg;
  61. };
  62. static DEFINE_RWLOCK(fib6_walker_lock);
  63. #ifdef CONFIG_IPV6_SUBTREES
  64. #define FWS_INIT FWS_S
  65. #else
  66. #define FWS_INIT FWS_L
  67. #endif
  68. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  69. struct rt6_info *rt);
  70. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
  71. static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
  72. static int fib6_walk(struct fib6_walker_t *w);
  73. static int fib6_walk_continue(struct fib6_walker_t *w);
  74. /*
  75. * A routing update causes an increase of the serial number on the
  76. * affected subtree. This allows for cached routes to be asynchronously
  77. * tested when modifications are made to the destination cache as a
  78. * result of redirects, path MTU changes, etc.
  79. */
  80. static __u32 rt_sernum;
  81. static void fib6_gc_timer_cb(unsigned long arg);
  82. static struct fib6_walker_t fib6_walker_list = {
  83. .prev = &fib6_walker_list,
  84. .next = &fib6_walker_list,
  85. };
  86. #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
  87. static inline void fib6_walker_link(struct fib6_walker_t *w)
  88. {
  89. write_lock_bh(&fib6_walker_lock);
  90. w->next = fib6_walker_list.next;
  91. w->prev = &fib6_walker_list;
  92. w->next->prev = w;
  93. w->prev->next = w;
  94. write_unlock_bh(&fib6_walker_lock);
  95. }
  96. static inline void fib6_walker_unlink(struct fib6_walker_t *w)
  97. {
  98. write_lock_bh(&fib6_walker_lock);
  99. w->next->prev = w->prev;
  100. w->prev->next = w->next;
  101. w->prev = w->next = w;
  102. write_unlock_bh(&fib6_walker_lock);
  103. }
  104. static __inline__ u32 fib6_new_sernum(void)
  105. {
  106. u32 n = ++rt_sernum;
  107. if ((__s32)n <= 0)
  108. rt_sernum = n = 1;
  109. return n;
  110. }
  111. /*
  112. * Auxiliary address test functions for the radix tree.
  113. *
  114. * These assume a 32bit processor (although it will work on
  115. * 64bit processors)
  116. */
  117. /*
  118. * test bit
  119. */
  120. static __inline__ __be32 addr_bit_set(void *token, int fn_bit)
  121. {
  122. __be32 *addr = token;
  123. return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
  124. }
  125. static __inline__ struct fib6_node * node_alloc(void)
  126. {
  127. struct fib6_node *fn;
  128. fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
  129. return fn;
  130. }
  131. static __inline__ void node_free(struct fib6_node * fn)
  132. {
  133. kmem_cache_free(fib6_node_kmem, fn);
  134. }
  135. static __inline__ void rt6_release(struct rt6_info *rt)
  136. {
  137. if (atomic_dec_and_test(&rt->rt6i_ref))
  138. dst_free(&rt->u.dst);
  139. }
  140. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  141. #define FIB_TABLE_HASHSZ 256
  142. #else
  143. #define FIB_TABLE_HASHSZ 1
  144. #endif
  145. static void fib6_link_table(struct net *net, struct fib6_table *tb)
  146. {
  147. unsigned int h;
  148. /*
  149. * Initialize table lock at a single place to give lockdep a key,
  150. * tables aren't visible prior to being linked to the list.
  151. */
  152. rwlock_init(&tb->tb6_lock);
  153. h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
  154. /*
  155. * No protection necessary, this is the only list mutatation
  156. * operation, tables never disappear once they exist.
  157. */
  158. hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
  159. }
  160. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  161. static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
  162. {
  163. struct fib6_table *table;
  164. table = kzalloc(sizeof(*table), GFP_ATOMIC);
  165. if (table != NULL) {
  166. table->tb6_id = id;
  167. table->tb6_root.leaf = net->ipv6.ip6_null_entry;
  168. table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  169. }
  170. return table;
  171. }
  172. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  173. {
  174. struct fib6_table *tb;
  175. if (id == 0)
  176. id = RT6_TABLE_MAIN;
  177. tb = fib6_get_table(net, id);
  178. if (tb)
  179. return tb;
  180. tb = fib6_alloc_table(net, id);
  181. if (tb != NULL)
  182. fib6_link_table(net, tb);
  183. return tb;
  184. }
  185. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  186. {
  187. struct fib6_table *tb;
  188. struct hlist_head *head;
  189. struct hlist_node *node;
  190. unsigned int h;
  191. if (id == 0)
  192. id = RT6_TABLE_MAIN;
  193. h = id & (FIB_TABLE_HASHSZ - 1);
  194. rcu_read_lock();
  195. head = &net->ipv6.fib_table_hash[h];
  196. hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) {
  197. if (tb->tb6_id == id) {
  198. rcu_read_unlock();
  199. return tb;
  200. }
  201. }
  202. rcu_read_unlock();
  203. return NULL;
  204. }
  205. static void fib6_tables_init(struct net *net)
  206. {
  207. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  208. fib6_link_table(net, net->ipv6.fib6_local_tbl);
  209. }
  210. #else
  211. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  212. {
  213. return fib6_get_table(net, id);
  214. }
  215. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  216. {
  217. return net->ipv6.fib6_main_tbl;
  218. }
  219. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
  220. int flags, pol_lookup_t lookup)
  221. {
  222. return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl, flags);
  223. }
  224. static void fib6_tables_init(struct net *net)
  225. {
  226. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  227. }
  228. #endif
  229. static int fib6_dump_node(struct fib6_walker_t *w)
  230. {
  231. int res;
  232. struct rt6_info *rt;
  233. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  234. res = rt6_dump_route(rt, w->args);
  235. if (res < 0) {
  236. /* Frame is full, suspend walking */
  237. w->leaf = rt;
  238. return 1;
  239. }
  240. BUG_TRAP(res!=0);
  241. }
  242. w->leaf = NULL;
  243. return 0;
  244. }
  245. static void fib6_dump_end(struct netlink_callback *cb)
  246. {
  247. struct fib6_walker_t *w = (void*)cb->args[2];
  248. if (w) {
  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. read_lock_bh(&table->tb6_lock);
  269. res = fib6_walk(w);
  270. read_unlock_bh(&table->tb6_lock);
  271. if (res > 0)
  272. cb->args[4] = 1;
  273. } else {
  274. read_lock_bh(&table->tb6_lock);
  275. res = fib6_walk_continue(w);
  276. read_unlock_bh(&table->tb6_lock);
  277. if (res != 0) {
  278. if (res < 0)
  279. fib6_walker_unlink(w);
  280. goto end;
  281. }
  282. fib6_walker_unlink(w);
  283. cb->args[4] = 0;
  284. }
  285. end:
  286. return res;
  287. }
  288. static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  289. {
  290. struct net *net = sock_net(skb->sk);
  291. unsigned int h, s_h;
  292. unsigned int e = 0, s_e;
  293. struct rt6_rtnl_dump_arg arg;
  294. struct fib6_walker_t *w;
  295. struct fib6_table *tb;
  296. struct hlist_node *node;
  297. struct hlist_head *head;
  298. int res = 0;
  299. s_h = cb->args[0];
  300. s_e = cb->args[1];
  301. w = (void *)cb->args[2];
  302. if (w == NULL) {
  303. /* New dump:
  304. *
  305. * 1. hook callback destructor.
  306. */
  307. cb->args[3] = (long)cb->done;
  308. cb->done = fib6_dump_done;
  309. /*
  310. * 2. allocate and initialize walker.
  311. */
  312. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  313. if (w == NULL)
  314. return -ENOMEM;
  315. w->func = fib6_dump_node;
  316. cb->args[2] = (long)w;
  317. }
  318. arg.skb = skb;
  319. arg.cb = cb;
  320. w->args = &arg;
  321. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  322. e = 0;
  323. head = &net->ipv6.fib_table_hash[h];
  324. hlist_for_each_entry(tb, node, head, tb6_hlist) {
  325. if (e < s_e)
  326. goto next;
  327. res = fib6_dump_table(tb, skb, cb);
  328. if (res != 0)
  329. goto out;
  330. next:
  331. e++;
  332. }
  333. }
  334. out:
  335. cb->args[1] = e;
  336. cb->args[0] = h;
  337. res = res < 0 ? res : skb->len;
  338. if (res <= 0)
  339. fib6_dump_end(cb);
  340. return res;
  341. }
  342. /*
  343. * Routing Table
  344. *
  345. * return the appropriate node for a routing tree "add" operation
  346. * by either creating and inserting or by returning an existing
  347. * node.
  348. */
  349. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  350. int addrlen, int plen,
  351. int offset)
  352. {
  353. struct fib6_node *fn, *in, *ln;
  354. struct fib6_node *pn = NULL;
  355. struct rt6key *key;
  356. int bit;
  357. __be32 dir = 0;
  358. __u32 sernum = fib6_new_sernum();
  359. RT6_TRACE("fib6_add_1\n");
  360. /* insert node in tree */
  361. fn = root;
  362. do {
  363. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  364. /*
  365. * Prefix match
  366. */
  367. if (plen < fn->fn_bit ||
  368. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  369. goto insert_above;
  370. /*
  371. * Exact match ?
  372. */
  373. if (plen == fn->fn_bit) {
  374. /* clean up an intermediate node */
  375. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  376. rt6_release(fn->leaf);
  377. fn->leaf = NULL;
  378. }
  379. fn->fn_sernum = sernum;
  380. return fn;
  381. }
  382. /*
  383. * We have more bits to go
  384. */
  385. /* Try to walk down on tree. */
  386. fn->fn_sernum = sernum;
  387. dir = addr_bit_set(addr, fn->fn_bit);
  388. pn = fn;
  389. fn = dir ? fn->right: fn->left;
  390. } while (fn);
  391. /*
  392. * We walked to the bottom of tree.
  393. * Create new leaf node without children.
  394. */
  395. ln = node_alloc();
  396. if (ln == NULL)
  397. return NULL;
  398. ln->fn_bit = plen;
  399. ln->parent = pn;
  400. ln->fn_sernum = sernum;
  401. if (dir)
  402. pn->right = ln;
  403. else
  404. pn->left = ln;
  405. return ln;
  406. insert_above:
  407. /*
  408. * split since we don't have a common prefix anymore or
  409. * we have a less significant route.
  410. * we've to insert an intermediate node on the list
  411. * this new node will point to the one we need to create
  412. * and the current
  413. */
  414. pn = fn->parent;
  415. /* find 1st bit in difference between the 2 addrs.
  416. See comment in __ipv6_addr_diff: bit may be an invalid value,
  417. but if it is >= plen, the value is ignored in any case.
  418. */
  419. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  420. /*
  421. * (intermediate)[in]
  422. * / \
  423. * (new leaf node)[ln] (old node)[fn]
  424. */
  425. if (plen > bit) {
  426. in = node_alloc();
  427. ln = node_alloc();
  428. if (in == NULL || ln == NULL) {
  429. if (in)
  430. node_free(in);
  431. if (ln)
  432. node_free(ln);
  433. return NULL;
  434. }
  435. /*
  436. * new intermediate node.
  437. * RTN_RTINFO will
  438. * be off since that an address that chooses one of
  439. * the branches would not match less specific routes
  440. * in the other branch
  441. */
  442. in->fn_bit = bit;
  443. in->parent = pn;
  444. in->leaf = fn->leaf;
  445. atomic_inc(&in->leaf->rt6i_ref);
  446. in->fn_sernum = sernum;
  447. /* update parent pointer */
  448. if (dir)
  449. pn->right = in;
  450. else
  451. pn->left = in;
  452. ln->fn_bit = plen;
  453. ln->parent = in;
  454. fn->parent = in;
  455. ln->fn_sernum = sernum;
  456. if (addr_bit_set(addr, bit)) {
  457. in->right = ln;
  458. in->left = fn;
  459. } else {
  460. in->left = ln;
  461. in->right = fn;
  462. }
  463. } else { /* plen <= bit */
  464. /*
  465. * (new leaf node)[ln]
  466. * / \
  467. * (old node)[fn] NULL
  468. */
  469. ln = node_alloc();
  470. if (ln == NULL)
  471. return NULL;
  472. ln->fn_bit = plen;
  473. ln->parent = pn;
  474. ln->fn_sernum = sernum;
  475. if (dir)
  476. pn->right = ln;
  477. else
  478. pn->left = ln;
  479. if (addr_bit_set(&key->addr, plen))
  480. ln->right = fn;
  481. else
  482. ln->left = fn;
  483. fn->parent = ln;
  484. }
  485. return ln;
  486. }
  487. /*
  488. * Insert routing information in a node.
  489. */
  490. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  491. struct nl_info *info)
  492. {
  493. struct rt6_info *iter = NULL;
  494. struct rt6_info **ins;
  495. ins = &fn->leaf;
  496. for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
  497. /*
  498. * Search for duplicates
  499. */
  500. if (iter->rt6i_metric == rt->rt6i_metric) {
  501. /*
  502. * Same priority level
  503. */
  504. if (iter->rt6i_dev == rt->rt6i_dev &&
  505. iter->rt6i_idev == rt->rt6i_idev &&
  506. ipv6_addr_equal(&iter->rt6i_gateway,
  507. &rt->rt6i_gateway)) {
  508. if (!(iter->rt6i_flags&RTF_EXPIRES))
  509. return -EEXIST;
  510. iter->rt6i_expires = rt->rt6i_expires;
  511. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  512. iter->rt6i_flags &= ~RTF_EXPIRES;
  513. iter->rt6i_expires = 0;
  514. }
  515. return -EEXIST;
  516. }
  517. }
  518. if (iter->rt6i_metric > rt->rt6i_metric)
  519. break;
  520. ins = &iter->u.dst.rt6_next;
  521. }
  522. /* Reset round-robin state, if necessary */
  523. if (ins == &fn->leaf)
  524. fn->rr_ptr = NULL;
  525. /*
  526. * insert node
  527. */
  528. rt->u.dst.rt6_next = iter;
  529. *ins = rt;
  530. rt->rt6i_node = fn;
  531. atomic_inc(&rt->rt6i_ref);
  532. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  533. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  534. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  535. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  536. fn->fn_flags |= RTN_RTINFO;
  537. }
  538. return 0;
  539. }
  540. static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
  541. {
  542. if (net->ipv6.ip6_fib_timer->expires == 0 &&
  543. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  544. mod_timer(net->ipv6.ip6_fib_timer, jiffies +
  545. net->ipv6.sysctl.ip6_rt_gc_interval);
  546. }
  547. void fib6_force_start_gc(struct net *net)
  548. {
  549. if (net->ipv6.ip6_fib_timer->expires == 0)
  550. mod_timer(net->ipv6.ip6_fib_timer, jiffies +
  551. net->ipv6.sysctl.ip6_rt_gc_interval);
  552. }
  553. /*
  554. * Add routing information to the routing tree.
  555. * <destination addr>/<source addr>
  556. * with source addr info in sub-trees
  557. */
  558. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  559. {
  560. struct fib6_node *fn, *pn = NULL;
  561. int err = -ENOMEM;
  562. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  563. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  564. if (fn == NULL)
  565. goto out;
  566. pn = fn;
  567. #ifdef CONFIG_IPV6_SUBTREES
  568. if (rt->rt6i_src.plen) {
  569. struct fib6_node *sn;
  570. if (fn->subtree == NULL) {
  571. struct fib6_node *sfn;
  572. /*
  573. * Create subtree.
  574. *
  575. * fn[main tree]
  576. * |
  577. * sfn[subtree root]
  578. * \
  579. * sn[new leaf node]
  580. */
  581. /* Create subtree root node */
  582. sfn = node_alloc();
  583. if (sfn == NULL)
  584. goto st_failure;
  585. sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
  586. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  587. sfn->fn_flags = RTN_ROOT;
  588. sfn->fn_sernum = fib6_new_sernum();
  589. /* Now add the first leaf node to new subtree */
  590. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  591. sizeof(struct in6_addr), rt->rt6i_src.plen,
  592. offsetof(struct rt6_info, rt6i_src));
  593. if (sn == NULL) {
  594. /* If it is failed, discard just allocated
  595. root, and then (in st_failure) stale node
  596. in main tree.
  597. */
  598. node_free(sfn);
  599. goto st_failure;
  600. }
  601. /* Now link new subtree to main tree */
  602. sfn->parent = fn;
  603. fn->subtree = sfn;
  604. } else {
  605. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  606. sizeof(struct in6_addr), rt->rt6i_src.plen,
  607. offsetof(struct rt6_info, rt6i_src));
  608. if (sn == NULL)
  609. goto st_failure;
  610. }
  611. if (fn->leaf == NULL) {
  612. fn->leaf = rt;
  613. atomic_inc(&rt->rt6i_ref);
  614. }
  615. fn = sn;
  616. }
  617. #endif
  618. err = fib6_add_rt2node(fn, rt, info);
  619. if (err == 0) {
  620. fib6_start_gc(info->nl_net, rt);
  621. if (!(rt->rt6i_flags&RTF_CACHE))
  622. fib6_prune_clones(info->nl_net, pn, rt);
  623. }
  624. out:
  625. if (err) {
  626. #ifdef CONFIG_IPV6_SUBTREES
  627. /*
  628. * If fib6_add_1 has cleared the old leaf pointer in the
  629. * super-tree leaf node we have to find a new one for it.
  630. */
  631. if (pn != fn && pn->leaf == rt) {
  632. pn->leaf = NULL;
  633. atomic_dec(&rt->rt6i_ref);
  634. }
  635. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  636. pn->leaf = fib6_find_prefix(info->nl_net, pn);
  637. #if RT6_DEBUG >= 2
  638. if (!pn->leaf) {
  639. BUG_TRAP(pn->leaf != NULL);
  640. pn->leaf = info->nl_net->ipv6.ip6_null_entry;
  641. }
  642. #endif
  643. atomic_inc(&pn->leaf->rt6i_ref);
  644. }
  645. #endif
  646. dst_free(&rt->u.dst);
  647. }
  648. return err;
  649. #ifdef CONFIG_IPV6_SUBTREES
  650. /* Subtree creation failed, probably main tree node
  651. is orphan. If it is, shoot it.
  652. */
  653. st_failure:
  654. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  655. fib6_repair_tree(info->nl_net, fn);
  656. dst_free(&rt->u.dst);
  657. return err;
  658. #endif
  659. }
  660. /*
  661. * Routing tree lookup
  662. *
  663. */
  664. struct lookup_args {
  665. int offset; /* key offset on rt6_info */
  666. struct in6_addr *addr; /* search key */
  667. };
  668. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  669. struct lookup_args *args)
  670. {
  671. struct fib6_node *fn;
  672. __be32 dir;
  673. if (unlikely(args->offset == 0))
  674. return NULL;
  675. /*
  676. * Descend on a tree
  677. */
  678. fn = root;
  679. for (;;) {
  680. struct fib6_node *next;
  681. dir = addr_bit_set(args->addr, fn->fn_bit);
  682. next = dir ? fn->right : fn->left;
  683. if (next) {
  684. fn = next;
  685. continue;
  686. }
  687. break;
  688. }
  689. while(fn) {
  690. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  691. struct rt6key *key;
  692. key = (struct rt6key *) ((u8 *) fn->leaf +
  693. args->offset);
  694. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  695. #ifdef CONFIG_IPV6_SUBTREES
  696. if (fn->subtree)
  697. fn = fib6_lookup_1(fn->subtree, args + 1);
  698. #endif
  699. if (!fn || fn->fn_flags & RTN_RTINFO)
  700. return fn;
  701. }
  702. }
  703. if (fn->fn_flags & RTN_ROOT)
  704. break;
  705. fn = fn->parent;
  706. }
  707. return NULL;
  708. }
  709. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  710. struct in6_addr *saddr)
  711. {
  712. struct fib6_node *fn;
  713. struct lookup_args args[] = {
  714. {
  715. .offset = offsetof(struct rt6_info, rt6i_dst),
  716. .addr = daddr,
  717. },
  718. #ifdef CONFIG_IPV6_SUBTREES
  719. {
  720. .offset = offsetof(struct rt6_info, rt6i_src),
  721. .addr = saddr,
  722. },
  723. #endif
  724. {
  725. .offset = 0, /* sentinel */
  726. }
  727. };
  728. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  729. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  730. fn = root;
  731. return fn;
  732. }
  733. /*
  734. * Get node with specified destination prefix (and source prefix,
  735. * if subtrees are used)
  736. */
  737. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  738. struct in6_addr *addr,
  739. int plen, int offset)
  740. {
  741. struct fib6_node *fn;
  742. for (fn = root; fn ; ) {
  743. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  744. /*
  745. * Prefix match
  746. */
  747. if (plen < fn->fn_bit ||
  748. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  749. return NULL;
  750. if (plen == fn->fn_bit)
  751. return fn;
  752. /*
  753. * We have more bits to go
  754. */
  755. if (addr_bit_set(addr, fn->fn_bit))
  756. fn = fn->right;
  757. else
  758. fn = fn->left;
  759. }
  760. return NULL;
  761. }
  762. struct fib6_node * fib6_locate(struct fib6_node *root,
  763. struct in6_addr *daddr, int dst_len,
  764. struct in6_addr *saddr, int src_len)
  765. {
  766. struct fib6_node *fn;
  767. fn = fib6_locate_1(root, daddr, dst_len,
  768. offsetof(struct rt6_info, rt6i_dst));
  769. #ifdef CONFIG_IPV6_SUBTREES
  770. if (src_len) {
  771. BUG_TRAP(saddr!=NULL);
  772. if (fn && fn->subtree)
  773. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  774. offsetof(struct rt6_info, rt6i_src));
  775. }
  776. #endif
  777. if (fn && fn->fn_flags&RTN_RTINFO)
  778. return fn;
  779. return NULL;
  780. }
  781. /*
  782. * Deletion
  783. *
  784. */
  785. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
  786. {
  787. if (fn->fn_flags&RTN_ROOT)
  788. return net->ipv6.ip6_null_entry;
  789. while(fn) {
  790. if(fn->left)
  791. return fn->left->leaf;
  792. if(fn->right)
  793. return fn->right->leaf;
  794. fn = FIB6_SUBTREE(fn);
  795. }
  796. return NULL;
  797. }
  798. /*
  799. * Called to trim the tree of intermediate nodes when possible. "fn"
  800. * is the node we want to try and remove.
  801. */
  802. static struct fib6_node *fib6_repair_tree(struct net *net,
  803. struct fib6_node *fn)
  804. {
  805. int children;
  806. int nstate;
  807. struct fib6_node *child, *pn;
  808. struct fib6_walker_t *w;
  809. int iter = 0;
  810. for (;;) {
  811. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  812. iter++;
  813. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  814. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  815. BUG_TRAP(fn->leaf==NULL);
  816. children = 0;
  817. child = NULL;
  818. if (fn->right) child = fn->right, children |= 1;
  819. if (fn->left) child = fn->left, children |= 2;
  820. if (children == 3 || FIB6_SUBTREE(fn)
  821. #ifdef CONFIG_IPV6_SUBTREES
  822. /* Subtree root (i.e. fn) may have one child */
  823. || (children && fn->fn_flags&RTN_ROOT)
  824. #endif
  825. ) {
  826. fn->leaf = fib6_find_prefix(net, fn);
  827. #if RT6_DEBUG >= 2
  828. if (fn->leaf==NULL) {
  829. BUG_TRAP(fn->leaf);
  830. fn->leaf = net->ipv6.ip6_null_entry;
  831. }
  832. #endif
  833. atomic_inc(&fn->leaf->rt6i_ref);
  834. return fn->parent;
  835. }
  836. pn = fn->parent;
  837. #ifdef CONFIG_IPV6_SUBTREES
  838. if (FIB6_SUBTREE(pn) == fn) {
  839. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  840. FIB6_SUBTREE(pn) = NULL;
  841. nstate = FWS_L;
  842. } else {
  843. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  844. #endif
  845. if (pn->right == fn) pn->right = child;
  846. else if (pn->left == fn) pn->left = child;
  847. #if RT6_DEBUG >= 2
  848. else BUG_TRAP(0);
  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. BUG_TRAP(fn==NULL);
  955. return -ENOENT;
  956. }
  957. #endif
  958. if (fn == NULL || rt == net->ipv6.ip6_null_entry)
  959. return -ENOENT;
  960. BUG_TRAP(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. BUG_TRAP(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. BUG_TRAP(0);
  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. BUG_TRAP(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. local_bh_disable();
  1218. if (!spin_trylock(&fib6_gc_lock)) {
  1219. mod_timer(net->ipv6.ip6_fib_timer, jiffies + HZ);
  1220. local_bh_enable();
  1221. return;
  1222. }
  1223. gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
  1224. }
  1225. gc_args.more = 0;
  1226. icmp6_dst_gc(&gc_args.more);
  1227. fib6_clean_all(net, fib6_age, 0, NULL);
  1228. if (gc_args.more)
  1229. mod_timer(net->ipv6.ip6_fib_timer, jiffies +
  1230. net->ipv6.sysctl.ip6_rt_gc_interval);
  1231. else {
  1232. del_timer(net->ipv6.ip6_fib_timer);
  1233. net->ipv6.ip6_fib_timer->expires = 0;
  1234. }
  1235. spin_unlock_bh(&fib6_gc_lock);
  1236. }
  1237. static void fib6_gc_timer_cb(unsigned long arg)
  1238. {
  1239. fib6_run_gc(0, (struct net *)arg);
  1240. }
  1241. static int fib6_net_init(struct net *net)
  1242. {
  1243. int ret;
  1244. struct timer_list *timer;
  1245. ret = -ENOMEM;
  1246. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  1247. if (!timer)
  1248. goto out;
  1249. setup_timer(timer, fib6_gc_timer_cb, (unsigned long)net);
  1250. net->ipv6.ip6_fib_timer = timer;
  1251. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1252. if (!net->ipv6.rt6_stats)
  1253. goto out_timer;
  1254. net->ipv6.fib_table_hash =
  1255. kzalloc(sizeof(*net->ipv6.fib_table_hash)*FIB_TABLE_HASHSZ,
  1256. GFP_KERNEL);
  1257. if (!net->ipv6.fib_table_hash)
  1258. goto out_rt6_stats;
  1259. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1260. GFP_KERNEL);
  1261. if (!net->ipv6.fib6_main_tbl)
  1262. goto out_fib_table_hash;
  1263. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1264. net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1265. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1266. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1267. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1268. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1269. GFP_KERNEL);
  1270. if (!net->ipv6.fib6_local_tbl)
  1271. goto out_fib6_main_tbl;
  1272. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1273. net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1274. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1275. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1276. #endif
  1277. fib6_tables_init(net);
  1278. ret = 0;
  1279. out:
  1280. return ret;
  1281. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1282. out_fib6_main_tbl:
  1283. kfree(net->ipv6.fib6_main_tbl);
  1284. #endif
  1285. out_fib_table_hash:
  1286. kfree(net->ipv6.fib_table_hash);
  1287. out_rt6_stats:
  1288. kfree(net->ipv6.rt6_stats);
  1289. out_timer:
  1290. kfree(timer);
  1291. goto out;
  1292. }
  1293. static void fib6_net_exit(struct net *net)
  1294. {
  1295. rt6_ifdown(net, NULL);
  1296. del_timer_sync(net->ipv6.ip6_fib_timer);
  1297. kfree(net->ipv6.ip6_fib_timer);
  1298. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1299. kfree(net->ipv6.fib6_local_tbl);
  1300. #endif
  1301. kfree(net->ipv6.fib6_main_tbl);
  1302. kfree(net->ipv6.fib_table_hash);
  1303. kfree(net->ipv6.rt6_stats);
  1304. }
  1305. static struct pernet_operations fib6_net_ops = {
  1306. .init = fib6_net_init,
  1307. .exit = fib6_net_exit,
  1308. };
  1309. int __init fib6_init(void)
  1310. {
  1311. int ret = -ENOMEM;
  1312. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1313. sizeof(struct fib6_node),
  1314. 0, SLAB_HWCACHE_ALIGN,
  1315. NULL);
  1316. if (!fib6_node_kmem)
  1317. goto out;
  1318. ret = register_pernet_subsys(&fib6_net_ops);
  1319. if (ret)
  1320. goto out_kmem_cache_create;
  1321. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
  1322. if (ret)
  1323. goto out_unregister_subsys;
  1324. out:
  1325. return ret;
  1326. out_unregister_subsys:
  1327. unregister_pernet_subsys(&fib6_net_ops);
  1328. out_kmem_cache_create:
  1329. kmem_cache_destroy(fib6_node_kmem);
  1330. goto out;
  1331. }
  1332. void fib6_gc_cleanup(void)
  1333. {
  1334. unregister_pernet_subsys(&fib6_net_ops);
  1335. kmem_cache_destroy(fib6_node_kmem);
  1336. }